-- This a SPARQL script to test SPIN Rules in Virtuoso 8 -- It should compute the area of an instance of the class Rectangle given its width and its height. -- This script must be run in the SQL console of Virtuoso -- Additional commands SPARQL CLEAR GRAPH ; SPARQL CLEAR GRAPH ; SPARQL DROP SPIN LIBRARY ; -- create a named graph -- SPARQL CREATE GRAPH ; SPARQL PREFIX shapes: PREFIX rdf: PREFIX owl: WITH INSERT { shapes:Rectangle rdf:type owl:Class . shapes:rectangle_1 rdf:type shapes:Rectangle ; shapes:height "5" ; shapes:width "49" . shapes:area rdf:type owl:DatatypeProperty . shapes:height rdf:type owl:DatatypeProperty . shapes:width rdf:type owl:DatatypeProperty . } ; -- Test Generated Data Control Check SPARQL PREFIX shapes: PREFIX spin: PREFIX sp: PREFIX xsd: PREFIX owl: # CONSTRUCT { ?this ?area . } SELECT ?this ?area FROM WHERE { ?this ?width . ?this ?height . BIND ((xsd:float(?height) * xsd:float(?width)) AS ?area) . } ; -- create the rule --- SPARQL CREATE SPIN MACRO LIBRARY using Named Graph ; -- EXEC ('SPARQL ' || SPARQL_SPIN_GRAPH_TO_DEFSPIN('urn:spin:rule:bind:geometry:lib')); -- Add Macro to Library Manually -- UNION STORAGE fuses actual and calculated area when enabled, and if it is disabled it will only deal with calculated area? EXEC ( 'SPARQL ' || 'CREATE SPIN LIBRARY { CLASS { RULE { CONSTRUCT { ?this ?area } WHERE { { SELECT ?this ?area WHERE { ?this ?width ; ?height . BIND ((xsd:float(?height) * xsd:float(?width)) AS ?area) . } } } UNION STORAGE } } } ASK WHERE { }' ) ; --- ATTACH TO virtrdf:DefaultQuadStorage --- meaning: it is applied to all queries scoped to this storage --- thereby not requiring invocation via pragma SPARQL ALTER QUAD STORAGE virtrdf:DefaultQuadStorage { ATTACH MACRO LIBRARY }; -- run a sparql query to test the spin rule -- Test 1 (Reasoning & Inference via Pragma ) SPARQL DEFINE input:macro-lib PREFIX shapes: SELECT * FROM WHERE { ?s a shapes:Rectangle ; shapes:area ?area . } ; -- Test 2 (Reasoning & Inference without Pragma ) -- Courtesy of ATTACH TO virtrdf:DefaultQuadStorage, -- reasoning and inference occurs i.e., same solution as Test 1 SPARQL # DEFINE input:macro-lib PREFIX shapes: SELECT * FROM WHERE { ?s a shapes:Rectangle ; shapes:area ?area . } ; -- Test 3 (Reasoning & Inference Context Enabled ) SPARQL DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s shapes:area ?area }. } ; -- Test 4 (Reasoning & Inference without Pragma) -- Courtesy of ATTACH TO virtrdf:DefaultQuadStorage, -- reasoning and inference occurs i.e., same solution as Test 3 SPARQL # DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s shapes:area ?area }. } ; -- Test 5 SPARQL DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s a shapes:Rectangle ; shapes:area ?area }. } ; -- Test 6 -- Courtesy of ATTACH TO virtrdf:DefaultQuadStorage, -- reasoning and inference occurs i.e., same solution as Test 5 SPARQL # DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s a shapes:Rectangle ; shapes:area ?area }. } ; -- Detach Macro Library thereby requiring pragmas for reasoning and inference using -- Custom Inference Rules associated with Macro Library SPARQL ALTER QUAD STORAGE virtrdf:DefaultQuadStorage { DETACH SPIN LIBRARY } ; -- Test 7 (Reasoning & Inference via Pragma ) SPARQL DEFINE input:macro-lib PREFIX shapes: SELECT * FROM WHERE { ?s a shapes:Rectangle ; shapes:area ?area . } ; -- Test 8 (Reasoning & Inference without Pragma ) SPARQL # DEFINE input:macro-lib PREFIX shapes: SELECT * FROM WHERE { ?s a shapes:Rectangle ; shapes:area ?area . } ; -- Test 9 (Reasoning & Inference Context Enabled ) SPARQL DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s shapes:area ?area }. } ; -- Test 10 (Reasoning & Inference without Pragma) SPARQL # DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s shapes:area ?area }. } ; -- Test 11 SPARQL DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s a shapes:Rectangle ; shapes:area ?area }. } ; -- Test 12 SPARQL # DEFINE input:macro-lib PREFIX shapes: SELECT ?s xsd:float(?w) as ?width xsd:float(?h) as ?height xsd:float(?area) as ?area FROM WHERE { ?s a shapes:Rectangle; shapes:width ?w ; shapes:height ?h . OPTIONAL { ?s a shapes:Rectangle ; shapes:area ?area }. } ;