Hi Folks,

Working on my Jess book. You may recall that Part 1 is a Jess
reference of sorts, while the subsequent half-dozen parts are large
tutorial examples. Thought you'd be interested to hear about the
synergy between the writing and Jess development. As I write the
various tutorials I sometimes see practical areas in which working
with Jess could be easier.

In one of the early tutorials, I had the need to produce some sorted
output. Since the amount of data is fairly small, I used the classic
solution: 

(deffacts numbers
  (number 1)
  (number 2)
  ...
  (number 100))

(defrule largest-1
  (number ?x)
  (not (number ?y&:(> ?y ?x)))
  =>
  (printout t "largest is " ?x crlf))

This is easy to understand. The Jess pattern-matcher finds the single
case where this rule can be activated (the case where no number y is
larger than x.)  Unfortunately this rule performs like n^2 in the
number of facts, since it has to compare every fact to every other
fact. Bad sorting algorithms like bubble sort are n^2; good ones are
more like n ln(n). Is there a way to write a Jess program that does
this sorting but scales as n ln(n)?

I came up with this idea:

(defglobal ?*l2s* = 1)

(defrule largest-2
  (declare (salience ?*l2ss*))
  (number ?x)
  (test (progn (bind ?*l2s* ?x) TRUE))
  =>
  (printout t "largest is " ?x crlf)
  (halt))

(set-salience-evaluation when-activated)

This is trickier. The idea is that the rule will be activated once for
every (number) fact, but the salience will be proportional to the sort
key, so the rule with the highest salience will match the highest
number, fire, and terminate the program. Because the agenda is
implemented using a priority queue, putting all the activations on the
agenda will be an n ln(n) operation!

The only problem is that, unfortunately, this doesn't work. In current
versions of Jess, salience is a property of the defrule, not of the
activation, so if a rule is activated twice, both activations always
have the same salience. This is clearly not right, anyway, and it is
an easy fix to make individual activations carry their own salience
values. For the majority of cases, there will be no observed
alteration of behavior with this change. But in the present case, it
makes largest-2 work. largest-1 takes almost eight seconds to sort
2000 (number) facts on my machine, while largest-2 takes a small
fraction of a second. The next Jess release will include this change.


---------------------------------------------------------
Ernest Friedman-Hill  
Distributed Systems Research        Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
Org. 8920, MS 9012                  [EMAIL PROTECTED]
PO Box 969                  http://herzberg.ca.sandia.gov
Livermore, CA 94550

--------------------------------------------------------------------
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