Monthly Archive for July, 2008

Speaking Construct Protocols

All messages that are exchanged between Construct services and applications take the following format: [3 bytes][10 bytes][payload]

  • The first 3 bytes correspond to the message type identifier (see below).

The next 10 bytes correspond to the length of the message payload in bytes. For example, a message with a 64 byte payload would be written as 0000000064.

  • Finally, the message payload consists of a number of bytes indicated by the previous part of the message. The contents of the payload are protocol specific.

The message identifier codes used are as follows:

The protocol identifier for querying QUERY 200
The protocol identifier used for responding to queries QUERY_RESPONSE 210
The protocol identifier used for sending rdf statements to the data port RDF_ADD 100
The protocol identifier used for responding to an add rdf statements request RDF_ADD_RESPONSE 110
The protocol identifier used for sending a service descriptor SERVICE_DESCRIPTOR_RESPONSE 310

When most service send a response to the client, the payload also takes the form of a code:

The operation succeeded OK 600
An error occured during the operation ERROR 610
The service code was unrecognised UNKNOWN 650

Working with the discovery service

Open a connection to the host/port given in the bonjour resolution (there is no need to send any data). You will be sent an XML descriptor file of the form -

  1. <services>
  2. <servicecomponentdescriptor>
  3. <name>Construct DataPort</name>
  4. <description>Raw data port: Connect via a socket and send N-TRIPLE RDF strings. Response string will be ok or error if it fails.</description>
  5. <host>erdinger</host>
  6. <port>3528</port>
  7. <misc>See example applications.</misc>
  8. </servicecomponentdescriptor>
  9. <servicecomponentdescriptor>
  10. <name>Construct QueryService</name>
  11. <description>The query service: Connect via a socket and send SPARQL queries. Response string will be a SPARQL result set in RDF.</description>
  12. <host>erdinger</host>
  13. <port>3531</port>
  14. <misc>See example applications.</misc>
  15. </servicecomponentdescriptor>
  16. </services>

This provides you with all the information required to open a connection to the data port or query service directly.

Working with the data port

Use the protocol described above. This is an example of adding a single line:

  • 1000000000043<http://hello> <http://construct> "world" .

NOTE: The data RDF Triple must have the trailing full stop . Example responses might be:

  • 1100000000003600 (OK)
  • 1100000000003610 (ERROR)
  • 1100000000003650 (UNKNOWN)

Working with the query service

The point of contact for application developers to Construct is the Query Service. It takes a SPARQL query as an input. This query is run on the data store (a list of RDF triples). If the query is valid and answerable, a string of data will be returned. This section will try to help you write a SPARQL query, and make sense of the returned information.

Writing a SPARQL query

The presence of a query service implies that there must be something to query. The data in Construct is stored in the data store. All the data is represented in RDF triples. Here’s a few examples of these: For readability, we will replace the URIs with the prefixes “sighting:” and “person:”. NOTE: The prefixes in the query(below) is valid SPARQL syntax.

  1. PREFIX: sighting:http://srg.ucd.ie/construct/sighting#
  2. PREFIX: person:http://www.pervasive-ontologies.org/ontologies/context/person.owl#
  3. <sighting:Waldo109> <sighting:person> <person:Waldo>
  4. <sighting:Waldo109> <sighing:computer> <http://srg.cs.ucd.ie/construct/computer/waldo.ucd.ie>
  5. <sighting:Waldo109> <sighting:time> <2006-10-04T13:17:59Z>
  6. <sighitng:Waldo109> <sightingstatus> <Active>

These four statements represent a sighting from a computer activity sensor.

The Query

