next up previous

5.4.1.7 PatternMatching with Object Patterns

Instances of userdefined classes in COOL can be patternmatched on the lefthand side of rules. Patterns can only match objects of classes which are defined before the pattern and which are in scope for the current module. Any classes which could have objects which match the pattern cannot be deleted or changed until the pattern is deleted. Even if a rule is deleted by its RHS, the classes bound to its patterns cannot be changed until after the RHS finishes executing.

When an instance is created or deleted, all patterns applicable to that object are updated. However, when a slot is changed, only those patterns which explicitly match on that slot are affected. Thus, one could use logical dependencies to hook to a change to a particular slot (rather than a change to any slot, which is all that is possible with deftemplates).

Changes to nonreactive slots or instances of nonreactive classes (see sections 9.3.2.2 and 9.3.3.7) will have no effect on rules. Also Rete network activity will not be immediately apparent after changes to slots are made if patternmatching is being delayed through the use of the makeinstance, initializeinstance, modifyinstance, messagemodifyinstance, duplicateinstance, messageduplicateinstance or objectpatternmatchdelay functions.

Syntax

<object-pattern>       ::= (object <attribute-constraint>*)
<attribute-constraint> ::= (is-a <constraint>) |
                           (name <constraint>) |
                           (<slot-name> <constraint>*)

The isa constraint is used for specifying class constraints such as ìIs this object a member of class FOO?î. The isa constraint also encompasses subclasses of the matching classes unless specifically excluded by the pattern. The name constraint is used for specifying a specific instance on which to patternmatch. The evaluation of the name constraint must be of primitive type instancename, not symbol. Multifield constraints (such as $?) cannot be used with the isa or name constraints. Other than these special cases, constraints used in object slots work similarly to constraints used in deftemplate slots. As with deftemplate patterns, slot names for object patterns must be symbols and can not contain any other constraints.

Example 1

The following rules illustrate patternmatching on an object's class.

(defrule class-match-1
  (object)
  =>)
(defrule class-match-2
  (object (is-a FOO))
  =>)
(defrule class-match-3
  (object (is-a FOO | BAR))
  =>)
(defrule class-match-4
  (object (is-a ?x))
  (object (is-a ~?x))
  =>)

Rule classmatch1 is satisified by all instances of any reactive class. Rule classmatch2 is satisfied by all instances of class FOO. Rule classmatch3 is satisfied by all instances of class FOO or BAR. Rule classmatch4 will be satisfied by any two instances of mutually exclusive classes.

Example 2

The following rules illustrate patternmatching on various attributes of an object's slots.

(defrule slot-match-1
  (object (width))
  =>)
(defrule slot-match-2
  (object (width ?))
  =>)
(defrule slot-match-3
  (object (width $?))
  =>)

Rule slotmatch1 is satisfied by all instances of reactive classes that contain a reactive width slot with a zero length multifield value. Rule slotmatch2 is satisfied by all instances of reactive classes that contain a reactive single or multifield width slot that is bound to a single value. Rule slotmatch3 is satisfied by all instances of reactive classes that contain a reactive single or multifield width slot that is bound to any number of values. Note that a slot containing a zero length multifield value would satisfy rules slotmatch1 and slotmatch3, but not rule slotmatch2 (because the value's cardinality is zero).

Example 3

The following rules illustrate patternmatching on the slot values of an object.

(defrule value-match-1
  (object (width 10)
  =>)
(defrule value-match-2
  (object (width ?x&:(> ?x 20)))
  =>)
(defrule value-match-3
  (object (width ?x) (height ?x))
  =>)

Rule valuematch1 is satisified by all instances of reactive classes that contain a reactive width slot with value 10. Rule valuematch2 is satisfied by all instances of reactive classes that contain a reactive width slot that has a value greater than 20. Rule valuematch3 is satisfied by all instances of reactive classes that contain a reactive width and height slots with the same value.


next up previous