Inferring using Jena - owl

InfModel infmodel = ModelFactory.createInfModel(reasoner, m);
Resource vegetarian = infmodel.getResource(source + "Vegetarian");
Resource margherita = infmodel.getResource(source + "Example-Margherita");
if (infmodel.contains(margherita, RDF., vegetarian)) {
System.out.println("Margherita is a memberOf Vegetarian pizza");
}
The example given above is formed by formal pizza.owl. In this owl, Example-Margherita is an individual of Margherita class. So, it is already written in owl file. However, the problem is that the reasoner should infer that margherita-example should be also an vegetarian pizza.
Could anyone please give an example that shows how to find an individual's possible inferred classes like in Protege ?(Protege correctly infers that Example-Margherita is a Vegetarian Pizza. However, I can't infer programmatically)

I solved my question. I think there was a problem with my ontology. Therefore, I created another ontology to infer individuals. The ontology that I created contains Person and subclasses of Person : MalePerson, FemalePerson and MarriedPerson. And, there are two object properties(hasSpouse, hasSibling) and one data type property(hasAge).
And, I created 3 individuals.
John - MalePerson - hasAge(20) - hasSibling(Jane)
Jane - FemalePerson - hasSibling(John) - hasSpouse(Bob)
Bob - MalePerson - hasSpouse(Jane)
And, I put two restrictions for MalePerson and FemalePerson classes.
For MalePerson :
hasSpouse max 1
hasSpouse only MalePerson
For FemalePerson :
hasSpouse max 1
hasSpouse only FemalePerson
Lastly, I made MarriedPerson to be a defined class. Before reasoning, MarriedPerson has no individual. However, the model should infer that Jane and Bob are married. Therefore, at the end, MarriedPerson class should have 2 individuals.
When I ran this code in Java using Jena, I got 2 inferred individuals.
OntModel ontModel = ModelFactory.createOntologyModel();
InputStream in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException( "File: " + inputFileName + " not found");
}
ontModel.read(in, "");
Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
reasoner = reasoner.bindSchema(ontModel);
// Obtain standard OWL-DL spec and attach the Pellet reasoner
OntModelSpec ontModelSpec = OntModelSpec.OWL_DL_MEM;
ontModelSpec.setReasoner(reasoner);
// Create ontology model with reasoner support
OntModel model = ModelFactory.createOntologyModel(ontModelSpec, ontModel);
// MarriedPerson has no asserted instances
// However, if an inference engine is used, two of the three
// individuals in the example presented here will be
// recognized as MarriedPersons
//ns is the uri
OntClass marPerson = model.getOntClass(ns + "OWLClass_00000003866036241880"); // this is the uri for MarriedPerson class
ExtendedIterator married = marPerson.listInstances();
while(married.hasNext()) {
OntResource mp = (OntResource)married.next();
System.out.println(mp.getURI());
} // this code returns 2 individuals with the help of reasoner

Related

How to infer an object property based on data properties?

I'm a protégé newbie and did the pizza tutorial and read the 101 documentation. I'm trying to model a ontology like the following picture:
I have a person who has a style. The style can be a style_active or style_passive.
This style is determined by a index of two data properties:
ind_passive and ind_active, the bigger value should infer the style.
I had to create two individuals: style_active and style_passive, because they must be individuals to be assigned to the object property has_style.
How to infer the value of has_style object property based on ind_passive and ind_active data properties? Using a reasoner?
Is something wrong with this model?
I suceed in calculate the value of style object property based on two swrl rules. It does not work with a reasoner like HermiT (default in Protégé 5.5), but works in SWRLTab, that uses Drools.
The rules I used:
Name: Passive
Rule: Person(?p) ^ ind_passive(?p, ?ip) ^ ind_active(?p, ?ia) ^ swrlb:greaterThan(?ip, ?ia) -> has_style(?p, style_passive)
Name: Active
Rule: Person(?p) ^ ind_passive(?p, ?ip) ^ ind_active(?p, ?ia) ^ swrlb:greaterThan(?ia, ?ip) -> has_style(?p, style_active)
The reasoner Hermit could not be enabled after using this rules, because it does not supports "built-in atoms", like swrlb:greaterThan.

OWL API: Traverse imported ontology

I'm trying to import an ontology to the primary ontology, and traverse over all classes:
manager = OWLManager.createOWLOntologyManager();
ontology = manager.loadOntologyFromOntologyDocument(new File("data/prim.owl"));
factory = manager.getOWLDataFactory();
OWLImportsDeclaration im = factory.getOWLImportsDeclaration(IRI.create("https://protege.stanford.edu/ontologies/pizza/pizza.owl"));
manager.applyChange(new AddImport(ontology,im));
reasoner = OpenlletReasonerFactory.getInstance().createReasoner(ontology);
I’m running this code to get all classes:
//*********************
Set<OWLClass> allCls = ontology.getClassesInSignature();
allCls.forEach(System.out::println);
Classes belonging to prim.owl are returned, but classes in the imported ontology (pizza.owl) are not returned.
The code in the question contains a mistake: it does not load the desired imported ontology (pizza) into the manager.
OWLImportsDeclaration im = factory.getOWLImportsDeclaration(IRI.create("https://protege.stanford.edu/ontologies/pizza/pizza.owl"));
manager.applyChange(new AddImport(ontology,im));
These lines just add the owl:imports declaration into the ontology header (_:x a owl:Ontology) for the pizza-iri.
To make the code work, you need to load the pizza-ontology separately:
OWLOntology pizza = manager.loadOntology(IRI.create("https://protege.stanford.edu/ontologies/pizza/pizza.owl"));
OWLImportsDeclaration im = factory.getOWLImportsDeclaration(pizza.getOntologyID().getOntologyIRI().orElseThrow(AssertionError::new));
manager.applyChange(new AddImport(ontology, im));
Now you can check that all imports and references are really present and correct, and, therefore, your ontology has a reference to the pizza ontology:
Assert.assertEquals(1, ontology.importsDeclarations().count());
Assert.assertEquals(1, ontology.imports().count());
Assert.assertEquals(2, manager.ontologies().count());
Then you can get all OWL-classes from both ontologies as a single collection or java-Stream:
ontology.classesInSignature(Imports.INCLUDED).forEach(System.err::println);
Also please note: the method Set<OWLClass> getClassesInSignature(boolean includeImportsClosure) is deprecated (in OWL-API v5).

