Hi Wolfgang or anyone else?
Would you mind taking a look at this? I rewrote the loop using the
Java API - but I can't execute my method now because I can't use the
object as its type. Once inserted into the Rete engine its type is
"widened" to a "Fact" and I get the message:
Jess reported an error in routine call
while executing (call ?rand extractRoots)
while executing deffunction do-extract
while executing (do-extract ?a)
while executing defrule MAIN::modulus223.
Message: No method named 'extractRoots' found in class jess.Fact.
<--- Here is the problem
I'm trying to execute the "extractRoots" method in the on the
RandomNumber class (via the deffunction "do-extract") Consequent via a
function call...
Code is below...
Thanks,
David
===================
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
import jess.Context;
import jess.Fact;
import jess.JessException;
import jess.Rete;
import jess.Value;
/**
* By default, creates 10,000 rules each matching a single prime number -
* then inserts 100 random integers into the engine and collects those
* rules which have fired, and all their roots outputting the timing
* for each stage and each calculation.
*
* See the main() method (at the bottom) for program flow.
*
* @author David Ray
*/
public class PrimeFactorsTest
{
private long seed;
private long start;
private long total;
private int numRules;
private int[] primes;
private int[] randomNumbers;
private Rete rete;
private static long[] timepoint = new long[2];
PrimeFactorsTest(int seed)
{
this.seed = seed;
this.rete = new Rete();
}
/**
* Generates "numFacts" number of random numbers.
*
* @param numFacts
*/
public void createRandomNums(int numFacts)
{
System.out.println("Generating random number knowledge...");
Random rand = new Random(seed);
randomNumbers = new int[numFacts];
start = System.currentTimeMillis();
for(int i = 0;i < numFacts;i++) {
int nextRand = rand.nextInt(primes[numRules - 1]) + 1;
if(nextRand < 1) {--i; continue;}
randomNumbers[i] = nextRand;
}
total = (System.currentTimeMillis() - start);
System.out.println("Generated "+numFacts+" random numbers in
["+total+" msecs.]");
}
/**
* Inserts a "Jess" formatted command into the Jess Rete engine.
*
* @param statement
* @return Value A jess.Value object representing the result
obtained
* from executing the specified
statement.
*/
public Value insertStatement(String statement)
{
Value retVal = null;
try {
retVal = rete.eval(statement);
}catch(JessException e) {
e.printStackTrace();
}
return retVal;
}
/**
* Inserts 10,000 rules into the working memory - each representing
* a "pattern" which will only activate if the LHS is true (meaning
* that the unique prime number correlated with each rule is a prime
* root of the asserted random number).
*/
public void prepareConstructs()
{
/////////////////////////////////////////////
//Used for debugging
//String statement = "(watch all)";
//insertStatement(statement);
System.out.println("-----------------------");
System.out.println("Creating New RuleBase...");
start = System.currentTimeMillis();
String statement = "(deftemplate RandomNumber (declare
(from-class
RandomNumber)))";
insertStatement(statement);
statement = "(deffunction do-extract (?rand) "+
"(?rand extractRoots))";
insertStatement(statement);
for(int divisor : primes)
{
statement = "(defrule modulus"+divisor+
" ?a <- (RandomNumber (value ?x))" +
" (test (eq 0 (mod ?x "+divisor+")))"+
"=>"+
"(do-extract ?a))";
insertStatement(statement);
}
total = (System.currentTimeMillis() - start) / 1000;
System.out.println("Created new RuleBase with "+numRules+" rules
in ["+total+" secs.]");
}
public void execute()
{
System.out.println("-----------------------");
System.out.println("Starting test...");
total = 0;
stopwatch(0);
try
{
for(int randNum : randomNumbers)
{
stopwatch( 1 );
RandomNumber rand = new RandomNumber(randNum);
Value val = rete.add(rand);
Context context = rete.getGlobalContext();
Fact fact = val.factValue(context);
rete.run();
rete.retract(fact);
System.out.println(rand.toString()+" : "+stopwatch(1));
}
}
catch(JessException je)
{
je.printStackTrace();
}
System.out.println("Total Engine Time: " + stopwatch(0));
}
/**
* Reads a file containing "numRules" number of prime numbers into
* an array which is later used to generate a unique rule for each
* prime number.
*
* @param numRules
*/
public void loadPrimeList(int numRules)
{
this.numRules = numRules;
BufferedReader buf = null;
try {
buf = new BufferedReader(
new InputStreamReader(
getClass().getClassLoader().getResourceAsStream("1100000-primes.txt")));
String line = null;
int count = 0;
System.out.println("Reading "+numRules+" Prime Numbers");
start = System.currentTimeMillis();
primes = new int[numRules];
while((line = buf.readLine()) != null && ((count) <
numRules)) {
primes[count++] = Integer.parseInt(line.trim());
}
total = (System.currentTimeMillis() - start);
System.out.println("Read "+primes.length+" Prime Numbers
["+total+" msecs.]");
}
catch(Exception e) { e.printStackTrace(); }
finally {
try { buf.close(); }catch(Exception ignore) {}
}
}
/**
* Returns and records time points specified by the index "i"
* passed in.
*
* @param i
* @return message A formatted String containing the specified
time point.
*/
private static String stopwatch(int i)
{
long now = System.currentTimeMillis( );
String message = " [" + ( now - timepoint[i] ) + " msecs]";
timepoint[i] = now;
return message;
}
public static void main(String[] args)
{
int seed = 0;
int numRules = 10000;
int numFacts = 100;
if(args != null && args.length == 3) {
seed = Integer.parseInt(args[2]);
numRules = Integer.parseInt(args[0]);
numFacts = Integer.parseInt(args[1]);
}
PrimeFactorsTest pft = new PrimeFactorsTest(seed);
System.out.println("Number of Rules: "+numRules);
System.out.println("Number of Facts: "+numFacts);
System.out.println("Random Seed: "+seed);
pft.loadPrimeList(numRules);
pft.createRandomNums(numFacts);
pft.prepareConstructs();
pft.execute();
}
}
--------------------------------------------------------------------
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].
--------------------------------------------------------------------