To retrieve information from construct, a query must be written in the SPARQL format. This has a similar “Select X From Y Where Z” form to an SQL query. Here is an example query which relates to the RDF Triples above. It finds all the times of sightings of Waldo(note the time triple above).

  1. String personQuery =  “PREFIX sighting:<http://srg.ucd.ie/construct/sighting#> “
  2. + “PREFIX user:<http://www.pervasive-ontologies.org/ontologies/context/person.owl#> “
  3. + “SELECT ?time “
  4. + “WHERE {”
  5. + “?sighting sighting:person user:Waldo . “
  6. + “?sighting sighting:status sighting:Active . “
  7. + “?sighting sighting:time ?time”
  8. + “}”;

For a more comprehensive tutorial on SPARQL, visit the website at: http://www.w3.org/TR/rdf-sparql-query/

In order to send a query to the query service we must use the protocol described above. Below is an example.

  • 200[payload length][SPARQL QUERY]
The Response

You will be sent back a string of the form

  • 210[payload length][SPARQL result set in RDF form]
  • OR 2100000000003610 (if an error occured)

This String is a SPARQL ResultSet. This is a valid RDF string, represented in N-TRIPLE format. This means that way in which it is processed is dependent on the RDF parsing capabilities of the language used. For example, in java, a new Jena model (or indeed a ResultSet object) can be created from it, and it can be then easily traversed.

Construct Merchandise?

Steve modelling the Construct T-ShirtSteve, an original member of the Construct Dev team, left the Systems Research Group last week. He’s off to get hitched and travel the world - so we won’t hold it against him!

As part of his leaving presents we made up this rather fetching (even if we do say so ourselves) T-Shirt, so that he can spread the word of Construct to the world.

Good luck Steve - Bon Voyage!

CfP: Ubiquitous Systems Evaluation 2008 (USE ‘08)

The 2nd International Workshop on Ubiquitous Systems Evalution (USE ‘08) will take place in Seoul, South Korea on 21st September, 2008. The workshop is being organised by Construct developers Graeme Stevenson and Steve Neely of University College Dublin, and by Christian Kray of Newcastle University.

Following on from last year’s workshop in Innsbruck, USE ‘08 aims to bring together practitioners from a wide range of disciplines to discuss best practice and challenges in the evaluation of ubiquitous systems. Recognised evaluation strategies are essential in order that the contribution of new techniques can be quantified objectively. Experience has shown that evaluating ubiquitous systems is extremely difficult; approaches tend to be subjective, piecemeal or both. Individual approaches to evaluation risk being incomplete and comparisons between systems can be difficult.

The submission deadline for USE ‘08 is July 07, 2008. Please visit http://www.useworkshop.org for further details.

Querying Named Graphs in Construct (Some Examples)

The next release of Construct contains named graphs. These allow you to store information that describes your RDF statements. I’ll give a simple run through of how to query Construct using named graphs (if you want more detail check out the w3c recommendation ).

When querying named graphs in Construct using SPARQL and the QueryService you need to add the GRAPH keyword to pattern match against graphs in the dataset. Lets start with the easiest example, selecting all the graphs and all the statements…

  1. SELECT * WHERE { GRAPH ?graph { ?s ?p ?o } }

So now your resultset is going to be able to return four pieces of info for every statement.

Graphs in Construct probably don’t confer too much useful information on their own however, as they are just unique ids, created by the data store.

