Title: Jena A Semantic Web Framework for Java
1Jena A Semantic Web Framework for Java
Query Ontology with Jena
2- Jena API
- Building Semantic web application
- RDF, RDFS, OWL
- RDQL SPARQL
3Skill.rdf
lt?xml version"1.0"?gt lt!DOCTYPE rdfRDF
lt!ENTITY rdf 'http//www.w3.org/1999/02/22-rdf-sy
ntax-ns'gt lt!ENTITY rdfs 'http//www.w3.org/20
00/01/rdf-schema'gt lt!ENTITY expertise
'http//jena.hpl.hp.com/expertise'gtgt ltrdfRDF
xmlnsrdf"rdf" xmlnsrdfs"rdfs"
xmlnsexpertise"expertise"
xmlns"expertise"gt ltrdfDescription
rdfabout"expertiseA."gt ltlabel
rdfdatatype"http//www.w3.org/2001/XMLSchemastr
ing"gtGeneral Literaturelt/labelgt ltsubClassOf
rdfresource"expertiseSkill"/gt lt/rdfDescriptio
ngt ltrdfDescription rdfabout"expertiseA.0"gt ltl
abel rdfdatatype"http//www.w3.org/2001/XMLSchem
astring"gtGENERALlt/labelgt ltsubClassOf
rdfresource"expertiseA."/gt lt/rdfDescriptiongt
ltrdfDescription rdfabout"expertiseA.0_0"gt ltla
bel rdfdatatype"http//www.w3.org/2001/XMLSchema
string"gtBiographies/autobiographieslt/labelgt ltsubC
lassOf rdfresource"expertiseA.0"/gt lt/rdfDescr
iptiongt ltrdfDescription rdfabout"expertiseA.0
_1"gt ltlabel rdfdatatype"http//www.w3.org/2001/X
MLSchemastring"gtConference proceedingslt/labelgt lts
ubClassOf rdfresource"expertiseA.0"/gt lt/rdfDe
scriptiongt ltrdfDescription rdfabout"expertise
A.1"gt ltlabel rdfdatatype"http//www.w3.org/2001/
XMLSchemastring"gtINTRODUCTORY AND
SURVEYlt/labelgt ltsubClassOf rdfresource"expertis
eA."/gt lt/rdfDescriptiongt ltrdfDescription
rdfabout"expertiseB."gt ltlabel
rdfdatatype"http//www.w3.org/2001/XMLSchemastr
ing"gtHardwarelt/labelgt ltsubClassOf
rdfresource"expertiseSkill"/gt lt/rdfDescriptio
ngt
4Statement
the subject is the resource the predicate is the
property the object is the resource or literal
pointed to A statement is sometimes called a
triple, because of its three parts.
5List Satements
Model modelSkill ModelFactory.createDefaultMode
l() modelSkill.read(new
FileInputStream("C\\Skill.rdf"),
"http//jena.hpl.hp.com/expertise",
"RDF/XML")
???????????? Model ??????? Jena
Model StmtIterator iter model.listStatements()
while (iter.hasNext()) Statement stmt
iter.nextStatement() Resource subject
stmt.getSubject() Property predicate
stmt.getPredicate() RDFNode object
stmt.getObject() System.out.print(subject.toStr
ing()) System.out.print(predicate.toString())
System.out.print(object.toString())
list all statements
http//jena.hpl.hp.com/A. http//jena.hpl.hp.com/e
xpertiselabel "General Literature
6Query Model
Model modelSkill ModelFactory.createDefaultMode
l() modelSkill.read(new
FileInputStream("C\\Skill.rdf"),
"http//jena.hpl.hp.com/expertise",
"RDF/XML")
???????????? Model ??????? Jena
private static Property label ResIterator iter
modelSkill.listSubjectsWithProperty(label)
while (iter.hasNext())
Resource r iter.nextResource()
System.out.println(r.getLocalName())
http//jena.hpl.hp.com/A. http//jena.hpl.hp.com/A
.0 http//jena.hpl.hp.com/A.0.0 http//jena.hpl.hp
.com/A.0.1 http//jena.hpl.hp.com/A.1 http//jena.
hpl.hp.com/B.
7Query Model
Model modelSkill ModelFactory.createDefaultMode
l() modelSkill.read(new
FileInputStream("C\\Skill.rdf"),
"http//jena.hpl.hp.com/expertise",
"RDF/XML")
???????????? Model ??????? Jena
ResIterator iter modelSkill.listSubjectsWithPro
perty(label, General Literature)
while (iter.hasNext()) Resource
r iter.nextResource()
System.out.println(r.getLocalName())
http//jena.hpl.hp.com/A.
General Literature
8Query Model
Model modelSkill ModelFactory.createDefaultMode
l() modelSkill.read(new
FileInputStream("C\\Skill.rdf"),
"http//jena.hpl.hp.com/expertise",
"RDF/XML")
???????????? Model ??????? Jena
NodeIterator iter modelSkill.listObjects()
while (iter.hasNext())
Object o iter.nextNode() if
(o instanceof Resource)
System.out.println("Resource "o.toString())
else //if o
is a literal
System.out.println("Literal " o.toString())
Resource http//jena.hpl.hp.com/A.0 Literal
Program synthesishttp//www.w3.org/2001/XMLSchema
string
9Query Engine RDQL
Model modelSkill ModelFactory.createDefaultMode
l() modelSkill.read(new
FileInputStream("C\\Skill.rdf"),
"http//jena.hpl.hp.com/expertise",
"RDF/XML")
???????????? Model ??????? Jena
1. String queryString "SELECT ?x ?label "
"WHERE (?x, lthttp//jena.hpl.hp.c
om/expertiselabelgt, ?lable)" 2.Query query
new Query(queryString) 3.query.setSource(modelSki
ll) 4.QueryExecution qe (QueryExecution) new
QueryEngine(query) 5.QueryResults results
qe.exec() 6.for (Iterator iter results
iter.hasNext()) ResultBinding res
(ResultBinding) iter.next()
Resource r (Resource) res.get("x")
Literal l (Literal) res.get("label")
System.out.println("Resource "
r.getLocalName() " Literal "
l.getLocalName) results.close()
RDQL Query
Resource A. Literal General
Literature Resource A.0 Literal
GENERAL Resource B. Literal Hardware .... ....
10Query Engine RDQL
Ex.
SELECT ?label WHERE
(lthttp//jena.hpl.hp.com/expertiseA.0gt,
lthttp//jena.hpl.hp.com/expertiselabelgt, ?lable)
Output General Literature
SELECT ?x WHERE (?x, lthttp//jena.hpl.hp.com/exper
tisesubClassOfgt, lthttp//jena.hpl.hp.com/expertis
eA.gt)
Output A.0 , A.1
????????????????????????? A. ?
- Reasoning
?
11Reasoning
String demoURI "http//jena.hpl.hp.com/expertis
e" PrintUtil.registerPrefix("expertise",
demoURI) // Create an (RDF) specification of a
hybrid reasoner which // loads its data from an
external file. Model m ModelFactory.createDefau
ltModel() Resource configuration
m.createResource() configuration.addProperty(Rea
sonerVocabulary.PROPruleSet, "C\\demo.rules")
configuration.addProperty(ReasonerVocabulary.PROPr
uleMode, "hybrid") // Create an instance of
such a reasoner Reasoner reasoner
GenericRuleReasonerFactory.theInstance().create(c
onfiguration) // Load test data InfModel
infmodel ModelFactory.createInfModel(reasoner,
modelSkill) // Query for things related "a" by
"p" Property p modelSkill.getProperty(demoURI,
"subClassOf") Resource r modelSkill.getResou
rce(demoURI "B.")
printStatements(infmodel, r, p, null)
public static void printStatements(Model m,
Resource r, Property p, Resource o) for
(StmtIterator i m.listStatements(o, p, r)
i.hasNext()) Statement stmt
i.nextStatement() String skill
stmt.getSubject().getLocalName()
System.out.println(skill)
12Reasoning
demo.rules
r1 (?x ?p ?a),(?b ?p ?x) -gt (?b ?p ?a)
?x, ?a, ?b Resource, Object ?p Property
(SubClassOf)
??? x ???? subClassOf a ??? b ???? SubClassOf
x ?????? b ?????? SubClassOf a ???????
Query ????????????????????????? A.
Output A.0, A.0.0, A.0.1, A.1
13??????????????????????????
http//jena.sourceforge.net/inference/ http//www.
w3.org/Submission/RDQL/ Download Jena API
http//sourceforge.net/projects/jena/files/