OK, after tinkering with writing conflict resolution strategies for
about a day, I have figured it out.

What follows is a very simple example.

Here is the Strategy class. It orders activations based on the length of
the rule names of activations. The longer the rule name, the earlier in
the agenda it is. (In practice, you would never use such a strategy).

The important thing to note is that the return value of
compare(Activation act1, Activation act2) should be -1, 0, or 1 iff act1
is ordered before, the same, or after act2 in the agenda. Only the sign
of the return value matters.

-----------------------------------------------------

package elect.jess.support;
import jess.Activation;
import jess.Strategy;

public class RuleNameLength implements Strategy
{
  private static final String name = "RuleNameLength";

  public RuleNameLength()
  {
  }
  
  public int compare(Activation act1, Activation act2) 
  {
    int result;
    String name1 = act1.getRule().getName();
    String name2 = act2.getRule().getName();
    
    if(name1.length() < name2.length()) 
    {
      result = 1;
    }
    else if(name1.length() > name2.length()) 
    {
      result = -1;
    }
    else 
    {
      result = 0;
    }
    return result;
  }

  public String getName() 
  {
    return name;
  }
}

-----------------------------------------------------

Here is a sample Jess file that uses the above Strategy:

-----------------------------------------------------

(set-strategy elect.jess.support.RuleNameLength)

(deftemplate car (slot owner) (slot make) (slot model) (slot year) (slot
color) (slot body-type))

(deffacts init
  (car (owner steve) (make volvo) (model 850) (year 1994) (color blue)
(body-type wagon)))

(defrule car-make-model-year-color-body-type
  (car (owner ?name) (make ?make) (model ?model) (year ?year) (color
?color) (body-type ?body-type))
  =>
  (printout t ?name " drives a " ?color " " ?year " " ?make " " ?model "
" ?body-type "." crlf))

(defrule car-make-model-body-type
  (car (owner ?name) (make ?make) (model ?model) (body-type ?body-type))
  =>
  (printout t ?name " drives a " ?make " " ?model " " ?body-type "."
crlf))

(defrule car-make-model-year
  (car (owner ?name) (make ?make) (model ?model) (year ?year))
  =>
  (printout t ?name " drives a " ?year " " ?make " " ?model "." crlf))

(defrule car-make-color
  (car (owner ?name) (make ?make) (color ?color))
  =>
  (printout t ?name " drives a " ?color " " ?make "." crlf))

(defrule car-old
  (car (owner ?name) (year ?year&:(< ?year 1996)))
  =>
  (printout t ?name " drives a late-model car." crlf))

(defrule car
  (car (owner ?name))
  =>
  (printout t ?name " drives a car." crlf))

-----------------------------------------------------

When you load and run the above program, the rules fire in order on rule
name length:

Jess> (run)
steve drives a blue 1994 volvo 850 wagon.
steve drives a volvo 850 wagon.
steve drives a 1994 volvo 850.
steve drives a blue volvo.
steve drives a late-model car.
steve drives a car.

Note that using the default depth strategy, each of the rules has the
same agenda order as every other rule. Since the depth strategy only
looks at recency (after salience), each of the six activations has the
same recency as every other activation. If you run the same program with
the depth strategy, the order in which the rules fire is undetermined:

Jess> (run)
steve drives a blue 1994 volvo 850 wagon.
steve drives a late-model car.
steve drives a car.
steve drives a blue volvo.
steve drives a 1994 volvo 850.
steve drives a volvo 850 wagon.

-----------------------------------------------------

Steve Solomon
USC Institute for Creative Technologies


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