Download presentation
Presentation is loading. Please wait.
Published byGeraldine Thornton Modified over 9 years ago
1
Using Java in Linked Data Applications Fuming Shih Oct 12
2
Java Libraries Jena - Java API for semantic web applications Jastor – typesafe, Ontology Driven RDF Access from Java – http://jastor.sourceforge.net/
3
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
4
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);
5
Task today Load Lalana’s foaf file at http://www.csail.mit.edu/~lkagal/foaf 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
6
Installation 1.Download JDK 2.Download and install Eclipse 3.Download Jena and Jastor library 4.Tuning your Eclipse 5.Create a java project 6.Append Jena & Jastor libraries to your classpath
7
Code Model model = ModelFactory.createDefaultModel(); // read the RDF/XML file model.read("http://www.csail.mit.edu/~lkagal/foaf", null); 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); 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); StmtIterator workPages = model_friend.listStatements(null, workPlaceHomepage,(RDFNode) null); while(workPages.hasNext()){ String workPlace = workPages.nextStatement().getObject().toString(); queryWorkPlace(friend.getURI(), workPlace); ….
8
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
9
SW for JAVA Programmer RDF DB Ontology files Mapping tool Jena2 Platform (RDF model + Reasoning Engine + Persistence System) Jena2 Platform (RDF model + Reasoning Engine + Persistence System) JAVA VM Java Objects listener Operator FOAF iCal SIOC Tag 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
10
Code JastorContext ctx = new JastorContext(); ctx.addOntologyToGenerate(new FileInputStream("src/data/Tag.owl"), "http://www.mit.edu/dig/ns/tag", "edu.mit.dig.model.Tag"); JastorGenerator gen = new JastorGenerator( new File("gensrc").getCanonicalFile(), ctx); gen.run(); JastorContext ctx = new JastorContext(); ctx.addOntologyToGenerate(new FileInputStream("src/data/Tag.owl"), "http://www.mit.edu/dig/ns/tag", "edu.mit.dig.model.Tag"); JastorGenerator gen = new JastorGenerator( new File("gensrc").getCanonicalFile(), ctx); gen.run(); Tag tag = edu.mit.dig.model.Tag.tagFactory.createTag(NS_PREFIX + "id_1", model); tag.addName("A tag"); tag.addX(45); tag.addY(32); Tag tag = edu.mit.dig.model.Tag.tagFactory.createTag(NS_PREFIX + "id_1", model); tag.addName("A tag"); tag.addX(45); tag.addY(32); Create mapping Make use of the class
11
Demo
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.