Search code examples
clipspyclips

Avoid pattern matching (errors) until a slot is set correctly


The LHS of a rule R_blup contains

(test (>= ?s2 2))

that is, it checks if ?s2 is greater or equal to 2. ?s2 corresponds to an instance slot named s2.

Unfortunately, I get the error

Function >= expected argument #1 to be of type integer or float

The problem is that my code executes the (test ... before I can set argument #1, i.e. before I can set s2 to an integer or float value. s2 is supposed to be set to an integer inside a python-call that is triggered by another rule R_blah.

The error is triggered in the middle of another python-call belonging to another rule R_xyz. This python-call modifies an instance via clips_instance.Slots["slot_name"] = some_value.

How is this normally handled? I see three solutions I don't like too much:

  1. Setting a default (integer) value for s2.
  2. Modifying the (test ... to check against nil first.
  3. Adding another check/rule to wait until s2 is not nil any more

Is it maybe possible to try/except/pass the error?


Solution

  • Use the function object-pattern-match-delay to delay pattern matching to create an atomic operation for a series of changes:

    CLIPS> (defclass POINT (is-a USER) (slot x) (slot y))
    CLIPS> 
    (defrule check 
       (object (is-a POINT) (x ?s2))
       (test (>= ?s2 2))
       =>)
    CLIPS> (make-instance [p1] of POINT)
    [ARGACCES5] Function >= expected argument #1 to be of type integer or float
    
    [DRIVE1] This error occurred in the join network
       Problem resides in associated join
          Of pattern #1 in rule check
    
    [p1]
    CLIPS> (agenda)
    CLIPS> 
    (object-pattern-match-delay
       (make-instance [p2] of POINT)
       (make-instance [p3] of POINT)
       (send [p2] put-x 3)
       (send [p3] put-x 0))
    0
    CLIPS> (agenda)
    0      check: [p2]
    For a total of 1 activation.
    CLIPS>