SPARQL - PowerPoint PPT Presentation

Loading...

PPT – SPARQL PowerPoint presentation | free to download - id: 767589-ZjM2M



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

SPARQL

Description:

SPARQL & JENA ... sparql & jena – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 45
Provided by: mel1191
Learn more at: http://www.intelligence.tuc.gr
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: SPARQL


1
SPARQL JENA
2
Ge????
  • JENA ???a? ??a java framework ??a t?
    d?µ??????a semantic web ?fa?µ????.
  • ?p??e? ?a ???s?µ?p????e? ??a ?a d???????µe se
    RDF/RDFS , OWL , ??a ?a ?????µe storage ,
    inference ?a? ??a e??t?se?? ( queries ) µ?s? t??
    SPARQL.

3
Ge????
  • SPARQL ???a? µ?a query language p?? µa?
    ep?t??pe? ?a ?????µe e??t?se?? se ded?µ??a t??
    semantic web. ???a? ??a t? semantic web ?t? ?a? ?
    SQL ??a t?? s?es?a??? ß?se?? ded?µ????.

4
Part I SPARQL
?e??e??µe?a
Basics 59 Query Structure 10 Prefix
1112 Select 1315 From 1619 Where
2027 Modifiers 2831 Links 32
S??t?µ? pe????af? t?? SPARQL
?e????af? t?? d?µ?? e??? SPARQL query
?pe????s? t?? SPARQL query prefixes
?pe????s? t?? Select Select Distinct
?pe????s? t?? FROM FROM NAME Default Named
Graphs
?pe????s? t?? WHERE FILTERS OPTIONAL - UNION
?pe????s? t?? LIMIT OFFSET ORDER BY
???s?µ?? s??desµ?? ??a t? SPARQL
5
SPARQL Basics
  • H SPARQL ???e? e??t?se?? se t??p??te? rdf. ??t?µe
    d??ad? p??? se ded?µ??a t?? µ??f?? subject
    predicate - object ???s?µ?p????ta? µetaß??t??
    ?p?? ?????µe ?a p????µe apa?t?se??.
  • ??. select ?x where ex1company
    ex1hasEmployee ?x
  • ?? ? e??a? t? a?a?????st??? t?? µetaß??t?? ( ??
    ?x,?y, ?temp1 , ?_temp )

6
SPARQL Basics
  • ??e? ?? t??p??te? se ??a SPARQL query ???????te
    µeta?? t??? µe . .

??. select ?x where ex1company
ex1hasEmployee ?x. ?x ex1employeeName
JOHN
7
SPARQL Basics
  • ?ta? 2 ? pe??ss?te?e? t??p??te? µ???????te t?
    ?d?? subject t?te µp????µe ?a t? pa?a?e????µe
    ???s?µ?p????ta?

??. select ?x where ?x rdftype
ex1Company excompanyName
C1 exhasEmail C1_at_yahoo.gr

8
SPARQL Basics
  • To keyword a t?? SPARQL ???s?µ?p??e?ta? ??a ?a
    d??µe t?? t?p? e??? a?t??e?µ???? (t?? ???s? t??).
    ??s?ast??? e??a? µ?a s??t?µ???af?a ??a t?
    rdftype.

??. select ?x where ?x a
ex1Company
9
SPARQL Basics
  • Se µ?a t??p??ta ded?µ???? t?? Rdf de? e??a?
    ap?a?t?t? ?a d????eta? t? subject ? t? object.
    ??t? a?t?? ????µe ta ?e??µe?a BLANK NODES .
    ?p????µe d??ad? ?a ????µe ded?µ??a t?? µ??f??
    subject predicate ? predicate object
  • ( p? blank node employeeName george )

10
SPARQL Query Structure
  • Lorem ipsum dolor sit amet, consectetuer
    adipiscing elit. Vivamus et magna. Fusce sed sem
    sed magna suscipit egestas.
  • Lorem ipsum dolor sit amet, consectetuer
    adipiscing elit. Vivamus et magna. Fusce sed sem
    sed magna suscipit egestas.

11
SPARQL - Prefix
  • G?a ?a µ?? ??e???eta? ?a ???f??µe ???????a ta
    URIs ,?ta? ???f??µe ??a query , ???s?µ?p????µe
    Prefixes st?? a??? t?? query.
  • ?e???? s?µa?t??? URIs ?a? ta prefixes p??
    ???s?µ?p????µe