Inconsistent classification of individuals in Protege 4.3

I have been studying Protege and I have a question. I added an individual with name aaa in ontology "people.owl" which I downloaded from the internet.
I set type assertion as:
sheep and hasCC value 200,
where hasCC is data property.
Then, in class animal I created two classes: hhh and lll.
I set hhh as equivalent and there wrote:
animal and (hasCC some decimal[>= 150])
For lll I set:
animal and (hasCC some decimal[< 250])
When I run HermiT reasoner I see that the individual aaa is classified in both lll and hhh classes.
But when I set lll and hhh classes to be disjoint and run the reasoner the classes are not classified in the class "Nothing" as, for example, the class "mad_cow". I get a message:
"Your ontology is inconsistent which means that the OWL reasoner will no longer be able to provide any useful information about the ontology." After clicking the button "Explain" I get:
Could you say, why lll and hhh classes cannot be classified as Nothing?
What should I correct to let lll and hhh classes to be classified as Nothing?
Thank you!
"Your ontology is inconsistent which means that the OWL reasoner will no longer be able to provide any useful information about the ontology."
Isn't this saying it all already? You complain that the reasoner is not doing something but the reasoner explicitly tells you that it is not able to provide any useful information! Make your ontology consistent, then you can expect something from the reasoner.
In any case, you cannot expect a class that contains an instance (here the classes hhh and lll both must contain instance aaa) to be equivalent to Nothing. Nothing precisely means "the class that does not have any instance".

OWLAPI slow calculating disjoint axioms

My goal: find the disjoint axioms (asserted and inferred) in an ontology which contains around 5000 axioms.
My code:
for (OWLClass clazz1 : ontology.getClassesInSignature()) {
for (OWLClass clazz2 : ontology.getClassesInSignature()) {
OWLAxiom axiom = MyModel.factory.getOWLDisjointClassesAxiom(clazz2, clazz1);
if( !(ontology.containsAxiom(axiom)) && reasoner.isEntailed(axiom))
{
System.out.println(clazz2.toString() + " disjoint with " + clazz1.toString());
}
The problem: the execution time is extremely slow, I'd say eternal. Even if I reduce the number of comparison with some if statement, the situation is still the same.
Protege seems to be very quick to compute those inferred axioms and it's based on the same API I am using (OWLAPI). So, am I in the wrong approach?
Profiling the code will very likely reveal that the slow part is
reasoner.isEntailed(axiom)
This form requires the reasoner to recompute entailments for each class pair, including the pairs where clazz1 and clazz2 are equal (you might want to skip that).
Alternatively, you can iterate through the classes in signature once and use the reasoner to get all disjoint classes:
Set<OWLClass> visited=new HashSet<>();
for (OWLClass c: ontology.getClassesInSignature()) {
if (visited.add(c)) {
NodeSet set = reasoner.getDisjointClasses(c);
for (Node node: set.getNodes()) {
System.out.println("Disjoint with "+c+": "+node);
visited.addAll(node.getEntities());
}
}
}
Worst case scenario, this will make one reasoner call per class (because no class is disjoint). Best case scenario, all classes are disjoint or equivalent to another class, so only one reasoner call is required.

Reclassifying instances asserted using SWRLJessTab Protege

I am using Protege 3.4 and I built my ontology and extended it with SWRL rules using SWRL and sqwrl built ins . The rules classifies instances of a class of the ontology ; the RHS is class assertion of an instance .It works successfully but when I change the values of the causes in the LHS no reclassification is done the instance is asserted to the same class
Why does this happen ? How to reclassify instances ?
ex:
SWRL rules:
1. Message(?m) hasInterest(?m,?i) hasCategory(?m,?c) sqwrl:makeset(?s1,?i) sqwrl:makeset(?s2,?c) sqwrl:intersection(?s3,?s1,?s2) sqwrl:size(?n,?s3) swrlb:greaterThan(?n,0) -> Ham(?m)
Message(?m) hasInterest(?m,?i) hasCategory(?m,?c) sqwrl:makeset(?s1,?i) sqwrl:makeset(?s2,?c) sqwrl:difference(?s3,?s1,?s2) sqwrl:size(?n,?s3) swrlb:greaterThan(?n,0) -> Spam(?m)
So once the message instance is classified as ham for example , whenever I change the values of i or c it will always be ham .

Resources