Maybe this is coming close to what you want.

; your fact template
(deftemplate Fact (slot id)(slot a)(slot b)(slot c))

; supposed to contain the rating functions for the slot values
(deftemplate Func (slot fa)(slot fb)(slot fc))

; the computed ranking for a fact
(deftemplate Rank (slot r)(slot f))

; the collected results
(deftemplate Count (slot n)(multislot facts))

; some facts
(deffacts someT
   (Fact (id "A")(a 42)(b 23)(c 17))
   (Fact (id "B")(a 21)(b 11)(c  7))
   (Fact (id "C")(a 15)(b 10)(c 12))
   (Fact (id "D")(a 33)(b 12)(c  9))
)

; rating functions for the slots
(deffacts aFunc
   (Func (fa (lambda (?a) (return (- ?a 10))))
         (fb (lambda (?b) (return (/ ?b  3))))
         (fc (lambda (?c) (return (* ?c  2)))))
)

; a rule computing the fact ranking as the sum of the slot rankings
(defrule setRank
 ?fact <- (Fact (a ?a) (b ?b) (c ?c))
 (Func (fa ?fa)(fb ?fb)(fc ?fc))
=>
 (bind ?rank (+ (call ?fa ?a)(call ?fb ?b)(call ?fc ?c)))
 (assert (Rank (r ?rank)(f ?fact)))
)

(reset)
(run)

; getting & displaying the n best rankings
(defrule getBest
  ?count <- (Count (n ?n &: (> ?n 0))(facts $?facts))
  ?rank  <- (Rank  (r ?r)(f ?fact))
  (not ( Rank (r ?r1 &: (> ?r1 ?r))))
=>
  (-- ?n)
  (modify ?count (n ?n)(facts (list $?facts ?fact)))
  (retract ?rank)
)
(defrule showBest
  ?count <- (Count (n 0)(facts $?facts))
=>
  (retract ?count)
  (foreach ?f $?facts
     (printout t (fact-slot-value ?f id) crlf)
  )
)

; top 3
(assert (Count (n 3)))
(run)

Notice that some Rank facts remain, but these can be easily retracted.

Kind regards
Wolfgang


Jan Willem Lokin wrote:

Hi,

Consider the following situation. I have asserted quite a number of
facts, with various values for their slots. Now I want to select a
limited number of facts that are most suitable, according to some
user-defined criterion.

My idea was to set up a defquery and then use Java sorting to order
the QuertResult according to my criterion. However, there seems to be
no way to transform a QueyResult into a Collection.

Note that my problems would be simple if I had fixed limits on my slot
values, but I haven't: if i woudl have slot values of 2, 4 and 6 and I
would like to have the smallest two, a limit of 5 would do it, but
this would not work when the values were 13, 15 and 29.

My suspicion is that I am trying to solve the problem in the wrong
manner, or even trying to solve the wrong problem. Any suggestions are
appreciated.

Regards,

JW Lokin

--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------


--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------

Reply via email to