Jena
Jena is an open source Semantic Web framework for Java. It provides an API to extract data from and write to RDF graphs. (Wikipedia)
Contents
Setup
Setup of Jena with Pellet, which is an open source Java OWL DL reasoner.
Requirements:
Download and unpack
Setup Eclipse project
- Create a User Library named Jena > Add External JARs > select all jar files in the Jena lib folder
- Create also a library named Pallet and add all jar from the pallet lib folder (the JARs form the subfolders are not necessary)
- Download Chapter 2 ZIP file which includes a Hello World Eclipse project for semantic web
- Import the download Eclipse project and add both User Libraries to the Java build path
- Run the file in the src folder
Example
Simple example
import com.hp.hpl.jena.vocabulary.*;
import com.hp.hpl.jena.rdf.model.*;
/*
* Simple Jena example, which creates a model, adds resources
* and print the final model on the console.
*/
public class JenaExample {
static String namespace = "http://www.example.org/";
public static void main(String ... args) {
Model m = ModelFactory.createDefaultModel();
m.setNsPrefix("example", namespace);
m.setNsPrefix("rdf", RDF.getURI());
Resource bmw = m.createResource(namespace + "BWM");
Resource car = m.createResource(namespace + "Car");
m.add(bwm, RDF.type, car);
m.write(System.out);
m.write(System.out, "TURTLE");
}
}
Read, modify and create RDF file
import java.io.FileOutputStream;
import java.util.Map;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.sparql.vocabulary.FOAF;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.FileUtils;
import com.hp.hpl.jena.vocabulary.RDF;
public class Simpsons {
public static void main(String ... args) {
/*
* Handle arguments
*/
if (args.length < 2) {
System.err.println("Less arguments");
System.out.println("Example: input.ttl outout.ttl");
System.exit(0);
}
String inputFile = args[0];
String inputLang = FileUtils.guessLang(inputFile);
String outputFile = args[1];
String outputLang = FileUtils.guessLang(outputFile);
/*
* Run program
*/
try {
modifyRDFfile(inputFile, inputLang, outputFile, outputLang);
} catch (Exception e) {
System.err.println("Error:");
System.err.println(e.getMessage());
}
}
/**
* Modify the given file and save changes in output file
*
* @param inputFile
* Input file
* @param inputLang
* Input RDF language
* @param outputFile
* Output file
* @param outputLang
* Output RDF language
* @throws Exception
*/
private static void modifyRDFfile(String inputFile, String inputLang, String outputFile, String outputLang) throws Exception {
/*
* Create model from file
*/
Model model = FileManager.get().loadModel(inputFile, inputLang);
/*
* Get prefixes
*/
@SuppressWarnings("unchecked")
Map<String, String> prefixMap = model.getNsPrefixMap();
String sim = prefixMap.get("sim");
String fam = prefixMap.get("fam");
/*
* Get properties
*/
Property foafAge = model.getProperty(FOAF.NS + "age");
Property famHasSpouse = model.getProperty(fam + "hasSpouse");
/*
* Modify resources
*/
// Mona
Resource simMona = model.getResource(sim + "Mona");
simMona.addProperty(RDF.type, FOAF.Person);
simMona.addProperty(FOAF.name, "Mona Simpson");
simMona.addProperty(foafAge, "70", XSDDatatype.XSDint);
// Abraham
Resource simAbraham = model.getResource(sim + "Abraham");
simAbraham.addProperty(RDF.type, FOAF.Person);
simAbraham.addProperty(FOAF.name, "Abraham Simpson");
simAbraham.addProperty(foafAge, "78", XSDDatatype.XSDint);
// Abraham is the spouse of Mona, and Mona is the spouse of Abraham
model.add(simAbraham, famHasSpouse, simMona);
model.add(simMona, famHasSpouse, simAbraham);
/*
* Iterate about all resource of type foaf:Person
*/
ResIterator foafPersonIterator = model.listResourcesWithProperty(RDF.type, FOAF.Person);
Resource foafPerson;
while (foafPersonIterator.hasNext()) {
foafPerson = foafPersonIterator.nextResource();
if (foafPerson.hasProperty(foafAge)) {
int age = foafPerson.getProperty(foafAge).getInt();
if(age < 18) {
foafPerson.addProperty(RDF.type, model.getResource(fam + "Minor"));
}
if(age > 70){
foafPerson.addProperty(RDF.type, model.getResource(fam + "Old"));
}
}
}
/*
* Write model into file
*/
model.write(new FileOutputStream(outputFile), outputLang);
}
}
SPARQL
see Jena - SPARQL