Presentation is loading. Please wait.

Presentation is loading. Please wait.

SPARQL for the Pragmatic Ontologist

Similar presentations


Presentation on theme: "SPARQL for the Pragmatic Ontologist"— Presentation transcript:

1 SPARQL for the Pragmatic Ontologist
The Washington Semantic Web Meetup SPARQL for the Pragmatic Ontologist Daniel Yacob Mekonnen Semantic Solutions Architect, TopQuadrant

2 The query was just the beginning.
SPARQL by Application Q: What is the SQL of RDF? SPARQL! Q: What is the XSLT of RDF? SPARQL! Q: What is the JavaDoc of RDF? SPARQL! Q: What is the Lint of RDF? SPARQL! Q: What is the Unit Test of RDF? The query was just the beginning. SPARQL! “Its in the way that you use it” - Eric Clapton “The query was just the beginning” - Hey, I said that! 2

3 Thinking SPARQL To think in SPARQL, think in patterns
graph patterns or shapes of data think in graphs a similar mindset to regular expressions thinking The pattern language is N3 “Notation 3” A language for expressing RDF Learn N3 as you learn SPARQL

4 Yes. SPARQL 1.0 is READ-ONLY. Is SPARQL READ-ONLY?
SPARQL Vocabulary ASK BASE BOUND CONSTRUCT DATATYPE DESCRIBE DISTINCT false FILTER FROM FROM NAMED GRAPH isIRI isLITERAL isURI LANG LANGMATCHES LIMIT OFFSET OPTIONAL ORDER BY PREFIX REDUCED REGEX sameTerm SELECT STR true UNION WHERE Yes. SPARQL 1.0 is READ-ONLY. Is SPARQL READ-ONLY? Terms I’ve used at least once in 2 years. Terms I use regularly.

5 Terms I’ve used at least once in the last year.
SPARQL Vocabulary Jena ARQ Extensions CLEAR COUNT CREATE DELETE DELETE DATA DROP GROUP BY HAVING (SPARQL 2 Candidates) INSERT INSERT DATA INTO MODIFY SERVICE SILENT These are read-write extension to SPARQL. Supported by TBC . Gruff is read-only Terms I’ve used at least once in the last year. Terms I use regularly.

6 Exercise Dataset 7 ontologies altogether:
1 ontology of Airport codes: IANA Code, City, State, Country. 1 ontology of National Capitals: Nation, Capital City, with Latitude and Longitude. 5 ontologies work as a group providing Country, City and US States relationships. These 3 groupings are disjoint –but we can link the data with SPARQL! Query are executed over ALL ontologies.

7 Simple Select “Find all country instances and their name properties”
“Find the names of all countries” I’m a Class! rdf:type country:name country:Country Objects “?” indicates a variable, something unknown. Predicates I’m an unknown property value. I’m a Property value! I’m an unknown instance. I’m an Instance! “Italy” ?country ?name Triple I’m a Property! Subject Triple

8 ‘?’ is the sigil in SPARQL.
Simple Select “Find the names of all countries” country:Country rdf:type country:name ?name ?country ‘?’ is the sigil in SPARQL. ‘$’ is also legal (sigil to taste). SELECT ?country ?name WHERE { ?country rdf:type country:Country . ?country country:name ?name } SELECT $country $name WHERE { $country rdf:type country:Country . $country country:name $name } SELECT ?country ?name WHERE { ?country rdf:type country:Country . ?country country:name ?name }

9 Simple Select “Find the names of all countries” SELECT ?country ?name
WHERE { ?country a country:Country . ?country country:name ?name } SELECT ?country ?name WHERE { ?country a country:Country ; country:name ?name } SELECT ?country ?name WHERE { ?country rdf:type country:Country . ?country country:name ?name } SELECT ?country ?name { ?country country:name ?name } SELECT ?country ?name { ?country a country:Country ; country:name ?name }

10 Ordered Select “Find the states and their cities”
SELECT ?state ?city WHERE { ?cityR city:name ?city ; uscity:state ?stateR . ?stateR state:name ?state }

11 Ordered Select “Find the states and their cities -sorted by state, then city name” SELECT ?state ?city WHERE { ?cityR city:name ?city ; uscity:state ?stateR . ?stateR state:name ?state } ORDER BY ?state ?city

12 Ordered Select “Find the states and their cities -sorted by ascending state name, then by descending city name” SELECT ?state ?city WHERE { ?cityR city:name ?city ; uscity:state ?stateR . ?stateR state:name ?state } ORDER BY ASC(?state) DESC(?city)

13 Ordered Select Compactified Expression
Found in the wild, but best to avoid –more cryptic and error prone. SELECT ?state ?city WHERE { ?cityR city:name ?city ; uscity:state [ state:name ?state ] } ORDER BY ASC(?state) DESC(?city)

