[rules-users] Drools function not working with Javassist facts
Wolfgang Laun
wolfgang.laun at gmail.com
Fri May 18 05:37:01 EDT 2012
Set-up of the classpath for compile and execute:
ADDON=javassist/javassist-3.16.1-GA/javassist.jar
root=/extra/drools-distribution-5.3.0.Final/binaries
tag=5.3.0.Final
export CLASSPATH=.:$root/drools-core-${tag}.jar:$root/knowledge-api-${tag}.jar:$root/drools-compiler-${tag}.jar:$root/antlr-2.7.7.jar:$root/antlr-3.3.jar:$root/antlr-runtime-3.3.jar:$root/mvel2-2.1.0.drools7.jar:$root/ecj-3.5.1.jar:/extra/quartz-1.8.3/quartz-1.8.3.jar:$root/drools-decisiontables-${tag}.jar:$root/drools-templates-${tag}.jar:$root/xstream-1.4.1.jar:$root/xmlpull-1.1.3.1.jar:$ADDON
// === dynamic/Main.java ================
package dynamic;
import java.io.IOException;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.conf.EventProcessingOption;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import dynamic.FactGenerator;
public class Main {
void myCode () throws Exception {
System.out.println ("myCode()");
final Object obj = new FactGenerator ().getFact ();
final KnowledgeBase knowledgeBase = createKnowledgeBase ();
final StatefulKnowledgeSession session =
knowledgeBase.newStatefulKnowledgeSession ();
try {
session.insert (obj);
System.out.println ("Firing rule...");
final int count = session.fireAllRules ();
System.out.println ("rules count: " + count);
} finally {
session.dispose ();
}
}
private static KnowledgeBase createKnowledgeBase () {
System.out.println ("DroolsInvokerServlet.createKnowledgeBase()");
final KnowledgeBaseConfiguration config =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration ();
config.setOption (EventProcessingOption.STREAM);
final KnowledgeBuilder builder =
KnowledgeBuilderFactory.newKnowledgeBuilder ();
builder.add (ResourceFactory.newClassPathResource
("dynamic/testRule.drl"), ResourceType.DRL);
if (builder.hasErrors ()) {
throw new RuntimeException (builder.getErrors ().toString ());
}
final KnowledgeBase knowledgeBase =
KnowledgeBaseFactory.newKnowledgeBase (config);
knowledgeBase.addKnowledgePackages (builder.getKnowledgePackages ());
return knowledgeBase;
}
public static void main (final String[] args) throws Exception {
Main m = new Main();
m.myCode ();
}
}
// === FactGenerator.java ====================
package dynamic;
import java.io.*;
import java.lang.reflect.Modifier;
import javassist.CannotCompileException;
import javassist.NotFoundException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
public class FactGenerator {
static Class msgClass;
public Object getFact () throws IOException, NotFoundException,
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);
msgCtClass.writeFile();
msgClass = msgCtClass.toClass ();
return msgClass.newInstance ();
}
}
}
import drools.dynamic.Message;
function void logMessage() {
}
// === testRule.drl =====================
rule "Hello World"
## dialect "mvel"
when
$m : Message( count == 0 )
then
System.out.println("count: "+$m.getCount());
logMessage();
retract($m);
end
On 18/05/2012, Ayush <ayush.vatsyayan at alcatel-lucent.com> wrote:
> Thanks Laune, I tried executing it after adding msgCtClass.writeFile();
> but
> it's still throwing the same exception.
>
> If you could please share the version of lib files you are using along with
> the file with the changes?
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/Drools-function-not-working-with-Javassist-facts-tp3996584p4000536.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
More information about the rules-users
mailing list