0

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

Andrea
  • 27
  • 6

1 Answers1

1

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

Ignazio
  • 10,504
  • 1
  • 14
  • 25
  • Ok, but how can I programmatically create an ontology, and add some rules without getting the error? (without reload the ontology before adding the rules) – Andrea Nov 23 '17 at 08:22
  • Use the full iri for the classes and properties in the string, or look at the swrl rules methods in OWLDataFactory to create the rules at OWLAPI level. – Ignazio Nov 23 '17 at 08:46
  • Now it works. `manager.getOntologyFormat(ontology).asPrefixOWLOntologyFormat().setDefaultPrefix(base + "#")` it was necessary. – Andrea Jan 02 '18 at 14:12