Specifying class equivalence in Jena rules - jena

Given an ontology with Class A and Class B I am stating a rule:
[-> (ns:Class_A owl:equivalentClass ns:Class_B)]
However the Jena generic rule reasoner is only applying the tag equivalentClass to Class A, i. e. it is not stating Class B equivalentClass Class A.
Furthermore, I have an object A that is an individual of Class A, the reasoner is not applying Class B as a type of object A.
Is there another way to specify class equivalence through Jena rules? And is it better to use the generic rule reasoner for this reasoning task or call the in-built Jena OWL reasoner? Or is there another reasoner that accepts Jena rules?

This problem was solved through the application of the owl reasoner Jena provides. We generated the model using the generic rule reasoner and then fed that model to the owl reasoner. The owl reasoner proceeds to apply the equivalentClass tag to all applicable classes

Related

SWRL rule in Protégé 4.3

I want to create a new class "Adult" to my ontology by using a SWRL rule, i used this rule:
Person(?p), age(?p, true), greaterThan(age, 18) -> Adult(?p)
I also installed pellet reasoner to execute this rule but the class is not created after reasonig.
So what is the correct way to use swrl rules in protege 4.3 to add new classes to the ontology?

What are the Jena OWL Reasoners Limitations?

I have been making some tests using Jena OWL Reasoner, but I don´t understand some results obtained, for example, if I have the following KB:
Class A
Class B
Class C rdfs:subClassOf A
A owl:disjointWith B
...and if I ask "C owl:disjointWith B"? to the inference model, the answer sould be "yes", but the Jena OWL Reasoner answer is NO...I check this using...
if (infmodel.contains(A, OWL.disjointWith, C)) {
...
}
....
So, is there some limitations to make inferences with this reasoner?
Thanks
Your query contains A owl:disjointWith C, which cannot be inferred from your ontology. Are you sure it's the correct query?

How can I distinguish an axiom from an inferred statement in a Jena RDFS-INF model?

