001: package org.ontoware.rdf2go.example;
002:
003: import org.ontoware.aifbcommons.collection.ClosableIterator;
004: import org.ontoware.rdf2go.Reasoning;
005: import org.ontoware.rdf2go.impl.jena24.DumpUtils;
006: import org.ontoware.rdf2go.impl.jena24.ModelImplJena24;
007: import org.ontoware.rdf2go.model.Model;
008: import org.ontoware.rdf2go.model.Statement;
009: import org.ontoware.rdf2go.model.node.BlankNode;
010: import org.ontoware.rdf2go.model.node.Resource;
011: import org.ontoware.rdf2go.model.node.URI;
012: import org.ontoware.rdf2go.model.node.Variable;
013: import org.ontoware.rdf2go.vocabulary.RDF;
014: import org.ontoware.rdf2go.vocabulary.RDFS;
015:
016: /**
017: * This is a simple example of how to use RDF2GO to create some statments about
018: * persons using the FOAF vocabulary.
019: *
020: * It shows
021: * <ul>
022: * <li>how to create a model</li>
023: * <li>how to add some statements to the model</li>
024: * <li>how to query the model
025: * <li>
026: * </ul>
027: *
028: * @author mvo (Max V�lkel), wth (Werner Thiemann)
029: *
030: */
031: public class FoafExample {
032:
033: /**
034: * Create a foaf file
035: *
036: * @param args
037: * None
038: * @throws Exception
039: */
040: public static void main(String[] args) throws Exception {
041:
042: /*
043: * first of all we have to instantiate a RDF2GO model with an underlying
044: * framework here we choose Jena
045: */
046:
047: // no inferencing
048: Model model = new ModelImplJena24(Reasoning.none);
049: // alternatives
050: // new ModelImplYars();
051: // new ModelImplNG4J();
052:
053: /*
054: * before we can do anything useful, we have to define the uris we want
055: * to use
056: */
057: // use the uris defined by foaf
058: URI foafName = model
059: .createURI("http://xmlns.com/foaf/0.1/name");
060: URI foafPerson = model
061: .createURI("http://xmlns.com/foaf/0.1/Person");
062: URI foafTitle = model
063: .createURI("http://xmlns.com/foaf/0.1/title");
064: URI foafKnows = model
065: .createURI("http://xmlns.com/foaf/0.1/knows");
066: URI foafHomepage = model
067: .createURI("http://xmlns.com/foaf/0.1/homepage");
068: // use a blank node for the person
069: BlankNode werner = model.createBlankNode();
070:
071: /*
072: * now we can add statements to the model (for easier reading we
073: * replaced the blank nodes cryptical letters with a human readable
074: * version - you will see something different when exectuing this
075: * example
076: */
077: // _:blankNodeWerner
078: // <http://xmlns.com/foaf/0.1/homepage>
079: // <http://www.blue-agents.com> .
080: model.addStatement(werner, foafHomepage, model
081: .createURI("http://www.blue-agents.com"));
082: // _:blankNodeWerner
083: // <http://xmlns.com/foaf/0.1/title>
084: // "Mr" .
085: model.addStatement(werner, foafTitle, "Mr");
086: // _:blankNodeWerner
087: // <http://xmlns.com/foaf/0.1/name>
088: // "Werner Thiemann" .
089: model.addStatement(werner, foafName, "Werner Thiemann");
090:
091: // _:blankNodeWerner
092: // <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
093: // <http://xmlns.com/foaf/0.1/Person> .
094: model.addStatement(werner, RDF.type, foafPerson);
095:
096: BlankNode max = model.createBlankNode();
097: // _:blankNodeMax
098: // <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>
099: // <http://xmlns.com/foaf/0.1/Person> .
100: model.addStatement(max, RDF.type, foafPerson);
101: // _:blankNodeMax
102: // <http://xmlns.com/foaf/0.1/name>
103: // "Max Voelkel" .
104: model.addStatement(max, foafName, "Max V�lkel");
105: // _:blankNodeMax
106: // <http://www.w3.org/2000/01/rdf-schema#seeAlso>
107: // <http://www.aifb.uni-karlsruhe.de/WBS/mvo/foaf.rdf.xml> .
108: model.addStatement(max, RDFS.seeAlso, model
109: .createURI("http://www.xam.de/foaf.rdf.xml"));
110:
111: // link via foaf:knows
112: // _:blankNodeWerner
113: // <http://xmlns.com/foaf/0.1/knows>
114: // _:blankNodeMax.
115: model.addStatement(werner, foafKnows, max);
116:
117: // default dump
118: DumpUtils.dump(model, null);
119:
120: // queries
121: // get all persons
122:
123: ClosableIterator<? extends Statement> it = model
124: .findStatements(Variable.ANY, RDF.type, foafPerson);
125: while (it.hasNext()) {
126: Resource person = it.next().getSubject();
127: System.out.println(person + " is a person");
128:
129: // get foaf:name
130: ClosableIterator<? extends Statement> it2 = model
131: .findStatements(person, foafName, Variable.ANY);
132: while (it2.hasNext()) {
133: System.out.println(person + " has the foaf:name "
134: + it2.next().getObject());
135: }
136: }
137: }
138:
139: }
|