SPARQL Part III Jan Pettersen Nytun, UiA 1 S Agenda O P - - PowerPoint PPT Presentation

sparql
SMART_READER_LITE
LIVE PREVIEW

SPARQL Part III Jan Pettersen Nytun, UiA 1 S Agenda O P - - PowerPoint PPT Presentation

SPARQL Part III Jan Pettersen Nytun, UiA 1 S Agenda O P Example with: - ORDER BY - SUM Example continues with: - GROUP BY - GROUP BY together with SUM Example continues with: - HAVING - BIND - CONCAT New example with:


slide-1
SLIDE 1

1

SPARQL

Part III

Jan Pettersen Nytun, UiA

slide-2
SLIDE 2

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 2

Agenda

slide-3
SLIDE 3

S O

P

Example: Explore salaries of the employees of a company…

Jan Pettersen Nytun, UIA, page 3

slide-4
SLIDE 4

4

slide-5
SLIDE 5

S O

P

Jan Pettersen Nytun, UIA, page 5

slide-6
SLIDE 6

S O

P

List Salaries, i.e., list employee, year and amount

Jan Pettersen Nytun, UIA, page 6

Step 1: What to list

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX : <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary#>

SELECT ?employee ?year ?amount WHERE { … }

slide-7
SLIDE 7

S O

P

Jan Pettersen Nytun, UIA, page 7

?salary ?employee :hasSalary

Step 2: Get all resources with salary

SELECT ?employee ?year ?amount WHERE { ?employee :hasSalary :salary }

slide-8
SLIDE 8

S O

P

Jan Pettersen Nytun, UIA, page 8

?employee :hasSalary ?salary

Totally 6 matches:

:bob :hasSalary :bobSalaray2013 . :bob :hasSalary :bobSalaray2014 . …

slide-9
SLIDE 9

S O

P

What if some other types of resources have salaries?

Jan Pettersen Nytun, UIA, page 9

Employee ?employee rdf:type

SELECT ?employee ?year ?amount WHERE { ?employee :hasSalary ?salary .

?employee rdf:type :Employee }

:hasSalary ?salary

slide-10
SLIDE 10

S O

P

Jan Pettersen Nytun, UIA, page 10

Employee ?employee rdf:type :hasSalary ?salary Totally 6 matches, but showing only one match: Again there are 6 matches. Showing one of them:

(composed of 2 triples):

:bob :hasSalary :bobSalaray2014 . :bob rdf:type :Employee .

slide-11
SLIDE 11

S O

P

Jan Pettersen Nytun, UIA, page 11

Step 3: Get year and amount also [4]

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX : <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary#>

SELECT ?employee ?year ?amount WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount}

slide-12
SLIDE 12

S O

P

List Salaries Ordered By Year [4]

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX : <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary#>

SELECT ?employee ?year ?amount WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount} ORDER BY ?year

Jan Pettersen Nytun, UIA, page 12

slide-13
SLIDE 13

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 13

Agenda

slide-14
SLIDE 14

S O

P

Total Sum Of All Salaries [4]

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX : <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary#>

SELECT (SUM (?amount) AS ?total) WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount}

Jan Pettersen Nytun, UIA, page 14

slide-15
SLIDE 15

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 15

Agenda

slide-16
SLIDE 16

S O

P

How to get: Salaries Per Year

Jan Pettersen Nytun, UIA, page 16

slide-17
SLIDE 17

S O

P

Jan Pettersen Nytun, UIA, page 17

WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount} :e1 :hasSalary :s1 . :s1 :hasYear “2001”. :s1 :hasAmount “100” .

Assume this gives the following 4 matches:

:e2 :hasSalary :s2 . :s2 :hasYear “2003”. :s2 :hasAmount “200” . :e3 :hasSalary :s3 . :s3 :hasYear “2003”. :s3 :hasAmount “1300” . :e4 :hasSalary :s4 . :s4 :hasYear “2001”. :s4 :hasAmount “400” .

[3] By default a solution set consists of a single group, containing all solutions.

slide-18
SLIDE 18

S O

P

SELECT … WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount} GROUP BY ?year

Jan Pettersen Nytun, UIA, page 18

Grouping may be specified using GROUP BY.

slide-19
SLIDE 19

S O

P

page 19

WHERE { … } GROUP BY ?year

:e1 :hasSalary :s1 . :s1 :hasYear “2001”. :s1 :hasAmount “100” .

