Archive for the 'Tutorials' Category

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!

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!