Download Jena (Java)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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’s foaf 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
1.
2.
3.
4.
5.
6.
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);
StmtIterator workPages =
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
iCal
SIOC
Tag
FOAF
listener
Operator
Mapping
tool
Jena2 Platform
(RDF model + Reasoning Engine +
Persistence System)
Ontology
files
JAVA VM
RDF DB
Code
Create mapping
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();
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);
Demo