Jess Information

Jess Home
Jess 7 Features
Download Now!
Online Demo

Documentation
FAQ
Manual
Mailing List
Jess Wiki

More information Related Web Sites
User Contributions
JSR94 Info
Developer's Log
About This Site

JESS ®, the Rule Engine for the JavaTM Platform

Jess Wiki: Conflict Resolution Strategy 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;
import java.io.Serializable;

public class RuleNameLength implements Strategy, Serializable
{
  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.

-- SteveSolomon


Front Page | Sandbox | Recent Changes | Powered by Friki | Last Edited: 29 January 2007