W3C home > Mailing lists > Public > semantic-web@w3.org > April 2005

RDFSchema in end-user application: Which Java-API to use?

From: Max Voelkel <max@xam.de>
Date: Fri, 22 Apr 2005 11:34:00 +0200
Message-ID: <1166274654.20050422113400@xam.de>
To: semantic-web@w3.org
CC: max@xam.de

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...

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
------------------
Received on Friday, 22 April 2005 09:34:09 GMT

This archive was generated by hypermail 2.3.1 : Tuesday, 26 March 2013 21:45:05 GMT