PREFIX owllthttp//www.w3.org/2002/07/owlgt
PREFIX xsdlthttp//www.w3.org/2001/XMLSchema
gt PREFIX rdfslthttp//www.w3.org/2000/01/rdf
-schemagt PREFIX rdflthttp//www.w3.org/1999
/02/22-rdf-syntax-nsgt PREFIX
timelthttp//www.w3.org/2006/timegt
12
SPARQL - Prefix
Query ????? prefix
SELECT ?x WHERE lthttp//example.org/bookbook1gt
lthttp//example.org/bookhasWritergt ?x Query
µe prefix PREFIX book lthttp//example.org/book
gt SELECT ?x WHERE bookbook1
bookhasWriter ?x
13
SPARQL - Select
  • ?e t? SELECT ep??????µe t? ?p?s????? p?? ?????µe
    ap? ta ded?µ??a p?? ep?st??fe? ? ap??t?s?.
  • ??

1) SELECT ?x WHERE ?x ex1hasEmployee ?y Ta
ep?t???e? µ??? t? ? µetaß??t? ( ?? Company1 ,
Company2 ) 2) SELECT ?x , ?y WHERE ?x
ex1hasEmployee ?y Ta ep?st???e? ?a? t?? d??
µetaß??t?? ?,y ( ?? Company1,Employee1 ,
Company2,Employee2 3) SELECT WHERE ?x
ex1hasEmployee ?y ?p?st??fe? p??ta ??e? t??
µetaß??t??!
14
SPARQL - Select Distinct
  • To distinct t? ???s?µ?p????µe ??a ?a e?a?e????µe
    t?? eµf???s? t?? ?d??? ap?te??sµat?? pe??ss?te?e?
    ap? µ?a f???? st? ??s?.
  • ??. ?st? ?t? ta ded?µ??a µa? e??a?

Company1 hasEmail c1_at_yahoo.com Company1
hasEmail Company1_at_yahoo.com Company1 hasEmail
Comp1_at_yahoo.com
15
SPARQL - Select Distinct
  • ??? ??t?s??µe µe t? query
  • ?a ap?te??sµata p?? ?a p????µe ?a e??a?
  • ??? a? ??t?s??µe µe
  • Ta p????µe

SELECT ?x where ?x exhasEmail ?y
Company1 , Company1 , Company1
SELECT DISTINCT ?x where ?x exhasEmail ?y
Company1
16
SPARQL - From
  • St? FROM ep??????µe t? dataSet p??? st? ?p???
    ?????µe ?a ??t?s??µe.
  • ?ta? de? ?a???????µe datasets t?te ??t?µe st?
    default dataSet (??a? ???f??).
  • ? SPARQL µa? d??e? t? d??at?t?ta ?a ??t?s??µe st?
    default dataset ?a? se ??a s????? ap? named
    graphs p?? ?a???????ta? ap? t? URI t???.

17
SPARQL Default Graph
  • ?a???????µe t? default graph

PREFIX ex lthttp//example.org/companies/ex1/gt
SELECT ?cname FROM lthttp//example.org/companies
gt WHERE ?x excompanyName ?cname

18
SPARQL Named Graphs
  • ???s?µ?p????ta? named graphs

PREFIX ex lthttp//example.org/companies/ex1/gt
SELECT ?src ?cname FROM lthttp//example.org/comp
anies.rdfgt FROM NAMED lthttp//example.org/ex1/gt
FROM NAMED lthttp//example.org/ex2/gt WHERE
GRAPH ?src ?x
excompanyName ?cname

19
SPARQL Named Graphs
  • ?a??de??µa 2

SELECT DISTINCT ?person WHERE ?person
exname ?name . GRAPH ?g1 ?person a
foafPerson GRAPH ?g2 ?person a foafPerson
GRAPH ?g3 ?person a foafPerson FILTER(
?g1 ! ?g2 ?g1 ! ?g3 ?g2 ! ?g3) .
20
SPARQL - WHERE
  • triplets
  • Filters
  • Optional path expressions
  • Unions
  • ?? WHERE keyword e??a? optional !

Select ?x where ?x ?y ?z
21
SPARQL - Filters
  • Ta filters ???s?µ?p?????te µ?sa st? where clause
    ??a ?a e?a?e????µe , ???s?µ?p????ta? ???????
    pa?ast?se??, a?ep???µ?ta ap?te??sµata.

??. SELECT ?x , ?y WHERE ?x
extakesSalary ?y FILTER( ?y gt 1000 )

22
SPARQL - Filters
  • Logical !, ,
  • Math , -, , /
  • Comparison , !, gt, lt, ...
  • SPARQL tests isURI, isBlank, isLiteral, bound
  • SPARQL accessors str, lang, datatype
  • Other sameTerm, langMatches, regex

