Nicola Ben
Nicola Ben

Reputation: 11367

CLIPS: allowed-symbol

I defined a class with a restricted options for a slot:

(defclass TARGET (is-a USER)
    (slot uuid
        (type STRING))
    (slot function
        (type SYMBOL)
        (allowed-symbols a1 a2 b c d e f g))
)

(make-instance target of TARGET
    (uuid "a123")  
    (function zzz)    
)

I expected CLIPS to complain for the "zzz" (not allowed), but it didn't. Why?

Best regards. Nicola

Upvotes: 0

Views: 1031

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

Constraint checking is done statically (during parsing) and dynamically (during execution). By default, only static constraint checking is enabled. Slot assignments for instances are done dynamically when messages passing is invoked because it's possible for an illegal value to be replaced with a legal value during execution of a message handler.

In the following case, the definstances does not generate an error when it is defined because the invalid value could be replaced at run time, but the defrule does generate an error because object patterns directly grab the value of a slot without using message passing.

CLIPS> (clear)
CLIPS> 
(defclass TARGET (is-a USER)
   (slot uuid
      (type STRING))
   (slot function
      (type SYMBOL)
      (allowed-symbols a1 a2 b c d e f g)))
CLIPS> 
(definstances static
   (target1 of TARGET (uuid "a123") (function zzz)))
CLIPS>    
(defrule static
   (object (is-a TARGET) (function zzz))
   =>)

[CSTRNCHK1] A literal restriction value found in CE #1
does not match the allowed values for slot function.

ERROR:
(defrule MAIN::static
   (object (is-a TARGET)
           (function zzz))
   =>)
CLIPS> (reset)
CLIPS>          
(make-instance target2 of TARGET
    (uuid "b456")  
    (function zzz))
[target2]
CLIPS>

If you enable dynamic constraint checking, you'll see errors during execution when the instances are actually created:

CLIPS> (set-dynamic-constraint-checking TRUE)
FALSE
CLIPS> 
(make-instance target3 of TARGET
    (uuid "c789")  
    (function zzz))
[CSTRNCHK1] zzz for slot function of instance [target3] found in put-function primary in class TARGET
does not match the allowed values.
[PRCCODE4] Execution halted during the actions of message-handler put-function primary in class TARGET
FALSE
CLIPS> (reset)
[CSTRNCHK1] zzz for slot function of instance [target1] found in put-function primary in class TARGET
does not match the allowed values.
[PRCCODE4] Execution halted during the actions of message-handler put-function primary in class TARGET
CLIPS> 

Upvotes: 2

Related Questions