<export><workspace name="Inference-002"><query name="insert-1" focus="false" listorder="2" taborder="2" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql-update">PREFIX rdf:   &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:  &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX prod:  &lt;http://example.com/products/&gt; 
PREFIX ex:    &lt;http://example.com/&gt;

## insert some triples into &lt;http://marklogic.com/semantics/sb/products/inf-1&gt;
## prod:1001 is a blue Henley
## prod:1002 is a blue shirt
INSERT DATA 
{ 
  GRAPH &lt;http://marklogic.com/semantics/sb/products/inf-1&gt; 
  {
      prod:1001	  rdf:type	ex:Henley ;
                 	ex:color	"blue"	.
      prod:1002  	rdf:type	ex:Shirt ;
                 	ex:color	"blue" .

  }
}
</query><query name="query-1" focus="false" listorder="3" taborder="3" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql">PREFIX rdf:    &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:   &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX prod:   &lt;http://example.com/products/&gt; 
PREFIX ex:     &lt;http://example.com/&gt;

# search for blue shirts

SELECT ?product
FROM &lt;http://marklogic.com/semantics/sb/products/inf-1&gt;
WHERE 
  { 
    ?product  rdf:type  ex:Shirt ;
              ex:color  "blue"
  }
</query><query name="insert-ont-1" focus="false" listorder="4" taborder="4" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql-update">PREFIX rdf:   &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:  &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX prod:  &lt;http://example.com/products/&gt; 
PREFIX ex:    &lt;http://example.com/&gt;

## insert some ontology data into http://marklogic.com/semantics/sb/products/inf-1
INSERT DATA 
{ 
  GRAPH &lt;http://marklogic.com/semantics/sb/products/inf-1&gt; 
  {
      ex:Henley 	rdfs:subClassOf	ex:Shirt 	.
  }
}</query><query name="query-2-RDFS" focus="false" listorder="5" taborder="5" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="xquery">(: 
  If you query for blue shirts, even though the ontology says that Henleys are blue shirts, you won't get blue Henleys unless you associate a ruleset with the query.
  * create a store that uses the RDFS ruleset for inferencing 
  * add that store to the query
  * run the query and you'll see the blue shirt AND the blue Henley
:)
let $rdfs-store := sem:ruleset-store("rdfs.rules",sem:store() )

return
  (: use the store you just created - pass it into sem:sparql() :)
  sem:sparql('
PREFIX rdf:    &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:   &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX prod:   &lt;http://example.com/products/&gt; 
PREFIX ex:     &lt;http://example.com/&gt;

SELECT ?product
FROM &lt;http://marklogic.com/semantics/sb/products/inf-1&gt;
WHERE 
  { 
    ?product  rdf:type  ex:Shirt ;
              ex:color  "blue"
  }
  ',
  (),
  (),
  $rdfs-store
  )
  
</query><query name="my-ruleset-insert-1" focus="false" listorder="7" taborder="7" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql-update">## insert some triples from geonames
## * "Greater London" has parentFeature "England"

PREFIX gn: &lt;http://www.geonames.org/ontology#&gt;
PREFIX owl: &lt;http://www.w3.org/2002/07/owl#&gt;
PREFIX rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;

INSERT DATA 
{
  GRAPH &lt;http://marklogic.com/semantics/sb/customers/inf-1&gt; {
    &lt;http://sws.geonames.org/2648110/&gt; rdfs:isA gn:Feature ;
       rdfs:isDefinedBy &lt;http://sws.geonames.org/2648110/about.rdf&gt; ;
       gn:name "Greater London" ;
       gn:parentFeature &lt;http://sws.geonames.org/6269131/&gt; .

&lt;http://sws.geonames.org/6269131/&gt; rdfs:isA gn:Feature ;
   rdfs:isDefinedBy &lt;http://sws.geonames.org/6269131/about.rdf&gt; ;
      gn:name "England" ;
      gn:parentFeature &lt;http://sws.geonames.org/2635167/&gt; .
    }
}</query><query name="my-ruleset-insert-2" focus="false" listorder="8" taborder="8" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql-update">## insert some triples about a customer - John Smith
## * John Smith lives in "Greater London"

PREFIX gn:   &lt;http://www.geonames.org/ontology#&gt;
PREFIX owl:  &lt;http://www.w3.org/2002/07/owl#&gt;
PREFIX rdf:  &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
PREFIX rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX cus:  &lt;http://marklogic.com/semantics/sb/customers/inf-1#&gt;
PREFIX ex:   &lt;http://example.com/&gt;

WITH &lt;http://marklogic.com/semantics/sb/customers/inf-1&gt;
INSERT
{
    cus:JohnSmith ex:livesIn ?city .
    }
  WHERE {
    ?city gn:name "Greater London" .
    }
