protegeproject / swrlapi

Java API for working with the SWRL rule and SQWRL query languages
Other
99 stars 40 forks source link

Prefixresolver #33

Closed jngy36 closed 3 years ago

jngy36 commented 7 years ago

Hi,

I'm currently working on inserting newly created SWRL rules to an OWL file and I'm having issues with the Prefix. I'm calling the IRIResolver but it seems that the resolver removes the '#'-symbol from the Prefix which makes the IRI invalid:

org.swrlapi.parser.SWRLParseException: Invalid SWRL atom predicate 'Person' at org.swrlapi.parser.SWRLParser.generateEndOfRuleException(SWRLParser.java:488) Optional[http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8Person] at org.swrlapi.parser.SWRLParser.parseSWRLAtom(SWRLParser.java:219)

The rule that I'm trying to insert is: Person(?p) ^ Car(?c) ^ hasAge(?p, ?a) ^ ownsCar(?p, ?c) ^ swrlb:greaterThan(?a, 18) -> Person(?p) ^ Car(?c) ^ canDrive(?p, ?c)

Apparently, it works when the '#'-symbol is written in front of the classes and properties:

Person(?p) ^ #Car(?c) ^ #hasAge(?p, ?a) ^ #ownsCar(?p, ?c) ^ swrlb:greaterThan(?a, 18) -> #Person(?p) ^ #Car(?c) ^ #canDrive(?p, ?c)

Here's the full error message: org.swrlapi.parser.SWRLParseException: Invalid SWRL atom predicate 'Person' at org.swrlapi.parser.SWRLParser.generateEndOfRuleException(SWRLParser.java:488) Optional[http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8Person] at org.swrlapi.parser.SWRLParser.parseSWRLAtom(SWRLParser.java:219)

at org.swrlapi.parser.SWRLParser.parseSWRLRule(SWRLParser.java:115)
at org.swrlapi.factory.DefaultSWRLAPIOWLOntology.createSWRLRule(DefaultSWRLAPIOWLOntology.java:218)
at org.swrlapi.factory.DefaultSWRLAPIOWLOntology.createSWRLRule(DefaultSWRLAPIOWLOntology.java:212)
at org.swrlapi.factory.DefaultSWRLRuleAndQueryEngine.createSWRLRule(DefaultSWRLRuleAndQueryEngine.java:251)
at SWRLReaderTest.testSWRLPrettyPrint(SWRLReaderTest.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I'm grateful for anyone who can tell me why this happened.

kind regards,

Johannes

martinjoconnor commented 7 years ago

Can you post the ontology here. (Change .owl file extension to .owl.txt since GitHub does not support the .owl extension.)

jngy36 commented 7 years ago

Sure, here you go. Thanks for the quick response.

CarOntology.owl.txt

kind regards, Johannes

martinjoconnor commented 7 years ago

I would use the RDF/XML serialization instead of the OWL/XML serialization used here. Protege does not always handle the OWL/XML serialization properly.

After converting I would also add a hash to the default ontology namepace, e.g.,

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8#"
     xml:base="http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8"

instead of:

<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8"
     xml:base="http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8"
jngy36 commented 7 years ago

I just tried your suggested approach but the problem still exists. I saved the file in the reccomended RDF/XML format and added a hash to to the default ontology namespace. The file then looked just as described above.

<?xml version="1.0"?> <rdf:RDF xmlns="http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8#" xml:base="http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:owl="http://www.w3.org/2002/07/owl#"

When reading the rules from the ontology the prefixes are being resolved correcty but the ontology still misses the hash when I try to save the rules back to the file:

Optional[http://www.semanticweb.org/farrenkopf/ontologies/2017/5/untitled-ontology-8Person] org.swrlapi.parser.SWRLParseException: Invalid SWRL atom predicate 'Person'