Hi,

How exactly is the compare(Activation a1, Activation a2) method of the
Strategy interface to be written? The documentation is a little vague.
It says it should behave like Comparable.compare(). I assume this is a
typo for Comparator.compare(). (There is no Comparable.compare().
There's a Comparable.compareTo(), but it takes a single argument.)

Is compare(Activation a1, Activation a2) supposed to return a negative
int, 0, or a positive int if a1 is ordered less than a2, equal to a2, or
greater than a2 in the agenda?

Below is a Strategy class I wrote, but doesn't work. Perhaps Dr.
Friedman-Hill or other knowledgeable person could point out my mistake:
--------------------------------------------------------------

public class RecencyThenSalience implements Strategy
{
  private static final String name = "RecencyThenSalience";
  
  public RecencyThenSalience()
  {
  }
  
  public int compare(Activation act1, Activation act2) 
  {
    int result = 0;
        
    Token token1 = act1.getToken();
    Token token2 = act2.getToken();
    
    if(token1.size() < token2.size()) 
    {
      result = -1;
    }
    else if(token1.size() > token2.size()) 
    {
      result = 1;
    }
    else 
    {
      ArrayList<Integer> act1_times = new ArrayList<Integer>();
      ArrayList<Integer> act2_times = new ArrayList<Integer>();
  
      for(int i = 0; i < token1.size(); i++)
      {
        act1_times.add(new Integer(token1.fact(i).getTime()));
      }
      for(int i = 0; i < token2.size(); i++)
      {
        act2_times.add(new Integer(token2.fact(i).getTime()));
      }
      // sort the pseudo-times in descending order
      Collections.sort(act1_times, Collections.reverseOrder());
      Collections.sort(act2_times, Collections.reverseOrder());
      Iterator<Integer> i = act1_times.iterator();
      Iterator<Integer> j = act2_times.iterator();
      while(i.hasNext()) 
      {
        int time1 = i.next().intValue();
        int time2 = j.next().intValue();
        if(time1 < time2) 
        {
          result = -1;
          break;
        }
        else if(time1 > time2) 
        {
          result = 1;
          break;
        }
      }
      if(result == 0) result = (act1.getSalience() <
act2.getSalience())? -1 : 1;
    }
   
    return result;
  }
  
  public String getName() 
  {
    return name;
  }
}

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

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

(deftemplate task (slot goal) (multislot args))

(deffacts init
  (stapler ritchie)
  (stapler big-guy)
  (stapler ralphy)
  (stapler tony-b)
  (stapler jackie-jr)
  (hole-punch livia)
  (hole-punch artie)
  (hole-punch ritchie)
  (hole-punch tony-b)
  (task (goal start)))

(defrule start
  (task (goal start))
  =>
  (assert (task (goal stapler-owners)))
  (assert (task (goal hole-punch-owners))))

(defrule hole-punch-and-stapler-owners
  (task (goal hole-punch-owners))
  (task (goal stapler-owners))
  (stapler ?name)
  (hole-punch ?name)
  =>
  (printout t ?name " owns both a hole-punch and a stapler." crlf))

(defrule hole-punch-owners
  (task (goal hole-punch-owners))
  (hole-punch ?name)
  (not (stapler ?name))
  =>
  (printout t ?name " owns just a hole-punch." crlf))

(defrule stapler-owners
  (task (goal stapler-owners))
  (stapler ?name)
  (not (hole-punch ?name))
  =>
  (printout t ?name " owns just a stapler." crlf))

(defrule gc-task
  ?t <- (task)
  =>
  (retract ?t))

-------------------------------------------------------------
Here is the error I get from the console:

Jess> (clear)
TRUE
Jess> (batch "C:/steve/jess/stapler.clp")
TRUE
Jess> (reset)
Jess reported an error in routine reset
        while executing (reset).
  Message: Error during execution.
  Program text: ( reset )  at line 75.

Nested exception is:
null


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