use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class ReactiveObjectUtil method notifyModification.
public static void notifyModification(Object object, Collection<Tuple> leftTuples, ModificationType type) {
for (Tuple leftTuple : leftTuples) {
if (!((ReactiveFromNodeLeftTuple) leftTuple).updateModificationState(object, type)) {
continue;
}
PropagationContext propagationContext = leftTuple.getPropagationContext();
ReactiveFromNode node = leftTuple.getTupleSink();
LeftTupleSinkNode sink = node.getSinkPropagator().getFirstLeftTupleSink();
ReteEvaluator reteEvaluator = propagationContext.getFactHandle().getReteEvaluator();
reteEvaluator.addPropagation(new ReactivePropagation(object, (ReactiveFromNodeLeftTuple) leftTuple, propagationContext, node, sink, type));
}
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class PredicateGenerator method generate.
public static void generate(final PredicateStub stub, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final ReteEvaluator reteEvaluator) {
final String[] globals = stub.getGlobals();
final String[] globalTypes = stub.getGlobalTypes();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(previousDeclarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, reteEvaluator).setInterfaces(PredicateExpression.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "createContext", generator.methodDescr(Object.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(Boolean.TYPE, InternalFactHandle.class, Tuple.class, Declaration[].class, Declaration[].class, ReteEvaluator.class, Object.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
objAstorePos = 9;
int[] previousDeclarationsParamsPos = new int[previousDeclarations.length];
mv.visitVarInsn(ALOAD, 1);
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
mv.visitVarInsn(ASTORE, 1);
mv.visitVarInsn(ALOAD, 2);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 7);
Tuple currentTuple = tuple;
for (DeclarationMatcher matcher : declarationMatchers) {
int i = matcher.getMatcherIndex();
previousDeclarationsParamsPos[i] = objAstorePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getTupleIndex(), 7);
mv.visitVarInsn(ALOAD, 3);
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// reteEvaluator
mv.visitVarInsn(ALOAD, 5);
mv.visitVarInsn(ALOAD, 7);
invokeInterface(LeftTuple.class, "getFactHandle", InternalFactHandle.class);
// tuple.getFactHandle().getObject()
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(previousDeclarations[i], previousDeclarations[i].getTypeName());
}
int[] localDeclarationsParamsPos = parseDeclarations(localDeclarations, 4, 2, 5, false);
// @{ruleClassName}.@{methodName}(@foreach{previousDeclarations}, @foreach{localDeclarations}, @foreach{globals})
StringBuilder predicateMethodDescr = new StringBuilder("(");
for (int i = 0; i < previousDeclarations.length; i++) {
// previousDeclarations[i]
load(previousDeclarationsParamsPos[i]);
predicateMethodDescr.append(typeDescr(previousDeclarations[i].getTypeName()));
}
for (int i = 0; i < localDeclarations.length; i++) {
// localDeclarations[i]
load(localDeclarationsParamsPos[i]);
predicateMethodDescr.append(typeDescr(localDeclarations[i].getTypeName()));
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(globals, globalTypes, 5, predicateMethodDescr);
predicateMethodDescr.append(")Z");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), predicateMethodDescr.toString());
mv.visitInsn(IRETURN);
}
});
stub.setPredicate(generator.<PredicateExpression>newInstance());
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class EvalGenerator method generate.
public static void generate(final EvalStub stub, final Tuple tuple, final Declaration[] declarations, final ReteEvaluator reteEvaluator) {
final String[] globals = stub.getGlobals();
final String[] globalTypes = stub.getGlobalTypes();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(declarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, "_" + evalId.getAndIncrement(), reteEvaluator).setInterfaces(EvalExpression.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "createContext", generator.methodDescr(Object.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "clone", generator.methodDescr(EvalExpression.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitVarInsn(ALOAD, 0);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(Boolean.TYPE, Tuple.class, Declaration[].class, ReteEvaluator.class, Object.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
objAstorePos = 7;
String[] expectedDeclarations = stub.getExpectedDeclarationTypes();
int[] declarationsParamsPos = new int[declarations.length];
mv.visitVarInsn(ALOAD, 1);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 5);
Tuple currentTuple = tuple;
for (DeclarationMatcher matcher : declarationMatchers) {
int i = matcher.getMatcherIndex();
declarationsParamsPos[i] = objAstorePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getTupleIndex(), 5);
mv.visitVarInsn(ALOAD, 2);
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// reteEvaluator
mv.visitVarInsn(ALOAD, 3);
mv.visitVarInsn(ALOAD, 5);
invokeInterface(LeftTuple.class, "getFactHandle", InternalFactHandle.class);
// tuple.getFactHandle().getObject()
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(declarations[i], expectedDeclarations[i]);
}
// @{ruleClassName}.@{methodName}(@foreach{declarations}, @foreach{globals})
StringBuilder evalMethodDescr = new StringBuilder("(");
for (int i = 0; i < declarations.length; i++) {
// declarations[i]
load(declarationsParamsPos[i]);
evalMethodDescr.append(typeDescr(expectedDeclarations[i]));
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(globals, globalTypes, 3, evalMethodDescr);
evalMethodDescr.append(")Z");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), evalMethodDescr.toString());
mv.visitInsn(IRETURN);
}
});
stub.setEval(generator.<EvalExpression>newInstance());
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class CepEspTest method testExpireLogicalEvent.
@Test
public void testExpireLogicalEvent() {
final String drl = "package org.drools; " + "declare Foo " + " @role(event) " + " @expires(10ms) " + "end " + "rule In " + "when " + "then " + " insertLogical( new Foo() ); " + "end ";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
ksession.fireAllRules();
((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(1, TimeUnit.SECONDS);
ksession.fireAllRules();
assertEquals(0, ksession.getObjects().size());
assertEquals(0, TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem((ReteEvaluator) ksession).getEqualityKeyMap().size());
} finally {
ksession.dispose();
}
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class RuleBaseEventSupportTest method setUp.
/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
@Before
public void setUp() throws Exception {
kBase = KnowledgeBaseFactory.newKnowledgeBase();
;
listener1 = new TestRuleBaseListener("(listener-1) ");
listener2 = new TestRuleBaseListener("(listener-2) ");
kBase.addEventListener(listener1);
kBase.addEventListener(listener2);
final RuleImpl rule1 = new RuleImpl("test1");
final ClassObjectType cheeseObjectType = new ClassObjectType(Cheese.class);
final Pattern pattern = new Pattern(0, cheeseObjectType);
ClassFieldAccessorStore store = new ClassFieldAccessorStore();
store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
store.setEagerWire(true);
AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "cheddar", useLambdaConstraint);
pattern.addConstraint(constraint);
rule1.addPattern(pattern);
rule1.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
final RuleImpl rule2 = new RuleImpl("test2");
final ClassObjectType cheeseObjectType2 = new ClassObjectType(Cheese.class);
final Pattern pattern2 = new Pattern(0, cheeseObjectType2);
AlphaNodeFieldConstraint constraint2 = ConstraintTestUtil.createCheeseTypeEqualsConstraint(store, "stilton", useLambdaConstraint);
pattern2.addConstraint(constraint2);
rule2.addPattern(pattern2);
rule2.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools.test1");
pkg.addRule(rule1);
pkg.addRule(rule2);
}
Aggregations