[rules-users] Drools function not working with Javassist facts
Ayush
ayush.vatsyayan at alcatel-lucent.com
Wed May 16 08:31:51 EDT 2012
Laune,
Thank you a lot for your reply. Here I go explaining the code.
*I'm generating dynamic java objects. In this the class file is made once
and on each request we make a new instance and insert it in drools session*
public Object getFact ()
throws CannotCompileException, InstantiationException,
IllegalAccessException {
if (msgClass != null) {
return msgClass.newInstance ();
} else {
final ClassPool pool = ClassPool.getDefault ();
CtClass msgCtClass = null;
System.out.println ("creating new class...");
msgCtClass = pool.makeClass ("drools.dynamic.Message");
final CtField field = new CtField (CtClass.intType, "Count",
msgCtClass);
field.setModifiers (Modifier.PRIVATE);
msgCtClass.addField (field);
final CtMethod getter = CtNewMethod.getter ("getCount", field);
msgCtClass.addMethod (getter);
final CtMethod setter = CtNewMethod.getter ("setCount", field);
msgCtClass.addMethod (setter);
msgClass = msgCtClass.toClass ();
return msgClass.newInstance ();
}
}
*After getting the object I create drools knowledgebase and session*
final Object obj = new FactGenerator ().getFact ();
final KnowledgeBase knowledgeBase = createKnowledgeBase ();
final StatefulKnowledgeSession session =
knowledgeBase.newStatefulKnowledgeSession ();
try {
// final Message msg = new Message ();
// msg.setMessage ("Test message");
// msg.setStatus (0);
// session.insert (msg);
session.insert (obj);
System.out.println ("Firing rule...");
final int count = session.fireAllRules ();
System.out.println ("rules count: " + count);
} finally {
session.dispose ();
}
*Here is the drl file I'm using *
import drools.dynamic.Message;
rule "Hello World"
dialect "mvel"
when
$m : Message()
then
//System.out.println("count: "+$m.getCount());
System.out.println("count: -999");
//logMessage();
//System.out.println("Message: "+message);
retract($m);
end
function void logMessage() {
}
-----------------------------------
The above rule works perfect when I'm running it without the function
logMessage().
*Below are the jar files I'm using in project *
- antlr-2.7.7.jar
- antlr-3.3.jar
- antlr-runtime-3.3.jar
- drools-clips-5.3.0.Final.jar
- drools-compiler-5.3.1.Final.jar
- drools-core-5.3.0.Final.jar
- drools-transformer-jxls-5.1.0.M2.jar
- knowledge-api-5.3.0.Final.jar
- mvel2-2.1.0.drools14.jar
--
View this message in context: http://drools.46999.n3.nabble.com/Drools-function-not-working-with-Javassist-facts-tp3996584p3996964.html
Sent from the Drools: User forum mailing list archive at Nabble.com.
More information about the rules-users
mailing list