Semweb4j/models/step2
Up | Previous step | Next lesson
Contents |
[edit] Step 2: Nodes & Literals
We will take a look at the different kinds of Nodes which can be used in statements. Blank node creation and language tagged literals are also discussed here. Literals are special Nodes.
[edit] foaf
As in the last example, we will use foaf like this:
String foafURI = "http://xmlns.com/foaf/0.1/"; URI name = model.createURI(foafURI+"#term_name"); URI icqId = model.createURI(foafURI+"#term_icqChatID");
[edit] the PlainLiteral class
URI max = model.createURI("http://xam.de/foaf.rdf.xml#i");
PlainLiteral maxNameAsPlainLiteral = model.createPlainLiteral("Max Völkel");
[edit] the Statement class
Statement maxNameStatement = model.createStatement(max, name, maxNameAsPlainLiteral); model.addStatement(maxNameStatement);
[edit] blank nodes
We can create blank nodes (nodes without URI) and statements with them like this:
BlankNode konrad = model.createBlankNode(); model.addStatement(konrad, name, "Konrad");
[edit] typed literals (DatatypeLiteral)
URI typeInteger = XSD._integer;
DatatypeLiteral number = model.createDatatypeLiteral("123", typeInteger);
model.addStatement(konrad, icqId, number);
Note that XSD is org.ontoware.rdf2go.vocabulary.XSD, the XML Scheme Datatypes vocabulary.
[edit] language tagged literals
LanguageTagLiteral konradNameEnglish = model.createLanguageTagLiteral("Konrad Woelkel", "en");
LanguageTagLiteral konradNameGerman = model.createLanguageTagLiteral("Konrad Wölkel", "de");
[edit] sets of Statement objects
Statement konradNameEnglishStatement = model.createStatement(konrad, name, konradNameEnglish); Statement konradNameGermanStatement = model.createStatement(konrad, name, konradNameGerman); ArrayList<Statement> konradNameStatements = new ArrayList<Statement>(); konradNameStatements.add(konradNameGermanStatement); konradNameStatements.add(konradNameEnglishStatement); model.addAll(konradNameStatements.iterator());
Model.addAll takes any Iterator, so you could use something different than ArrayList here.
[edit] specifying an adapter / a triple store to use
To decide which triple store to use in RDF2Go, just use
RDF2Go.register( new org.ontoware.rdf2go.impl.jena24.ModelFactoryImpl() );
before
ModelFactory modelFactory = RDF2Go.getModelFactory();
RDF2Go will usually look if you've registered any ModelFactoryImpl (as above). If there is no registered ModelFactoryImpl, RDF2Go looks at your classpath and dynamically registers any ModelFactoryImpl found (e.g. Jena or Sesame).
[edit] the complete program
Now we're putting the parts together and get a runnable Java application:
package org.ontoware.semweb4j.lessons.lesson1;
import java.util.ArrayList;
import org.ontoware.rdf2go.RDF2Go;
import org.ontoware.rdf2go.exception.ModelRuntimeException;
import org.ontoware.rdf2go.model.Model;
import org.ontoware.rdf2go.model.Statement;
import org.ontoware.rdf2go.model.node.BlankNode;
import org.ontoware.rdf2go.model.node.DatatypeLiteral;
import org.ontoware.rdf2go.model.node.LanguageTagLiteral;
import org.ontoware.rdf2go.model.node.PlainLiteral;
import org.ontoware.rdf2go.model.node.URI;
import org.ontoware.rdf2go.vocabulary.XSD;
public class Step2 {
private static Model model;
private static void init() throws ModelRuntimeException {
// specify to use Jena here:
RDF2Go.register( new org.ontoware.rdf2go.impl.jena24.ModelFactoryImpl() );
// if not specified, RDF2Go.getModelFactory() looks into your classpath for ModelFactoryImpls to register.
model = RDF2Go.getModelFactory().createModel();
model.open();
}
public static void main(String[] args) throws ModelRuntimeException {
init();
// creating resources
String foafURI = "http://xmlns.com/foaf/0.1/";
URI max = model.createURI("http://xam.de/foaf.rdf.xml#i");
URI name = model.createURI(foafURI+"#term_name");
URI icqId = model.createURI(foafURI+"#term_icqChatID");
URI typeInteger = XSD._integer;
BlankNode konrad = model.createBlankNode();
PlainLiteral maxNameAsPlainLiteral = model.createPlainLiteral("Max Völkel");
DatatypeLiteral number = model.createDatatypeLiteral("123", typeInteger);
LanguageTagLiteral konradNameEnglish = model.createLanguageTagLiteral("Konrad Woelkel", "en");
LanguageTagLiteral konradNameGerman = model.createLanguageTagLiteral("Konrad Wölkel", "de");
// adding statements to the model
// adding a bnode statement
model.addStatement(konrad, name, "Konrad");
// adding a statement with a DatatypeLiteral
model.addStatement(konrad, icqId, number);
// adding a Statement object
Statement maxNameStatement = model.createStatement(max, name, maxNameAsPlainLiteral);
model.addStatement(maxNameStatement);
// adding a set of Statement objects
Statement konradNameEnglishStatement = model.createStatement(konrad, name, konradNameEnglish);
Statement konradNameGermanStatement = model.createStatement(konrad, name, konradNameGerman);
ArrayList<Statement> konradNameStatements = new ArrayList<Statement>();
konradNameStatements.add(konradNameGermanStatement);
konradNameStatements.add(konradNameEnglishStatement);
model.addAll(konradNameStatements.iterator());
// dumping model to the screen
model.dump();
}
}
You can get the code by downloading semweb4j also.