Archive

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.

HTTP Port - Part 1

The latest release of Construct has the new HTTP Port enabled by default. The HTTP Port has similar functionality to the data port — you can use it to insert and query for data in Construct. The difference is that the data port works over raw TCP sockets, whereas the HTTP Port works over HTTP.

Now we can query Construct through a web browser and web apps… neat, huh?

First off, the construct.properties file now contains:

  1. <!– http port –>
  2. <component interface=“org.construct_infrastructure.component.httpport.HttpPort” implementation=“org.construct_infrastructure.component.httpport.HttpPortImpl”>
  3. <property name=“port” value=“8888″ />
  4. </component>

these lines activate the HTTP Port on port 8888. When you have Construct running, open your web browser and go to http://localhost:8888/ and you should a simple set of forms for querying and inserting data using HTTP GET and HTTP POST. It looks something like this:

Screenshot of the HTTP Port

From this page you can send RDF data in N-3 format or SPARQL queries to Construct. Try clicking on one of the buttons to test it out.

This is the end of Part 1 on the HTTP Port. Part 2 of this post will explain how to build your own web forms and use of XSL stylesheets for your results.

Construct and Python - Part 3

This tutorial shows how to insert RDF from a file (joebloggs_foaf.rdf) into Construct. It then shows how to send a SPARQL query to query this data from Construct. The resulting QueryResults object is printed in N3 format.

  1. from construct.proxy import proxy
  2. from construct.constructservice import ServiceError
  3. from rdflib.Graph import ConjunctiveGraph
  4. # Create a new proxy object.
  5. proxy = proxy()
  6. print “Executing Script”
  7. try:
  8. # Generate a piece of FOAF RDF
  9. store = ConjunctiveGraph()
  10. store.load(“joebloggs_foaf.rdf”)
  11. data = store.serialize(format=“nt”)
  12. # Send the FOAF RDF to the data store
  13. if(proxy.insert(data)):
  14. # Now query for joebloggs web address
  15. query = “”“SELECT ?nickname WHERE{
  16. ?subject <http://xmlns.com/foaf/0.1/name> “Joe Bloggs“.
  17. ?subject <http://xmlns.com/foaf/0.1/nick> ?nickname.}
  18. “”
  19. results = proxy.query(query)
  20. print “Here is the N3 form of the QueryResults Object:”
  21. print results
  22. except ServiceError, e:
  23. print e
  24. # Close the proxy.
  25. proxy.close()

If this script executes properly something like the following should be printed out:

Executing Script

Here is the N3 form of the QueryResults Object:

_:A6a94e801X3aX118515eda06X3aXX2dX7ffa <http://www.w3.org/2001/sw/DataAccess/tests/result-set#value> “joe” .

_:A6a94e801X3aX118515eda06X3aXX2dX7ffa <http://www.w3.org/2001/sw/DataAccess/tests/result-set#variable> “nickname” .

_:A6a94e801X3aX118515eda06X3aXX2dX7ffb <http://www.w3.org/2001/sw/DataAccess/tests/result-set#binding> _:A6a94e801X3aX118515eda06X3aXX2dX7ffa .

_:A6a94e801X3aX118515eda06X3aXX2dX7ffc <http://www.w3.org/2001/sw/DataAccess/tests/result-set#solution> _:A6a94e801X3aX118515eda06X3aXX2dX7ffb .

_:A6a94e801X3aX118515eda06X3aXX2dX7ffc <http://www.w3.org/2001/sw/DataAccess/tests/result-set#resultVariable> “nickname” .

_:A6a94e801X3aX118515eda06X3aXX2dX7ffc <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2001/sw/DataAccess/tests/result-set#ResultSet> .
This post is the final part of a python and Construct tutorial. Part 1 is here and part 2 is here.

Construct and Python - Part 2

This code demonstrates how to interact with the client proxy using the RDFLib package:

  1. from construct.proxy import proxy
  2. from construct.constructservice import ServiceError
  3. from rdflib import RDF, Namespace, Literal
  4. from rdflib.Graph import ConjunctiveGraph
  5. FOAF = Namespace(“http://xmlns.com/foaf/0.1/”)
  6. exampleNS = Namespace(“http://www.example.com/”)
  7. # Create a new Proxy object.
  8. proxy = proxy()
  9. print “Executing Script”
  10. try:
  11. # Generate a piece of FOAF RDF
  12. store = ConjunctiveGraph()
  13. store.bind(“foaf”, “http://xmlns.com/foaf/0.1/”)
  14. store.add((exampleNS["~joebloggs"], RDF.type, FOAF["Person"]))
  15. store.add((exampleNS["~joebloggs"], FOAF["name"], Literal(“Joe Bloggs”)))
  16. store.add((exampleNS["~joebloggs"], FOAF["nick"], Literal(“joe”)))
  17. store.add((exampleNS["~joebloggs"], FOAF["givenname"], Literal(“Joe”)))
  18. store.add((exampleNS["~joebloggs"], FOAF["family_name"], Literal(“Bloggs”)))
  19. data = store.serialize(format=“nt”)
  20. # Send the FOAF RDF to the data store.
  21. if proxy.insert(data):
  22. print “The following data were added correctly:”
  23. print data
  24. else:
  25. print “Problem encountered when adding the following data:”
  26. print data
  27. except ServiceError, e:
  28. print e
  29. # Close the proxy.
  30. proxy.close()

If this code has run correctly the following should be printed:

Executing Script

The following data were added correctly:

<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/givenname> “Joe”.

<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/nick> “joe”.

<http://www.example.com/~joebloggs> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person>.

<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/name> “Joe Bloggs”.

<http://www.example.com/~joebloggs> <http://xmlns.com/foaf/0.1/family_name> “Bloggs”.
This post is the second part of a tutorial on construct and python. Back to Part 1 of this tutorial or on to Part 3.

Construct and Python - Part 1

This post will show how to connect to a Construct Data Port and submit RDF data.

If an instance of Construct is available, this code will insert two pieces of RDF in N3 format. The first of these is good RDF, the second of these is not. The script is below, and example output beneath that again.

  1. #Import the python construct proxy.
  2. from construct.proxy import proxy
  3. from construct.constructservice import ServiceError
  4. #Create a new Proxy object.
  5. proxy = proxy()
  6. print “Executing Script”
  7. try:
  8. # Send a VALID piece of RDF to the data store
  9. insertGoodResponse = proxy.insert(“<http://hello><http://construct>\”world\”.”);
  10. print “Response to good RDF: “ + str(insertGoodResponse)
  11. # response should be “1″
  12. # Send an INVALID piece of RDF to the data store.
  13. insertBadResponse = proxy.insert(“<http://badly><http://formed>\”rd\”f\”.”);
  14. print “Response to bad RDF: “ + str(insertBadResponse)
  15. # response should be “None”
  16. except ServiceError, e:
  17. print e.value
  18. # Close the proxy.
  19. proxy.close()

If this code has executed correctly (i.e., if an instance of Construct is discovered) the following will be printed:

Executing Script

Response to good RDF: 1

Response to bad RDF: None

If the Construct proxy is not found the following will be printed:

Executing Script

Error - unable to contact an instance node of Construct (using address localhost:3826). Is the Construct Proxy running at that address?

and if no instance of Construct is discovered the following will be printed:

Executing Script

Error: No instance of Construct found. Please ensure you are within Zeroconf range of a running instance of Construct
This post is the first part of a tutorial on construct and python. Part 2 of this tutorial is here and part 3 is here

Construct and Java - Part 2

In Part 1 of this series of posts, I described how to add data to Construct. This post will teach you how to get data back out again using the Query Service.

The Java libraries bundled with Construct make use Jena, and the SPARQL query language in the process of querying Construct. If you’re already familiar with Jena, then there is only a couple of additional steps to learn.

Working with Jena and SPARQL

(download the sample code)

To start, we need to open a connection to the Query Service. This is done in the same way as a connection to the Data Port, by creating a new instance of the appropriate class.

  1. final QueryServiceProxy qsProxy = new QueryServiceProxy()

The next step is to create the SPARQL query. In this example, we wish to query for the name property of the subject http://example.com/people/bob that we created in Part 1.

  1. final String query = “SELECT ?name WHERE{
  2. <http://example.com/people/bob>
  3. <http://example.com/terms#name>
  4. ?name}”;

Then we call the query() method of the proxy object, passing the query as an argument. The return value is a Jena ResultSet object corresponding to the result of the query.

  1. final ResultSet resultSet = qsProxy.query(query);

What you do with the result set is obviously dependent on your need for the data. For the purposes of this example, we’ll print the result to the console.

  1. if (resultSet.hasNext()) {
  2. final QuerySolution solution = resultSet.nextSolution();
  3. final Literal name = solution.getLiteral(“name”);
  4. System.out.println(name.toString());
  5. }

Finally, we close the connection to the query service.

  1. qsProxy.close();

That’s the basics of querying data with Construct.

Construct and Java - Part 1

After you install Construct, the next thing you might want to do is add some data. This post will describe the basics.

The Java libraries bundled with Construct give you a couple of ways to contribute date: You can either pass RDF triples in directly, or work with Jena and submit a model when you’re ready to publish its contents.

Working with Triples

(download the sample code)

The first step is to create a connection to the DataPort. This is achieved by creating a new instance of the DataPortProxy class.

  1. final DataPortProxy proxy = new DataPortProxy();

Next, we call the add() method of the proxy object, passing the RDF triple as an argument. Multiple triples can be added simultaneously by separating them with a period. The return indicates whether the add operation was successful.

  1. final boolean response = proxy.add(“<http://example.com/people/bob>
  2. <http://example.com/terms#name>
  3. \”Bob Smith\”.”);

Finally, we close the connection to the data port.

  1. proxy.close();

Working with Jena

(download the sample code)

When using Jena, the process is very similar. As before you should begin by creating a new instance of the DataPortProxy object. Next, a new model should be created, and populated with data.

  1. final Model model = ModelFactory.createDefaultModel();
  2. final Resource person = model.createResource(“http://example.com/people/bob”);
  3. final Property name = model.createProperty(“http://example.com/terms#name”);
  4. person.addProperty(name, “Bob Smith”);

Then, the contents of the Jena model is added to Construct through another of the proxy object’s add() methods. Remember to close the connection as before when you are finished.

  1. final boolean response = proxy.add(model);

That’s the basics of adding data to Construct. The next post in this series will describe how to get data from Construct using the Query Service.