</query><query name="insert-my-ruleset" focus="false" listorder="9" taborder="9" active="true" database="12509723534964595163" server="14950309219653925761" database-name="Schemas" server-name="App-Services" mode="xquery">(: 
  Create a ruleset that defines the predicate "livesIn"
  * create a rules file and insert it into the Schemas database
  Note: run this from Query Console with "Content Source" set to "Schemas" 
:)

xdmp:document-insert(
  '/rules/livesin.rules' ,
  text{
  '
# my rules for inference
prefix rdf:     &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;
prefix rdfs:    &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
prefix ex:      &lt;http://example.com/&gt;
prefix gn:      &lt;http://www.geonames.org/ontology#&gt;

rule "lives in" construct {
  ?person ex:livesIn ?place2
} {
  ?person ex:livesIn ?place1 .
  ?place1 gn:parentFeature ?place2
 }
 '
 }
 )
</query><query name="query-my-ruleset" focus="false" listorder="10" taborder="10" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="xquery">(: 
  find places that John Smith lives in – with inferencing, using my ruleset 
:)

let $my-store := sem:ruleset-store("/rules/livesin.rules",sem:store() )

return
  (: use the store you just created - pass it in to sem:sparql() :)
  sem:sparql('
PREFIX rdf:    &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:   &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX gn:     &lt;http://www.geonames.org/ontology#&gt;
PREFIX ex:     &lt;http://example.com/&gt;

SELECT ?person ?placeName
FROM &lt;http://marklogic.com/semantics/sb/customers/inf-1&gt;
WHERE 
  { 
    ?person  ex:livesIn ?place .
    ?place   gn:name ?placeName
  }
  ORDER BY ?person
  ',
  (),
  (),
  $my-store
  )
</query><query name="query-sans-my-ruleset" focus="false" listorder="11" taborder="11" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql">## to check: find places that John Smith lives in – without inferencing

PREFIX rdf:    &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:   &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX gn:     &lt;http://www.geonames.org/ontology#&gt;
PREFIX ex:     &lt;http://example.com/&gt;

SELECT ?person ?placeName
FROM &lt;http://marklogic.com/semantics/sb/customers/inf-1&gt;
WHERE 
  { 
    ?person  ex:livesIn ?place .
    ?place   gn:name ?placeName
  }</query><query name="query-my-ruleset-js" focus="false" listorder="12" taborder="12" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="javascript">/*
  Query using my rukleset - JavaScript
*/

var myStore = sem.rulesetStore("/rules/livesin.rules",sem.store() )

var query = "    \
PREFIX rdf:    &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt;     \
PREFIX rdfs:   &lt;http://www.w3.org/2000/01/rdf-schema#&gt;     \
PREFIX gn:     &lt;http://www.geonames.org/ontology#&gt;     \
PREFIX ex:     &lt;http://example.com/&gt;     \
     \
SELECT ?person ?placeName     \
FROM &lt;http://marklogic.com/semantics/sb/customers/inf-1&gt;     \
WHERE      \
  {      \
    ?person  ex:livesIn ?place .     \
    ?place   gn:name ?placeName     \
  }"
    
sem.sparql(    
  query,
  [],
  [],
  myStore
  )
</query><query name="query-no-inf-1" focus="false" listorder="13" taborder="13" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql">##
## Find blue shirts WITHOUT the need for an inference rule
## * use a proprtty path
## * Note: this is generally much more efficient than using inference
##

PREFIX rdf:    &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:   &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX prod:   &lt;http://example.com/products/&gt; 
PREFIX ex:     &lt;http://example.com/&gt;

SELECT ?product
FROM &lt;http://marklogic.com/semantics/sb/products/inf-1&gt;
WHERE 
{
    ?product rdf:type/rdfs:subClassOf* ex:Shirt .
}

