SWRL rules creation in OWLAPI ontology

萝らか妹 提交于 2019-12-23 05:08:48

问题


I have run (in Neatbeans 8.2) the following simple java code, in order to experiment with SWRL language:

String base = "http://www.prova/testont.owl";
IRI ontologyIRI = IRI.create(base);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
OWLOntology ontology = manager.createOntology(ontologyIRI);
OWLDataFactory factory = manager.getOWLDataFactory();

OWLClass adult = factory.getOWLClass(IRI.create(ontologyIRI + "#Adult"));
OWLClass person = factory.getOWLClass(IRI.create(ontologyIRI + "#Person"));
OWLDataProperty hasAge = factory.getOWLDataProperty(IRI.create(ontologyIRI + "#hasAge"));

OWLNamedIndividual john = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#John"));
OWLNamedIndividual andrea = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#Andrea"));

OWLClassAssertionAxiom classAssertion = factory.getOWLClassAssertionAxiom(person, john);
manager.addAxiom(ontology, classAssertion);
classAssertion = factory.getOWLClassAssertionAxiom(person, andrea);
manager.addAxiom(ontology, classAssertion);

OWLDatatype integerDatatype = factory.getOWLDatatype(OWL2Datatype.XSD_INTEGER.getIRI());
OWLLiteral literal = factory.getOWLLiteral("41", integerDatatype);
OWLAxiom ax = factory.getOWLDataPropertyAssertionAxiom(hasAge, andrea, literal);
manager.addAxiom(ontology, ax);

literal = factory.getOWLLiteral("15", integerDatatype);
ax = factory.getOWLDataPropertyAssertionAxiom(hasAge, john, literal);
manager.addAxiom(ontology, ax);

SWRLRuleEngine ruleEngine = SWRLAPIFactory.createSWRLRuleEngine(ontology);
ruleEngine.createSWRLRule("r1", "Person(?p)^hasAge(?p,?age)^swrlb:greaterThan(?age,17) -> Adult(?p)");

manager.saveOntology(ontology, IRI.create(((new File("FILE_PATH")).toURI())));

I used maven with the following dependency:

 <dependency>
     <groupId>edu.stanford.swrl</groupId>
     <artifactId>swrlapi-drools-engine</artifactId>
     <version>1.1.4</version>
  </dependency>

I'm getting the following error:

Exception in thread "main" org.swrlapi.parser.SWRLParseException: Invalid SWRL atom predicate 'Person' at org.swrlapi.parser.SWRLParser.generateEndOfRuleException(SWRLParser.java:479) at org.swrlapi.parser.SWRLParser.parseSWRLAtom(SWRLParser.java:210) at org.swrlapi.parser.SWRLParser.parseSWRLRule(SWRLParser.java:106) at org.swrlapi.factory.DefaultSWRLAPIOWLOntology.createSWRLRule(DefaultSWRLAPIOWLOntology.java:219) at org.swrlapi.factory.DefaultSWRLAPIOWLOntology.createSWRLRule(DefaultSWRLAPIOWLOntology.java:213) at org.swrlapi.factory.DefaultSWRLRuleAndQueryEngine.createSWRLRule(DefaultSWRLRuleAndQueryEngine.java:249) at ilc.cnr.it.swrl4morphology.SimpleToSWRL.main(SimpleToSWRL.java:450)

However if I persist the ontology on file and then I reload it, I don't get the error anymore. It seems that default prefixes are added during first save. It sounds pretty strange to me....

Please, could you help me to understand what I'm getting wrong ?

Thanks in advance, Andrea


回答1:


On save and parse, relative IRIs such as ”Person" will be turned into absolute IRIs, using the ontology IRI as base.



来源:https://stackoverflow.com/questions/47440589/swrl-rules-creation-in-owlapi-ontology

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!