23
SPARQL - Filters
  • EXAMPLES
  • FILTER ( langMatches( lang( ?employeeName ) ,
    "EN"))
  • FILTER ( ?date gt 01-02-2004 xsddate
  • ?date lt 0-03-2004xsddate )
  • FILTER ( REGEX( ?name , opoulos )

24
SPARQL Optional Path Expressions
  • ???s?µ?p??e?ta? ?ta? ?????µe ?a a???t?s??µe
    p??a??et??? ??p??a ded?µ??a.
  • ???????G??
  • ??st? ?t? ?????µe ?a p????µe ta ded?µ??a ???? t??
    e??a??µ???? (employees) p?? d???e???? se µ?a
    eta???a ( Company1 )

25
SPARQL Optional Path Expressions
SELECT DISTINCT ?name , ?phone ?email
WHERE ?employee exworksfor excompany1
exemployeeName ?name
exemployeePhone ?phone
exemployeeEmail ?email

??t?? e??a? ??a? ??T?S t??p?? ?a ??t?µe ded?µ??a
??at? se pe??pt?s? p?? ??p???? ?p??????? de? ??e?
??p??? ap? ta st???e?a p?? ??t?µe t?te de? ?a
ep?st?afe? st?? ap??t?s?! ?? a? ??a? ?p???????
de? ??e? email t?te t? ap?t??esµa a?t?
??ßeta?. ?µe?? ?µ?? ?????µe ????? t???
?pa??????? t?? eta???a?. ? ??s? e??a? ? ???s?
t?? optional.
26
SPARQL Optional Path Expressions
SELECT DISTINCT ?name , ?phone ?email
WHERE ?employee exworksfor excompany1
exemployeeName ?name.
optional ?employee exemployeePhone
?phone optional ?employee
exemployeeEmail ?email

??t?? e??a? o SOS??S t??p??. - ?? Optional
?p??e? ?a ???s?µ?p????e? ?a? se Nested
queries ?? optional optional ?t? ?t?.
27
SPARQL - Union
  • ?? UNION t? ???s?µ?p????µe ??a ?a p????µe t??
    ???s? ap? ??p??a set ap?te?esµ?t??.

??. PREFIX ex1 lthttp//example.org/company1/emp
loyeesgt PREFIX ex2 lthttp//example.org/com
pany2/employeesgt SELECT ?name WHERE
?employee ex1employeeName ?name UNION
?employee ex2employeeName ?name
28
SPARQL MODIFIERS
  • ???s?µ?p????µe MODIFIERS ??a ?a
    ?a????s??µe/ep??????µe t? ?p?s????? t??
    ap?te?esµ?t?? p?? ?????µe st?? ap??t?s?.
  • ?as???? MODIFIERS
  • LIMIT ORDER BY OFFSET

29
SPARQL - Limit
  • ?? LIMIT t? ???s?µ?p????µe ??a ?a pe?????s??µe
    t?? a???µ? t?? ap?te?esµ?t?? p?? ?a ep?st?af???.

??. SELECT DISTINCT ?x WHERE ?x a
ex1Company LIMIT 50
30
SPARQL - Order By
  • To ORDER BY ???s?µ?p??e?ta? ??a ?a ta????µ?s??µe
    ta ap?te??sµata (µe ß?s? µ?a µetaß??t? ) µe
    a????sa (ASC) ? f?????sa(DESC) se???.

??. SELECT DISTINCT ?x , ?y WHERE
?x extakesSalary ?y ORDER BY
DESC ( ?y ) LIMIT 50

31
SPARQL - Offset
  • To OFFSET ???s?µ?p??e?ta? se s??d?asµ? µe ta
    LIMIT ?a? ORDER BY ??a ?a ep??????µe t? ?p?s?????
    t?? ap?te?esµ?t?? p?? ?????µe.

??. SELECT DISTINCT ?x , ?y WHERE
?x extakesSalary ?y ORDER BY
DESC ( ?y ) LIMIT 50
OFFSET 20
32
SPARQL Usefull Links
  • SPARQL W3C http//www.w3.org/TR/rdf-sparql-query
    /
  • SPARQL TUTORIALS http//www.cambridgesemantics.c
    om/2008/09/sparql-by-example/
  • http//linkeddatatools.com/querying-semantic-data

33
Part I? JENA
?e??e??µe?a
Basics 34 Loading an Ontology 35-36
Reading writing Ontology Data
3740 Inference 41 Executing Queries
42 Links 43
S??t?µ? pe????af? t?? d??at?t?t?? t?? JENA
??? f??t????µe µ?a ??t?????a ( µe reasoner ?
????? )
??? d?aß????µe ?a? ???f??µe ded?µ??a se µ?a
??t?????a ( classes , properties , statements ?t?
)
??? ?a???µe ??a reasoner ( ?a??de??µa Pellet )
??? e?te???µe ??a SPARQL query µ?s? JENA
???s?µ?? s??desµ?? ??a t? JENA
34
JENA - Basics
  • To framework t?? JENA pe???aµß??e?
  • ??a RDF API
  • E?a OWL API
  • ???at?t?ta ap????e?s?? t?s? st? µ??µ? ?s? ?a? st?
    d?s??.
  • ???at?t?te? Reasoning
  • SPARQL query engine

35
JENA Loading an Ontology
Simple ontology Loader
public OntologyLoader(String fileName) //
ontology that will be used String ontologyUrl
setOntologyUrl("file///" fileName)// the
third slash is needed for windows xp // create
an empty ontology model model
ModelFactory.createOntologyModel(OntModelSpec.OWL_
DL) // read the file model.read( ont )
36
JENA Loading an Ontology
Loading an Ontology With Pellet Reasoner.
public OntologyLoader(String fileName)
private String uri "" private String
ontologyUrl private String fileName
private OntModel ontModel private
OntDocumentManager dm
setOntologyUrl("file///" fileName)// the
third slash is needed for windows xp
OntModel base ModelFactory.createOntologyModel()
//empty model dm base.getDocumentManager(
)// used in this class dm.addAltEntry(uri,
ontologyUrl)// used in this class
base.read(uri) ontModel
ModelFactory.createOntologyModel(PelletReasonerFac
tory.THE_SPEC,base)
37
JENA Reading Writing Data
Reading the Classes from the Model
public void readClasses() ExtendedIteratorltOn
tClassgt iter ontoModel.listClasses()
while(iter.hasNext()) OntClass ontClass
iter.next() System.out.println("CLASS
"ontClass.getLocalName())
Reading the Instances of a Class from the Model
OntClass newClass model.getOntClass( classUrl
) Iterator instances newClass.listInstances()
38
JENA Reading Writing Data
Reading the Datatype Object Properties from the
Model
public void readProperties()
ExtendedIteratorltDatatypePropertygt iter
ontoModel.listDatatypeProperties()
ExtendedIteratorltObjectPropertygt iter2
ontoModel.listObjectProperties()
while(iter.hasNext()) DatatypeProperty
dataProperty iter.next()
System.out.println( dataProperty.getLocalName()
) while(iter.hasNext())
ObjectProperty objProperty iter.next()
System.out.println( objectProperty.getLocalName()
)
39
JENA Reading Writing Data
Reading Statements
Public void readAllStatements(OntModel model)
StmtIterator iter Statement stmt iter
model.listStatements() while (iter.hasNext())
stmt iter.next() // Using the
statement to read SUBJECT-PREDICATE-OBJECT Propert
y predicate Resource subject RDFNode obj
subject stmt.getSubject() System.out.println(
"Subject " subject.getURI()) predicate
stmt.getPredicate() System.out.println("Predicate
"predicate.getLocalName()) obj
stmt.getObject() System.out.println("Object "
obj.toString())
40
JENA Reading Writing Data
Writing Adding Statements to the model.
. . . Model model String namespace
"http//www.example.org" . . . Resource res
model.createResource("http//www.example.com/compa
niesCompany1") Property property1
model.createProperty(namespace,
numOfEmployees") res.addProperty(property1 ,
25) Property property2 model.createProperty(na
mespace, Location") res.addProperty( property2 ,
Athens")
41
JENA Inference
How to use the Pellet reasoner Using the
Ontology Loader from slide 37
// Creating the model using Pellet ontModel
ModelFactory.createOntologyModel(PelletReasonerFac
tory.THE_SPEC,base) // Creating a new Pellet
reasoner PelletInfGraph reasoner reasoner
(PelletInfGraph) ontModel.getGraph() // Calling
the reasoner whenever needed
Reasoner.clasify() Reasoner.realize()
42
JENA Executing a Query
// Create a new queryString queryString
PREFIX ex1 lthttp//example.org/ex1/gt
SELECT ?x WHERE  ?x ex1employeeName
\John\ Query query QueryFactory.create(qu
eryString) // Execute the query and obtain
results. model is an OntModel. QueryExecution qe
QueryExecutionFactory.create(query,
model)ResultSet results qe.execSelect() //
Output query resultsResultSetFormatter.out(System
.out, results, query) // Important free up
resources used running the queryqe.close()
43
JENA Links
The JENA Framework http//jena.sourceforge.net/
TUTORIAL ??a a????????. ??? pe???µe t??
ß?ß??????e? t?? JENA st? Eclipse
http//www.iandickinson.me.uk/articles/jena-ecli
pse-helloworld/ IBM JENA Tutorial http//www.ibm.
com/developerworks/xml/library/j-jena/
44
(No Transcript)
About PowerShow.com