Maybe this is a workaround:
Assuming that the rules file you reload contains all deftemplates
referenced by these rules (or, in other words, if this rules file can be
batched into a virgin rete engine), then you could batch the file with
the updated rules into an auxiliary Rete, iterate over all rules in the
auxiliary and add them to the working Rete.
Below is a Userfunction (load-rules <pathname>) doing just that.
kr
Wolfgang
package at.laune.jess;
import java.util.Iterator;
import jess.Context;
import jess.HasLHS;
import jess.JessException;
import jess.Rete;
import jess.RU;
import jess.Userfunction;
import jess.Value;
import jess.ValueVector;
/**
* A Jess Userfunction class (re-)loading rules from a Jess file.
*
* (load-rules <pathname>)
*
* @author Wolfgang Laun
*/
public class LoadRules implements Userfunction {
private static final String NAME = "load-rules";
/** Get the name of the Jess function.
* @see jess.Userfunction#getName()
*/
public String getName(){
return NAME;
}
/** Call the userfunction
* @see jess.Userfunction#call(jess.ValueVector, jess.Context)
*/
public Value call( ValueVector vv, Context context ) throws
JessException {
int narg = vv.size() - 1;
// Check that we have no element.
if( narg != 1 )
throw new JessException( NAME,
"missing argument:", "pathname");
Rete theRete = context.getEngine();
String path = vv.get( 1 ).stringValue( context );
Rete aux = new Rete();
aux.batch( path );
// Iterate over all rules and queries.
int nRules = 0;
Iterator ruleIt = aux.listDefrules();
while( ruleIt.hasNext() ){
HasLHS rq = (HasLHS)ruleIt.next();
theRete.addDefrule( rq );
nRules++;
}
return new Value( nRules , RU.INTEGER );
}
}
Greenblatt, Howard wrote:
Hi there,
In our system we sometimes need to reload a rules file that may
contain deftemplates. This could happen for example if a rule changes
and needs to be redefined in the system. We reload the rules file with
the updated rule. Since deftemplates cannot be redefined (without
clearing out the rules engine first), we don't allow them to be
modified; however they still may be in the file.
I have seen the jess src code (jess70p1 - Deftemplate.java - line 155
"equals" method) where you check to see if a new deftemplate object
matches an existing one of the same name. If they are identical there
is no error. This works fine except where the deftemplate extends
another deftemplate. It appears that the parent id of the extended
deftemplate is different even though the parent template is identical.
This error can be duplicated in a single script. Consider the
following:
(deftemplate t1
(slot s1))
(deftemplate t2 extends t1
(slot s2))
;
; redefining t1 is no problem
;
(deftemplate t1
(slot s1))
;
; redefining t2 causes an error where you cannot redefine
; the template even though it is exactly the same.
;
(deftemplate t2 extends t1
(slot s2))
I believe what is happening is that when t1 is redefined and added to
the m_deftemplates TreeMap (Defmodule,java - line 125 "addDeftemplate"
method) it is assigned a new id which does not match the id of t1 (as
a parent) when template t2 is redefined.
Thanks,
- Howard
--------------------------------------------------------------------
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]
--------------------------------------------------------------------