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.

Creating Custom Rules for Jena

In this post I will show you

  1. how to add your own custom rules to Jena,
  2. how to use rule primitives, and
  3. I will mention some things you may want to keep in mind when using rules.

Add and Activate Rules

We assume we start with the following simple triples:

:Peet :takesCourse :ComputerScience .
:Ruth :teachesCourse :ComputerScience .

for which we add the following rule to a student1.rules file:

[hasStudentRule: 
  (?student :takesCourse ?course) 
  (?lecturer :teachesCourse ?course) 
    -> (?lecturer :hasStudent ?student)] 

The hasStudentRule says that if a student takes a course and that course is presented by some lecturer, then the student is a student of that lecturer. The (?student :takesCourse ?course) (?lecturer :teachesCourse ?course) part of the rule is referred to as the premise or body of the rule and the (?lecturer :hasStudent ?student) part as the conclusion or head of the rule.

To activate the rules in Jena we have to create a model that is capable of doing inferences. In the context of Jena inferencing means that it will re-evaluate the rules against the data, which will cause additional statements to be potentially added to the model. This can be achieved by calling methods that will cause Jena to re-evaluate the model, i.e. calls like InfModel.rebind() and InfModel.validate().

Path path = Paths.get(".").toAbsolutePath().normalize();

// Load RDF data
String data = path.toFile().getAbsolutePath() + 
  "/src/main/resources/data.ttl";
Model model = ModelFactory.createDefaultModel();
model.read(data);
    
// Load rules
String rules = path.toFile().getAbsolutePath() + 
  "/src/main/resources/student.rules";
Reasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL(rules));

InfModel infModel = ModelFactory.createInfModel(reasoner, model);   
infModel.rebind();

When the hasStudentRule is activated a new statement will be added to the Jena model:

:Ruth :hasStudent :Peet .

Using Rule Primitives

Jena supports a number of builtin rule primitives that are intuitive to understand, i.e.

[pensionerRule: 
  (?person :hasAge ?age) greaterThan(?age, 60) 
    -> (?person a :Pensioner)]

which states that when ?person has an age greater than 60, ?person is considered to be a pensioner. Assuming we have the data

:Peet :hasAge 90 .

the following triple will be added to the Jena model:

:Peet a :Pensioner .

Things to Keep in Mind

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

  1. The purpose of rules are to manipulate the triples in the Jena model. For the most part it adds triples to the model, but it can also remove triples from the model if primitives like remove and drop are used.
  2. Adding and removing of triples can be achieved through SPARQL queries which can perform better or worse than rules. It is therefore best to check both approaches for your given use case.

Conclusion

In this post I gave a brief introduction in how to use custom rules and builtin rule primitives in Jena. This code is available at github.