Search code examples
clips

clips: avoiding connective constraints


Let's suppose we have the following CLIPS template and rule:

;; The queue sequence starts at 0.
;; -1 is a placeholder value to identify a newly inserted element.
;; The idea is to put a newly inserted element at the end of the queue.
(deftemplate queue-element
    (slot order (type INTEGER) (default -1))

(deftemplate put-at-the-end
    ?e1 <- (queue-element (order -1))
    ?e2 <- (queue-element (order ?o1))
    (not (queue-element (order ?o2&:(> ?o2 ?o1))))
  =>
    (modify ?e1 (order (+ ?o1 + 1))))

Is there a way to move the "connective constraint" (> ?o2 ?o1) out of the pattern and move it to something similar to a (test (> ?o2 ?o1)) construct instead?

The idea is to avoid these in-line conditions enterily.


Solution

  • You can use the and conditional element to place several conditional elements within a not conditional element.

    CLIPS> 
    (deftemplate queue-element
        (slot order (type INTEGER) (default -1)))
    CLIPS> 
    (defrule put-at-the-end
        ?e1 <- (queue-element (order -1))
        ?e2 <- (queue-element (order ?o1))
        (not (and (queue-element (order ?o2))
                  (test (> ?o2 ?o1))))
      =>
        (modify ?e1 (order (+ ?o1 1))))
    CLIPS>