When a rule creates an object that depends on a specific set of preconditions, it is sometimes necessary to erase that object when those preconditions no longer hold. This is an example of truth maintenance.
KnowledgeWorks provides a mechanism to track logical dependencies between objects and preconditions which cause any dependent objects to be erased automatically. This is achieved using a logical
clause in a forward chaining rule, with a precondition of the form:
(logical <forward-condition>+)
The enclosed forward conditions in this clause are matched as normal, but if the rule fires and creates new objects (by assert or make-instance) then these objects are associated with the enclosed conditions. If the conditions are found to be false in the future, then the created objects are erased automatically (see erase).
NB: There can be at most one logical
clause in a rule (though it can contain multiple subclauses) and it must be the first clause in the rule. Other clauses can follow the logical clause, but they are not part of the logical dependency.
Given the following classes and rules:
(def-kb-class number-object () ((value :initarg :value))) (def-kb-class have-some-large-numbers () ()) (defrule notice-a-large-number :forward (logical (number-object ? value ?value) (test (> ?value 100))) --> (assert (have-some-large-numbers ?)))
then a have-some-large-numbers
object will be created when a number larger than 100 exists:
(setq n1 (make-instance 'number-object :value 10)) (infer) (any '?x '(have-some-large-numbers ?x)) ==> false (setf (slot-value n1 'value) 200) ; this is large (infer) (any '?x '(have-some-large-numbers ?x)) ==> true
In addition, when the large number becomes smaller, the have-some-large-numbers
object will be erased again:
(setf (slot-value n1 'value) 55) (infer) (any '?x '(have-some-large-numbers ?x)) ==> false
because a logical dependency was tracked between the preconditions:
(number-object ? value ?value) (test (> ?value 100)
and the have-some-large-numbers
object.
KnowledgeWorks and Prolog User Guide (Unix version) - 01 Dec 2021 19:35:49