%META:TOPICPARENT{name="VirtTipsAndTricksSPARQL11FeaturesExamplesCollection"}% ---+VOS vs Commercial Edition Features %TOC% The following Guide presents collection of Virtuoso VOS/Commercial Edition Supported Features examples usage and hints. ---++Simple SPARQL based Data Integration Example The following example showcase the combined effects of: * SPARQL Named Graphs (think: Table Names in SQL). * SPARQL 1.1 Property Paths . * Reasoning . ---+++The vCard Address Book Dataset Assume the following Address Book data constructed using terms from the vCard vocabulary: <#vcardRick> "Richard" ; "Mutt" ; . <#vcardAl> "Alan" ; "Smithee" ; . ---+++The FOAF Address Book Dataset Assume the following Address Book data constructed using terms from the FOAF vocabulary: <#foafBill> "Billy" ; "Shears" ; . <#foafNate> "Nanker" ; "Phelge" ; . ---+++Define the Problem In the data management realm, data definition eternally varies across designers, applications, and systems in general. In this simple example we have two Address Books containing the same data, but modeled using two different vocabularies (i.e., vCard and FOAF). This kind of "data definition-induced disparity" makes accurate data access, integration, and dissemination unnecessarily difficult, and serves as a powerful (albeit often unintentional) generator of data silos. ---+++The Solution We can work around this by using the expressive power of SPARQL to declaratively exploit entity relationship model-based data structure and machine-readable entity relationship semantics, as delivered by RDF. Basically, our query expressions will deliver identical query results, using properties from either ontology. ---+++How It's Done Simply, we create an additional dataset, that holds mappings between the properties defined by the two vocabularies, vCard and FOAF. ---++++Step 1.: vCard to FOAF Mapping Dataset * A mapping oriented Data Object constructed using terms from the OWL Ontology to describe entity relationship semantics. * This data object is comprised of statments (triples) that map certain properties across the vCard and FOAF vocabularies. * These mappings express machine readable entity relationship semantics usable by a Reasoner to produce (by inference) smart query results: . . . ---++++Step 2 Load Data ---+++++Step 2.1: Loading Data from Turtle Files using SPARQL 1.1 LOAD Command SPARQL 1.1 offers a LOAD command that provides an alternative to SPARQL 1.1 INSERT with regards to data load operations. For this guide, the steps would be as follows: 1. Copy and paste the Turtle content for each data source above into its own file, and save with an obvious name to a network accessible directory/folder, e.g., vcard_addressbook.ttl, foaf_addressbook.ttl, and vcard_foaf_mappings.ttl. 1. Open a Virtuoso SPARQL interface using /sparql-auth or /sparql-oauth or /sparql-webid -- each of which will provide authenticated access to Virtuoso's RDF store using an identity associated with DBMS operation privileges . 1. Execute the SPARQL 1.1 LOAD command for each file using the pattern: LOAD INTO 1 Example of these hyperlinks (URLs) denote the Turtle documents created in Kingsley's personal data space: * [[http://kingsley.idehen.net/DAV/home/kidehen/Public/Linked%20Data%20Documents/vcard_addressbook.ttl][vcard_addressbook.ttl]] * [[http://kingsley.idehen.net/DAV/home/kidehen/Public/Linked%20Data%20Documents/foaf_addressbook.ttl][foaf_addressbook.ttl]] * [[http://kingsley.idehen.net/DAV/home/kidehen/Public/Linked%20Data%20Documents/vcard_foaf_mapping.ttl][vcard_foaf_mapping.ttl]] 1 The actual LOAD commands are: LOAD INTO LOAD INTO LOAD INTO ---+++++Step 2.2: Loading Data into a Virtuoso RDF Store/DBMS via INSERT commands Example usage of Virtuoso's SPASQL (SQL+SPARQL hybrid language) and SPARQL 1.1's INSERT Language: 1 SPASQL * Load data to create the vCard-based Address Book Data Object: SPASQL INSERT INTO GRAPH { <#vcardRick> "Richard" ; "Mutt" ; . <#vcardAl> "Alan" ; "Smithee" ; . } * Load data to create the FOAF-based Address Book Data Object: INSERT INTO GRAPH { <#foafBill> "Billy" ; "Shears" ; . <#foafNate> "Nanker" ; "Phelge" ; . } * Load data to create the OWL-based vCard-to-FOAF mapping Data Object: INSERT INTO GRAPH { . . . } 1 SPARQL 1.1 * Load data to create the vCard-based Address Book Data Object: INSERT { GRAPH { <#vcardRick> "Richard" ; "Mutt" ; . <#vcardAl> "Alan" ; "Smithee" ; . } } * Load data to create the FOAF-based Address Book Data Object: INSERT { GRAPH { <#foafBill> "Billy" ; "Shears" ; . <#foafNate> "Nanker" ; "Phelge" ; . } } * Load data to create the OWL-based vCard-to-FOAF mapping Data Object: INSERT { GRAPH { . . . } } ---++++Step 3.: Verify Data Irrespective of the method used to load data into your Virtuoso (or any other SPARQL compliant RDF store/database), you should verify the load was successful using the following commands: 1 vCard-based Address Book: SELECT * FROM WHERE {?s ?p ?o} * [[http://bit.ly/14Wz9Gt][View the SPARQL Query Definition via SPARQL Protocol URL]] * [[http://bit.ly/YjM2Gu][View the SPARQL Query Results via SPARQL Protocol URL]] 1 FOAF-based Address Book: SELECT * FROM WHERE {?s ?p ?o} * [[http://bit.ly/XSzia2][View the SPARQL Query Definition via SPARQL Protocol URL]] * [[http://bit.ly/XboTXq][View the SPARQL Query Results via SPARQL Protocol URL]] 1 vCard-to-FOAF mappings: SELECT * FROM WHERE {?s ?p ?o} * [[http://bit.ly/YvAWgr][View the SPARQL Query Definition via SPARQL Protocol URL]] * [[http://bit.ly/14WzeKz][View the SPARQL Query Results via SPARQL Protocol URL]] ---++++Step 3.:Setting Up Inference Context Virtuoso requires the use of a SQL command to associate an Ontology with an Inference Rule. Once this association is in place, you simply use the Virtuoso SPARQL processor's pragma feature to conditionally invoke reasoning based on the rules you want. For this exercise, we'll create the rule "vcardTofoaf" by executing the following command using Virtuoso's SQL processor via command-line or HTML variants of iSQL: rdfs_rule_set ('vcardTofoaf','urn:sparql:fed:demo:terms:mapping') ---++++Step 4.: SPARQL Queries Demonstrating Problem Solution 1 Reasoning Disabled: PREFIX foaf: SELECT ?email ?fn ?ln FROM FROM WHERE { ?s foaf:firstName ?fn ; foaf:lastName ?ln ; foaf:email ?email . } * [[http://bit.ly/11KxyWs][View the SPARQL Query Definition via SPARQL Protocol URL]] * [[http://bit.ly/VkER20][View the SPARQL Query Results via SPARQL Protocol URL]] 1 Inference Context Enabled: ## Reasoning Enabled ## DEFINE input:inference "vcardTofoaf" PREFIX foaf: SELECT ?s ?ln ?fn ?email FROM FROM WHERE { ?s foaf:familyName ?ln ; foaf:givenName ?fn ; foaf:mbox ?email . } * [[http://bit.ly/VYWRwj][View the SPARQL Query Definition via SPARQL Protocol URL]] * [[http://bit.ly/XbpbO2][View the SPARQL Query Results via SPARQL Protocol URL]] 1 Inline Inference using Property Paths: As an alternative to using the Virtuoso reasoner, and more cross SPARQL 1.1 compliant RDF store/dbms compatible, you can apply the vCard to FOAF ontology mappings inline -- courtesy SPARQL 1.1 property paths based query patterns: ## Property Paths Query based mapping between vCard and FOAF ## ## where "*" operator is used to deliver a magic predicate for inline mapping of ## ## key FOAF ontology properties (givenName, familyName, and mbox) to vCard ontology properties. ## SELECT ?email ?fn ?ln WHERE { * ?given_nameProp . * ?last_nameProp . * ?emailProp . ?s ?given_nameProp ?fn ; ?last_nameProp ?ln ; ?emailProp ?email . } LIMIT 10 * [[http://bit.ly/WuB7ue][View the SPARQL Query Definition via SPARQL Protocol URL]] * [[http://bit.ly/Xbpjgq][View the SPARQL Query Results via SPARQL Protocol URL]] ---++Import an existing ontology (OWL/RDF)? Use one of these: 1 [[virtuoso.openlinksw.com/dataspace/dav/wiki/Main/VirtSponger][Virtuoso Sponger]] 1 SPARQL LOAD -- via Conductor, iSQL, or /sparql-auth (which authenticates against SQL accounts) or /sparql-webid (on ssl port) for WebID ACL based authentication ---++++Links * [[http://wiki.usnet.private/dataspace/doc/dav/wiki/Main/VirtTipsAndTricksN3IsPropertyOfSyntaxInTurtle][Using {is property of} for inverse relations in Turtle]] ---++Connect two or more databases (Oracle, Informix, Postgres)? Using the VDB layer via Conductor or ATTACH statement via SQL. ---++++Links 1. [[Attaching Remote ODBC accessible data sources][http://virtuoso.openlinksw.com/dataspace/dav/wiki/Main/VirtLinkRemoteTables]] 1. [[http://virtuoso.openlinksw.com/dataspace/dav/wiki/Main/VOSSQL2RDF][Linked Data Views]] ---++ Create merged/unified classes? So for example, Person object from Oracle database A, and Person object from Informix database B should be able to be unified. Based on the above, you simply add triples to a TBox triples oriented named graph. Example: a foaf:Person . a foaf:Person . ---++ Map the ontology classes to DB classes? Just more TBox triples, for example: owl:equivalentClass . rdfs:subClassOf . owl:equivalentProperty . rdfs:subPropertyOf . ---++ Add conditions mapping? For example, in a table, if a column is 1, the row represents a student, but if it's 2, the row represents a faculty member. As this part of the R2RML or Native Linked Data Views declarations and it is in SPASQL, you can leverage CASE WHEN etc.. ---++ Get data from web services and apply the same mapping to these services? This requires the Sponger Middleware component. If we don't have a cartridge in place, then a custom cartridge can be developed. ---++++Links 1. [[http://uriburner.com][Virtuoso Public instance of the Sponger]] 1. [[http://uriburner.com/sponger_architecture.vsp#how_it_works][How the sponger middleware runs]] ---++ Provide RDF's to external systems? You have the following options: 1. SPARQL endpoint -- for systems that support SPARQL-FED or capable of de-referencing a SPARQL Protocol URL 1. Saving Query output to a local or network drive -- you can even leverage the ability to save SPARQL Query Results to an HTTP accessible WebDAV folder (once enabled via the conductor). ---++Related * [[VirtSPARQLReasoningTutorial][Tutorial Demonstrating Reasoning via SPARQL]] * [[http://www.snee.com/bobdc.blog/2012/04/simple-federated-queries-with.html][Bob DuCharme's original SPARQL demo/tutorial]] * [[http://www.w3.org/TR/vcard-rdf/][vCard Vocabulary]] * [[http://xmlns.com/foaf/0.1/][FOAF Vocabulary]] * [[http://www.w3.org/2002/07/owl#][OWL Vocabulary (specifically defines entity relationship semantics e.g., equivalence)]] * [[http://bit.ly/Xk333m][An Introductory Turtle Notation Tutorial]] * [[http://bit.ly/VaX0zx][Other Turtle Notation Tutorials]] * [[http://bit.ly/Wk19i4][DBpedia Inference based Data Cleansing]] * [[http://bit.ly/W3iQoE][Exploiting the Power of OWL Entity Relationship Semantics via Linked Vocabularies]] * [[http://www.w3.org/TR/sparql11-query/#propertypath-syntaxforms][SPARQL 1.1 Property Paths]] * [[VirtTipsAndTricksGuide][Virtuoso Tips and Tricks Collection]]