An updated version of this blog post is now available as a chapter in the TopBraid Application Development Quickstart Guide (pdf).

In earlier entries, we’ve seen how to create a TopBraid Ensemble application and how to create and run a semantic web service. When you combine these, the user interface that you create with TopBraid Ensemble (TBE) can take advantage of all the power of SPARQLMotion scripts, which can accept parameters from a TBE app, run in the background, and pass the results back to be incorporated into the TBE user interface.

In this posting, we’ll see how to add an “Age at death” choice to the TBE gear menu, which triggers SPARQLMotion scripts. When your end user selects a person in the kennedy data and picks this menu choice, a SPARQLMotion script will return either a message about the person’s age at death (assuming that they’ve already had their birthday in the year of their death) or the phrase “Still alive” for display in TBE.

Defining the function to call

As we saw in the posting on creating web services, the first step is to define a SPIN function that points to a SPARQLMotion script, and then we create the script. (The following description assumes that you’ve read the more detailed description of the same steps in How to: create and run a semantic web service.) Start by creating a new SPARQLMotion file called personScripts, and remember to check “Script will declare (Web) Services or Functions (.sms extension)” on the Create SPARQLMotion File dialog box.

Next, in the Classes view, create a subclass of spin:Functions called AgeAtDeath. The rdfs:label value for this function is important, because that’s the text that will appear on the TBE gear menu; drag this property from the Properties view onto the class form for your new function and enter “Age at death” there.

The next step is to use the function’s spin:constraint property to identify the argument being passed to the function. For our new application, we want the argument to be the identifier for the currently selected member of the Kennedy family in the TBE application—or, in TBE development terms, the URI for the selected resource—so that the function knows whose age at death to calculate. To make the selected resource the argument, on the AgeAtDeath class form click the spin:constraint property’s white triangle context menu and pick “Create from SPIN template”. On the Create from SPIN template dialog box, pick sml:SelectedResourceArgument from the selection of “Available Ask/Construct Templates”. Click the plus sign next to the predicate field and pick sml:selectedResource. You’re now finished specifying the spin:constraint.

Remember, a SPIN function identifies the SPARQLMotion script to call by naming the script’s last module in the function’s sm:returnModule property. The module that we need to point to doesn’t exist yet, so pick “Create and add…” from this property’s context menu, and then pick sml:ReturnText from the sml:ExportToRemoteModules section of the sml:ExportModules choices and name your new module ReturnComputedAge. It’s exporting to a remote module because a script on the server is returning the value to a calling process on the client, and when a TBE application gets text passed by a Return Text module, it displays that text in a message box, which is what we want for this application.

Creating the SPARQLMotion script

You don’t need to create a new SPARQLMotion script, because when you defined the sml:selectedResource and sm:returnModule modules as part of the AgeAtDeath function, TopBraid Composer put them into a new script for you. Select “Edit SPARQLMotion Script” from the Scripts menu, and you’ll see see this script with icons representing these two modules waiting for you to add new modules and connect them up.

Drag the Selected Resource icon to the top of the screen, because it will be the beginning of your script. Under it, create a new module to perform the query that will compute the age at death of the selected resource by dragging a Bind by select icon from the Control Flow section of the SPARQLMotion workspace palette onto the workspace. Name this module ComputeAge and set its sml:selectQuery property to the following SPARQL query:

PREFIX k: <>
SELECT ?deathAge
  ?selectedResource k:birthYear ?birthYear .
      ?selectedResource k:deathYear ?deathYear .
  } .
  LET (?deathAge := smf:if(bound(?deathYear),
                           (?deathYear - ?birthYear),
                           "Still alive")) .

It uses the SPARQLMotion extension function sml:if to calculate and return the person’s age at death if the ?deathYear variable is bound and to return the string “Still alive” if it’s not bound.

The sml:selectQuery property is the only one to set for this module, so you’re ready to add a connection from the Selected Resource module icon to the ComputeAge one.

The ComputeAge module knows what SPARQL query to execute, but it doesn’t know what data to execute it with. Drag an Import current RDF module, which passes all of the triples of the currently open model to the script, from the Import from Various section of the palette to the workspace. Name it ImportTBETriples and add a connection from this module to the ComputeAge one. You don’t need to set any of its properties; it will know what to do.

The query above binds the computed answer to the variable ?deathAge. This is what we want to return to the TBE application that calls the AgeAtDeath function, so set the sml:text property of the returnComputedAge module that’s been waiting for you to use it to {?deathAge} and connect the ComputeAge module icon to the returnComputedAge one. Your completed script should look like this:

The gear menu in TBE won’t know about the AgeAtDeath function until you register it with the server, so select “Refresh/Display SPARQLMotion functions…” from from the TopBraid Composer Script menu, and after a few seconds you’ll see the updated list of registered functions on the Console view.

Testing it

From the TBL main screen, pick “Default Application (configurable),” and on the “TopBraid Live Data Selection” screen pick kennedys from the “Data from Project TopBraid” section. When the application appears, click Person in the Tree component in the upper-left to list data about members of the Person class on the Results Grid. Once they appear there, click a row that includes both a “Year of birth” and a “Year of death” value to select it.

Click the gear menu in the upper-right, and you should see your new “Age at death” menu choice:

Select it, and you’ll see a message box showing the result of the calculation from the SPARQLMotion script:

Try again with a row of the Results Grid that has a “Year of birth” but no “Year of death” value, and you should see a similar message box appear with the “Still alive” message.

Taking it further

If you created ten different applications like this, the menu choices for all ten would appear on all gear menus in all of your TBE applications. To limit the menu choice to only appear on the gear menu for applications showing instances of a particular class, such as members of the Person class from the kennedys data, you can name that class in the ValueType property of the sml:SelectedResourceArgument when you configure the constraint in the function you’re defining.

The SPARQL query used in your SPARQLMotion script set the return value of ?deathAge to a very simple value: either (?deathYear - ?birthYear) or the string “Still alive”. Browse through the “TopBraid SPARQLMotion Functions Library” help panel and you’ll see an extensive choice of functions that you can use to assemble a much more complex string to return, with substring and case manipulation functions, regular expression matching, concatenation functions to assemble multiple pieces together, and more.

This SPARQLMotion script also had very limited input: the URI of the resource to query about and the data from the kennedys file. As we’ve seen in other postings, a SPARQLMotion script can open up many other kinds of both local and remote data, so the URI passed from TBE can be used in much more sophisticated kinds of processing. And, as we saw in How to: create a TopBraid Ensemble application, you can custom-design your own TBE screens to be the front end of your application. It’s a great combination of front end client-side design flexibility and back end server-side scripting capabilities.