Gives 2 groups:

:e2 :hasSalary :s2 . :s2 :hasYear “2003”. :s2 :hasAmount “200” . :e3 :hasSalary :s3 . :s3 :hasYear “2003”. :s3 :hasAmount “1300” . :e4 :hasSalary :s4 . :s4 :hasYear “2001”. :s4 :hasAmount “400” .

:e1 :hasSalary :s1 . :s1 :hasYear “2001”. :s1 :hasAmount “100” . :e2 :hasSalary :s2 . :s2 :hasYear “2003”. :s2 :hasAmount “200” . :e3 :hasSalary :s3 . :s3 :hasYear “2003”. :s3 :hasAmount “1300” . :e4 :hasSalary :s4 . :s4 :hasYear “2001”. :s4 :hasAmount “400” .

slide-20
SLIDE 20

S O

P

Aggregates

Aggregates apply expressions over groups of solutions. Aggregates defined in version 1.1 of SPARQL are COUNT, SUM, MIN, MAX, AVG, ... SELECT ?year (SUM (?amount) AS ?total) WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount} GROUP BY ?year

Jan Pettersen Nytun, UIA, page 20

slide-21
SLIDE 21

S O

P

page 21

SELECT ?year (SUM (?amount) AS ?total) ….

Gives 2 results:

year total 2001 500 2003 1500

:e1 :hasSalary :s1 . :s1 :hasYear “2001”. :s1 :hasAmount “100” . :e2 :hasSalary :s2 . :s2 :hasYear “2003”. :s2 :hasAmount “200” . :e3 :hasSalary :s3 . :s3 :hasYear “2003”. :s3 :hasAmount “1300” . :e4 :hasSalary :s4 . :s4 :hasYear “2001”. :s4 :hasAmount “400” .

slide-22
SLIDE 22

S O

P

Salaries Per Year [4]

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX : <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary#>

SELECT ?year (SUM (?amount) AS ?total) WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount} GROUP BY ?year

page 22

slide-23
SLIDE 23

S O

P

Adding several salaries per year for some employees :

Jan Pettersen Nytun, UIA, page 23

slide-24
SLIDE 24

24 @prefix : <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @base <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary> . <http://www.uia.no/ikt437/janpettersennytun/ontologies/salary> rdf:type owl:Ontology . :hasSalary rdf:type owl:ObjectProperty ; rdfs:domain :Employee ; rdfs:range :Salary . :hasAmount rdf:type owl:DatatypeProperty ; rdfs:range xsd:int . :hasName rdf:type owl:DatatypeProperty . :hasYear rdf:type owl:DatatypeProperty ; rdfs:range xsd:int .

Ontology used:

slide-25
SLIDE 25

S O

P

How to see the salaries summed up for each employees by year [4]

SELECT ?year ?employee (SUM (?amount) AS ?total) WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount } GROUP BY ?year ?employee

Jan Pettersen Nytun, UIA, page 25

slide-26
SLIDE 26

S O

P

[4]

SELECT ?year ?employee (SUM (?amount) AS ?total) WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount } GROUP BY ?year ?employee ORDER BY ?year ?employee

slide-27
SLIDE 27

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 27

Agenda

slide-28
SLIDE 28

S O

P

Only showing yearly salaries higher than 250000 [4]

SELECT ?year ?employee (SUM (?amount) AS ?total) WHERE { ?employee :hasSalary ?salary . ?salary :hasYear ?year . ?salary :hasAmount ?amount } GROUP BY ?year ?employee HAVING (?total > 250000) ORDER BY ?year ?employee

Jan Pettersen Nytun, UIA, page 28

Old query result:

slide-29
SLIDE 29

S O

P

[4]: HAVING and FILTER are very similar ... FILTER refers to variables bound within a particular graph pattern … always appears in the pattern (between “{“ and ”}”), while HAVING refers to variables defined by aggregations in the SELECT clause, and hence always appears outside a graph pattern.

Jan Pettersen Nytun, UIA, page 29

slide-30
SLIDE 30

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 30

Agenda

slide-31
SLIDE 31

S O

P

Assignment

[4]: An assignment lets the query write specifically the value of a variable through some computation—“assigning” a value to that variable, rather than matching some value in the data.

Jan Pettersen Nytun, UIA, page 31

slide-32
SLIDE 32

S O

