| Tables | categories Tables, Rules and Hypothetical Reasoning Rules |
Hypothetical Reasoning |
A rule in CLAIRE is made by associating an event condition to an expression. The rule is attached to a set of free variables of given types: each time that an event that matches the condition becomes occurs for a given binding of the variables (i.e., association of one value to each variable), the expression will be evaluated with this binding. The interest of rules is to attach an expression not to a functional call (as with methods) but to an event, with a binding that is more flexible (many rules can be combined for one event) and more incremental.
Definition : A rule is an object that binds a condition to an action, called its conclusion. Each time the condition becomes true for a set of objects because of a new event, the conclusion is executed. The condition is expressed as a logic formula on one or more free variables that represent objects to which the rule applies. The conclusion is a CLAIRE expression that uses the same free variables. An event is an update on these objects, either the change of a slot or a table value, or the instantiation of a class. A rule condition is checked if and only if an event has occurred. |
A novelty in CLAIRE 3.0 is the introduction of event logic. There are two events that can be matched precisely: the update of a slot or a table, and the instantiation of a class. CLAIRE 3.2 use expressions called event pattern to specify which kind of events the rule is associated with. For instance, the expression x.r := y is an event expression that says both that x.r = y and that the last event is actually the update of x.r from a previous value. More precisely, here are the events that are supported :
In addition, a new event pattern was introduced in CLAIRE 3.0 to capture the transition from an old to a new value. This is achieved with the expression x.r := (z <- y) which says that the last event is the update of x.r from z to y. For instance, here is the event expression that states that x.salary crossed the 100000 limit :
| x.salary := (y <- z) & y < 100000 & z >= 100000 |
Virtual event may be used for many purposes. The creation of a virtual event requires no time nor memory; thus, it is a convenient technique to capture state transition in your object system. For instance, we can create an event signaling the instantiation of a class as follows :
| instantiation :: property(domain = myClass, range = string) [close(x:MyClass) : MyClass -> instantiation(x,date!(1)), x] controlRule() :: rule(instantiation(x,s) => printf("--- create ~S at ~A \n",x,s)) |
| r1() :: rule(x.friends :add y => for z in y.friend x.friends :add z) |
For instance, let us define the following rule to fill the table fib with the Fibonacci sequence :
| r3() :: rule(y := fib[x] & x % (0 .. 100) => when z := get(fib,x - 1) in fib[x + 1] := y + z) (fib[0] := 1, fib[1] := 1) |