- From: Jeen Broekstra <jeen@aduna.biz>
- Date: Mon, 25 Apr 2005 10:49:04 +0200
- To: Max Voelkel <max@xam.de>
- Cc: semantic-web@w3.org
Max Voelkel wrote: > Scenario: > > I have a given RDF Schema [2] and would like to programatically add > some statements to a model. The statements should be instances of the > classes and properties given in the schema. Imagine for example that I am writing a > FOAF-editor. > > Later, I also want to query my model and prints > all friendship-relations like this: > (person name) +" knows "+ (other person name) > Note that the end-user sees no URIs, just nice Strings. Example output > given in [4]. > > I used Jena 2.1 and came up with the code [3]. > > Questions to the community: > -------------------------- > 1. Is there a nicer way (shorter, more readdable) to do this in Jena? > > 2. Are there other Java-APIs that make this nicer (shorter, more > readable)? As I've never tried other APIs, I would be glad to see an example. > > If you can send me other code-examples for the same data and schema > they will make a nice introduction to the other APIs: I will put them > all togehter on some webpage... Here's how this would be done in Sesame. Or at least, one alternative. I've used a SeRQL query instead of using direct access methods to query the model, since it is more flexible, but both are possible. Depending on whether the actual schema is present in the repository or not, the whole thing can be made more efficient by retrieving the properties to be filled from the repository rather than creating them on the spot. For more discussion on Sesame's API, we probably should continue off-list, by e-mail or (preferably) on the Sesame user forum at http://www.openrdf.org/. // Create a Sesame service LocalService service = Sesame.getService(); // create an in-memory store (switch RDFS inferencing on) boolean inferencing = true; LocalRepository repository = service.createRepository("myRep", inferencing); ValueFactory valFactory = repository.getValueFactory(); // create RDF [1] according to schema [2] URI name = valFactory.createURI("schema://hasName"); URI knows = valFactory.createURI("schema://knows"); URI p1 = valFactory.createURI("data://p1"); URI p2 = valFactory.createURI("data://p2"); URI person = valFactory.createURI("schema://Person"); p1.addProperty(URIImpl.RDF_TYPE, person); p2.addProperty(URIImpl.RDF_TYPE, person); p1.addProperty(name, valFactory.createLiteral("Rudi Studer")); p2.addProperty(name, valFactory.createLiteral("York Sure")); p1.addProperty(knows, p2); // query model using SeRQL query String query = " SELECT nameOfX, nameOfY " + " FROM {X} <" + name + "> {nameOfX}; " + foaf:knows {Y} <" + name + "> {nameOfY} " + " USING NAMESPACE " + " foaf = <http://rdfreactor.ontoware.org/2005/04/foaf#>"; QueryResultsTable result = repository.performTableQuery(QueryLanguage.SERQL, query); // render output for (int i = 0; i < result.getRowCount(); i++) { Literal subject = (Literal)result.getValue(i,0); Literal object = (Literal)result.getValue(i,1); System.out.println(subject + " knows " + object); } > Thanks in advance, > > Max Völkel > -- > University of Karlsruhe, AIFB, Knowledge Management Group > room #258, building 11.40 > mvo@aifb.uni-karlsruhe.de +49 721 608-4754 www.xam.de > > > - Code snippets --- > > > [1] Desired RDF output (N-TRIPLES format) > ------------------------------------------------- > <data://p1> <schema://hasFriend> <data://p2> . > <data://p1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "schema://Person" . > <data://p2> <schema://hasName> "York Sure" . > <data://p1> <schema://hasName> "Rudi Studer" . > <data://p2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "schema://Person" . > ------------------------------------------------- > > > > [2] Given RDF Schema (N3) > ------------------------------------------------- > @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . > @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . > @prefix foaf: <http://rdfreactor.ontoware.org/2005/04/foaf#> . > > foaf:Person a rdfs:Class. > > foaf:hasName a rdf:Property > ; rdfs:domain foaf:Person > ; rdfs:range rdfs:Literal > . > > foaf:knows a rdf:Property > ; rdfs:domain foaf:Person > ; rdfs:range foaf:Person > . > ------------------------------------------------- > > [2] Given RDF Schema (N-TRIPLES format) > ------------------------------------------------- > <http://rdfreactor.ontoware.org/2005/04/foaf#hasName> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> . > <http://rdfreactor.ontoware.org/2005/04/foaf#knows> <http://www.w3.org/2000/01/rdf-schema#range> <http://rdfreactor.ontoware.org/2005/04/foaf#Person> . > <http://rdfreactor.ontoware.org/2005/04/foaf#knows> <http://www.w3.org/2000/01/rdf-schema#domain> <http://rdfreactor.ontoware.org/2005/04/foaf#Person> . > <http://rdfreactor.ontoware.org/2005/04/foaf#knows> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> . > <http://rdfreactor.ontoware.org/2005/04/foaf#Person> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> . > <http://rdfreactor.ontoware.org/2005/04/foaf#hasName> <http://www.w3.org/2000/01/rdf-schema#range> <http://www.w3.org/2000/01/rdf-schema#Literal> . > <http://rdfreactor.ontoware.org/2005/04/foaf#hasName> <http://www.w3.org/2000/01/rdf-schema#domain> <http://rdfreactor.ontoware.org/2005/04/foaf#Person> . > ------------------------------------------------- > > > [3] Source code in Jena > ------------------------------------------------- > jenaDataModel = ModelFactory.createDefaultModel(); > > // create RDF [1] according to Schema [2] > Property name = jenaDataModel.createProperty("schema://hasName"); > Property knows = jenaDataModel.createProperty("schema://hasFriend"); > > Resource p1 = jenaDataModel.createResource("data://p1"); > Resource p2 = jenaDataModel.createResource("data://p2"); > p1.addProperty( RDF.type, "schema://Person"); > p2.addProperty( RDF.type, "schema://Person"); > > p1.addProperty(name, "Rudi Studer"); > p2.addProperty(name, "York Sure"); > p1.addProperty(knows, p2); > > // query model and render output > StmtIterator it = p1.listProperties(knows); > while (it.hasNext()) { > Statement s = it.nextStatement(); > System.out.println(s.getSubject().getProperty(name).getLiteral() > .getLexicalForm() > + " knows " > + jenaDataModel.createResource( > s.getObject().asNode().getURI()).getProperty(name) > .getLiteral().getLexicalForm()); > > } > ------------------------------------------------- > > [4] Example output > ------------------- > Rudi Studer knows York Sure > ------------------ > > > -- Jeen Broekstra Aduna BV Knowledge Engineer Julianaplein 14b, 3817 CS Amersfoort http://aduna.biz The Netherlands tel. +31 33 46599877
Received on Monday, 25 April 2005 08:47:05 UTC