Jan Pettersen Nytun, UiA

Slides:



Advertisements
Similar presentations
Alexandra Cristea & Matthew Yau 1.
Advertisements

SPARQL Dimitar Kazakov, with references to material by Noureddin Sadawi ARIN, 2014.
CH-4 Ontologies, Querying and Data Integration. Introduction to RDF(S) RDF stands for Resource Description Framework. RDF is a standard for describing.
RDF Tutorial.
 Copyright 2004 Digital Enterprise Research Institute. All rights reserved. SPARQL Query Language for RDF presented by Cristina Feier.
Chapter 3 Querying RDF stores with SPARQL. TL;DR We will want to query large RDF datasets, e.g. LOD SPARQL is the SQL of RDF SPARQL is a language to query.
Semantic Web Andrejs Lesovskis. Publishing on the Web Making information available without knowing the eventual use; reuse, collaboration; reproduction.
Chapter 3A Semantic Web Primer 1 Chapter 3 Querying the Semantic Web Grigoris Antoniou Paul Groth Frank van Harmelen Rinke Hoekstra.
Ontologies: Making Computers Smarter to Deal with Data Kei Cheung, PhD Yale Center for Medical Informatics CBB752, February 9, 2015, Yale University.
Logics for Data and Knowledge Representation SPARQL Protocol and RDF Query Language (SPARQL) Feroz Farazi.
1 Ontology Query and Reasoning Payam Barnaghi Institute for Communication Systems (ICS) Faculty of Engineering and Physical Sciences University of Surrey.
SPARQL All slides are adapted from the W3C Recommendation SPARQL Query Language for RDF Web link:
Master Informatique 1 Semantic Technologies Part 5SPARQL Werner Nutt.
Chapter 6 Understanding Each Other CSE 431 – Intelligent Agents.
Example: Jena and Fuseki
RDF Query language The following slides are from Grigoris Antoniou, Frank van Harmelen, “A Semantic Web Primer” Dean Allemang, Jim Hendler, “Semantic Web.
Logics for Data and Knowledge Representation
The Semantic Web Web Science Systems Development Spring 2015.
Chapter 3 Querying RDF stores with SPARQL. Why an RDF Query Language? Why not use an XML query language? XML at a lower level of abstraction than RDF.
Master Informatique 1 Semantic Technologies Part 11Direct Mapping Werner Nutt.
SPARQL W3C Simple Protocol And RDF Query Language
Using the DAWG Test Cases with Relational Databases Matthew Gheen October 26, 2007.
Ontology engineering Lab #8 – October 20, 2014.
The Semantic Web Matt Klubertanz. What is it? “The Semantic Web is an extension of the current web in which information is given well- defined meaning,
05/01/2016 SPARQL SPARQL Protocol and RDF Query Language S. Garlatti.
Chapter 3 Querying RDF stores with SPARQL
Alexandra Cristea 1.  pronounced "sparkle“  recursive acronym for: ◦ SPARQL Protocol and RDF Query Language  a semantic query language  a query language.
Of 38 lecture 6: rdf – axiomatic semantics and query.
CC L A W EB DE D ATOS P RIMAVERA 2015 Lecture 7: SPARQL (1.0) Aidan Hogan
Steven Perry Dave Vieglais. W a s a b i Web Applications for the Semantic Architecture of Biodiversity Informatics Overview WASABI is a framework for.
Semantic Web for the Working Ontologist Dean Allemang Jim Hendler SNU IDB laboratory.
NEDA ALIPANAH, MARIA ADELA GRANDO DBMI 11/19/2012.
1 Knowledge Representation XI – IKT437 Knowledge Representation XI – IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena.
1 RDF Storage and Retrieval Systems Jan Pettersen Nytun, UiA.
Jan Pettersen Nytun, UIA, page 1 Knowledge Representation Part IV The Semantics Web Starting with XML Jan Pettersen Nytun, UiA.
OWL (Ontology Web Language and Applications) Maw-Sheng Horng Department of Mathematics and Information Education National Taipei University of Education.
Knowledge Representation XI – IKT437 Part II OWL, SPARQL, …
Knowledge Representation Part IV The Semantics Web Starting with XML
Knowledge Representation Part VI
Charlie Abela Department of Intelligent Computer Systems
Knowledge Representation Part V RDF
Vincenzo Maltese, Fausto Giunchiglia University of Trento
CC La Web de Datos Primavera 2017 Lecture 7: SPARQL [i]
SPARQL.
Reasoning with Rules SWRL as Example
Knowledge Representation Part II Description Logic & Introduction to Protégé Jan Pettersen Nytun.
Triple Stores.
Knowledge Representation Part VII Protégé / RDFS / OWL / ++
Knowledge Representation Part VI
Middleware independent Information Service
Knowledge Representation Part II
SPARQL: A query language for RDF
SPARQL + RDF Based on: Prof. Benny Kimelfled’s lecture notes
SPARQL Exercise Much of this exercise has been copied from: INF3580/INF4580 – MANDATORY EXERCISE 3
Bus Routes.
Logics for Data and Knowledge Representation
CC La Web de Datos Primavera 2016 Lecture 7: SPARQL (1.0)
Hong Sun, AGFA Healthcare
Sparql Examples.
Triple Stores.
Linking Guide Michel Böhms.
SPARQL Exercise Most of this exercise has been copied from: INF3580/INF4580 – MANDATORY EXERCISE 3
CC La Web de Datos Primavera 2018 Lecture 8: SPARQL [1.1]
Example: Jena and Fuseki
Sheet 10 RDF (2).
Knowledge Representation Part VII Protégé / RDFS / OWL / ++
Logics for Data and Knowledge Representation
Triple Stores.
Triple Stores.
Login Main Functions Via SAS Information Delivery Portal
Presentation transcript:

