org.cfpm.caves.za
Class SortValues

java.lang.Object
  extended by org.cfpm.caves.za.SortValues
All Implemented Interfaces:
jess.Userfunction

public class SortValues
extends java.lang.Object
implements jess.Userfunction

This class implements a user function in Jess. The function "sort-values" is used to sort a list of endorsement values in either descending or ascending order to allow the agent calling this function to pick the best / worst out of a group of endorsed agents.

Author:
Ruth

Constructor Summary
SortValues()
           
 
Method Summary
 jess.Value call(jess.ValueVector vv, jess.Context context)
          The first argument of the call is a list of numerical values, e.g.
 java.lang.String getName()
          returns the name of the function in Jess
(package private) static void merge(java.lang.Double[] values, int lo, int m, int hi)
          internal function: the merge step in Mergesort
static void mergesort(java.lang.Double[] values, int lo, int hi)
          internal function: recursive mergesort
private  void print(java.lang.Double[] list)
          internal helper function for debugging: prints the given list of doubles
private  void print(int[] list)
          internal helper function for debugging: prints the given list of integers
private  void print(jess.ValueVector list)
          internal helper function for debugging: prints the given list of Values
private  int[] sort(java.lang.Double[] values)
          internal function: sorts the list of given values and returns the sorted list of indices.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SortValues

public SortValues()
Method Detail

getName

public java.lang.String getName()
returns the name of the function in Jess

Specified by:
getName in interface jess.Userfunction

call

public jess.Value call(jess.ValueVector vv,
                       jess.Context context)
                throws jess.JessException
The first argument of the call is a list of numerical values, e.g. endorsement values, to be sorted, the second determines if the list should be sorted in descending order (TRUE) or ascending order (FALSE). The function returns the sorted list in the form of the original one-based INDICES. These indices can then be used to access the original list or any other associated list in the sorted order via Jess' nth$ function. Example: Let (5 4 5 6 3 0 7) be the list of (endorsement) values to be sorted in descending order. Calling sort-values will return the following list: (7 4 1 3 2 5 6), meaning that element #7 (7) in the endorsement value list is the largest, element #4 (6) is the second-largest, etc.

Specified by:
call in interface jess.Userfunction
Parameters:
vv - -- the ValueVector containing the header and parameters
context - -- the context of the Rete engine
Throws:
jess.JessException
See Also:
Userfunction.call(jess.ValueVector, jess.Context)

sort

private int[] sort(java.lang.Double[] values)
internal function: sorts the list of given values and returns the sorted list of indices.


mergesort

public static void mergesort(java.lang.Double[] values,
                             int lo,
                             int hi)
internal function: recursive mergesort


merge

static void merge(java.lang.Double[] values,
                  int lo,
                  int m,
                  int hi)
internal function: the merge step in Mergesort


print

private void print(java.lang.Double[] list)
internal helper function for debugging: prints the given list of doubles


print

private void print(int[] list)
internal helper function for debugging: prints the given list of integers


print

private void print(jess.ValueVector list)
            throws jess.JessException
internal helper function for debugging: prints the given list of Values

Throws:
jess.JessException