Search in sources :

Example 16 with ReteEvaluator

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));
    }
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) PropagationContext(org.drools.core.spi.PropagationContext) ReactiveFromNode(org.drools.core.reteoo.ReactiveFromNode) LeftTupleSinkNode(org.drools.core.reteoo.LeftTupleSinkNode) ReactiveFromNodeLeftTuple(org.drools.core.reteoo.ReactiveFromNodeLeftTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) PhreakFromNode.deleteChildLeftTuple(org.drools.core.phreak.PhreakFromNode.deleteChildLeftTuple) Tuple(org.drools.core.spi.Tuple) ReactiveFromNodeLeftTuple(org.drools.core.reteoo.ReactiveFromNodeLeftTuple)

Example 17 with ReteEvaluator

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());
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) DeclarationMatcher(org.drools.mvel.asm.GeneratorHelper.DeclarationMatcher) LeftTuple(org.drools.core.reteoo.LeftTuple) MethodVisitor(org.mvel2.asm.MethodVisitor) GeneratorHelper.createInvokerClassGenerator(org.drools.mvel.asm.GeneratorHelper.createInvokerClassGenerator) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple) GeneratorHelper.matchDeclarationsToTuple(org.drools.mvel.asm.GeneratorHelper.matchDeclarationsToTuple)

Example 18 with ReteEvaluator

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());
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) DeclarationMatcher(org.drools.mvel.asm.GeneratorHelper.DeclarationMatcher) LeftTuple(org.drools.core.reteoo.LeftTuple) MethodVisitor(org.mvel2.asm.MethodVisitor) EvalExpression(org.drools.core.spi.EvalExpression) GeneratorHelper.createInvokerClassGenerator(org.drools.mvel.asm.GeneratorHelper.createInvokerClassGenerator) InternalFactHandle(org.drools.core.common.InternalFactHandle) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple) GeneratorHelper.matchDeclarationsToTuple(org.drools.mvel.asm.GeneratorHelper.matchDeclarationsToTuple)

Example 19 with ReteEvaluator

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();
    }
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 20 with ReteEvaluator

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);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) IOException(java.io.IOException) IOException(java.io.IOException) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) ObjectInput(java.io.ObjectInput) Before(org.junit.Before)

Aggregations

ReteEvaluator (org.drools.core.common.ReteEvaluator)22 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)9 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)8 InternalFactHandle (org.drools.core.common.InternalFactHandle)7 Consequence (org.drools.core.spi.Consequence)7 Tuple (org.drools.core.spi.Tuple)7 Test (org.junit.Test)7 ObjectInput (java.io.ObjectInput)5 ObjectOutput (java.io.ObjectOutput)5 ClassObjectType (org.drools.core.base.ClassObjectType)5 LeftTuple (org.drools.core.reteoo.LeftTuple)5 Pattern (org.drools.core.rule.Pattern)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)4 DeclarationMatcher (org.drools.mvel.asm.GeneratorHelper.DeclarationMatcher)4 GeneratorHelper.createInvokerClassGenerator (org.drools.mvel.asm.GeneratorHelper.createInvokerClassGenerator)4 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)3 RuleTerminalNodeLeftTuple (org.drools.core.reteoo.RuleTerminalNodeLeftTuple)3 Declaration (org.drools.core.rule.Declaration)3