14 Select with OPTIONAL SELECT ?state ?capital ?airport WHERE { ?stateR state:name ?state ; state:capital ?capitalR . ?capitalR city:name ?capital OPTIONAL { ?airportR airports:city ?capital ; airports:airport ?airport } bridge across ontologies You may nest options within options.

15 Select with UNION SELECT ?state WHERE { { ?stateR state:borderstate usstate:AL, usstate:TN . ?stateR state:name ?state . } UNION ?stateR state:borderstate usstate:ID . Comma between objects means the subject and predicate are the same.

16 With !bound() we effectively look for the absence of a resource
Select with FILTER “Find all states that do NOT border another state” SELECT ?state WHERE { ?stateR state:name ?state . OPTIONAL { ?stateR state:borderstate ?borderState . } FILTER ( !bound(?borderState) ) FILTER means “keep” With !bound() we effectively look for the absence of a resource

17 Select with FILTER “Find all states, and cities, where the city name begins with the letter ‘Y’ ” SELECT ?state ?city WHERE { ?cityR uscity:state ?stateR . ?cityR city:name ?city . ?stateR state:name ?state FILTER( regex( xsd:string(?city), "^Y") ) } Cast untyped literals into datatype needed by function.

18 Performance Notes Filter early, near the top of an expression –if a condition is not met the pattern match aborts immediately. Use OPTIONAL sparingly, they will slow down a query, sometimes drastically. Use ASK when you do not need the results of the match –ASK will terminate when the first match if found, returns a boolean DISTINCT and ORDERED BY may also be slow over large result sets USE LIMIT and OFFSET with large result sets. Explain that ASK is identical to SELECT sans the ?variable list

19 Select with DISTINCT DISTINCT avoids duplicate result sets
SELECT DISTINCT ?state ?city WHERE { ?person uscity:address ?address . ?address uscity:city ?city . ?city uscity:state ?state } Duplicates Explained: ?person is not in the SELECT list, but many, many people will live in a ?city. Without DISTINCT one result set of ?state ?city would appear per ?person! Without DISTINCT, one SELECT result is returned per graph pattern match.

20 CONSTRUCT Query Has anyone thought it strange that we query a graph, with a graph (WHERE clause), but then process a tabular result set? CONSTRUCT allows us to specify a graph to return. CONSTRUCTed graphs are not inserted into the queried graph –but some tools allow the constructed graph to be asserted. CONSTRUCT is extremely useful for transforming graphs.

21 Now the bad news… Not all SPARQL implementations are created equal
Gruff TBC CONSTRUCT INSERT DELETE LET COUNT MODIFY DESCRIBE

22 CONSTRUCT Query CONSTRUCT { ?countryR owl:sameAs ?nationR } WHERE { ?countryR a country:Country; country:name ?country . ?nationR a capitals:Nation; rdfs:label ?country; Linkage, equating, of resources. ?country bridges two ontologies in the same graph.

23 Start comments anywhere with “#” Use LET() to assign variables.
CONSTRUCT Query CONSTRUCT { ?airportR wswm:country ?countryR ; wswm:city ?cityR ; wswm:state ?stateR } WHERE { ?airportR a airports:AirportCode ; airports:city ?city ; airports:country ?country . { # Link to International City Instances ?cityR a capitals:Capital; rdfs:label ?city . ?countryR a capitals:Nation ; rdfs:label ?country ; capitals:capital ?cityR . UNION # Link to US City Instances LET ( ?countryR := country:UNITED-STATES ) ?airportR airports:state ?state . ?stateR state:code ?state . ?cityR city:name ?city ; uscity:state ?stateR Construct resource linkages across the 3 ontology groups. The data is now linked and we can find geo coordinates for airports (in capital cities only). Start comments anywhere with “#” Use LET() to assign variables.

24 SPARQL 2 DELETE –use to remove unwanted triples and clean up an ontology DELETE { ?s country:cia ?o } WHERE {

25 SPARQL 2 DELETE and INSERT –use together in sequence to move content:
?state rdfs:label ?name } WHERE { ?state state:name ?name DELETE {

26 Advanced Topics Named Graphs – useful when the same namespaces are used in multiple graphs: SELECT ?state ?births FROM NAMED < WHERE { ?stateR a state:State ; state:name ?state ; stats:birthRate ?births } Graphs names are automatic with TBC. With Gruff the names must be specified at import time into the Allegograph triplestore. Doesn’t significantly occur in our example data.

27 Advanced Topics SELECT ?state ?births ?deaths WHERE { ?stateR a state:State ; state:name ?state ; GRAPH < { stats:birthRate ?births } GRAPH < { stats:deathRate ?deaths

28 The graph can also be a variable
Advanced Topics The graph can also be a variable SELECT ?state ?deaths ?graph WHERE { ?stateR a state:State ; state:name ?state ; GRAPH ?graph { stats:deathRate ?deaths }

29 User function to convert Celsius to Fahrenheit.
Advanced Topics Custom FILTER, LET and Property functions bound to Java code. SELECT ?town ?temperature WHERE { ?town a geo:Town; geo:zipcode ?zipcodePlus4 . ?zipcode my:shortenZip( ?zipcodePlus4 ). LET ( ?temperatureC := my:weather( ?zipcode ) ) FILTER( my:toFahrenheit(?temperatureC) > 100 ) } A user defined function to shorted a 9 digit zip code to a 5 digit zip code. User function to get current temperature at zip code. User function to convert Celsius to Fahrenheit.

30 TopBraid Suite Advanced Product Training
TopBraid Suite offers extensive SPARQL support In-depth SPARQL full Jena ARQ and TBC extensions using SPARQL in applications Next-generation SPARQL SPARQLMotion Scripts SPIN (SPARQL Inference Notation) March 9-12, 2009, Washington, DC area (first day is optional Semantic Web overview)

31 SPARQLMotion Import Processing Export SPARQLMotion Engine
SPARQLMotion is designed to tie the data lifecycle into an automated process Import (Spreadsheets, DBs, XML) SPARQLMotion Engine Processing (Editing, querying, transforming) Export (Converting, browsing, visualizing)

32 SPARQLMotion Language
Scripts consist of modules Modules have a type (e.g. LoadXML) The output of one module is the input to its successors Branching and merging supported Drag & drop modules to create a script step, fill out required attributes, connect 32

33 SPARQLMotion Script Script define data processing steps Graph view
properties define relationships between modules ‘next’ means result triples from on module sent to next Graph view shows processing pipeline Form view modules are instances of (SPARQLMotion) classes

34 SPARQLMotion Example

35 SPIN SPIN - SPARQL Inferencing Notation
define constraints and inference rules on Semantic Web models Specification for representing SPARQL with RDF RDF syntax for SPARQL queries Modeling vocabulary constraints, constructors, rules templates, functions Standard Modules Library small set of frequently needed SPARQL queries SPIN is a collection of RDF vocabularies enabling the use of SPARQL to define constraints and inference rules on Semantic Web models. SPIN also provides meta-modeling capabilities that allow users to define their own SPARQL functions and query templates. Finally, SPIN includes a ready to use library of common functions.

36 SPIN Syntax Problem: SPARQL is represented as a string
can add as a property value to an ontology …ala that query library but what if you wanted to associate a query with a specific resource? # must be at least 18 years old ASK WHERE { ?this my:age ?age . FILTER (?age >= 18) . } [ a sp:Ask ; rdfs:comment "must be at least 18 years old"^^xsd:string ; sp:where ([ sp:object sp:_age ; sp:predicate my:age ; sp:subject spin:_this ] [ a sp:Filter ; sp:expression [ sp:arg1 sp:_age ; sp:arg2 18 ; a sp:ge ] ]) Query represented as RDF nodes (not for human consumption)

37 Using SPIN and Composer-ME
Incremental inferencing (Composer) compute values when editing ontology can turn incremental inferencing on and off, run on command, etc. Calculate the value of a property based on other properties age of a person as a difference between today's date and person's birthday computation is declared in SPIN property Perform constraint checking with closed world semantics e.g. raise inconsistency flags when currently available information does not fit specified integrity constraints constraints specified in SPIN Isolate a set of rules to be executed under certain conditions e.g. class constructors initialize certain values when a resource is first created, or to drive interactive applications

38 SPIN Modeling Vocabulary
Constraints link classes with SPARQL ASK or CONSTRUCT queries e.g. link Parents to query when true, constraint is violated (note ‘<‘ not ‘>=’) CONSTRUCT will generate triples when constraint is violated Rules link SPARQL CONSTRUCT queries to instances of classes …and all subclasses apply the rule to all instances Constructors same as Rules but applied only when instance is created ASK WHERE { ?this my:age ?age . FILTER (?age < 18) . }

39 SPIN Meta-Modeling Vocabulary
Reusable SPARQL queries can use in other contexts particularly Rules and Constraints encapsulate SPARQL query templates Templates create a template class with SPARQL query create instance of template Functions define new functions to be used in FILTER or LET clauses

40 SPIN Standard Modules Library
Set of frequently used SPARQL queries Functions spl:hasValue spl:hasValueOfType spl:instanceOf spl:objectCount Templates spl:Argument spl:Attribute spl:ConstructDefaultValues spl:hasValueOfType(rdfs:Class, rdfs:label, xsd:string) # true spl:hasValueOfType(rdf:Class, rdfs:label, xsd:int) # false

41 Additional Information
SPARQL Guide SPARQL FAQ Jena (ARQ) Property & Filter Functions My Delicious Bookmarks – sparql and semantic tags SPARQLPedia – a query repository SPARQLMotion – scripting of SPARQL SPIN – stored procedures for SPARQL


Download ppt "SPARQL for the Pragmatic Ontologist"

Similar presentations


Ads by Google