When I create a RDFS_MEM_RDFS_INF model in Jena and read some RDFS-File, a number of statements, that were not explicitly stated in the file are added. E.g. if we have a triple
a p b
and p is a rdfs:subPropertyOf q, than
a q b is
also in the model. A concrete example is the following: if
a skos:related b
is in the file
a skos:semanticRelation b
is also in the model.
Is there any possibility to check whether a statement in the model is an axiom or an inferred one? There are such methods for OWL Models, but I use the RDFS Model. A trivial solution would be to build two models, one without and one with inference, but I would prefer a less memory consuming solution.
Jena InfModel has a method getRawModel(). This Model wont contain the inferred statements, it will contain only the axioms in the file. use a check against that. If you are using the OntModel it has got a method getBaseModel().
To preserve djthequest's answer from a comment:
Jena InfModel has a method getRawModel(). This Model wont contain the
inferred statements, it will contain only the axioms in the file. use
a check against that. If you are using the OntModel it has got a
method getBaseModel().
and Christian Wartena's response indicating that this was a solution:
Thanks. This works fine! I didn't find that method when I was reading
the documentation last week.
(I'll remove this answer if djthequest posts one.)

How to add Datalog rules to Pellet reasoner in Jena?

I've multiple personal inference rules in Datalog Form.
I can extend Jena GenericRuleReasoner in order to take them into account during the inference step. Here is an example of code to do that :
String rules = "[r1: (?e1 st:runningTask st:gic_eth0) -> (?e1 rdf:type st:dataFromEthernet2IP)]";
Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
reasoner.setDerivationLogging(true);
InfModel inf = ModelFactory.createInfModel(reasoner, rawData);
Actually, I'm want to use Pellet reasoner since it is easy to plug to Jena. I wonder if Pellet is extensible as GenericRuleReasoner ? and if it is, how to add my Datalog rules in it ?

Using SWRL with Jena and Pellet

I was unable to find some decent simple code examples of using SWRL and Jena with
Pellet, or at least using SWRL? I have studied some examples in Pellet documentation, but there is no example about using SWRL. Most examples on the web are incomplete and confusing.
The only solution I found was with the Jess Rule Engine but it is not free and is under commercial license. I found that Pellet support SWRL rules but could not find running example.
The only example I found is this, but I do not understand it:
OWLOntologyManager m = create();
OWLOntology o = m.createOntology(example_iri);
// Get hold of references to class A and class B.
OWLClass clsA = df.getOWLClass( IRI.create(example_iri + "#A" ));
OWLClass clsB = df.getOWLClass(IRI.create(example_iri + "#B" ));
SWRLVariable var = df.getSWRLVariable(IRI.create(example_iri + "#x" ));
SWRLClassAtom body = df.getSWRLClassAtom(clsA, var);
SWRLClassAtom head = df.getSWRLClassAtom(clsB, var);
SWRLRule rule = df.getSWRLRule(Collections.singleton(body),
Collections.singleton(head));
m.applyChange(new AddAxiom(o, rule));
Pellet Rules and Jena Rules are Very Different™
The short answer is that Pellet supports SWRL rules. If you have an ontology that contains SWRL rules and ask Pellet to reason over it, it will take them into consideration.
Jena has its own rules language, which is described in the documentation page, Reasoners and rule engines: Jena inference support. It supports both forward and backward chaining rules.
However, though both Pellet and Jena support a notion of rules, the intended domains of SWRL rules and Jena rules are very different. SWRL rules are OWL-level constructs; the unary predicates in a SWRL rule are class expressions, and the binary predicates are object and data properties. Additionally, SWRL rules only match on named individuals; they don't match for individuals whose existence is only inferred. Jena rules, on the other hand, are RDF-level, and designed to work on RDF-graphs. While RDF and OWL are often used together, (e.g., OWL data is serialized in RDF), the two are conceptually distinct. An OWL reasoner could be implemented that makes no use of RDF, and a SWRL engine could be built that make no use of RDF graphs.
Jena or OWL API?
The code that you've shown, based on the presence of an OWLOntologyManager, is based on the OWL API, not on Jena's API. The OWL API will have more direct functionality for working with OWL and SWRL rules, while Jena will not. (Jena's OntModels work well with OWL1, but support for OWL2 is not complete (and still “open for contributors”).
Rather than using the OWL API or trying to use Jena's API, you'll probably find it easier to create rules using an editor such as Protégé. Martin Kuba has written a very nice OWL2 and SWRL Tutorial that can help you here.
SWRL rules work with Pellet API. I created my ontology and SWRL rules using Protégé and I was able to create OWL individuals dynamically using Java code. This whole ontology is used as aggregatedOwl in the following code. This code loads ontology (base OWL + individuals if any + SWRL rules) and runs the Pellet reasoner on it and saves the inferred result in a string.
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.StringDocumentTarget;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;
import org.semanticweb.owlapi.util.InferredAxiomGenerator;
import org.semanticweb.owlapi.util.InferredOntologyGenerator;
import org.semanticweb.owlapi.util.InferredPropertyAssertionGenerator;
import com.clarkparsia.pellet.owlapiv3.PelletReasoner;
import com.clarkparsia.pellet.owlapiv3.PelletReasonerFactory;
try {
manager = OWLManager.createOWLOntologyManager();
InputStream owlInputStream = new ByteArrayInputStream(aggregatedOwl.getBytes("UTF-8"));
inferredOntology = manager.loadOntologyFromOntologyDocument(owlInputStream);
PelletReasoner reasoner = PelletReasonerFactory.getInstance().createReasoner(inferredOntology);
reasoner.getKB().realize();
List<InferredAxiomGenerator<? extends OWLAxiom>> axiomGenerators = new ArrayList<InferredAxiomGenerator<? extends OWLAxiom>>();
axiomGenerators.add( new InferredPropertyAssertionGenerator() );
InferredOntologyGenerator iog = new InferredOntologyGenerator(reasoner,axiomGenerators);
iog.fillOntology(manager, inferredOntology);
// Save the new ontology
OutputStream owlOutputStream = new ByteArrayOutputStream();
manager.saveOntology(inferredOntology, owlOutputStream);
inferredData = owlOutputStream.toString();
}
catch ( Exception e ) {
throw new Exception("Exception occurred in applying reasoner");
}
Hope this is helpful to you.

Resources