Skip to content

Instantly share code, notes, and snippets.

@asanchez75
Created September 1, 2018 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asanchez75/93ff4079819022bcd2cf42d4ec6bc335 to your computer and use it in GitHub Desktop.
Save asanchez75/93ff4079819022bcd2cf42d4ec6bc335 to your computer and use it in GitHub Desktop.
-- This a SPARQL script to test SPIN Rules chaining in Virtuoso 8
-- It should compute the volume of an instance of the class Solid in two steps
-- a) given the width and the height computes the area
-- b) the area computed previously is multiplied by the height (again) to compute the volume.
-- This script must be run in the SQL console of Virtuoso
-- Additional commands
SPARQL CLEAR GRAPH <http://geometry> ;
SPARQL CLEAR GRAPH <urn:spin:rule:geometry:lib> ;
SPARQL DROP SPIN LIBRARY <urn:spin:rule:geometry:lib> ;
-- create a named graph
-- SPARQL CREATE GRAPH <http://geometry> ;
SPARQL
PREFIX shapes: <http://example.org/shapes#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
WITH <http://geometry>
INSERT {
shapes:Solid
rdf:type owl:Class .
shapes:solid_1
rdf:type shapes:Solid ;
shapes:height "5" ;
shapes:width "49" .
shapes:area rdf:type owl:DatatypeProperty .
shapes:height rdf:type owl:DatatypeProperty .
shapes:volume rdf:type owl:DatatypeProperty .
shapes:width rdf:type owl:DatatypeProperty .
} ;
-- Test Generated Data Control Check
SPARQL
PREFIX shapes: <http://example.org/shapes#>
PREFIX spin: <http://spinrdf.org/spin#>
PREFIX sp: <http://spinrdf.org/sp#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
# CONSTRUCT { ?this <http://example.org/shapes#area> ?area . }
SELECT ?this ?area
FROM <http://geometry>
WHERE {
?this <http://example.org/shapes#width> ?width .
?this <http://example.org/shapes#height> ?height .
BIND ((xsd:float(?height) * xsd:float(?width)) AS ?area) .
} ;
-- create the rule
-- SPARQL CREATE GRAPH <urn:spin:rule:geometry:lib> ;
SPARQL
PREFIX shapes: <http://example.org/shapes#>
PREFIX spin: <http://spinrdf.org/spin#>
PREFIX sp: <http://spinrdf.org/sp#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
WITH <urn:spin:rule:geometry:lib>
INSERT {
shapes:Solid
rdf:type owl:Class;
rdfs:label "Solid class";
spin:rule [
a sp:Construct;
sp:text """
CONSTRUCT { ?this <http://example.org/shapes#area> ?area }
WHERE {
{
SELECT ?this (xsd:float(?height) * xsd:float(?width)) AS ?area
WHERE {
?this <http://example.org/shapes#width> ?width ;
<http://example.org/shapes#height> ?height .
}
}
}
"""
] ;
spin:rule [
a sp:Construct;
sp:text """
CONSTRUCT { ?this <http://example.org/shapes#volume> ?volume }
WHERE {
{
SELECT ?this (xsd:float(?height) * xsd:float(?area)) AS ?volume
WHERE {
?this <http://example.org/shapes#area> ?area ;
<http://example.org/shapes#height> ?height .
}
}
}
"""
] .
} ;
EXEC ('SPARQL ' || SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:spin:rule:geometry:lib'));
-- run a sparql query to test the spin rule
-- Test 1 (Reasoning & Inference Context Enabled )
SPARQL
DEFINE input:macro-lib <urn:spin:rule:geometry:lib>
PREFIX shapes: <http://example.org/shapes#>
SELECT *
FROM <http://geometry>
WHERE
{
?s a shapes:Solid ;
shapes:area ?area ;
shapes:volume ?volume .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment