Henriette's Notes

Home » Semantic technologies (Page 2)

Category Archives: Semantic technologies

Understanding OWL Universal Property Restrictions

In my previous post I explained existential property restrictions. In this post I want to deal with universal property restrictions. In designing ontologies existential property restrictions tend to be used more often than universal property restrictions. However, beginners in ontology design tend to prefer universal property restrictions, which can lead to inconsistencies that can be very difficult to debug, even for experienced ontology designers [1, 2].

We again start with a very simple ontolgy:

ObjectProperty: owns
        
Class: Cat
  SubClassOf: Pet
    
Class: Dog
  SubClassOf: Pet
    
Class: Person
  DisjointWith: Pet
        
Class: Pet
  DisjointUnionOf: Cat, Dog
  DisjointWith: Person

We will use this ontology as basis for explaining universal property restrictions. We assume we want to extend this ontology with a DogLover class to represent persons owning only dogs. In this post:

  1. I will introduce the DogLover class which I will define as Class: DogLover EquivalentTo: owns only Dog,
  2. I will show you how this definition can lead to inconsistencies that can be difficult to debug, and
  3. I will explain how we can fix this error.

Class: DogLover EquivalentTo: owns only Dog

Let us start out by defining the DogLover class as

Class: DogLover
  EquivalentTo: owns only Dog

We can run the reasoner to ensure that our ontology is currently consistent. We can test our ontology by adding a aDogOnlyOwner individual owning a Dog.

Individual: aDog
  Types: Dog

Individual: aDogOnlyOwner
  Facts: owns aDog

If we run the reasoner it will not infer that aDogOnlyOwner is a DogLover, reason being that due to the open world assumption the reasoner has no information from which it can derive that aDogOnlyOwner owns only dogs. We can try and fix this by stating that

Individual: aDogOnlyOwner
  Types: owns max 0 Cat

Again the reasoner will not infer that aDogOnlyOwner owns only dogs. This is because it still allows for the possibility that aDogOnlyOwner can own, say a house. To exclude all other options we have to define aDogOnlyOwner as follows:

Individual: aDogOnlyOwner
  Types: owns max 0 (not Dog)

which enforces that aDogOnlyOwner owns nothing besides dogs. The reasoner will now infer that aDogOnlyOwner is a DogLover. We can also test that individuals of type DogLover cannot own cats, for example:

Individual: aCat
  Types: Cat

Individual: aDogLover
  Types: DogLover
  Facts: owns aCat

If we run the reasoner, it will give an inconsistency. As I said in my previous post, it is always a good idea to review the explanations for an inconsistency, even when you expect an inconsistency, as seen in Figure 1. It states that the inconsistency is due to

  1. aDogLover owning a cat (aDogLover owns aCat),
  2. a cat is not a dog (Pet DisjointUnionOf Cat, Dog),
  3. an individual that loves dogs owns only dogs (DogLover EquivalentTo owns only Dog),
  4. the aDogLover individual is of type DogLover (aDogLover Type DogLover), and
  5. the aCat individual is of type Cat (aCat Type Cat).
ExplanationForDogLoverWithCat

Figure 1

At this point you may think our definition of DogLover is exactly what we need, but it contains a rather serious flaw.

A Serious Flaw

To keep our ontology as simple as possible for this section, please remove any
individuals you may have added to your ontology, but leave the DogLover class as we have defined it in the previous section. Just to ensure that our ontology is consistent, you can run the reasoner to confirm that it is consistent. What we now want to do is to infer that when an individual owns something, that individual is a person. The way we can achieve this is by defining the domain for the owns property:

ObectProperty: owns
  Domain: Person

Now, this looks like a rather innocent change, but when you run the reasoner again you will find that Pet, Cat and Dog are all equivalent to owl:Nothing while Person is equivalent to owl:Thing. An explanation for why Pet is equivalent to owl:Nothing is given in Figure 2.

ExplanationForPetEquivalentToNothing

Figure 2

The explanation given in Figure 2 can be difficult to understand. Indeed, research has shown that there are explanations that are difficult to understand even for experienced ontology designers [1, 2]. In cases where it is hard to understand explanations, using laconic explanations can be helpful. Laconic justifications aim to remove subexpressions from axioms that do not contribute to explaining an entailment or inconsistency [1, 2]. Ticking the “Display laconic explanation” displays the laconic explanation in Figure 3.

LaconicExplanationForPetEquivalentToNothing

Figure 3

The main difference between the explanations in Figures 2 and 3 are

DogLover EquivalentTo owns only Dog

versus

owns only owl:Nothing SubClassOf DogLover

Where does owns only owl:nothing SubClassOf DogLover come from? Recall that A EquivalentTo B is just syntactical sugar for the two axioms A SubClassOf B and B SubClassOf A. What this explanation is saying is that there is a problem with the owns only Dog SubClassOf DogLover part of our axiom (there is no problem with the DogLover SubClassOf owns only Dog part of our axiom). Furthermore, it states that Dog is inferred to be equivalent to owl:Nothing. For this we need to understand the meaning of owns only Dog better.

OwnsOnlyDog

Figure 4

In Figure 4 I give an example domain where I make the assumption that for all individuals all ownership information is specified explicitly. Thus, individuals with no ownership links own nothing and individuals with ownership links own only what is specified and nothing else. The owns only Dog class includes all individuals that are known to own nothing besides dogs. However, a confusing aspect of the semantics of universal restrictions is that it also includes those individuals that owns nothing. To confirm this for yourself you can use the following ontology (from which I removed the domain restriction for the moment). This ontology will infer that anIndividualOwningNothing is of type DogLover.

ObjectProperty: owns
        
Class: Cat
  SubClassOf: Pet
    
Class: Dog
  SubClassOf: Pet
    
Class: Person
  DisjointWith: Pet
        
Class: Pet
  DisjointUnionOf: Cat, Dog
  DisjointWith: Person
  
Class: DogLover
  SubClassOf: Person
  owns only Dog SubClassOf: DogLover
  
Individual: anIndividualOwningNothing
  Types: owns only (not owl:Thing)

We are now ready to explain why Pet is equivalent to owl:Nothing:

  1. owns Domain Person states that whenever an individual owns something, that individual is a Person.
  2. owns only Pet SubClassOf DogLover includes saying that when an individual owns nothing at all, that individual is a DogLover.
  3. Since DogLover is a subclass of Person it means Person now includes individuals that owns something and individuals that owns nothing, which means Person is equivalent to owl:Thing.
  4. Person and Pet are disjoint, hence Pet must be equivalent to owl:Nothing.

Conclusion

So how do we fix our ontology? Simple, we enforce that for someone to be a DogLover, they must own at least 1 dog and nothing else but dogs.

Class: DogLover
  EquivalentTo: owns some Dog and owns only Dog

The example ontologies of this post can be found in github.

Bibliography

[1] M. Horridge, Justification Based Explanation in Ontologies, Ph.D. Thesis, University of Manchester, 2011.

[2] M. Horridge, S. Bail, B. Parsia, and U. Sattler, Toward Cognitive Support for OWL Justifications., Knowl.-Based Syst. 53 (2013), 66–79.

Understanding OWL Existential Property Restrictions

For many starting out with OWL ontologies understanding the exact meaning of property restrictions can be challenging. In this post I will use visual representations to explain the meaning of existential property restrictions. For the purpose of this post, we start out with a simple ontology for modelling the relation between a person and their pets. You can model this in the free Protégé ontology editor.

ObjectProperty: owns
        
Class: Cat
  SubClassOf: Pet
    
Class: Dog
  SubClassOf: Pet
    
Class: Person
  DisjointWith: Pet
        
Class: Pet
  DisjointUnionOf: Cat, Dog
  DisjointWith: Person

I have specified this ontology using the OWL 2 Manchester syntax. Other syntax can be used as well, notably the functional-style syntax, which is used to define the direct semantics of OWL 2. In this post I use the Manchester syntax because it tends to be more intuitive to non-logicians, but it has some weaknesses which I will point out when we get to it. Also for clarity and compactness I have omitted the prefixes.

This ontology states that in our domain we have persons (Class: Person) and pets (Class: Pet). Persons cannot be pets, and pets cannot be persons (Class: Pet DisjointWith: Person). Cats (Class: Cat SubClassOf: Pet) and dogs (Class: Dog SubClassOf: Pet) are pets. We further assume that cats and dogs are the only pets in our domain, and a cat is not a dog and vice versa (Class: Pet DisjointUnionOf: Cat, Dog). I have also defined an owns object property, which
will be the basis of our discussions on existential property restrictions. You can run the reasoner to confirm for yourself that this ontology is indeed consistent, that is, it contains no logical contradictions.

In this post I will explain through visual representations what is the meaning of

  1. owns some owl:Thing,
  2. C\nTolass: AnimalLover SubClassOf: owns some owl:Thing,
  3. owns some owl:Thing SubClassOf: AnimalLover, and
  4. Class: AnimalLover EquivalentTo: owns some Pet.

owns some owl:Thing

One of the first things one has to realize working with OWL ontologies is that OWL describes sets (in OWL called classes) and relations (in OWL called properties) between sets for some domain of interest. A domain of interest consists of elements (in OWL called individuals) that can belong to classes and/or form part of properties. The domain of interest represents the largest set of individuals that we are interested in, which in OWL is represented by owl:Thing. The smallest set we are interested in is the empty set, which in OWL is represented by owl:Nothing.

ownsSomeThing

Figure 1

In Figure 1 I have illustrated an example domain consisting of some individuals (the small circles) and some owns properties (the arrows) that exist between individuals. At this stage I have not indicated the Person or Pet classes as yet. The meaning of owns some owl:Thing is that it represents the set of those individuals that we know owns something. In our example it consists of the individuals represented by the green circles (i.e. 5 individuals). Note that due to the open world assumption, we cannot assume that individuals that do not form part of the owns property, necessarily do not own anything. Rather, OWL reasoners assume that it is not know whether these individuals own something or do not own something. Because owns some owl:Thing represents a set OWL reasoners and ontology editors some times refer to an expression like owns some owl:Thing as an anonymous class. I.e. it is a class just like Person, but unlike Person it does not have name.

ownsSomePet

Figure 2

To refer specifically to owners of pets we have to define our existential property restriction as owns some Pet. This is illustrated in Figure 2. As you can see owns some Pet is a subset (subclass) of owns some owl:Thing.

Class: AnimalLover SubClassOf: owns some Pet

Assume now we want to model a person that loves animals as someone who has at least 1 pet. We can model this as follows:

Class: AnimalLover
  SubClassOf: 
    Person,
    owns some Pet

To test our ontology we create an individual without a pet.

Individual: anAnimalLoverWithoutAPet
  Types: AnimalLover

Since our anAnimalLoverWithoutAPet individual is defined as belonging to the AnimalLover class and we have not stated that it owns a pet, we may expect that the reasoner will find our ontology inconsistent. However, this is not the case. The reason for this is again due to the open world assumption: there is nothing in our ontology that states that the anAnimalLoverWithoutAPet individual has no pets. To make explicit that anAnimalLoverWithoutAPet owns no pets, we change our definition of anAnimalLoverWithoutAPet as follows

Individual: anAnimalLoverWithoutAPet
  Types: 
    AnimalLover, 
    owns max 0 Pet

which states that anAnimalLoverWithoutAPet has a maximum of zero pets. If we now run the reasoner it will give an inconsistency. When your ontology gives an inconsistency (even when you expect it), it is good idea to check whether it gives an inconsistency for the correct reasons. This will help you to confirm whether you designed your ontology correctly for your desired outcomes. In this case the explanation for the inconsistency is given in Figure 3: It states that the inconsistency is due to the following reasons:

  1. anAnimalLoverWithoutAPet is an animal lover (anAnimalLoverWithoutAPet Types AnimalLover),
  2. who does not own a pet anAnimalLoverWithoutAPet Types owns max 0 owl:Thing,
  3. but the expectation is that an animal lover should own a pet (AnimalLover SubClassOf owns some Pet).
ExplanationAnimalLoverWithoutAPet

Figure 3

Explanations are minimal. That means that if you remove any 1 of the reasons given in an explanation, it is no longer an explanation for the inconsistency (or an entailment). Hence,

anAnimalLoverWithoutAPet Type AnimalLover
AnimalLover owns some Pet

is not an explanation for the inconsistency. This gives us a hint on how to remove an inconsistency from an ontology. If we can change our ontology such that any of the reasons given in an explanation for an inconsistency no longer holds, our ontology will be consistent. In this case we can (again) remove the owns max 0 Pet type from anAnimalLoverWithoutAPet.

An incorrect assumption we may make based on the design of our ontology is that when ever an individual has a pet, the reasoner will infer that the individual is an AnimalLover. However, as I said, this assumption is incorrect. Thus, changing our ontology as follows

Individual: aCat
  Types: Cat

Individual: aPetOwner
  Facts: 
    owns aCat

will not result in inferring that aPetOwner is an AnimalLover.

Figure 4

To understand this see Figure 4. Because we have defined AnimalLover as a subclass of owns some Pet it means the following possibilities exist:

  1. There may be persons who have pets who do not love animals.
  2. There may be individuals that have pets who are not persons.

Hence, from our current ontology, the reasoner can infer nothing more.

owns some Pet SubClassOf: AnimalLover

Now in this section, instead of defining AnimalLover as a subclass of owns some Pet, let us use a general class axiom and define owns some Pet SubClassOf: AnimalLover with AnimalLover defined as follows:

Class: AnimalLover
  SubClassOf: Person

Note that if you now save your ontology in Manchester syntax, you will loose the owns some Pet SubClassOf: AnimalLover general class axiom. Rather save it in say RDF/XML or OWL/XML syntax. We also add the following individuals:

Individual: aCat

Individual: aPetOwner
  Facts: 
    owns aCat

Ensure that you have specified no type information for aCat and aPetOwner. If you run the reasoner it will not infer that aCat is of type Cat. This is because aCat is not of type Cat. If you state that aCat is of type Cat, it will infer that aPetOwner is an AnimalLover.

One reason why this design may not be ideal is that if we again consider the case where an individual anAnimalLoverWithoutAPet of type AnimalLover does not have a pet (as defined in the previous section), it will not give an inconsistency. To understand the reason why the reasoner cannot make this inference, see Figure 5. Because we defined owns some Pet SubClassOf: AnimalLover the possibility exists that there are AnimalLovers who do not own any pets.

OwnsSomePetSubclassAnimalLover

Figure 5

Class: AnimalLover EquivalentTo: owns some Pet

From the previous 2 sections we have noted that defining AnimalLover as AnimalLover SubClassOf: owns some Pet and own some Pet SubClassOf: AnimalLover each have benefits and downsides. If only we could define AnimalLover in terms of both. This can be achieved by defining AnimalLover as follows:

Class: AnimalLover
  EquivalentTo: owns some Pet

With this definition in place, an individual who owns a pet will be inferred to be of type AnimalLover. If an individual is of type AnimalLover, but it is known to have no pets, it will cause an inconsistency. The reason way this definition is not suffering of the problems we encountered with our previous approaches is because the AnimalLover class is now equivalent to the owns some Pet class.

Does this mean it is better to define classes in terms of equivalence rather than subclasses? No. In this instance it turned out to be the case only because we made the assumption that AnimalLovers are equivalent to someone owning a pet. Clearly in reality there may be people owning pets but who do not like pets. Hence, given other assumptions a different ontology may have been a better choice.

Conclusion

The purpose of this post is to explain the meaning of existential property restrictions. For this reason I did not consider using domain and range restrictions to enable inferring that someone who owns a pet is necessarily an AnimalLover. This can easily be achieved by stating

ObjectProperty: owns
  Domain: AnimalLover
  Range: Pet

However, domain and range restrictions are really just syntactical sugar that can be expressed in terms of existential property restrictions (or universal property restrictions).

The example ontologies of this post can be found in github.

Creating Custom Rule Primitives for Jena

In this post I will show you

  1. how to add your own custom rule primitive,
  2. how to inform Jena of your custom rule primitive, and
  3. I will discuss things you have to keep in mind when writing a custom
    primitive.

Adding a Custom Rule Primitive

A powerful feature of Jena is that it allows you to create your own custom builtin rule primitives. Building on our student example of the previous post, assume we want to calculate the final mark for a student given their test result, exam result and project result. We assume we have the following data