Jan Pettersen Nytun, UiA SPARQL Part II Jan Pettersen Nytun, UiA

First a small SPARQL example Jan Pettersen Nytun, UiA

[4]: Ask: SELECT ?who WHERE { :JamesDean :playedIn ?what . ?what :directedBy ?who . } Answer: :GeorgeStevens, :EliaKazan, :NicholasRay, :FredGuiol

More Meaningful Variable Names [4]:

Installing Your Own SPARQL Endpoint Jan Pettersen Nytun, UiA

Apache Jena Fuseki SPARQL server / SPARQL endpoint enables users to query a knowledge base via the SPARQL language Has a user interface for server monitoring and administration. Provides the SPARQL 1.1 protocols for query and update.

TDB Fuseki is tightly integrated with TDB to provide a robust, transactional persistent storage layer. TDB is a component of Jena for RDF storage and query. It support the full range of Jena APIs. TDB can be used as a high performance RDF store on a single machine.

Download https://jena.apache.org/download/

Starting Fuseki Web Application

Using Your SPARQL Endpoint Jan Pettersen Nytun, UiA

You may copy the text on next slide into a ttl-file (OWL in Turtle format) and upload it.

@prefix : <http://www. uia. no/film_ontology#> @prefix : <http://www.uia.no/film_ontology#> . @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/film_ontology> . <http://www.uia.no/film_ontology> rdf:type owl:Ontology . :Film rdf:type owl:Class . :Human rdf:type owl:Class . :Man rdf:type owl:Class ; rdfs:subClassOf :Human . :Woman rdf:type owl:Class ; rdfs:subClassOf :Human . :playedIn rdf:type owl:ObjectProperty . :Giant rdf:type :Film , owl:NamedIndividual . :EastOfEden rdf:type :Film , owl:NamedIndividual . :CarrollBaker rdf:type :Woman , owl:NamedIndividual ; :playedIn :Giant . :ElizabethTaylor rdf:type :Woman , owl:NamedIndividual ; :playedIn :Giant . :JoVanFleet rdf:type :Woman , owl:NamedIndividual ; :playedIn :EastOfEden . :JulieHarris rdf:type :Woman , owl:NamedIndividual ; :playedIn :EastOfEden . :MercedesMcCambridge rdf:type :Woman , owl:NamedIndividual ; :playedIn :Giant . :RockHudson rdf:type :Man , owl:NamedIndividual ; :playedIn :Giant . :JamesDean rdf:type :Man , owl:NamedIndividual ; :playedIn :EastOfEden , :Giant .

The postfix of the file must be correct (must be ttl if Turtle)

Etc. …

Insert Data

Find the women that played in the same movies as James Dean.

Given the following graph, find the women that played in the same movies as James Dean.

Prefixes prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#>

