Semantic Web Semantic Web Andrejs Lesovskis Andrejs Lesovskis
Semantic WebSemantic Web
Andrejs LesovskisAndrejs Lesovskis
Publishing on the Web
Making information available
without knowing the eventual use;
reuse, collaboration;
reproduction of results.
Also, there are:
Economic issues;
DRM, copyright and licensing issues;
. . .
3
Publishing on the Semantic Web
SPARQL OWL, RDFS,
SKOS RDF URIs XML
• Data Access• Information
organisation• Information format• Identification• Serialization
Publishing via a query service on the web
SPARQL Protocol and RDF Query Language
What is SPARQL?
SPARQL
is the query language of the Semantic Web,
stays for SPARQL Protocol and RDF Query Language.
SPARQL 1.1 Protocol became a W3C recommendation on March 21, 2013.
This protocol was developed by the W3C SPARQL Working Group, part of the Semantic Web Activity.
Motivation
Having RDF data available is not enough
Need tools to process, transform, and reason with the information
Need a way to store the RDF data and interact with it
Are existing storage systems appropriate to store RDF data?
Are existing query languages appropriate to query RDF data?
Why SPARQL?
SPARQL allows to:
Pull values from structured and semi-structured data represented in RDF;
Explore RDF data by querying unknown relationships;
Perform complex joins of disparate RDF repositories in a single query;
Transform RDF data from one vocabulary to another;
Develop higher-level cross-platform application.
Anatomy of a SPARQL query
PREFIX foo: <…>PREFIX bar: <…>…SELECT …FROM <…>FROM NAMED <…>WHERE {
…}ORDER BY …LIMIT …OFFSET …
Declare prefixshortcuts (optional) Query result
clause
Triple patterns
Query modifiers(optional)
Define the dataset
(optional)
Basic patterns
A basic pattern is a set of triple patterns, all of which must be matched.
In this case matches the graph means find a set of bindings such that the substitution of variables for values creates a subgraph that is in the set of triples making up the graph.
SPARQL query forms
SELECT,
CONSTRUCT,
ASK,
DESCRIBE.
SELECT query
SPARQL’ SELECT is rather similar to SELECT from SQL.
The SELECT form of results returns variables and their bindings directly. It combines the operations of projecting the required variables with introducing new variable bindings into a query solution.
SELECT query example
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dbpedia2: <http://dbpedia.org/property/>
SELECT ?presName, ?birthday, ?startDate
WHERE
{
?presName skos:subject
<http://dbpedia.org/resource/Category:Presidents _of_the_United_States>;
dbpedia2:birth ?birthday;
dbpedia2:presidentStart ?startDate.
}
SELECT with OPTIONAL
SELECT with multiple OPTIONAL
CONSTRUCT query
The CONSTRUCT query form returns a single RDF graph specified by a graph template.
The result is an RDF graph formed by taking each query solution in the solution sequence, substituting for the variables in the graph template, and combining the triples into a single RDF graph by set union.
CONSTRUCT example@prefix : <http://www.snee.com/ns/demo#> . :jane :hasParent :gene .
:gene :hasParent :pat ;
:gender :female .
:joan :hasParent :pat ;
:gender :female .
:pat :gender :male .
:mike :hasParent :joan .
CONSTRUCT example (cont.)
PREFIX : <http://www.snee.com/ns/demo#>
CONSTRUCT { ?p :hasGrandfather ?g . }
WHERE { ?p :hasParent ?parent .?parent :hasParent ?g .?g :gender :male . }
CONSTRUCT example (cont.)
@prefix : <http://www.snee.com/ns/demo#> .
:jane :hasGrandfather :pat .
:mike :hasGrandfather :pat .
ASK query
Applications can use the ASK form to test whether or not a query pattern has a solution. No information is returned about the possible query solutions, just whether or not a solution exists.
ASK query example
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
_:a foaf:name "Alice" ._:a foaf:homepage <http://work.example.org/alice/> .
_:b foaf:name "Bob" ._:b foaf:mbox <mailto:[email protected]> .
PREFIX foaf: <http://xmlns.com/foaf/0.1/>ASK { ?x foaf:name "Alice" }
DESCRIBE query
The DESCRIBE form returns a single result RDF graph containing RDF data about resources.
This data is not prescribed by a SPARQL query, where the query client would need to know the structure of the RDF in the data source, but, instead, is determined by the SPARQL query processor.
Some SPARQL endpoint don’t support this type of queries.
PREFIX ent: <http://org.example.com/employees#>DESCRIBE ?x WHERE { ?x ent:employeeId "1234" }
@prefix foaf: <http://xmlns.com/foaf/0.1/> .@prefix vcard: <http://www.w3.org/2001/vcard-rdf/3.0> .@prefix exOrg: <http://org.example.com/employees#> .@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.@prefix owl: <http://www.w3.org/2002/07/owl#>
_:a exOrg:employeeId "1234" ; foaf:mbox_sha1sum "ABCD1234" ; vcard:N [ vcard:Family "Smith" ; vcard:Given "John" ] .
foaf:mbox_sha1sum rdf:type owl:InverseFunctionalProperty .
SPARQL query result formats
XML,
JSON (JavaScript Object Notation),
RDF,
HTML.
JavaScript Object Notation
JSON (JavaScript Object Notation) is a text-based, lightweight data-interchange format.
It's easy to read and write for humans and to parse and generate for machines.
JSON was originally specified by Douglas Crockford, and is described in RFC 4627.
JavaScript Object Notation{ "firstName": "John", "lastName" : "Smith", "age" : 25, "address" : { "streetAddress": "21 2nd Street", "city" : "New York", "state" : "NY", "postalCode" : "10021" }, … }
A RDF Graph Modeling Movies
movie1
movie:Movie
“Edward ScissorHands”
“1990”
rdf:type
movie:title
movie:year
movie:Genre
movie:Romance
movie:Comedy
rdf:type rdf:type
movie:genre
movie:genre
movie:Role
“Edward ScissorHands”
r1
actor1movie:playedBy
movie:characterName
rdf:type
movie:hasPart
Example Query 1Select the movies that has a character called “Edward Scissorhands”
PREFIX movie: <http://example.org/movies/>
SELECT DISTINCT ?x ?t
WHERE {
?x movie:title ?t ;
movie:hasPart ?y .
?y movie:characterName ?z .
FILTER (?z = “Edward Scissorhands”@en)
}
Example Query 1PREFIX movie: <http://example.org/movies/>
SELECT DISTINCT ?x ?t
WHERE {
?x movie:title ?t ;
movie:hasPart ?y .
?y movie:characterName ?z .
FILTER (?z = “Edward Scissorhands”@en)
}
Note the use of “;” This allows to create triples referring to the previous triple pattern (extended version would be ?x movie:hasPart ?y)
Note as well the use of the language speciation in the filter @en
Example Query 2Create a graph of actors and relate them to the movies they play in (through a new ‘playsInMovie’ relation).
PREFIX movie: <http://example.org/movies/>PREFIX foaf: <http://xmlns.com/foaf/0.1/>
CONSTRUCT {?x foaf:firstName ?fname.?x foaf:lastName ?lname.?x movie:playInMovie ?m}
WHERE {?m movie:title ?t ;movie:hasPart ?y .?y movie:playedBy ?x .?x foaf:firstName ?fname.?x foaf:lastName ?lname.
}
Example Query 3
Find all movies which share at least one genre with “Gone with the Wind”
PREFIX movie: <http://example.org/movies/>
SELECT DISTINCT ?x2 ?t2
WHERE {
?x1 movie:title ?t1.
?x1 movie:genre ?g1.
?x2 movie:genre ?g2.
?x2 movie:title ?t2.
FILTER (?t1 = “Gone with the Wind”@en &&
?x1!=?x2 && ?g1=?g2)
}
31
SPARQL and reasoningOWL, RDFS, application, rules
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>SELECT ?typeWHERE{ ?x rdf:type ?type .}
:x rdf:type :C .:C rdfs:subClassOf :D .
--------| type |========| :C || :D |--------
Thank you!Thank you!