Reading:
Software:
In this lab you will test various scenarios of using RDF/S data. You will navigate through, query, store and manipulate it from within a Java application. The aim of this lab is to give you a broad perspective on using RDF/S data, without exhaustively going into details. We hope you will be encouraged for independent research and tests.
The lab is divided into sections. You should allow aprox. 10 minutes for each section.
Force the accept header to text/plain regardless
Construct
query and analyze the resultPeriodicTable
and PlanetFeed
(Choose File → Open → examples/…
)Hint:
Data URL
field (web URL or local path) OR use FROM
construct to define your data source, e.g.PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?name FROM <http://home.agh.edu.pl/~wta/foaf.rdf> WHERE { ?x rdf:type foaf:Person . ?x foaf:name ?name } LIMIT 10
In this exercise use the FOAF files of your friends.
Run Twinkle and execute queries on chosen foaf file to retrive :
Hints:
DBPedia
MusicBrainz
Answer the question: What are the main limitations of using (querying for information) the RDF datasets such as DBPedia or MusicBrainz?
Jena is a Java framework for building Semantic Web applications. It provides a programmatic environment for RDF, RDFS and OWL, SPARQL and includes a rule-based inference engine.
Basic information (from the Introduction to Jena):
JohnSmith
). . Jena has object classes to represent graphs, resources, properties and literals. The interfaces representing resources, properties and literals are called Resource, Property and Literal respectively. In Jena, a graph is called a model and is represented by the Model interface.Model model = ModelFactory.createDefaultModel();
). Jena contains other implementations of the Model interface, e.g one which uses a relational database: these types of Model are also available from ModelFactory. Resource johnSmith = model.createResource(personURI);
), statements can be made about them and added the model (e.g. johnSmith.addProperty(VCARD.FN, fullName);
). in the example below the property is provided by a „constant” class VCARD which holds objects representing all the definitions in the VCARD schema. Jena provides constant classes for other well known schemas, such as RDF and RDF schema themselves, Dublin Core and DAML.vcard:N
property takes a resource as its value. Note also that the ellipse representing the compound name has no URI. It is known as an blank Node.// some definitions String personURI = "http://somewhere/JohnSmith"; String givenName = "John"; String familyName = "Smith"; String fullName = givenName + " " + familyName; // create an empty Model Model model = ModelFactory.createDefaultModel(); // create the resource // and add the properties cascading style Resource johnSmith = model.createResource(personURI) .addProperty(VCARD.FN, fullName) .addProperty(VCARD.N, model.createResource() .addProperty(VCARD.Given, givenName) .addProperty(VCARD.Family, familyName));
listStatements()
method which returns an StmtIterator
, a subtype of Java's Iterator over all the statements in a Model. StmtIterator
has a method nextStatement()
which returns the next statement from the iterator. The Statement
interface provides accessor methods to the subject, predicate and object of a statement.// list the statements in the Model StmtIterator iter = model.listStatements(); // print out the predicate, subject and object of each statement while (iter.hasNext()) { Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = stmt.getObject(); // get the object System.out.print(subject.toString()); System.out.print(" " + predicate.toString() + " "); if (object instanceof Resource) { System.out.print(object.toString()); } else { // object is a literal System.out.print(" \"" + object.toString() + "\""); } System.out.println(" ."); }
model.write(System.out);
). However, other syntaxes e.g. N-Triple are supported.inputFileName
variable defined, as well as defaultNameSpace
, one can you null
instead of the namespace in the read
method):try { InputStream myFile = FileManager.get().open( inputFileName ); model.read(myFile,defaultNameSpace); meFile.close(); } catch (IOException io){ System.out.println("File Error: " + io.getMessage()); }
model.read("http://home.agh.edu.pl/wta/foaf.rdf", "http://home.agh.edu.pl/wta/foaf.rdf#",null);
TASK:
Jena supports SPARQL via a dedicated module ARQ. In addition to implementing SPARQL, ARQ's query engine can also parse queries expressed in RDQL or its own internal query language.
Currently ARQ is a part of a standar Jena distribution. provides two basic ways of posing SPARQL queries to RDF graphs.
$ARQROOT
to point to the ARQ directory (the downloaded and unpacked Jena directory). Optionaly you may have to modify the access rights for the files in $ARQROOT/bin/
directory and add the bin
directory to the execution path.$ export ARQROOT=~/your_jena_dir $ chmod +rx $ARQROOT/bin/* $ export PATH=$PATH:$ARQROOT/bin $ sparql Usage: [--data URL] [exprString | --query file]
q1.rq
) and construct a SPARQL query in it, e.g.(and this is just an example, be creative, men )PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT ?url FROM <http://home.agh.edu.pl/wta/foaf.rdf> WHERE { ?p foaf:name ?n . ?p foaf:homepage ?url . }
sparql –query q1.rq
Using SPARQL queries within the Java application.
Read the Search RDF data with SPARQL tutorial.
ASK
query for your model: a SPARQL query that returns either „yes” or „no”, e.g. „Does Weronika T. Adrian (WTA) know Krzysztof Kluza (KKL)?”:public void queryForExistance() { String queryString = "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "ASK { ?x foaf:name \"Weronika T. Adrian (Furmańska)\" ; " + "foaf:knows ?y . " + "?y foaf:name \"Krzysztof Kluza\" . }"; Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(query, model); // Now run the query. ASK queries only return a boolean value. Boolean answer = qexec.execAsk(); // Output the result System.out.println(); System.out.println("Does WTA know KKL?"); if (answer) System.out.println("yes"); else System.out.println("no"); }
Tools:
Open Data Sets:
DB2RDF (RDF and Relational Databases):