﻿ Use an entity or relationship in a rule

# Use an entity or relationship in a rule

To write rules in Oracle Policy Modeling you need to understand how to refer to the different parts of the data model within your rules.

## What do you want to do?

### Refer to entities connected by a to-many relationship

Anytime you refer from one entity to another entity in a "to-many" relationship, you need to indicate whether one or all members of the target entity group need to satisfy the rule.

Consider the following rule:

A family may board the plane first if their child is under 8 years of age

We know that families can have more than one child, however, this rule does not specify whether one or all of the family's children must be under 8 years of age in order for the family to board the plane first. If the family had 2 children, one aged 4 and one aged 16, how would you decide?

The rule would be clearer if written in such a way that the reader can tell whether the rule applies to one or all children. For example:

A family may board the plane first if they have at least one child under 8 years of age

We use quantifiers, which are a type of Oracle Policy Modeling syntax, to write these kinds of rules. Quantifiers are operators which access data across the instances of an entity. The two quantifiers we use are:

• the universal quantifier - used to check that the condition returns true for every instance of an entity. For example, "All of the apples are red".
• the existential quantifier - used to check that the condition returns true for at least one instance of an entity. For example, "At least one of the bananas is yellow".

#### Check that a condition returns true for every instance of an entity

The universal quantifier must be used when you refer from one entity to another entity in a "to-many" relationship, AND you need to determine whether all members of the target entity group need to satisfy the rule. This quantifier works in much the same way across entities as the 'and' operator does across attributes. This means that the rule using the universal quantifier will only evaluate to true when the condition is true for all instances of an entity. In other words, the conclusion will evaluate to false if its condition is false for one of the targets of the relationship provided. This applies even when the relationship provided is only partially known.

There are two types of entity function that are used as universal quantifiers: the For All function and the For All Scope function. This section describes the use of the For All function which is used where there is only one condition (ie the rule only refers to one relationship). The use of the For All Scope function, where there are one or more conditions (eg when you want to reason across several different relationships in the one rule), is more advanced and is covered in Extend the For, For All and Exists functions.

As mentioned above, the For All function is used where there is only one condition. For example, you could have the following rule where 'the family' is an entity (the source entity), 'the child' is an entity (the target entity), and 'the family's children' is the relationship between the entities (the relationship text).

the family is ready to travel overseas if

