Search in sources :

Example 1 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class ReteEvaluatorTest method testPropertyReactivity.

@Test
public void testPropertyReactivity() {
    String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + "  $s : String()\n" + "  $p : Person(name == $s)\n" + "then\n" + "  modify($p) { setAge($p.getAge()+1) }\n" + "end";
    ReteEvaluator reteEvaluator = new StatefulKnowledgeSessionImpl(1L, getKBase(str));
    Person me = new Person("Mario", 40);
    reteEvaluator.insert("Mario");
    reteEvaluator.insert(me);
    assertEquals(1, reteEvaluator.fireAllRules());
    assertEquals(41, me.getAge());
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 2 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class NotNodeLeftTuple method getAccumulatedObjects.

@Override
public Collection<Object> getAccumulatedObjects() {
    if (NodeTypeEnums.ExistsNode != getTupleSink().getType()) {
        return Collections.emptyList();
    }
    BetaNode betaNode = ((BetaNode) getTupleSink());
    BetaConstraints constraints = betaNode.getRawConstraints();
    ReteEvaluator reteEvaluator = getFactHandle().getReteEvaluator();
    BetaMemory bm = (BetaMemory) reteEvaluator.getNodeMemory((MemoryFactory) getTupleSink());
    TupleMemory rtm = bm.getRightTupleMemory();
    FastIterator it = betaNode.getRightIterator(rtm);
    ContextEntry[] contextEntry = bm.getContext();
    constraints.updateFromTuple(contextEntry, reteEvaluator, this);
    Collection<Object> result = new ArrayList<>();
    for (RightTuple rightTuple = betaNode.getFirstRightTuple(this, rtm, it); rightTuple != null; ) {
        RightTuple nextRight = (RightTuple) it.next(rightTuple);
        if (!(rightTuple instanceof SubnetworkTuple)) {
            InternalFactHandle fh = rightTuple.getFactHandleForEvaluation();
            if (constraints.isAllowedCachedLeft(contextEntry, fh)) {
                result.add(fh.getObject());
            }
        }
        rightTuple = nextRight;
    }
    return result;
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) BetaConstraints(org.drools.core.common.BetaConstraints) ArrayList(java.util.ArrayList) MemoryFactory(org.drools.core.common.MemoryFactory) ContextEntry(org.drools.core.rule.ContextEntry) FastIterator(org.drools.core.util.FastIterator) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 3 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class CrossProductTest method setUp.

@Before
public void setUp() throws Exception {
    final ObjectType list1ObjectType = new ClassObjectType(String.class);
    final ObjectType list2ObjectType = new ClassObjectType(String.class);
    final RuleImpl rule = new RuleImpl("rule-1");
    final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
    final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
    rule.addPattern(list1Pattern);
    rule.addPattern(list2Pattern);
    final Declaration s1Declaration = rule.getDeclaration("s1");
    final Declaration s2Declaration = rule.getDeclaration("s2");
    this.values = new ArrayList();
    rule.setConsequence(new Consequence() {

        private static final long serialVersionUID = 510l;

        public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
            final String s1 = (String) knowledgeHelper.get(s1Declaration);
            final String s2 = (String) knowledgeHelper.get(s2Declaration);
            CrossProductTest.this.values.add(new String[] { s1, s2 });
        }

        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }

        public void writeExternal(ObjectOutput out) throws IOException {
        }

        public String getName() {
            return "default";
        }
    });
    this.pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools");
    this.pkg.addRule(rule);
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) IOException(java.io.IOException) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) Before(org.junit.Before)

Example 4 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class ReturnValueGenerator method generate.

public static void generate(final ReturnValueStub 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(ReturnValueExpression.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, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(FieldValue.class, Object.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, 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);
                // workingMemory
                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 returnValueMethodDescr = new StringBuilder("(");
            for (int i = 0; i < previousDeclarations.length; i++) {
                // previousDeclarations[i]
                load(previousDeclarationsParamsPos[i]);
                returnValueMethodDescr.append(typeDescr(previousDeclarations[i].getTypeName()));
            }
            for (int i = 0; i < localDeclarations.length; i++) {
                // localDeclarations[i]
                load(localDeclarationsParamsPos[i]);
                returnValueMethodDescr.append(typeDescr(localDeclarations[i].getTypeName()));
            }
            // @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
            parseGlobals(globals, globalTypes, 5, returnValueMethodDescr);
            returnValueMethodDescr.append(")Lorg/drools/core/spi/FieldValue;");
            mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), returnValueMethodDescr.toString());
            mv.visitInsn(ARETURN);
        }
    });
    stub.setReturnValue(generator.<ReturnValueExpression>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) FieldValue(org.drools.core.spi.FieldValue) 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 5 with ReteEvaluator

use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.

the class PhreakTimerNode method scheduleLeftTuple.

private void scheduleLeftTuple(final TimerNode timerNode, final TimerNodeMemory tm, final PathMemory pmem, final SegmentMemory smem, final LeftTupleSink sink, final ActivationsManager activationsManager, final Timer timer, final TimerService timerService, final long timestamp, final String[] calendarNames, final Calendars calendars, final LeftTuple leftTuple, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
    ReteEvaluator reteEvaluator = activationsManager.getReteEvaluator();
    if (leftTuple.getPropagationContext().getReaderContext() == null) {
        final Trigger trigger = createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
        // regular propagation
        scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, trigger);
    } else {
        // de-serializing, so we need to correlate timers before scheduling them
        Scheduler scheduler = new Scheduler() {

            @Override
            public void schedule(Trigger t) {
                scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, t);
                evaluate(pmem, activationsManager, sink, tm, trgLeftTuples);
            }

            @Override
            public Trigger getTrigger() {
                return createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
            }
        };
        TupleKey key = TupleKey.createTupleKey(leftTuple);
        leftTuple.getPropagationContext().getReaderContext().addTimerNodeScheduler(timerNode.getId(), key, scheduler);
        leftTuple.setContextObject(key);
    }
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Trigger(org.drools.core.time.Trigger) TupleKey(org.drools.core.marshalling.TupleKey)

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