Fuming Shih Oct 12 Java Libraries Jena Java API for semantic web applications Jastor typesafe Ontology Driven RDF Access from Java http jastorsourceforgenet Jena P rogrammatic environment for RDF RDFS and OWL SPARQL and includes a rulebased inference engine ID: 324052
Download Presentation The PPT/PDF document "Using Java in Linked Data Applications" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Using Java in Linked Data Applications
Fuming Shih
Oct 12Slide2
Java Libraries
Jena -
Java API for semantic web applications
Jastor
–
typesafe
, Ontology Driven RDF Access from Java
http://
jastor.sourceforge.net
/Slide3
Jena
P
rogrammatic 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 graphSlide4
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);Slide5
Task today
Load
Lalana’s
foaf
file at
http
://www.csail.mit.edu/~lkagal/foafExtract her friendsLook at the friends’ FOAF filesSee where they workUse 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 locatedSlide6
Installation
Download JDK
Download and install Eclipse
Download Jena and
Jastor
library
Tuning your Eclipse
Create a java projectAppend Jena & Jastor libraries to your classpathSlide7
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
);
StmtIterator
workPages
=
model_friend
.listStatements(
null
,
workPlaceHomepage,(RDFNode
) null);
while(workPages.hasNext
())
{
String
workPlace
=
workPages.nextStatement().getObject().toString
();
queryWorkPlace(friend.getURI
(),
workPlace
);
….Slide8
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 caresSlide9
SW for JAVA P
rogrammer
RDF DB
Ontology files
Mapping tool
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 ontologySlide10
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
();
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 classSlide11
Demo