Here's how you would typically do it with a simple comparison:
CLIPS> (clear)
CLIPS>
(deftemplate fact (slot name) (slot value))
CLIPS>
(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>
(defrule find-max-value
(fact (name ?name1) (value ?value1))
(not (fact (value ?value2&:(> ?value2 ?value1))))
=>
(printout t "Fact " ?name1 " is the maximum" crlf))
CLIPS> (reset)
CLIPS> (run)
Fact data-5 is the maximum
CLIPS>
The same basic approach also works if you bind all of the slot values needed for comparison and pass them into a function:
CLIPS> (clear)
CLIPS>
(deftemplate fact (slot name) (slot value))
CLIPS>
(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>
(deffunction my-predicate (?value1 ?value2)
(> ?value1 ?value2))
CLIPS>
(defrule find-max-value
(fact (name ?name1) (value ?value1))
(not (fact (value ?value2&:(my-predicate ?value2 ?value1))))
=>
(printout t "Fact " ?name1 " is the maximum" crlf))
CLIPS> (reset)
CLIPS> (run)
Fact data-5 is the maximum
CLIPS>
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):
CLIPS> (clear)
CLIPS>
(deftemplate fact (slot name) (slot value))
CLIPS>
(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>
(deffunction my-predicate (?fact1 ?fact2)
(> (fact-slot-value ?fact1 value) (fact-slot-value ?fact2 value)))
CLIPS>
(defrule find-max-value
?fact1 <- (fact (name ?name1))
(not (fact (name ?name2&:(my-predicate (nth$ 1 (find-fact ((?fact2 fact))
(eq ?fact2:name ?name2)))
?fact1))))
=>
(printout t "Fact " ?name1 " is the maximum" crlf))
CLIPS> (reset)
CLIPS> (run)
Fact data-5 is the maximum
CLIPS>
What I would suggest doing is to use the fact query functions to iterate over the facts to find the maximum value:
CLIPS> (clear)
CLIPS>
(deftemplate fact (slot name) (slot value))
CLIPS>
(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>
(deffunction my-predicate (?fact1 ?fact2)
(> (fact-slot-value ?fact1 value) (fact-slot-value ?fact2 value)))
CLIPS>
(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>
(reset)
CLIPS> (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> (find-max fact my-predicate)
<Fact-5>
CLIPS>
(defrule find-max
=>
(bind ?fact (find-max fact my-predicate))
(if ?fact
then
(printout t "Fact " (fact-slot-value ?fact name) " is the maximum" crlf)))
CLIPS> (run)
Fact data-5 is the maximum
CLIPS>
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:
Jess>
(deftemplate fact (slot name) (slot value))
TRUE
Jess>
(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> (if FALSE then 3 else 4)
4
Jess>
(defrule find-max-value
?c <- (accumulate (bind ?max FALSE)
(if (or (not ?max) (> ?value ?max))
then (bind ?max ?value))
?max
(fact (value ?value)))
=>
(printout t "The max is " ?c crlf))
TRUE
Jess> (reset)
TRUE
Jess> (run)
The max is 4
1
Jess>