ForAll(the family's children, the child has a passport)

There are several ways of writing a For All function - see the Entity and relationship function reference for more detail.

Note that if there are zero instances of the entity, then the rule using the For All operator will evaluate to true.

#### Check that a condition returns true for at least one instance of an entity

The existential quantifier must be used when you refer from one entity to another entity in a "to-many" relationship, AND you need to determine whether any members of the target entity group need to satisfy the rule. This quantifier works in much the same way across entities as the 'or' operator does across attributes. This means that only one instantiation of the entity must be true for the attribute using the operator to be true. In other words, the conclusion will evaluate to true if its condition is true for one of the targets of the relationship provided. This applies even when the relationship provided is only partially known.

There are two types of entity function that are used as existential quantifiers: the Exists function and the Exists Scope function. This section describes the use of the Exists function which is used where there is only one condition (ie the rule only refers to one relationship). The use of the Exists Scope function, where there are one or more conditions (eg when you want to reason across several different relationships in the one rule), is more advanced and is covered in Extend the For, For All and Exists functions.

As mentioned above, the Exists function is used where there is only one condition. For example, you could have the following rule where 'the family' is an entity (the source entity), 'the child' is an entity (the target entity), and 'the family's children' is the relationship between the entities (the relationship text).

the family is eligible for the benefit if

Exists(the family's children, the child is a qualifying child)

There are several ways of writing an Exists function - see the Entity and relationship function reference for more detail.

Note that if there are zero instances of the entity, then the rule using the Exists operator will evaluate to false.

### Refer to entities connected by a to-one relationship

When you refer from one entity to another entity in a "to-one" relationship that is not a containment relationship, you need to use a particular syntax to connect the two entities together. There are two types of entity functions used for this purpose: the For function and the For Scope function. This section describes the For function which is used where there is only one condition (ie the rule only refers to one relationship). The use of the For Scope function, where there are one or more conditions (eg when you want to reason across several different relationships in the one rule), is more advanced and is covered in Extend the For, For All and Exists functions.

As mentioned above, the For function is used where there is only one condition. For example, you could have the following rule where 'the child' is an entity (the source entity), 'the school' is an entity (the target entity), and 'the child's school' is the many-to-one relationship between the entities (the relationship text).

the child has a day off school if

For(the child's school, the school is closed)

There are a couple of ways of writing a For function - see the Entity and relationship function reference for more detail.

NOTES:

1. The For syntax can also be used for many-to-many relationships. (The only relationship type that it can't be used with is one-to-many.)
2. The For syntax does not need to be used when referring to a parent relationship in the entity's containment relationships. For example, if an entity 'the pet' is contained within an entity 'the child', you could write the following rule without needing to refer to the containment relationship explicitly:

the pet is playing outside if

the child is playing outside

### Compare entities within the same relationship

To compare entities within the same relationship, you need to add an alias to the entities involved. Aliasing allows you to provide an alternative name used to refer to an entity instance. For more information, see Remove ambiguity when reasoning about more than one instance of the same entity.

### Count the number of instances of an entity

To count the number of instances there are of an entity, you use the Instance Count function. The syntax for this function is:

• InstanceCount(<relationship text>)
• the number of <relationship text>

For example, the Instance Count function could be used to determine the number of children belonging to the claimant:

the number of children that the claimant has = InstanceCount(the claimant's children)

(For this rule to compile the following entities and relationship must be included in a properties files in the project: an entity 'the claimant' , an entity 'the child' and a one-to-many relationship 'the claimant's children'.)

The function returns a value of 4 for the following data:

the child
Anthony
Peter
Rebecca
Fiona

### Get the highest/most recent value of an entity-level variable

To obtain the highest or most recent value of an entity-level variable for all instances of the entity, you use the Instance Maximum function. The syntax for this function is:

• InstanceMaximum(<relationship text>,<entity-level variable>)

• the greatest of <entity-level variable> for all of <relationship text>

• <entity-level date> which is the latest for all of <relationship text>

•  the latest of all <entity-level date> for <relationship text>

• <entity-level variable> which is the greatest for all of <relationship text>

For example, the Instance Maximum function could be used to determine the highest bank balance for a child of the claimant:

the highest bank balance for a child of the claimant = InstanceMaximum(the claimant's children, the child's bank balance)

(For this rule to compile the following entities and relationship must be included in a properties files in the project: an entity 'the claimant' , an entity 'the child' and a one-to-many relationship 'the claimant's children'.

The function returns a value of \$175 for the following data:

the child the child's bank balance
Annabel \$50
Katrina \$175
Mike \$120

### Get the lowest/least recent value of an entity-level variable

To obtain the lowest or least recent value of an entity-level variable for all instances of the entity, you use the Instance Minimum function. The syntax for this function is:

• InstanceMinimum(<relationship text>,<entity-level variable>)

• the least of <entity-level variable> for all of <relationship text>

• <entity-level variable> which is the least for all of <relationship text>

• <entity-level date> which is the earliest for all of <relationship text>

• the earliest of all <entity-level date> for <relationship text>

For example, the Instance Minimum function could be used to determine the lightest of the claimant's children:

the lightest weight for a child of the claimant = InstanceMinimum(the claimant's children, the child's weight in kilograms)

(For this rule to compile the following entities and relationship must be included in a properties files in the project: an entity 'the claimant' , an entity 'the child' and a one-to-many relationship 'the claimant's children'.)

The function returns a value of 15 for the following data:

the child the child's weight in kilograms
Harry 15
Sharon 30
Fran 45

### Add up numerical values gathered from each instance of an entity

To obtain the sum of all instances of an entity-level variable, you use the Instance Sum function. The syntax for this function is:

• InstanceSum(<relationship text>,<entity-level variable>)

• <entity-level variable> totaled for all of <relationship text>

For example, the Instance Sum function could be used to determine the total Child Care Benefit payable to the claimant:

the total Child Care Benefit payable to the claimant = InstanceSum(the claimant's children, the Child Care Benefit amount for the child)

(For this rule to compile the following entities and relationship must be included in a properties files in the project: an entity 'the claimant' , an entity 'the child' and a one-to-many relationship 'the claimant's children'.)

The function returns a value of \$900 for the following data:

the child the Child Care Benefit amount for the child
Mary \$500
Sam \$250
Lizzie \$150