P :Bart :hasParent :Homer . :Bart :hasParent :Marge .

:Lisa :hasParent :Homer . :Lisa :hasParent :Marge . :Maggie :hasParent :Homer . :Maggie :hasParent :Marge . :Ling :hasParent :Selma .

###

:Herb :hasParent :Abraham . :Herb :hasParent :Mona . :Homer :hasParent :Abraham . :Homer :hasParent :Mona . :Marge :hasParent :Clancy . :Marge :hasParent :Jackie . :Patty :hasParent :Clancy . :Patty :hasParent :Jackie . :Selma :hasParent :Clancy . :Selma :hasParent :Jackie .

###

:Abraham :gender :male . :Mona :gender :female . :Clancy :gender :male . :Jackie :gender :female . :Herb :gender :male . :Homer :gender :male . :Marge :gender :female . :Patty :gender :female . :Selma :gender :female .

:Bart :gender :male .

:Lisa :gender :female . :Maggie :gender :female . :Ling :gender :female .

[http://www.englishexercises.org/makeagame/viewgame.asp?id=4]

@prefix : www.simpsons.no/simpsonsfamily

slide-33
SLIDE 33

S O

P

How to also list grandchild – which in our example is always :Maggie

Jan Pettersen Nytun, UIA, page 33

2 Etc.

slide-34
SLIDE 34

S O

P

PREFIX : <http://www.simpsons.no/simpsonsfamily#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?grandmother WHERE { :Maggie :hasParent ?parent. ?parent :hasMother ?grandmother } ORDER BY ?grandmother

Jan Pettersen Nytun, UIA, page 34

2 Etc.

slide-35
SLIDE 35

S O

PPREFIX : <http://www.simpsons.no/simpsonsfamily#>

PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?grandchildName ?grandmother WHERE { :Maggie foaf:name ?grandchildName . :Maggie :hasParent ?parent. ?parent :hasMother ?grandmother } ORDER BY ?grandmother

Jan Pettersen Nytun, UIA, page 35

2 Etc.

slide-36
SLIDE 36

S O

PPREFIX : <http://www.simpsons.no/simpsonsfamily#>

PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT ?grandchildName ?grandmother WHERE { :Maggie :hasParent ?parent. ?parent :hasMother ?grandmother BIND ("some name" as ?grandchildName) } ORDER BY ?grandmother

Jan Pettersen Nytun, UIA, page 36

2 Etc.

slide-37
SLIDE 37

S O

P

Jan Pettersen Nytun, UIA, page 37

[3]:

slide-38
SLIDE 38

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 38

Agenda

slide-39
SLIDE 39

S O

P

concat

SELECT (concat (?first, " ", ?last) AS ?fullname) WHERE { :WorkingOntologist dc:creator ?author . ?author :firstName ?first . ?author :lastName ?last . }

page 39

[4]: [3]:

slide-40
SLIDE 40

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 40

Agenda

slide-41
SLIDE 41

S O

P

page 41

slide-42
SLIDE 42

Who is who's grandfather?

?grandchild :hasParent ?parent . ?parent :hasParent ?grandfather .

slide-43
SLIDE 43

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX : <http://uia.no/fame#> SELECT ?grandchild ?grandfather WHERE { ?grandchild :hasParent ?parent . ?parent :hasParent ?grandfather . ?grandfather :gender :male }

slide-44
SLIDE 44

S O

P

SPARQL RULES—USING SPARQL AS A RULE LANGUAGE [4]

SPARQL CONSTRUCT allows us to specify templates of new information based on patterns found in old information… sometimes called a Rule… “Whenever you see this, conclude that.” Example.: If John’s father is Joe, then Joe’s son is John

Jan Pettersen Nytun, UIA, page 44

slide-45
SLIDE 45

S O

P

CONSTRUCT

  • The result of a query may be an RDF graph.
  • [3]: The CONSTRUCT query form returns a

single RDF graph specified by a graph template.

Jan Pettersen Nytun, UIA, page 45

slide-46
SLIDE 46

46

CONSTRUCT QUERY

Returns a single RDF graph specified by a graph template

slide-47
SLIDE 47

S O

P

USING RESULTS OF CONSTRUCT QUERIES [4]

Sophisticated RDF query systems provide workbenches … a variety of options for what to do with constructed triples:

  • Insert the constructed triples back into the
  • riginal data source…
  • Store the constructed triples as a separate

graph…

  • Store the constructed triples into a new dataset

(in another database)…

  • Serialize the results…… and save them to a file.

Jan Pettersen Nytun, UIA, page 47

slide-48
SLIDE 48

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 48

Agenda

slide-49
SLIDE 49

The CONSTRUCT Statement returns a separate graph. Use INSERT if you want to have the triples stored in the ontology.

slide-50
SLIDE 50

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX : <http://uia.no/fame#> INSERT {?grandchild :hasGrandFather ?grandfather } WHERE { ?grandchild :hasParent ?parent . ?parent :hasParent ?grandfather . ?grandfather :gender :male }

<http://uia.no/fame#Bart> <http://uia.no/fame#hasGrandFather> <http://uia.no/fame#Abraham> .

  • Etc. …

Adds to ontology:

slide-51
SLIDE 51

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 51

Agenda

slide-52
SLIDE 52

52

DELETE Statement

slide-53
SLIDE 53

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 53

Agenda

slide-54
SLIDE 54

S O

P

Rule Example: The Nephew/Niece-Uncle Relation

Construct all nephew-uncle triples given the following triples:

:Ester rdf:type :Woman ; :hasBrother :Olav , :Sigmund . :Jan rdf:type :Man ; :hasParent :Sigmund . :Kirsten rdf:type :Woman ; :hasParent :Sigmund . :Olav rdf:type :Man ; :hasSister :Ester ; :hasBrother :Sigmund . :Sigmund rdf:type :Man ; :hasSister :Ester . :Sigrund rdf:type :Woman ; :hasParent :Sigmund . The following triples should be produced: :Jan :hasUncle :Olav. :Kirsten :hasUncle :Olav.

Jan Pettersen Nytun, UIA, page 54

(The niece/nephew-ant relations are ignored.)

slide-55
SLIDE 55

S O

P

PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> CONSTRUCT {?n :hasUncle ?uncle} WHERE { ?n :hasParent ?parent. ?parent :hasBrother ?uncle }

Jan Pettersen Nytun, UIA, page 55

:Ester rdf:type :Woman ; :hasBrother :Olav , :Sigmund . :Jan rdf:type :Man ; :hasParent :Sigmund . :Kirsten rdf:type :Woman ; :hasParent :Sigmund . :Olav rdf:type :Man ; :hasSister :Ester ; :hasBrother :Sigmund . :Sigmund rdf:type :Man ; :hasSister :Ester . :Sigrund rdf:type :Woman ; :hasParent :Sigmund .

The result of this CONSTRUCT statement is empty – why?

slide-56
SLIDE 56

S O

P

PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> CONSTRUCT {?n :hasUncle ?uncle} WHERE { ?n :hasParent ?parent. ?parent :hasBrother ?uncle }

Jan Pettersen Nytun, UIA, page 56

:Ester rdf:type :Woman ; :hasBrother :Olav , :Sigmund . :Jan rdf:type :Man ; :hasParent :Sigmund . :Kirsten rdf:type :Woman ; :hasParent :Sigmund . :Olav rdf:type :Man ; :hasSister :Ester ; :hasBrother :Sigmund . :Sigmund rdf:type :Man ; :hasSister :Ester . :Sigrund rdf:type :Woman ; :hasParent :Sigmund .

Change order of ?parent and ?uncle in last triple of the WHERE-clause and there will be match on :Olav :hasBrother :Sigmund . However, one could add triple :Sigmund :hasBrother :Olav . to the RDF data.

slide-57
SLIDE 57

S O

P

PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> CONSTRUCT {?n :hasUncle ?uncle} WHERE { ?n :hasParent ?parent. ?uncle :hasBrother ?parent. ?uncle rdf:type :Man. }

Jan Pettersen Nytun, UIA, page 57

:Ester rdf:type :Woman ; :hasBrother :Olav , :Sigmund . :Jan rdf:type :Man ; :hasParent :Sigmund . :Kirsten rdf:type :Woman ; :hasParent :Sigmund . :Olav rdf:type :Man ; :hasSister :Ester ; :hasBrother :Sigmund . :Sigmund rdf:type :Man ; :hasSister :Ester . :Sigrund rdf:type :Woman ; :hasParent :Sigmund .

slide-58
SLIDE 58

S O

P

UNION

A graph pattern is made up of several triples—all

  • f which have to match in order for the pattern to

match… there is an implicit “and” operation between the triples… if we want one “or” another triple to match we may use UNION (if both match then there will be two different matches).

Jan Pettersen Nytun, UIA, page 58

slide-59
SLIDE 59

S O

P

Use UNION to fix the following:

Jan Pettersen Nytun, UIA, page 59

PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> CONSTRUCT {?n :hasUncle ?uncle} WHERE { ?n :hasParent ?parent. ?parent :hasBrother ?uncle }

We need an or (i.e., UNION) between “?parent :hasBrother ?uncle” and “?uncle :hasBrother ?parent”

slide-60
SLIDE 60

S O

P

Jan Pettersen Nytun, UIA, page 60

PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> CONSTRUCT {?n :hasUncle ?uncle} WHERE {?n :hasParent ?parent.

{?uncle :hasBrother ?parent.} UNION {?parent :hasBrother ?uncle } .

?uncle rdf:type :Man. }

:Ester rdf:type :Woman ; :hasBrother :Olav , :Sigmund . :Jan rdf:type :Man ; :hasParent :Sigmund . :Kirsten rdf:type :Woman ; :hasParent :Sigmund . :Olav rdf:type :Man ; :hasSister :Ester ; :hasBrother :Sigmund . :Sigmund rdf:type :Man ; :hasSister :Ester; :hasBrother :Rolf . :Sigrund rdf:type :Woman ; :hasParent :Sigmund . :Rolf rdf:type :Man.

slide-61
SLIDE 61

S O

P

[4] Mapping several related relationships (mother, father, son, daughter) onto a single hierarchy (parent); CONSTRUCT {?s :hasParent ?o} WHERE{ {?s :hasMother ?o} UNION {?s :hasFather ?o} UNION {?o :hasSon ?s} UNION {?o :hasDaughter ?s}}

Jan Pettersen Nytun, UIA, page 61

slide-62
SLIDE 62

S O

P

 Example with:

  • ORDER BY
  • SUM

 Example continues with:

  • GROUP BY
  • GROUP BY together with SUM

 Example continues with:

  • HAVING
  • BIND
  • CONCAT

 New example with:

  • CONSTRUCT
  • INSERT
  • DELETE
  • UNION
  • COUNT

page 62

Agenda

slide-63
SLIDE 63

S O

P

Aggregates And Grouping [3]

Jan Pettersen Nytun, UIA, page 63

Aggregates apply expressions over groups

  • f solutions.

By default a solution set consists of a single group, containing all solutions. Grouping may be specified using the GROUP BY. Aggregates: COUNT, SUM, MIN,….

slide-64
SLIDE 64

S O

P

Count

[3]: Count is a SPARQL set function which counts the number of times a given expression has a bound ... [4]: For example, we could find out how many movies James Dean has played in: SELECT (COUNT (?movie) AS ?howmany) WHERE {:JamesDean ?playedIn ?movie .}

Jan Pettersen Nytun, UIA, page 64

slide-65
SLIDE 65

S O

P

How many uncles do :Jan have?

Jan Pettersen Nytun, UIA, page 65

slide-66
SLIDE 66

S O

P

PREFIX : <http://www.uia.no/janpettersennytun/ontologies/family#> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX owl: <http://www.w3.org/2002/07/owl#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> SELECT DISTINCT (COUNT (?uncle) AS ?numberOfUnclesForJan) WHERE { :Jan :hasParent ?parent. {?uncle :hasBrother ?parent.} UNION {?parent :hasBrother ?uncle } . ?uncle rdf:type :Man. }

Jan Pettersen Nytun, UIA, page 66

slide-67
SLIDE 67

S O

P

References

Jan Pettersen Nytun, UIA, page 67

[1] Book: David Poole and Alan Mackworth, Artificial Intelligence: Foundations of Computational Agents, Cambridge University Press, 2010, http://artint.info/ [2] http://www.w3.org/TR/swbp-n-aryRelations/ [3] SPARQL 1.1 Query Language, W3C Recommendation 21 March 2013, http://www.w3.org/TR/2013/REC-sparql11-query-20130321/ [4] Semantic Web for the Working Ontologist, Second Edition: Effective Modeling in RDFS and OWL, May 20, 2011, by Dean Allemang, James Hendler [5] Appreciating SPARQL CONSTRUCT more, Bob DuCharme's weblog, http://www.snee.com/bobdc.blog/2009/09/appreciating-sparql-construct.html