110 likes | 318 Views
Using Java in Linked Data Applications. Fuming Shih Oct 12. Java Libraries. Jena - Java API for semantic web applications Jastor – typesafe , Ontology Driven RDF Access from Java http:// jastor.sourceforge.net /. Jena.
E N D
Using Java in Linked Data Applications Fuming Shih Oct 12
Java Libraries • Jena - Java API for semantic web applications • Jastor – typesafe, Ontology Driven RDF Access from Java • http://jastor.sourceforge.net/
Jena • Programmatic environment for RDF, RDFS and OWL, SPARQL and includes a rule-based inference engine. • Jena has : • Resource interface represent resources • Property interface represent properties • Literal interface represent literals • Model interface represent graph
Example code to create graph with jena // some definitions static String personURI = "http://somewhere/JohnSmith"; static String fullName = "John Smith"; // create an empty Model Model model = ModelFactory.createDefaultModel(); // create the resource Resource johnSmith = model.createResource(personURI); // add the property johnSmith.addProperty(VCARD.FN, fullName);
Task today • Load Lalana’sfoaf file at http://www.csail.mit.edu/~lkagal/foaf • Extract her friends • Look at the friends’ FOAF files • See where they work • Use the workplace URI or the labels to query dbpedia for it’s location • Print a list of friends, their workplaces and where the workplaces are located
Installation • Download JDK • Download and install Eclipse • Download Jena and Jastor library • Tuning your Eclipse • Create a java project • Append Jena & Jastor libraries to your classpath
Code Model model = ModelFactory.createDefaultModel(); // read the RDF/XML file model.read("http://www.csail.mit.edu/~lkagal/foaf", null); Property p_friend = model.createProperty("http://xmlns.com/foaf/0.1/knows"); StmtIterator friends = model.listStatements(null, p_friend, (RDFNode)null); while(friends.hasNext()){ Resource friend = (Resource) (friends.nextStatement().getObject()); Model model_friend = ModelFactory.createDefaultModel(); try{ model_friend.read(friend.getURI(),null); StmtIteratorworkPages = model_friend.listStatements(null, workPlaceHomepage,(RDFNode) null); while(workPages.hasNext()){ String workPlace = workPages.nextStatement().getObject().toString(); queryWorkPlace(friend.getURI(), workPlace); ….
From Protégé to Java Objects • From ontology engineering to writing Semantic Web application is not straightforward • Graph is fundamentally not intuitive to programmer • Save your time by choosing the right tools • But be aware that mapping from OO to ontology needs cares
SW for JAVA Programmer • Using ontology to represent domain knowledge • Use protégé to create/import different domain ontologies • Mapping ontologies to JAVA classes • Use Jastor library to generates Java interfaces, implementations, factories, and listeners based on the properties and class hierarchies in the Web ontologies • Easier for non-Semantic Web java developer to make use of ontology Java Objects Mapping tool listener Operator iCal SIOC Tag FOAF Jena2 Platform (RDF model + Reasoning Engine + Persistence System) RDF DB Ontology files JAVA VM
Code Create mapping JastorContextctx = new JastorContext(); ctx.addOntologyToGenerate(newFileInputStream("src/data/Tag.owl"), "http://www.mit.edu/dig/ns/tag", "edu.mit.dig.model.Tag"); JastorGeneratorgen = new JastorGenerator( new File("gensrc").getCanonicalFile(), ctx); gen.run(); Make use of the class Tag tag = edu.mit.dig.model.Tag.tagFactory.createTag(NS_PREFIX + "id_1", model); tag.addName("A tag"); tag.addX(45); tag.addY(32);