[4]: SELECT ?actress ?movie WHERE { :JamesDean :playedIn ?movie . ?actress :playedIn ?movie . ?actress rdf:type :Woman }

In Protégé:

In Fuseki

Defining Graphs in Fuseki Jan Pettersen Nytun, UiA

[https://www.w3.org/TR/sparql11-protocol/]: 2.1.4 Specifying an RDF Dataset A SPARQL query is executed against an RDF Dataset. The RDF Dataset for a query may be specified either via the default-graph-uri and named-graph-uri parameters in the SPARQL Protocol or in the SPARQL query string using the FROM and FROM NAMED keywords.

You may add GRAPH when uploading file

prefix rdf: <http://www. w3 prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#> SELECT ?actress ?movie WHERE { GRAPH <http://localhost:3030/Testing/data/JPNGRAPHH> { :JamesDean :playedIn ?movie . ?actress :playedIn ?movie . ?actress rdf:type :Woman }

Find all actors.

The result contains two occurrences of JamesDean! prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> prefix owl: <http://www.w3.org/2002/07/owl#> prefix xsd: <http://www.w3.org/2001/XMLSchema#> prefix : <http://www.uia.no/film_ontology#> SELECT ?actor WHERE { ?actor :playedIn ?movie . } The result contains two occurrences of JamesDean! actor 1 :CarrollBaker 2 :ElizabethTaylor 3 :JamesDean 4 :MercedesMcCambridge 5 :RockHudson 6 7 :JoVanFleet 8 :JulieHarris

Avoid duplicates by using DISTINCT

Ordering of the SPARQL statement triples Semantically the order is insignificant. In regard to efficiency: Typically the queries are processed in top-to-bottom order… Order triples in a query so that the first triples gives fewest matches, e.g., by have the triples with fewest new variables first.

New ontology for the next slides. You may copy the text on next slide into a ttl-file and upload it.

@prefix : <http://www. uia @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 rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @base <http://www.uia.no/janpettersennytun/ontologies/family> . <http://www.uia.no/janpettersennytun/ontologies/family> rdf:type owl:Ontology . :hasBrother rdf:type owl:ObjectProperty . :hasParent rdf:type owl:ObjectProperty . :hasSister rdf:type owl:ObjectProperty . :hasUncle rdf:type owl:ObjectProperty . :birthday rdf:type owl:DatatypeProperty ; rdfs:range xsd:dateTime . :diedOn rdf:type owl:DatatypeProperty ; rdfs:range xsd:dateTime . :hasName rdf:type owl:DatatypeProperty ; rdfs:range xsd:string . :CityMan rdf:type owl:Class ; rdfs:subClassOf :Man . :Man rdf:type owl:Class ; rdfs:subClassOf :Person . :Person rdf:type owl:Class . :Woman rdf:type owl:Class ; rdfs:subClassOf :Person . owl:NamedIndividual ; :Ester rdf:type :Woman , :hasName "Ester"^^xsd:string ; :Sigmund . :hasBrother :Olav , :Jan rdf:type :Man , :birthday "1957-12-16T04:30:00"^^xsd:dateTime ; :hasName "Jan"^^xsd:string ; :hasParent :Sigmund . :Kirsten rdf:type :Woman , :birthday "1956-05-12T14:00:00"^^xsd:dateTime ; :hasName "Kirsten"^^xsd:string ; :Olav rdf:type :Man , :hasName "Olav"^^xsd:string ; :hasSister :Ester ; :hasBrother :Sigmund . :Rolf rdf:type :Man , :hasName "Rolf"^^xsd:string . :Sigmund rdf:type :Man , :birthday "1933-08-21T15:00:00"^^xsd:dateTime ; :diedOn "1980/07/14T21:32:52"^^xsd:dateTime ; :hasName "Sigmund"^^xsd:string ; :hasBrother :Rolf . :Sigrund rdf:type :Woman , :birthday "1952-07-21T15:00:007"^^xsd:dateTime ; :hasName "Sigrund"^^xsd:string ;

Use SPARQL and get all parents.

PREFIX rdf: <http://www. w3 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/janpettersennytun/ontologies/family#> SELECT ?parent WHERE { ?child :hasParent ?parent }

Avoid duplicate listings of parents in result.

Avoid duplicates of parents in result.

Find all the properties of Jan together with their values.

Find all properties with values of Jan

Find all persons born after the beginning of 1940

But first find all persons. 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 ?person WHERE { ?person rdf:type :Person } OBS! The result set is empty!!

Find all men are easy! The result is all men: 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 ?person WHERE { ?person rdf:type :Man} The result is all men:

Find all persons, i.e., find all members of Person and the subclasses of Person 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 ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person } SPARQL 1.1 includes a transitive operator: * [4]: If we include a * after a property name, then the triple matches any number of chained occurrences of the same property.

