Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's how you would typically do it with a simple comparison:</p> <pre><code>CLIPS&gt; (clear) CLIPS&gt; (deftemplate fact (slot name) (slot value)) CLIPS&gt; (deffacts test-data (fact (name data-1) (value 3)) (fact (name data-2) (value 1)) (fact (name data-3) (value 2)) (fact (name data-4) (value 2)) (fact (name data-5) (value 4)) (fact (name data-6) (value 3))) CLIPS&gt; (defrule find-max-value (fact (name ?name1) (value ?value1)) (not (fact (value ?value2&amp;:(&gt; ?value2 ?value1)))) =&gt; (printout t "Fact " ?name1 " is the maximum" crlf)) CLIPS&gt; (reset) CLIPS&gt; (run) Fact data-5 is the maximum CLIPS&gt; </code></pre> <p>The same basic approach also works if you bind all of the slot values needed for comparison and pass them into a function:</p> <pre><code>CLIPS&gt; (clear) CLIPS&gt; (deftemplate fact (slot name) (slot value)) CLIPS&gt; (deffacts test-data (fact (name data-1) (value 3)) (fact (name data-2) (value 1)) (fact (name data-3) (value 2)) (fact (name data-4) (value 2)) (fact (name data-5) (value 4)) (fact (name data-6) (value 3))) CLIPS&gt; (deffunction my-predicate (?value1 ?value2) (&gt; ?value1 ?value2)) CLIPS&gt; (defrule find-max-value (fact (name ?name1) (value ?value1)) (not (fact (value ?value2&amp;:(my-predicate ?value2 ?value1)))) =&gt; (printout t "Fact " ?name1 " is the maximum" crlf)) CLIPS&gt; (reset) CLIPS&gt; (run) Fact data-5 is the maximum CLIPS&gt; </code></pre> <p>Passing in a fact to the function, however, is problematic because you can't bind the pattern to a fact address inside a not conditional element. If the fact has a slot that serves as a unique identifier, you can use this to retrieve a pointer to the fact (although I wouldn't recommend this approach):</p> <pre><code>CLIPS&gt; (clear) CLIPS&gt; (deftemplate fact (slot name) (slot value)) CLIPS&gt; (deffacts test-data (fact (name data-1) (value 3)) (fact (name data-2) (value 1)) (fact (name data-3) (value 2)) (fact (name data-4) (value 2)) (fact (name data-5) (value 4)) (fact (name data-6) (value 3))) CLIPS&gt; (deffunction my-predicate (?fact1 ?fact2) (&gt; (fact-slot-value ?fact1 value) (fact-slot-value ?fact2 value))) CLIPS&gt; (defrule find-max-value ?fact1 &lt;- (fact (name ?name1)) (not (fact (name ?name2&amp;:(my-predicate (nth$ 1 (find-fact ((?fact2 fact)) (eq ?fact2:name ?name2))) ?fact1)))) =&gt; (printout t "Fact " ?name1 " is the maximum" crlf)) CLIPS&gt; (reset) CLIPS&gt; (run) Fact data-5 is the maximum CLIPS&gt; </code></pre> <p>What I would suggest doing is to use the fact query functions to iterate over the facts to find the maximum value:</p> <pre><code>CLIPS&gt; (clear) CLIPS&gt; (deftemplate fact (slot name) (slot value)) CLIPS&gt; (deffacts test-data (fact (name data-1) (value 3)) (fact (name data-2) (value 1)) (fact (name data-3) (value 2)) (fact (name data-4) (value 2)) (fact (name data-5) (value 4)) (fact (name data-6) (value 3))) CLIPS&gt; (deffunction my-predicate (?fact1 ?fact2) (&gt; (fact-slot-value ?fact1 value) (fact-slot-value ?fact2 value))) CLIPS&gt; (deffunction find-max (?template ?predicate) (bind ?max FALSE) (do-for-all-facts ((?f ?template)) TRUE (if (or (not ?max) (funcall ?predicate ?f ?max)) then (bind ?max ?f))) (return ?max)) CLIPS&gt; (reset) CLIPS&gt; (facts) f-0 (initial-fact) f-1 (fact (name data-1) (value 3)) f-2 (fact (name data-2) (value 1)) f-3 (fact (name data-3) (value 2)) f-4 (fact (name data-4) (value 2)) f-5 (fact (name data-5) (value 4)) f-6 (fact (name data-6) (value 3)) For a total of 7 facts. CLIPS&gt; (find-max fact my-predicate) &lt;Fact-5&gt; CLIPS&gt; (defrule find-max =&gt; (bind ?fact (find-max fact my-predicate)) (if ?fact then (printout t "Fact " (fact-slot-value ?fact name) " is the maximum" crlf))) CLIPS&gt; (run) Fact data-5 is the maximum CLIPS&gt; </code></pre> <p>Although CLIPS does not, many languages do support accumulation conditional elements which makes it easier to perform this type of calculation. For example, in Jess you can do this:</p> <pre><code>Jess&gt; (deftemplate fact (slot name) (slot value)) TRUE Jess&gt; (deffacts test-data (fact (name data-1) (value 3)) (fact (name data-2) (value 1)) (fact (name data-3) (value 2)) (fact (name data-4) (value 2)) (fact (name data-5) (value 4)) (fact (name data-6) (value 3))) TRUE Jess&gt; (if FALSE then 3 else 4) 4 Jess&gt; (defrule find-max-value ?c &lt;- (accumulate (bind ?max FALSE) (if (or (not ?max) (&gt; ?value ?max)) then (bind ?max ?value)) ?max (fact (value ?value))) =&gt; (printout t "The max is " ?c crlf)) TRUE Jess&gt; (reset) TRUE Jess&gt; (run) The max is 4 1 Jess&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload