Archive for the 'Python' Category

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

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:”