Find all persons and their birthdays 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 ?person ?birthday WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday } (Persons without a birthday will not be found.)

Find all persons born after beginning of 1940 by using FILTER.

Find all persons born after beginning of 1940 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 ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday . FILTER( ?birthday > "1940-01-01T00:00:01"^^xsd:dateTime)}

Find all landlocked countries with a population greater than 15 million. [http://www.w3.org/2009/Talks/0615-qbe/] PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX type: <http://dbpedia.org/class/yago/> PREFIX prop: <http://dbpedia.org/property/> SELECT ?country_name ?population WHERE { ?country a type:LandlockedCountries ; rdfs:label ?country_name ; prop:populationEstimate ?population . FILTER (?population > 15000000) . } FILTER constraints use boolean conditions to filter out unwanted query results. rdfs:label is a common predicate for giving a human-friendly label to a resource.

Find all persons born 1940-1955

Find all persons born 1940-1955 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 ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . ?person :birthday ?birthday . FILTER( ?birthday > "1940-01-01T00:00:01"^^xsd:dateTime) FILTER( ?birthday < "1955-12-31T23:59:59"^^xsd:dateTime) }

Gives result: :Person, :Woman, :Man and :CityMan :Person rdf:type owl:Class . :Woman rdf:type owl:Class ; rdfs:subClassOf :Person . :Man rdf:type owl:Class ; rdfs:subClassOf :Person . :CityMan rdf:type owl:Class ; rdfs:subClassOf :Man . 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 ?subject WHERE { ?subject rdfs:subClassOf* :Person} Match: :Person – :Woman rdfs:subClassOf :Person – :Man rdfs:subClassOf :Person – :CityMan rdfs:subClassOf :Man rdfs:subClassOf :Person Gives result: :Person, :Woman, :Man and :CityMan Transitive operator – one more example

Gives result: :Woman, :Man and :CityMan :Person rdf:type owl:Class . :Woman rdf:type owl:Class ; rdfs:subClassOf :Person . :Man rdf:type owl:Class ; rdfs:subClassOf :Person . :CityMan rdf:type owl:Class ; rdfs:subClassOf :Man . 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 ?subject WHERE { ?subject rdfs:subClassOf+ :Person} Match: :Woman rdfs:subClassOf :Person - :Man rdfs:subClassOf :Person – :CityMan rdfs:subClassOf :Man rdfs:subClassOf :Person Gives result: :Woman, :Man and :CityMan

How to get the friends of your friends? Existing triples: @prefix : <http://whatever.org/whatever#> :jane :friend :frank . :frank : friend :sarah . :sarah : friend :steve .

My friends are your friends! @prefix : <http://rdfdata.org/whatever#> :jane :friend :frank . :frank : friend :sarah . :sarah : friend :steve . PREFIX : <http://rdfdata.org/whatever#> SELECT ?person WHERE { :jane :friend+ ?person . } Result: ------------- | person | ======== | :frank | | :sarah | | :steve |

How to list the names of all men together with death date – leave death date blank if missing?

Problem: List the names of all men together with their death date... [4]: Every triple in the graph pattern must match in the data set in order for the match to succeed; if any triple fails to match, then no row appears in the result set at all. The following dos not work, it only list the men with a death date!

Using Optional Matches To Solve The Problem

MINUS Example MINUS removes patterns from the result set that match the patterns specified by the MINUS construct. Get all people (assumed) alive: 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 ?person WHERE { ?person rdf:type ?class . ?class rdfs:subClassOf* :Person . MINUS {?person :diedOn ?d} } I did not get UNSAID or NOT EXISTS to work in the Protégé SPARQL plugin, but MINUS worked.

Yes/No Queries It is possible to ask Yes/No questions of a graph by using the ASK construct. [3]:

Is anybody dead, i.e., do anybody have a death date?

PREFIX : <http://www. uia 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#> ASK { ?person :diedOn ?deathday }

References [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