owl - SWRLb - greaterThan and lessThan triggered -
i have problem using swrl rules. have robot should notify human in case of 2 abnormal situations. test case use tension of user:
if tension less 14, robot alert tension low:
(:hastension :charles ?x) ∧ greaterthan(?x, "14.0"^^xsd:float) → (:hasalert :samii "your tension high"^^xsd::string)
if tension greater 14, robot alert tension high.
(:hastension :charles ?x) ∧ lessthan(?x, "14.0"^^xsd:float) → (:hasalert :samii "your tension low"^^xsd::string)
i using owl api pellet reasoner (openllet fork of pellet). have swrl rules using builtin swrlb:greaterthan , swrlb:lessthan. test add axiom
:charles :hastension "10"^^xsd:float
and query data property
:samii :hasalert ?alert
but when query ontology, 2 alerts: 1 greaterthan , other lessthan. think rules formed, , don't have warning or error api saying builtin not implemented, believe rules should work i'd expect. idea why i'm getting unexpected alert?
resources
my test "main"
package com.samii.restful; import java.util.arraylist; import org.apache.jena.rdf.model.statement; public class local { public static void main(string[] args) { string path = ""+ servlet.class.getresource("/ontologies/justatest_rdfxml.owl"); local.mainpelletowlapi(path); } public static void mainpelletowlapi( string path ){ owlmanagement owl_management = new owlmanagement( path ); system.out.println( "[main].main => \n\n== before adding axiom ==" ); owl_management.printontology(); system.out.println( "[main].main => \n\n== query ==" ); string uri = "file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#"; string ssubject = "samii"; string spredicate = "hasalert"; arraylist<object> literals = owl_management.querydataproperty(uri, ssubject, spredicate); system.out.println("[main].main() -> number of literals: " + literals.size() ); if( !literals.isempty() ){ for( object literal : literals){ system.out.print( "->" + ssubject + " " + spredicate + " " + literal ); } } ssubject = "charles"; spredicate = "hastension"; string sobject = "10"; owl_management.updatefunctionalobjectsproperty(uri, ssubject, spredicate, sobject, "float"); system.out.println( "[main].main => \n\n== after adding axiom ==" ); owl_management.printontology(); system.out.println( "[main].main => \n\n== query ==" ); ssubject = "samii"; spredicate = "hasalert"; literals = owl_management.querydataproperty(uri, ssubject, spredicate); system.out.println("[main].main() -> number of literals: " + literals.size() ); if( !literals.isempty() ){ for( object literal : literals){ system.out.println( "->" + ssubject + " " + spredicate + " " + literal ); } } } }
the core:
package com.samii.restful; import java.util.arraylist; import java.util.set; import java.lang.object; import com.clarkparsia.pellet.owlapi.pelletreasoner; import com.clarkparsia.pellet.owlapi.pelletreasonerfactory; import org.mindswap.pellet.exceptions.inconsistentontologyexception; import org.semanticweb.owlapi.apibinding.owlmanager; import org.semanticweb.owlapi.model.iri; import org.semanticweb.owlapi.model.addaxiom; import org.semanticweb.owlapi.model.removeaxiom; import org.semanticweb.owlapi.model.owlaxiom; import org.semanticweb.owlapi.model.owlclass; import org.semanticweb.owlapi.model.owldatatype; import org.semanticweb.owlapi.model.owlfunctionaldatapropertyaxiom; import org.semanticweb.owlapi.model.owlindividual; import org.semanticweb.owlapi.model.owldataproperty; import org.semanticweb.owlapi.model.owldatafactory; import org.semanticweb.owlapi.model.owlliteral; import org.semanticweb.owlapi.model.owlnamedindividual; import org.semanticweb.owlapi.model.owlobjectproperty; import org.semanticweb.owlapi.model.owlontology; import org.semanticweb.owlapi.model.owlontologymanager; import org.semanticweb.owlapi.reasoner.freshentitiesexception; import org.semanticweb.owlapi.reasoner.node; import org.semanticweb.owlapi.reasoner.nodeset; import org.semanticweb.owlapi.reasoner.reasonerinterruptedexception; import org.semanticweb.owlapi.reasoner.timeoutexception; import org.semanticweb.owlapi.vocab.owl2datatype; import org.semanticweb.owlapi.util.shortformprovider; import org.semanticweb.owlapi.util.simpleshortformprovider; import org.semanticweb.owlapi.io.systemoutdocumenttarget; import org.semanticweb.owlapi.io.owlfunctionalsyntaxontologyformat; public class owlmanagement { owlontologymanager _manager; owlontology _ontology; owldatafactory _factory; pelletreasoner _reasoner; owlmanagement(string ontology_file_path){ _manager = owlmanager.createowlontologymanager(); _factory = _manager.getowldatafactory(); try{ _ontology = _manager.loadontology(iri.create( ontology_file_path )); } catch(exception e){ system.out.println("exception " + e.tostring()); } //_reasoner = pelletreasonerfactory.getinstance().createreasoner(_ontology); _reasoner = pelletreasonerfactory.getinstance().createnonbufferingreasoner(_ontology); _reasoner.preparereasoner(); } public boolean updatefunctionalobjectsproperty( string uri, string ssubject, string spredicate, string sobject, string stype ){ owlliteral literal; switch(stype){ case "float": owldatatype xsd_float = _factory.getowldatatype( owl2datatype.xsd_float ); literal = _factory.getowlliteral(sobject , xsd_float); break; default: literal = _factory.getowlliteral("undefined type"); break; } owlnamedindividual individual = _factory.getowlnamedindividual(uri, ssubject); owldataproperty data_property = _factory.getowldataproperty(uri, spredicate); //owlfunctionaldatapropertyaxiom functional_data_property = _factory.getowlfunctionaldatapropertyaxiom( data_property ); owlaxiom axiom = _factory.getowldatapropertyassertionaxiom( data_property, individual, literal); set<owlliteral> literals = _reasoner.getdatapropertyvalues( individual, data_property ); if( !literals.isempty() ){ //_manager.removeaxiom( _ontology, axiom ); _manager.applychange(new removeaxiom( _ontology, axiom) ); } _manager.applychange(new addaxiom( _ontology, axiom) ); return true; } public void printontology(){ try{ // print out ontology on system.out _manager.saveontology(_ontology, new owlfunctionalsyntaxontologyformat(), new systemoutdocumenttarget()); } catch(exception e){ system.out.println("[owlmanagement].printontology() -> catched exception:"); system.out.println("exception " + e.tostring()); } //_manager.saveontology(o, new owlxmlontologyformat(), documentiri2); // save in rdf/xml //_manager.saveontology(o, documentiri2); // remove ontology manager //_manager.removeontology(o); } public arraylist<object> querydataproperty( string uri, string ssubject, string spredicate ){ owlnamedindividual individual = _factory.getowlnamedindividual(uri, ssubject); owldataproperty data_property = _factory.getowldataproperty(uri, spredicate); set<owlliteral> literals = null; try{ literals = _reasoner.getdatapropertyvalues( individual, data_property ); system.out.println("[owlmanagement].printontology() -> number of literals: " + literals.size() ); } catch( inconsistentontologyexception e ){ system.out.println("[owlmanagement].printontology() -> catched inconsistentontologyexception:"); system.out.println("exception " + e.tostring()); } catch( freshentitiesexception e ){ system.out.println("[owlmanagement].printontology() -> catched freshentitiesexception:"); system.out.println("exception " + e.tostring()); } catch( reasonerinterruptedexception e ){ system.out.println("[owlmanagement].printontology() -> catched reasonerinterruptedexception:"); system.out.println("exception " + e.tostring()); } catch( timeoutexception e ){ system.out.println("[owlmanagement].printontology() -> catched timeoutexception:"); system.out.println("exception " + e.tostring()); } catch( exception e){ system.out.println("[owlmanagement].printontology() -> catched exception:"); system.out.println("exception " + e.tostring()); } arraylist<object> objects = new arraylist<object>(); if( !literals.isempty() ){ for(owlliteral literal: literals){ if( literal.isinteger() ){ objects.add( literal.parseinteger() ); } else if( literal.isfloat() ){ objects.add( literal.parsefloat() ); } else if( literal.isdouble() ){ objects.add( literal.parsedouble() ); } else if( literal.isboolean() ){ objects.add( literal.parseboolean() ); } else{ objects.add( literal.getliteral() ); } } } system.out.println("[owlmanagement].printontology() -> number of objects: " + literals.size() ); return objects; } }
and ontolgy
<?xml version="1.0" encoding="utf-8"?> <!doctype rdf:rdf [ <!entity owl "http://www.w3.org/2002/07/owl#" > <!entity owl11 "http://www.w3.org/2006/12/owl11#" > <!entity xsd "http://www.w3.org/2001/xmlschema#" > <!entity owl11xml "http://www.w3.org/2006/12/owl11-xml#" > <!entity rdfs "http://www.w3.org/2000/01/rdf-schema#" > <!entity rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" > <!entity cr-owl-guide-20030818 "http://www.w3.org/tr/2003/cr-owl-guide-20030818/" > <!entity local "file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#" > <!entity swrlb "http://www.w3.org/2003/11/swrlb#" > <!entity swrl "http://www.w3.org/2003/11/swrl#" > ]> <rdf:rdf xml:base ="file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml" xmlns ="file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#" xmlns:local="file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xsd="http://www.w3.org/2001/xmlschema#" xmlns:var="urn:swrl#" xmlns:xml="http://www.w3.org/xml/1998/namespace" xmlns:swrlb="http://www.w3.org/2003/11/swrlb#" xmlns:swrl="http://www.w3.org/2003/11/swrl#"> <!--owl:ontology rdf:about="file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#"/--> <owl:ontology rdf:about=""/> <!--defining classes robots--> <owl:class rdf:about="&local;robot"/> <owl:class rdf:about="&local;humanoid"> <rdfs:subclassof rdf:resource="&local;robot"/> </owl:class> <owl:alldisjointclasses> <owl:members rdf:parsetype="collection"> <owl:class rdf:about="&local;animal"/> <owl:class rdf:about="&local;robot"/> </owl:members> </owl:alldisjointclasses> <!--defining classes animals--> <owl:class rdf:about="&local;animal"/> <owl:class rdf:about="&local;mammal"/> <owl:class rdf:about="&local;human"> <rdfs:subclassof rdf:resource="&local;mammal"/> </owl:class> <owl:class rdf:about="&local;person"> <owl:equivalentclass rdf:resource="&local;human"/> </owl:class> <owl:class rdf:about="&local;man"> <rdfs:subclassof rdf:resource="&local;human"/> </owl:class> <owl:class rdf:about="&local;woman"> <rdfs:subclassof rdf:resource="&local;human"/> </owl:class> <owl:alldisjointclasses> <owl:members rdf:parsetype="collection"> <owl:class rdf:about="&local;man"/> <owl:class rdf:about="&local;woman"/> </owl:members> </owl:alldisjointclasses> <owl:objectproperty rdf:about="&local;hasrobot"> <rdfs:domain rdf:resource="&local;human"/> <rdfs:range rdf:resource="&local;robot"/> </owl:objectproperty> <owl:datatypeproperty rdf:about="&local;hasalert"/> <!--owl:datatypeproperty rdf:about="&local;hasalert"> <rdfs:range rdf:resource="&xsd;string"/> </owl:datatypeproperty--> <!--rdfs:property rdf:about="&local;hasalert"> <rdfs:domain rdf:resource="&local;robot"/> <rdfs:range rdf:resource="&xsd;string"/> </rdfs:property--> <owl:datatypeproperty rdf:about="&local;hastemperature"> <rdf:type rdf:resource="&owl;functionalproperty"/> <!--rdfs:range rdf:resource="&xsd;nonnegativeinteger"/--> </owl:datatypeproperty> <owl:datatypeproperty rdf:about="&local;hastension"> <rdf:type rdf:resource="&owl;functionalproperty"/> <!--rdfs:range rdf:resource="&xsd;nonnegativeinteger"/--> </owl:datatypeproperty> <owl:namedindividual rdf:about="&local;charles"> <rdf:type rdf:resource="&local;man"/> <!--local:hastension rdf:datatype="&xsd;float">14</local:hastension--> </owl:namedindividual> <owl:negativepropertyassertion> <owl:sourceindividual rdf:resource="&local;charles"/> <owl:assertionproperty rdf:resource="&local;hasrobot"/> <owl:targetindividual rdf:resource="&local;samii"/> </owl:negativepropertyassertion> <owl:namedindividual rdf:about="&local;walid"> <rdf:type rdf:resource="&local;man"/> <local:hasrobot rdf:resource="&local;samii"/> <local:hastemperature rdf:datatype="&xsd;float">37.5</local:hastemperature> </owl:namedindividual> <owl:namedindividual rdf:about="&local;samii"> <rdf:type rdf:resource="&local;humanoid"/> </owl:namedindividual> <!-- rules --> <swrl:variable rdf:about="&local;x" /> <swrl:variable rdf:about="&local;alert" /> <swrl:imp rdf:about="&local;rulealerttensionbasse"> <swrl:head rdf:parsetype="collection"> <swrl:datavaluedpropertyatom> <swrl:propertypredicate rdf:resource="&local;hasalert"/> <swrl:argument1 rdf:resource="&local;samii" /> <swrl:argument2 rdf:datatype="&xsd;string">t'as tension est drôlement basse</swrl:argument2> </swrl:datavaluedpropertyatom> </swrl:head> <swrl:body rdf:parsetype="collection"> <swrl:datavaluedpropertyatom> <swrl:propertypredicate rdf:resource="&local;hastension"/> <swrl:argument1 rdf:resource="&local;charles" /> <swrl:argument2 rdf:resource="&local;x" /> </swrl:datavaluedpropertyatom> <swrl:builtinatom> <swrl:builtin rdf:resource="&swrlb;lessthan" /> <swrl:arguments> <rdf:list> <rdf:first rdf:resource="&local;x"/> <rdf:rest> <rdf:list> <rdf:first rdf:datatype="&xsd;float">14.0</rdf:first> <rdf:rest rdf:resource="&rdf;nil"/> </rdf:list> </rdf:rest> </rdf:list> </swrl:arguments> </swrl:builtinatom> </swrl:body> </swrl:imp> <swrl:imp rdf:about="&local;rulealerttensionhaute"> <swrl:head rdf:parsetype="collection"> <swrl:datavaluedpropertyatom> <swrl:propertypredicate rdf:resource="&local;hasalert"/> <swrl:argument1 rdf:resource="&local;samii" /> <swrl:argument2 rdf:datatype="&xsd;string">t'as tension est drôlement haute</swrl:argument2> </swrl:datavaluedpropertyatom> </swrl:head> <swrl:body rdf:parsetype="collection"> <swrl:datavaluedpropertyatom> <swrl:propertypredicate rdf:resource="&local;hastension"/> <swrl:argument1 rdf:resource="&local;charles" /> <swrl:argument2 rdf:resource="&local;x" /> </swrl:datavaluedpropertyatom> <swrl:builtinatom> <swrl:builtin rdf:resource="&swrlb;greaterthan" /> <swrl:arguments> <rdf:list> <rdf:first rdf:resource="&local;x"/> <rdf:rest> <rdf:list> <rdf:first rdf:datatype="&xsd;float">14.0</rdf:first> <rdf:rest rdf:resource="&rdf;nil"/> </rdf:list> </rdf:rest> </rdf:list> </swrl:arguments> </swrl:builtinatom> </swrl:body> </swrl:imp> </rdf:rdf>
the output of test case:
== before adding axiom == prefix(:=<file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#>) prefix(owl:=<http://www.w3.org/2002/07/owl#>) prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>) prefix(var:=<urn:swrl#>) prefix(xml:=<http://www.w3.org/xml/1998/namespace>) prefix(xsd:=<http://www.w3.org/2001/xmlschema#>) prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>) prefix(swrl:=<http://www.w3.org/2003/11/swrl#>) prefix(local:=<file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#>) prefix(owl11:=<http://www.w3.org/2006/12/owl11#>) prefix(swrlb:=<http://www.w3.org/2003/11/swrlb#>) prefix(owl11xml:=<http://www.w3.org/2006/12/owl11-xml#>) prefix(cr-owl-guide-20030818:=<http://www.w3.org/tr/2003/cr-owl-guide-20030818/>) ontology(<file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml> declaration(class(local:animal)) declaration(class(local:human)) declaration(class(local:humanoid)) declaration(class(local:mammal)) declaration(class(local:man)) declaration(class(local:person)) declaration(class(local:robot)) declaration(class(local:woman)) declaration(objectproperty(local:hasrobot)) declaration(dataproperty(local:hasalert)) declaration(dataproperty(local:hastemperature)) declaration(dataproperty(local:hastension)) declaration(namedindividual(local:charles)) declaration(namedindividual(local:samii)) declaration(namedindividual(local:walid)) ############################ # object properties ############################ # object property: local:hasrobot (local:hasrobot) objectpropertydomain(local:hasrobot local:human) objectpropertyrange(local:hasrobot local:robot) ############################ # data properties ############################ # data property: local:hastemperature (local:hastemperature) functionaldataproperty(local:hastemperature) # data property: local:hastension (local:hastension) functionaldataproperty(local:hastension) ############################ # classes ############################ # class: local:animal (local:animal) disjointclasses(local:animal local:robot) # class: local:human (local:human) equivalentclasses(local:human local:person) subclassof(local:human local:mammal) # class: local:humanoid (local:humanoid) subclassof(local:humanoid local:robot) # class: local:man (local:man) subclassof(local:man local:human) disjointclasses(local:man local:woman) # class: local:woman (local:woman) subclassof(local:woman local:human) ############################ # named individuals ############################ # individual: local:charles (local:charles) classassertion(local:man local:charles) negativeobjectpropertyassertion(local:hasrobot local:charles local:samii) # individual: local:samii (local:samii) classassertion(local:humanoid local:samii) # individual: local:walid (local:walid) classassertion(local:man local:walid) objectpropertyassertion(local:hasrobot local:walid local:samii) datapropertyassertion(local:hastemperature local:walid "37.5"^^xsd:float) dlsaferule(body(datapropertyatom(local:hastension local:charles variable(local:x)) builtinatom(swrlb:greaterthan variable(local:x) "14.0"^^xsd:float))head(datapropertyatom(local:hasalert local:samii "t'as tension est drôlement haute"^^xsd:string))) dlsaferule(body(datapropertyatom(local:hastension local:charles variable(local:x)) builtinatom(swrlb:lessthan variable(local:x) "14.0"^^xsd:float))head(datapropertyatom(local:hasalert local:samii "t'as tension est drôlement basse"^^xsd:string))) )[main].main => == query == [owlmanagement].printontology() -> number of literals: 0 [owlmanagement].printontology() -> number of objects: 0 [main].main() -> number of literals: 0 [main].main => == after adding axiom == prefix(:=<file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#>) prefix(owl:=<http://www.w3.org/2002/07/owl#>) prefix(rdf:=<http://www.w3.org/1999/02/22-rdf-syntax-ns#>) prefix(var:=<urn:swrl#>) prefix(xml:=<http://www.w3.org/xml/1998/namespace>) prefix(xsd:=<http://www.w3.org/2001/xmlschema#>) prefix(rdfs:=<http://www.w3.org/2000/01/rdf-schema#>) prefix(swrl:=<http://www.w3.org/2003/11/swrl#>) prefix(local:=<file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml#>) prefix(owl11:=<http://www.w3.org/2006/12/owl11#>) prefix(swrlb:=<http://www.w3.org/2003/11/swrlb#>) prefix(owl11xml:=<http://www.w3.org/2006/12/owl11-xml#>) prefix(cr-owl-guide-20030818:=<http://www.w3.org/tr/2003/cr-owl-guide-20030818/>) ontology(<file:///c:/users/cclercq/.eclipse/ws_samii/samii/webcontent/ontologies/justatest_rdfxml> declaration(class(local:animal)) declaration(class(local:human)) declaration(class(local:humanoid)) declaration(class(local:mammal)) declaration(class(local:man)) declaration(class(local:person)) declaration(class(local:robot)) declaration(class(local:woman)) declaration(objectproperty(local:hasrobot)) declaration(dataproperty(local:hasalert)) declaration(dataproperty(local:hastemperature)) declaration(dataproperty(local:hastension)) declaration(namedindividual(local:charles)) declaration(namedindividual(local:samii)) declaration(namedindividual(local:walid)) ############################ # object properties ############################ # object property: local:hasrobot (local:hasrobot) objectpropertydomain(local:hasrobot local:human) objectpropertyrange(local:hasrobot local:robot) ############################ # data properties ############################ # data property: local:hastemperature (local:hastemperature) functionaldataproperty(local:hastemperature) # data property: local:hastension (local:hastension) functionaldataproperty(local:hastension) ############################ # classes ############################ # class: local:animal (local:animal) disjointclasses(local:animal local:robot) # class: local:human (local:human) equivalentclasses(local:human local:person) subclassof(local:human local:mammal) # class: local:humanoid (local:humanoid) subclassof(local:humanoid local:robot) # class: local:man (local:man) subclassof(local:man local:human) disjointclasses(local:man local:woman) # class: local:woman (local:woman) subclassof(local:woman local:human) ############################ # named individuals ############################ # individual: local:charles (local:charles) classassertion(local:man local:charles) negativeobjectpropertyassertion(local:hasrobot local:charles local:samii) datapropertyassertion(local:hastension local:charles "10.0"^^xsd:float) # individual: local:samii (local:samii) classassertion(local:humanoid local:samii) # individual: local:walid (local:walid) classassertion(local:man local:walid) objectpropertyassertion(local:hasrobot local:walid local:samii) datapropertyassertion(local:hastemperature local:walid "37.5"^^xsd:float) dlsaferule(body(datapropertyatom(local:hastension local:charles variable(local:x)) builtinatom(swrlb:greaterthan variable(local:x) "14.0"^^xsd:float))head(datapropertyatom(local:hasalert local:samii "t'as tension est drôlement haute"^^xsd:string))) dlsaferule(body(datapropertyatom(local:hastension local:charles variable(local:x)) builtinatom(swrlb:lessthan variable(local:x) "14.0"^^xsd:float))head(datapropertyatom(local:hasalert local:samii "t'as tension est drôlement basse"^^xsd:string))) )[main].main => == query == [owlmanagement].printontology() -> number of literals: 2 [owlmanagement].printontology() -> number of objects: 2 [main].main() -> number of literals: 2 ->samii hasalert t'as tension est drôlement haute ->samii hasalert t'as tension est drôlement basse
it seems problem comes version of pellet or owl-api used. using openllet 2.5.1.
using last "official" version of pellet (2.3.6-ansell) version of owl-api (3.4.9.2-ansell), result expecting:
with "tension" < 14:
== query == [owlmanagement].printontology() -> number of literals: 1 [owlmanagement].printontology() -> number of objects: 1 [main].main() -> number of literals: 1 ->samii hasalert t'as tension est drôlement basse
with "tension" >14
== query == [owlmanagement].printontology() -> number of literals: 1 [owlmanagement].printontology() -> number of objects: 1 [main].main() -> number of literals: 1 ->samii hasalert t'as tension est drôlement haute
by way, tried version of ignazio1977 used owl-api version 4.0.2, got same problem openllet 2.5.1.
Comments
Post a Comment