It is useful though to know what graph(s) a statement or set of statements appears in. You could use something like this…

  1. SELECT DISTINCT ?graph WHERE { GRAPH ?graph { <http://construct-infrastructure.org/subject> ?p ?o } }

Now you’ve got the graph(s) that your statement(s) appears in, suppose you wanted to find all the metadata associated with those statements. All you have to do is add the following query…

  1. SELECT ?p ?o FROM NAMED <$GRAPH_URI> WHERE { GRAPH ?graph { ?graph ?p ?o } }

where $GRAPH_URI corresponds to the name of the graph(s) returned by the previous query. Now you’ve got all the metadata associated with your statement(s). This is only a small example of what kind of SPARQL queries can get the most out of the new data storage technique - all that’s left is to have a go!

Installing Bonjour for *nix Users

Every time Ubuntu’s Synaptic Package Manager updates the Avahi layer for Bonjour support it stomps on my Apple Bonjour install and breaks my Construct install. My guess is that the update puts Avahi first in the list of Bonjours to run when Construct starts. So I get this error:

*** WARNING *** The programme ‘java’ uses the Apple Bonjour compatiblity layer of Avahi.

*** WARNING *** Please fix your application to use the native API of Avahi!

*** WARNING *** For more information see

*** WARNING *** The programme ‘java’ called ‘DNSServiceQueryRecord()’ which is not supported (or only supported partially) in the Apple Bonjour compatiblity layer of Avahi.

*** WARNING *** Please fix your application to use the native API of Avahi!

*** WARNING *** For more information see

One way to address this is just to turn off the Avahi warning. Set an environmental variable list this export AVAHI_COMPAT_NOWARN=1 This isn’t a fix though it is just hiding the warning.

The best I have figured out so far is just to reinstall Bonjour:

Instructions for Installing Apple’s Bonjour on *nix

  1. Download the latest version of the Bonjour source code here.
  2. Decompress the downloaded archive
  3. cd to the mDNSPosix directory below the mDNSResponder-xxx directory (where xxx is the Bonjour version number).
  4. Edit Makefile and change line JDK = /usr/jdk to make it point at your Java installation (e.g., /usr/lib/jvm/java-6-sun)
  5. Type sudo make os=linux Java
  6. Type sudo make os=linux install — if you have problems with this, see below. Most likely you will need to install a specific version of gcc
  7. Copy the java specific files to your jre lib’s ext directory (cp build/prod/* /path/to/jre/lib/ext/)
  8. Open up /etc/nsswitch.conf and ensure that the ‘mdns’ switch appears on the “hosts:” line. My “hosts:” line looks like this:
hosts:          files dns mdns

Remember, whenever you use a new jre, make sure to include dns_sd.jar (Bonjour JAR file) in your jre/lib/ext directory

Problems with the install

Problems with stdlib.h

Lorcan had some problems with this install on Ubuntu (Edgy). He says: when running sudo make os=linux Java I got a lot of errors, starting with ../mDNSShared/dnssd_clientlib.c:71:20: error: stdlib.h: No such file or directory.

I installed the libc6-dev package. When I tried again everything worked fine

Problems with __stack_chk_fail_local

I had some major problems with this install on Ubuntu (Edgy), specifically when using gcc4.1 (to find out which version of gcc you are using type gcc --version).

lorcan@comp:~/Desktop/mDNSResponder-107.6/mDNSPosix$ sudo make os=linux install
Stopping Apple Darwin Multicast DNS / DNS Service Discovery daemon: mdnsd.
cp build/prod/mdnsd /usr/sbin/mdnsd
/usr/sbin/mdnsd installed
cp mdnsd.sh /etc/init.d/mdns
chmod ugo+x /etc/init.d/mdns
/etc/init.d/mdns start
Starting Apple Darwin Multicast DNS / DNS Service Discovery daemon: mdnsd.
ln -s -f /etc/init.d/mdns /etc/rc2.d/S52mdns
ln -s -f /etc/init.d/mdns /etc/rc3.d/S52mdns
ln -s -f /etc/init.d/mdns /etc/rc4.d/S52mdns
ln -s -f /etc/init.d/mdns /etc/rc5.d/S52mdns
ln -s -f /etc/init.d/mdns /etc/rc0.d/K16mdns
ln -s -f /etc/init.d/mdns /etc/rc6.d/K16mdns
/etc/init.d/mdns installed
cp build/prod/libdns_sd.so /usr/lib/libdns_sd.so.1
ln -s -f /usr/lib/libdns_sd.so.1 /usr/lib/libdns_sd.so
/usr/lib/libdns_sd.so.1 /usr/include/dns_sd.h installed
/usr/share/man/man8/mdnsd.8 installed
make[1]: Entering directory `/home/lorcan/Desktop/mDNSResponder-107.6/Clients’
mkdir build
cc dns-sd.c -L../mDNSPosix/build/prod/ -ldns_sd -I../mDNSShared -o build/dns-sd
/usr/bin/ld: build/dns-sd: hidden symbol `__stack_chk_fail_local’ in /usr/lib/libc_nonshared.a(stack_chk_fail_local.oS) is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status
make[1]: *** [build/dns-sd] Error 1
make[1]: Leaving directory `/home/lorcan/Desktop/mDNSResponder-107.6/Clients’

make: *** [../Clients/build/dns-sd] Error 2

Solution:
If the version of gcc is not 4.0.x Follow these instructions (EXACTLY!!!) to correct the problem:

  1. Ensure that gcc version 4.0.x is installed. This is a little tricky as the synaptic package manager stopped supporting this version of gcc since edgy. You’ll have to download the following packages:  gcc-4.0-base, cpp-4.0 , gcc-4.0
  2. Open the terminal and cd to where you saved the package.
  3. Do the following for each of the three packages: dpkg -i PACKAGE.deb Install them in the same order the are above. gcc base first, then cpp, then gccThis should put gcc-4.0 into your /usr/bin directory.
  4. cd /usr/bin
  5. sudo mv gcc gcc-backup
  6. sudo ln -s gcc-4.0 gcc

Now completely rerun the instructions above (at the top of this page) to install Bonjour (you must completely rerun them from step 2). When installation is complete continue these instructions.

  1. sudo cd /usr/bin
  2. sudo rm gcc
  3. sudo mv gcc-backup gcc

Now gcc should point to the original version of gcc (i.e. that version that you were using before you installed Bonjour).

Easy Web Access to Construct - Construct PHP Class

Using PHP to access construct is desirable to allow interesting construct based web applications to be developed that allow easy implementation, and rapid prototyping. On this basis, we developed a PHP class that allows a developer to connect to a running instance of Construct and manipulate data quicky and easily, without the need to run standalone JAVA applications.

(Download Construct PHP Class)

The PHP Class has been tested on Windows versions of PHP (using XAMPP) and on Mac XServe (Darwin), it should work with any webserver running PHP 5, (possibly 4) with sockets support.

The Construct PHP Class also contains a file called constructmananger.php and example.php, the Construct Manager uses the Construct class to give an advanced web interface to Construct, and the Example shows a simple implementation of a sparql query using the Construct class.

Put the files in any web accessible directory, and edit the example.php file to point at the machine that is running Construct.

<?php
$host = "localhost";
// find the class
include_once('Construct.class.php');
// create a new construct object
$construct = new Construct($host);
?>

After importing the class and instatiating it (see above) it is then possible to query Construct as shown below

// prepare a query
$sparql_query = "SELECT ?subject ?predicate ?object WHERE {?subject ?predicate ?object}";
// execute query
try{
$results = $construct->query($sparql_query);
}Catch(Exception $e){
// catch any problems
echo $e->getMessage();
exit();
}
// output the results
print_r($results);

The results are returned as a PHP array by default, but can also be returned as JSON, XML or a Simple XML Object.

$construct->query($querystring, [$xsl=null, [$format=null, [&$error=null]]]) 

Where $querystring is the SPARQL query, $xsl is the xsl-stylesheet to apply (send null if not used), $format indicated the format of the returned data, and $error is an array into which errors should be placed. Values in [] are optional.

The values for format are:

JSON, XML, XMLOBJECT or ARRAY

It is also possible to insert data, the following gives an example

$timeout = "30000"; // Time in ms to keep data in store
try{
$success = $construct->insert($rdftriplets, $timeout);
}Catch(Exception $e){
echo $e-getMessage();
}

Where $rdftriplets are in valid n-triple format.

If links are unavailable, you can try here

Adding Construct to your Ubuntu start menu.

In this post, I show you how to add construct to your ubuntu start menu. The assumption I am making is that you have the most recent version of construct (currently 0.74) downloaded and installed.

Step 1

This step allows you to run construct without being in the construct directory.

Open the ‘construct.sh’ file and change the line:

CONSTRUCT_HOME=.

to

CONSTRUCT_HOME=YOUR CONSTRUCT DIRECTORY

For me it is: $HOME/construct

Save the file and exit.

Step 2

Create a symbolic link to the executable shell script.

This allows you to just type ‘construct’ in the terminal, much the same way you would write ‘emacs’ etc.

Open a terminal and type:

cd /usr/bin/

now type:

ln -s LOCATION_OF_CONSTRUCT_DIRECTORY/construct.sh construct

Now type cd

Type ‘construct’. Construct should start running.

It will tell you, however that it could not find the properties file.

Step 3

Copy the construct.properties file to your home directory.

Exit construct if you have it open.

Open a terminal.

cd to your construct directory (in my case cd ~/construct)

Copy the file to your home directory:

cp construct.properties ~/ (~/ is a shorthand notation for your home directory)

Step 4

In your ubuntu status bar, double click on the ’start menu’ — the orange circular icon on the left.

Click on ‘Edit Menus’

Click on the ‘New Item’ button on the right

For ‘Name’, type ‘Construct’

For ‘Command’, type ‘construct’

Click OK.

That’s it!

Construct is now in your start menu!

Understanding the construct.properties file

As the name suggests, the construct.properties file defines settings for the Construct platform. The settings cover three things: global properties for Construct, the components to be instantiated at runtime, and the individual properties of each component.

In the current release, all (bar one) of the global properties are related to logging. The other refers to the hostname of the machine running the Construct software:

Logging level The level of log messages to be recorded
The Logging limit The maximum number of bytes to write to any one log file
The logging file count The number of log files to rotate
The logging file name The base name to use for log files
The logging directory The directory in which the log files are stored
The hostname The hostname or IP address of the machine running the Construct software

The components that you wish to instantiate once the Construct framework is started are also contained in the properties file. Some components also contain properties which are specific to those components and must be declared as children of the components in the properties file. The structure of the properties file is explained further on. Below is an example of some Construct components. More thorough explanations can be found elsewhere on the Wiki.

Data Store Manager Provides a single point of access to the data store
Query Service Processes application and entity queries made in SPARQL
Data Port Accepts RDF data provided by sensors and adds them to the data store
Registry Service Stores information on registered services
Discovery Service Handles requests from clients for available Construct services
Gossiping Components Allows distribution of data store contents
Extension Components Add-on components such as a Query Viewer

Once the start button is clicked on the Construct GUI, a Component Loader is created which reads in the contents of the properties file and instantiates any components that are specified. On instantiation, each component fetches it’s properties and caters for them.

Reading and Writing to the construct.properties File

The construct.properties file is and XML encoded file in which all properties and components must be contained within the parent node, construct (i.e. it takes the form … ). Global properties must be contained directly inside the construct tags and they are indicated by a property tag. Thus, all properties take the form,

  1. <property name=“[the name here]“ value=“[the value here]“ />

Components must also be defined directly inside the construct tags. The component specific properties must be declared directly inside the opening and closing tags of the component in question. Components are scripted as follows:

  1. <component interface=“[the interface to the component here]“ implementation=“[the implementation of the component here]“>
  2. <property/>
  3. </component>

where the interface specifies the path to the interface for the component and the implementation specifies the path to the concrete implementation of the component.

There is an example properties file contained in the release of Construct. To add new properties follow the structure of the document as explained above. HTML style comments (i.e. <!– [the code to comment here] –>) can be used to prevent a property or component contained in the file from being read in. Ensure to comment the property or component by adding the opening comment (<!–) before the opening tag of the component or property and adding the closing comment (–>) after the closing tag.

HTTP Port - Part 2

In part 1 of this post I described the new HTTP Port in Construct. In this part of the post I’ll explain how to write a web form and style the return values.

SPARQL Query

You can send a query to Construct with GET or POST. Here is how we do it in HTML with GET:

  1. <form method=“GET” action=“http://duvel.ucd.ie:8888/”>
  2. <textarea name=“q” cols=“64″ rows=“10″>SELECT ?subject ?predicate ?object WHERE {?subject ?predicate ?object}</textarea><br/>URL of XSLT to apply (optional):<br/><input type=“text” name=“xsl” size=“56″ value=“http://www.construct-infrastructure.org/stylesheets/default.xsl type=”submit” value=”Submit Query“>
  3. </form>

the only thing the HTTP Port wants is the query field to be called either “q” or “query”. The returned XML will be sent back to your browser. Something like this:

  1. <?xml version=“1.0″?>
  2. <?xml-stylesheet type=“text/xsl” href=“http://duvel.ucd.ie:8888/xsl/www.construct-infrastructure.org/stylesheets/default.xsl”?>
  3. <sparql
  4. xmlns:rdf=“http://www.w3.org/1999/02/22-rdf-syntax-ns#”
  5. xmlns:xs=“http://www.w3.org/2001/XMLSchema#”
  6. xmlns=“http://www.w3.org/2005/sparql-results#”>
  7. <head>
  8. <variable name=“subject”/>
  9. <variable name=“predicate”/>
  10. <variable name=“object”/>
  11. </head>
  12. <results>
  13. <result>
  14. <binding name=“subject”> <uri>http://www.pervasive-ontologies.org/ontologies/sensors/bluetooth#reading00:19:63:96:56:01@00:80:98:94:AE:4B@1201631502</uri>
  15. </binding>
  16. <binding name=“predicate”>
  17. <uri>http://www.pervasive-ontologies.org/ontologies/sensors/bluetooth#spotted</uri>
  18. </binding>
  19. <binding name=“object”>
  20. <literal>00:19:63:96:56:01</literal>
  21. </binding>
  22. </result>
  23. </results>
  24. </sparql>

To make the results look neater you can apply and XSL stylesheet. Send the URL for the XSL stylesheet as a field “xsl” in the form. This stylesheet must be web accessible. We’ve made a default example stylesheet you can use to get started.

Inserting N-3 RDF Data

You can insert new data into Construct using a form like this (we’re using POST this time for variety but GET works too):

  1. <form method=“POST” action=“http://duvel.ucd.ie:8888/”>
  2. <textarea name=“i” cols=“64″ rows=“10″><http://www.pervasive-ontologies.org/ontologies/sensors/bluetooth#reading00:19:63:96:56:01@00:80:98:94:AE:4B@1201631502><http://www.pervasive-ontologies.org/ontologies/sensors/bluetooth#spotted>“00:19:63:96:56:01″.</textarea><br/>Expiry time for data (optional):<br/><input type=“text” name=“expiry” size=“15″ value=“20000″><br/><input type=“submit” value=“Submit Data”>
  3. </form>

in this form you send the data to be inserted as a field “i” or “insert”.

The returned data from an insert is an XML document with a status code and any messages given back from Construct. It looks something like this:

  1. <response>
  2. <status>OK</status>
  3. <description>The data <http://www.pervasive-ontologies.org/ontologies/sensors/bluetooth#reading00:19:63:96:56:01@00:80:98:94:AE:4B@1201631502><http://www.pervasive-ontologies.org/ontologies/sensors/bluetooth#spotted>“00:19:63:96:56:01″. with given expiry 20000 was passed to the Construct data store without error.
  4. </description>
  5. </response>

Well, that is about it. You can make calls to Construct over HTTP from browsers or web applications using the HTTP Port. Have fun.