:Peet :hasTestResult 77 .
:Peet :hasExamResult 86 .
:Peet :hasProjectResult 91 .

for which we add the following rule

[calcStudentFinalMarkRule: 
  (?student :hasTestResult ?testResult) 
  (?student :hasExamResult ?examResult) 
  (?student :hasProjectResult ?projectResult) 
  calcFinalMark(?testResult, ?examResult, 
   ?projectResult, ?finalMark)
      -> (?student :hasFinalMark ?finalMark)]

The meat of the implementation is the doUserRequiredAction(args, length,
context)
method, which consists of the following steps:

  1. check that we have the correct number of parameters,
  2. retrieve the input parameters,
  3. verify the typing of input parameters,
  4. doing the actual calculation,
  5. creating a node for the output parameter, and
  6. binding the node to the output parameter.
private boolean doUserRequiredAction(Node[] args, 
  int length, RuleContext context) {
  
  // Check we received the correct number of parameters
  checkArgs(length, context);

  boolean success = false;
  
  // Retrieve the input arguments
  Node studentTestResult = getArg(0, args, context);
  Node studentExamResult = getArg(1, args, context);
  Node studentProjectResult = getArg(2, args, context);

  // Verify the typing of the parameters
  if (studentTestResult.isLiteral() && 
    studentExamResult.isLiteral() && 
    studentProjectResult.isLiteral()) {
    Node finalMark = null;
    if (studentTestResult.getLiteralValue() 
        instanceof Number && 
      studentExamResult.getLiteralValue() 
        instanceof Number &&
      studentProjectResult.getIndexingValue() 
        instanceof Number) {
    
      Number nvStudentTestResult = 
        (Number)studentTestResult.getLiteralValue();
      Number nvStudentExamResult = 
        (Number)studentExamResult.getLiteralValue();
      Number nvStudentProjectResult = 
        (Number)studentProjectResult.getLiteralValue();
    
      // Doing the calculation
      int nFinalMark = 
        (nvStudentTestResult.intValue() * 20)/100 + 
        (nvStudentExamResult.intValue() * 50)/100 +
        (nvStudentProjectResult.intValue() * 30)/100;
      
      // Creating a node for the output parameter
      finalMark = Util.makeIntNode(nFinalMark);  
      
      // Binding the output parameter to the node
      BindingEnvironment env = context.getEnv();    
      success = env.bind(args[3], finalMark);
    } 
  }   
  return success;
}

Registering a Custom Primitive with Jena

Our code for load our rules and activating it is similar to my previous post, except that you have to make a call to register the custom primitive:

// Load RDF data
String data = path.toFile().getAbsolutePath() + 
  "/src/main/resources/data2.ttl";
Model model = ModelFactory.createDefaultModel();
model.read(data);
      
// Register custom primitive
BuiltinRegistry.theRegistry.register(new CalcFinalMark());
      
// Load rules
String rules = path.toFile().getAbsolutePath() + 
  "/src/main/resources/student2.rules";
Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(rules));
     
InfModel infModel = ModelFactory.createInfModel(reasoner, model);  
infModel.rebind();   

Things to Keep in Mind

There are two main things I think one needs to keep in mind with Jena custom rule primitives:

  1. A primitive is suppose to be a elementary building block. Being able to create your own primitives may tempt you to add all sorts of interesting processing besides the manipulation of triples, but I strongly advice against that. Arbitrary processing in your builtin primitive can degrade performance of inferencing.
  2. Do not assume that you have control over when a rule will be triggered. Exactly when a rule will be triggered is dependent on when the Jena InfModel implementation decides to re-evaluate the rules, which is dependent on internal caching of the InfModel implementation and how it deals with modifications made to the model. Even though I believe InfModel implementations will avoid arbitrarily re-evaluating rules, I still think it is conceivable that under some circumstance the same rule may be triggered more than once for the same data. Furthermore, the Jena documentation of 3.6.0 states that the InfModel interface is still in flux, which could mean that even if a rule is only triggered once for given data currently, due to unforeseen changes it may be triggered more than once in future updates of Jena.

Conclusion

In this post I gave an example of how you can develop a custom rule primitive for Jena. The code for this example can be found at github.