</query><query name="add-default-ruleset" focus="false" listorder="14" taborder="14" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="xquery">
(:
  Set the default ruleset for the database "Documents" to "rdfs.rules"
  Now any SPARQL query against the "Documents" database will apply the RDFS ruleset by default.
  Note: other ways to do this:
      * in the Admin UI, click on database "Documents", scroll down to "default rulesets" on the left-hand side
      * use the REST management API - see [http://localhost:8002]/manage/v2/databases/inference/properties
:)

import module namespace admin = "http://marklogic.com/xdmp/admin" 
      at "/MarkLogic/admin.xqy";
	   
let $config := admin:get-configuration()
let $dbid   := admin:database-get-id($config, "Documents")
let $rules  := admin:database-ruleset("rdfs.rules")
let $new-config := admin:database-add-default-ruleset($config, $dbid, $rules)
return admin:save-configuration($new-config)</query><query name="get-default-rulesets" focus="false" listorder="15" taborder="15" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="xquery">   
(:
  Get the default ruleset(s) for the database "Documents"
  Note: other ways to do this:
      * in the Admin UI, click on database "Documents", scroll down to "default rulesets" on the left-hand side
      * use the REST management API - see [http://localhost:8002]/manage/v2/databases/Documents/properties
:)

import module namespace admin = "http://marklogic.com/xdmp/admin" 
      at "/MarkLogic/admin.xqy";
      
let $config := admin:get-configuration()
let $dbid   := admin:database-get-id($config, "Documents")
let $rules  := admin:database-ruleset("rdfs.rules")
let $db-rulesets := admin:database-get-default-rulesets($config, $dbid)
return $db-rulesets</query><query name="delete-default-rulesets" focus="false" listorder="16" taborder="16" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="xquery">import module namespace admin = "http://marklogic.com/xdmp/admin" 
      at "/MarkLogic/admin.xqy";
	   
(:
  Delete all the default rulesets for the database "Documents"
  Note: other ways to do this:
      * in the Admin UI, click on database "Documents", scroll down to "default rulesets" on the left-hand side
      * use the REST management API - see [http://localhost:8002]/manage/v2/databases/Documents/properties
:)

let $config := admin:get-configuration()
let $dbid   := admin:database-get-id($config, "Documents")
let $rules  := admin:database-ruleset("rdfs.rules")
let $db-rulesets := admin:database-get-default-rulesets($config, $dbid)
let $new-config := admin:database-delete-default-ruleset($config, $dbid, $db-rulesets)
return admin:save-configuration($new-config)</query><query name="insert-2" focus="false" listorder="18" taborder="18" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql-update">PREFIX rdf:   &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:  &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX owl:   &lt;http://www.w3.org/2002/07/owl#&gt;
PREFIX prod:  &lt;http://example.com/products/&gt; 
PREFIX ex:    &lt;http://example.com/&gt;


## insert another triple into &lt;http://marklogic.com/semantics/sb/products/inf-1&gt;
## prod:1003 is a Henley that has hue "blue"

INSERT DATA 
{ 
  GRAPH &lt;http://marklogic.com/semantics/sb/products/inf-1&gt; 
  {
      prod:1003	rdf:type	ex:Henley ;
                 	ex:hue	"blue"	.
  }
}
</query><query name="insert-ont-2" focus="false" listorder="19" taborder="19" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql-update">PREFIX rdf:   &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:  &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX owl:   &lt;http://www.w3.org/2002/07/owl#&gt;
PREFIX prod:  &lt;http://example.com/products/&gt; 
PREFIX ex:    &lt;http://example.com/&gt;

## insert some more ontology data into http://marklogic.com/semantics/sb/products/inf-1
## the predicate ex:color is equivalent to the predicate ex:hue
INSERT DATA 
{ 
  GRAPH &lt;http://marklogic.com/semantics/sb/products/inf-1&gt; 
  {
      ex:color 	owl:equivalentProperty	ex:hue 	.
  }
}</query><query name="query-RDFS-equiv" focus="true" listorder="20" taborder="20" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="xquery">(: 
  SPARQL query using 2 rulesets, RDFS and owl:equivalentProperty
  Other ways to do this:
  * set the default rulesets for the database to RDFS and owl:equivalentProperty;
  * query using SPARQL Query from query console or REST endpoint
:)

(: create a store that uses the RDFS and owl:equivalentProperty rulesets for inferencing :)
let $rdfs-store := sem:ruleset-store( ("rdfs.rules", "equivalentProperty.rules"), sem:store() )

return
  (: use the store you just created - pass it into sem:sparql() :)
  sem:sparql('
PREFIX rdf:    &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; 
PREFIX rdfs:   &lt;http://www.w3.org/2000/01/rdf-schema#&gt;
PREFIX prod:   &lt;http://example.com/products/&gt; 
PREFIX ex:     &lt;http://example.com/&gt;

SELECT ?product
FROM &lt;http://marklogic.com/semantics/sb/products/inf-1&gt;
WHERE 
  { 
    ?product  rdf:type  ex:Shirt ;
              ex:color  "blue"
  }
  ',
  (),
  (),
  $rdfs-store
  )
  
</query><query name="Description 1." focus="false" listorder="1" taborder="1" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql">## Use an ontology and a built-in ruleset for inference
</query><query name="Description 2." focus="false" listorder="6" taborder="6" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql">## write your own ruleset and use it in a query</query><query name="Description 3." focus="false" listorder="17" taborder="17" active="true" database="12000676252973117691" server="14950309219653925761" database-name="Documents" server-name="App-Services" mode="sparql">##
## Inference with more than 1 ruleset
## Note: you can also create your own ruleset that combines existing rulesets (using "import")
##  * see the rulesets in your MarkLogic install directory at $MARKLOGIC/Confid/*.rules
##
</query></workspace></export>
