Introduction to SPARQL Andrea Nuzzolese [email protected] Bologna, 28 Marzo 2011 Laurea...
-
date post
20-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Introduction to SPARQL Andrea Nuzzolese [email protected] Bologna, 28 Marzo 2011 Laurea...
Introduction to SPARQL
Andrea Nuzzolese [email protected]
Bologna, 28 Marzo 2011
Laurea Magistrale in Scienze di Internet KMDM a.a. 2010/2011
SPARQL
• SPARQL (pronounced "sparkle”) is an RDF query language
• it stands for Simple Protocol And RDF Query Language
• allows for a query to consist of triple patterns, conjunctions, disjunctions, and optional patterns
Anatomy of a QueryPREFIX foo: <…>PREFIX bar: <…>…SELECT …FROM <…>FROM NAMED <…>WHERE { …}GROUP BY …HAVING …ORDER BY …LIMIT …OFFSET …BINDINGS …
Declare prefixshortcuts (optional)
Query result clause
Query pattern
Query modifiers(optional)
Define the dataset (optional)
courtesy of Lee Feigenbaum
Types of SPARQL QueriesProject out specific variables and expressions:
SELECT ?c ?cap (1000 * ?people AS ?pop)
Project out all variables:SELECT *
Project out distinct combinations only:SELECT DISTINCT ?country
Results in a table of values (in XML or JSON):
SELECT queries
?c ?cap ?pop
ex:France ex:Paris 63,500,000
ex:Canada ex:Ottawa 32,900,000
ex:Italy ex:Rome 58,900,000
Construct RDF triples/graphs:CONSTRUCT { ?country a ex:HolidayDestination ; ex:arrive_at ?capital ; ex:population ?population .}
Results in RDF triples (in any RDF serialization):
ex:France a ex:HolidayDestination ; ex:arrive_at ex:Paris ; ex:population 635000000 .ex:Canada a ex:HolidayDestination ; ex:arrive_at ex:Ottawa ; ex:population 329000000 .
CONSTRUCT queries
Ask whether or not there are any matches:ASK
Result is either “true” or “false” (in XML or JSON):true, false
ASK queriesDescribe the resources matched by the given
variables:DESCRIBE ?country
Result is RDF triples (in any RDF serialization) :ex:France a geo:Country ; ex:continent geo:Europe ; ex:flag <http://…/flag-france.png> ; …
DESCRIBE queries
courtesy of Lee Feigenbaum
Nuts & Bolts
Write full URIs:<http://this.is.a/full/URI/written#out>
Abbreviate URIs with prefixes:PREFIX foo: <http://this.is.a/URI/prefix#>… foo:bar …
http://this.is.a/URI/prefix#bar
Shortcuts:a rdf:type
URIsPlain literals:
“a plain literal”
Plain literal with language tag:“bonjour”@fr
Typed literal:“13”^^xsd:integer
Shortcuts:true “true”^^xsd:boolean3 “3”^^xsd:integer 4.2 “4.2”^^xsd:decimal
Literals
Variables:?var1, ?anotherVar, ?and_one_more
VariablesComments:
# Comments start with a ‘#’# continue to the end of the line
Comments
Match an exact RDF triple:ex:myWidget ex:partNumber “XY24Z1” .
Match one variable:?person foaf:name “Lee Feigenbaum” .
Match multiple variables:conf:SemTech2009 ?property ?value .
Triple Patterns
courtesy of Lee Feigenbaum
Some Public SPARQL EndpointsName URL What’s there?
SPARQLer http://sparql.org/sparql.htmlGeneral-purpose query endpoint for Web-accessible data
DBPedia http://dbpedia.org/sparqlExtensive RDF data from Wikipedia
DBLP http://www4.wiwiss.fu-berlin.de/dblp/snorql/Bibliographic data from computer science journals and conferences
LinkedMDB
http://data.linkedmdb.org/sparqlFilms, actors, directors, writers, producers, etc.
WorldFactbook
http://www4.wiwiss.fu-berlin.de/factbook/snorql/
Country statistics from the CIA World Factbook
bio2rdf http://bio2rdf.org/sparqlBioinformatics data from around 40 public databases
SPARQL Over HTTP (the SPARQL Protocol)http://host.domain.com/sparql/endpoint?<parameters>
where <parameters> can include:
query=<encoded query string>
e.g. SELECT+*%0DWHERE+{…default-graph-uri=<encoded graph URI>
e.g. http%3A%2F%2Fexmaple.com%2Ffoo…n.b. zero of more occurrences of default-
graph-urinamed-graph-uri=<encoded graph URI>
e.g. http%3A%2F%2Fexmaple.com%2Fbar…n.b. zero of more occurrences of named-graph-
uri
HTTP GET or POST. Graphs given in the protocol override graphs given in the query.
SPARQL Resources
• The SPARQL Specification– http://www.w3.org/TR/rdf-sparql-query/
• SPARQL implementations– http://esw.w3.org/topic/SparqlImplementations
• SPARQL endpoints– http://esw.w3.org/topic/SparqlEndpoints
• SPARQL Frequently Asked Questions– http://www.thefigtrees.net/lee/sw/sparql-faq
• SPARQL Working Group– http://www.w3.org/2009/sparql/wiki/
• Common SPARQL extensions– http://esw.w3.org/topic/SPARQL/Extensions
Semantic Web Frameworks
Andrea Nuzzolese [email protected]
Bologna, 28 Marzo 2011
Laurea Magistrale in Scienze di Internet KMDM a.a. 2010/2011
Overview
OWL API
• It implements the OWL2 Model• it handles OWL model in Java
• It implements the RDF Model• It also offers API for OWL• It provides a SPARQL implementation called ARQ
• Semantic lifting of text• Knowledge representation• Reasoning• RDF refactoring• Triplification• RDF storage
What is Jena?• open-source Java implementation of core Semantic Web
- RDF graph manipulation API- RDFS and OWL reasoning API- Includes the de facto reference RDF/XML parser- RDF/XML, N3 and N-triple Input/Output- SPARQL query engine- rule-based inference engine
• in-memory or persistent storage- Jena TDB- Jena SDB
• developed by HP Laboratories
• now an Apache Incubator project
Key concepts
• the Jena API builds on RDF
• core API classes are– Model: the representation of an RDF model and is
composed by a set of statements– Statement: is a triple subject-object-predicate
• the subject is a resource• the predicate is a property• the object is a value or a resource
– Resource: an object identified by an URI– Property: a particular resource that describes relations
between resources
Creating a RDF graph
• we use Jena to create a new RDF graph, with a triple which states that– Sir Tim Berners-Lee is a person and his name
is “Tim Berners-Lee”
http://…/Berners-Leefoaf:Personrfd:type Tim
Berners-Lee
foaf:name
RDF API// basic definitionsString uri = "http://www.w3.org/People/Berners-Lee";String fullName = "Tim Berners-Lee";
// create an empty (default) model via ModelFactoryModel model = ModelFactory.createDefaultModel();
// create the resource that identifies Tim Berners-Lee as a personResource timBernersLee = model.createResource(uri,
FOAF.Person);
// add the name property to the resource identifying TBLtimBernersLee.addProperty(FOAF.name, fullName);
http://…/Berners-Leefoaf:Personrfd:type Tim
Berners-Lee
foaf:name
Jena I/O//we want to fetch the RDF graph of Tim Berners-Lee from DBpediaString tblURI = "http://dbpedia.org/resource/Tim_Berners-Lee";
//we load the RDF into a Jena ModelModel model = FileManager.get().loadModel(tblURI, "RDF/XML");
//we print the RDF to standard inputmodel.write(System.out);
//we save the RDF into a local filetry {
OutputStream os = new FileOutputStream(new File("out/tbl.n3"));model.write(os, "N3");
} catch (FileNotFoundException e) {e.printStackTrace();
}
SPARQL APIModel model = …;
// define the SPARQL query stringString queryString = "SELECT ?person ?name WHERE {" +
"?person <http://xmlns.com/foaf/0.1/name> ?name" + "}";
// create the query for JenaQuery query = QueryFactory.create(queryString);QueryExecution qExec = QueryExecutionFactory.create(query, model);
// execute the queryResultSet rs = qExec.execSelect();
while(rs.hasNext()){QuerySolution qSolution = rs.next();
// get the person and the nameResource person = qSolution.getResource("person");Literal name = qSolution.getLiteral("name");
System.out.println(person.getURI() + " has name " + name.getString());}
Storage and named graphs
// basic definitionsString bernersLeeName = "http://foo/Berners-Lee";String stebeJobsName = "http://foo/SteveJobs";
// create models named http://foo/Berners-Lee and http://foo/SteveJobs respectivelyModel bernersLeeModel = TDBFactory.createNamedModel(bernersLeeName,
"tmp/storage");Model steveJobsModel = TDBFactory.createNamedModel(stebeJobsName,
"tmp/storage");
// get the reference to the union graph of the graphs into the triple storeModel unionModel = TDBFactory.createNamedModel("urn:x-arq:UnionGraph",
"tmp/storage");
What is the OWL API?
• a Java framework for OWL2• a reference implementation for creating,
manipulating and serializing OWL Ontologies
• is open source and is available under the LGPL License
• provides an implementation of SWRL• is open to reasoners
OWL API
Key concepts
• OWL API completely builds on OWL2• the root object is the OWLOntologyManager,
that manages in-memory a set of OWL ontologies
• an OWLOntology is a set of OWL axioms• an OWLAxiom is a proposition that is not
proved but considered to be self-evident
OWL API
Adding OWL axioms// create the IRI for the ontologyString iriString = "http://foo/myontology";IRI ontologyIRI = IRI.create(iriString);
// create the ontology managerOWLOntologyManager manager = OWLManager.createOWLOntologyManager();
// create the ontology identified the ontologyIRI OWLOntology ontology = manager.createOntology(ontologyIRI);
// get the factory that allows to specify axiomsOWLDataFactory factory = manager.getOWLDataFactory();
OWLClass clsA = factory.getOWLClass(IRI.create(iriString+ "#A"));OWLClass clsB = factory.getOWLClass(IRI.create(iriString+ "#B"));
// create the axiomOWLAxiom axiom = factory.getOWLSubClassOfAxiom(clsA, clsB);
// add the axiom to the ontologyAddAxiom addAxiom = new AddAxiom(ontology, axiom);
// apply the change using the managermanager.applyChange(addAxiom);
OWL API
Reasoning// get the simple configuration for the reasonerOWLReasonerConfiguration config = new SimpleConfiguration();
// we need a concrete reasoner.// here there is a dependency with HermiT ReasonerFactory risfactory = new ReasonerFactory(); // create the reasonerOWLReasoner reasoner = risfactory.createReasoner(ontology,config); // ask the reasoner to do all the necessary work nowreasoner.precomputeInferences(); // ask if the ontology is consistentboolean consistent = reasoner.isConsistent(); if(consistent){ NodeSet<OWLClass> subClses = reasoner.getSubClasses(clsA, true);}else{ Node<OWLClass> bottomNode = reasoner.getUnsatisfiableClasses();}
OWL API
References
• http://owlapi.sourceforge.net/index.html
• Matthew Horridge, Sean Bechhofer.
The OWL API: A Java API for Working with OWL 2 Ontologies. OWLED 2009, 6th OWL Experienced and Directions Workshop, Chantilly, Virginia, October 2009.– http://www.webont.org/owled/2009/papers/
owled2009_submission_29.pdf
OWL API
What is Stanbol?• modular software stack • reusable set of components for semantic content
management• it offers:
- semantic lifting (Enhancer)- knowledge representation (OntoNet)- reasoning (Reasoner)- refactoring (Refactor)- triplification (Reengineer)- caching of entities from Linded Data (EntityHub)- storage of RDF (Store)
• Apache Incubator project• IKS project
The IKS stack
Semantic lifting (Enhancer)
• it implements a simple OSGi-based RESTful engine
• it enhances textual content, using pluggable enhancement engines– named entity recognition– Linked Data entity matching (DBPedia and
GeoNames)
KReS
Ontology Network
EntityHub
References
• the IKS wiki– http://wiki.iks-project.eu/index.php/Main_Page
• the Apache Stanbol home – http://incubator.apache.org/stanbol/index.html
• the source code– http://svn.apache.org/repos/asf/incubator/
stanbol/trunk/