Search in sources :

Example 16 with Tuple

use of org.drools.core.spi.Tuple 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 WorkingMemory workingMemory) {
    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, workingMemory).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, WorkingMemory.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.getOriginalIndex();
                previousDeclarationsParamsPos[i] = objAstorePos;
                currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 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 : WorkingMemory(org.drools.core.WorkingMemory) DeclarationMatcher(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.DeclarationMatcher) LeftTuple(org.drools.core.reteoo.LeftTuple) MethodVisitor(org.mvel2.asm.MethodVisitor) GeneratorHelper.createInvokerClassGenerator(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.createInvokerClassGenerator) FieldValue(org.drools.core.spi.FieldValue) InternalFactHandle(org.drools.core.common.InternalFactHandle) GeneratorHelper.matchDeclarationsToTuple(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.matchDeclarationsToTuple) LeftTuple(org.drools.core.reteoo.LeftTuple) Tuple(org.drools.core.spi.Tuple)

Example 17 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class ASMConditionEvaluatorJitter method jitEvaluator.

public static ConditionEvaluator jitEvaluator(String expression, Condition condition, Declaration[] declarations, EvaluatorWrapper[] operators, ClassLoader classLoader, Tuple tuple) {
    ClassGenerator generator = new ClassGenerator(getUniqueClassName(), classLoader).setInterfaces(ConditionEvaluator.class).addStaticField(ACC_PRIVATE | ACC_FINAL, "EXPRESSION", String.class, expression).addField(ACC_PRIVATE | ACC_FINAL, "declarations", Declaration[].class);
    generator.addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(boolean.class, InternalFactHandle.class, InternalWorkingMemory.class, Tuple.class), new EvaluateMethodGenerator(condition, declarations, operators, tuple));
    if (operators.length == 0) {
        generator.addDefaultConstructor(new ClassGenerator.MethodBody() {

            public void body(MethodVisitor mv) {
                putFieldInThisFromRegistry("declarations", Declaration[].class, 1);
                mv.visitInsn(RETURN);
            }
        }, Declaration[].class);
        return generator.newInstance(Declaration[].class, declarations);
    }
    generator.addField(ACC_PRIVATE | ACC_FINAL, "operators", EvaluatorWrapper[].class);
    generator.addDefaultConstructor(new ClassGenerator.MethodBody() {

        public void body(MethodVisitor mv) {
            putFieldInThisFromRegistry("declarations", Declaration[].class, 1);
            putFieldInThisFromRegistry("operators", EvaluatorWrapper[].class, 2);
            mv.visitInsn(RETURN);
        }
    }, Declaration[].class, EvaluatorWrapper[].class);
    return generator.newInstance(Declaration[].class, declarations, EvaluatorWrapper[].class, operators);
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ClassGenerator(org.drools.core.rule.builder.dialect.asm.ClassGenerator) EvaluatorWrapper(org.drools.core.base.EvaluatorWrapper) Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle) Tuple(org.drools.core.spi.Tuple) GeneratorHelper.matchDeclarationsToTuple(org.drools.core.rule.builder.dialect.asm.GeneratorHelper.matchDeclarationsToTuple) MethodVisitor(org.mvel2.asm.MethodVisitor)

Example 18 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class FieldIndexEntryTest method testTwoEntries.

@Test
public void testTwoEntries() {
    final ClassFieldReader extractor = store.getReader(Cheese.class, "type");
    final FieldIndex fieldIndex = new FieldIndex(extractor, null, MvelConstraint.INDEX_EVALUATOR);
    final SingleIndex singleIndex = new SingleIndex(new FieldIndex[] { fieldIndex }, 1);
    Tuple tuple = new RightTupleImpl(new DefaultFactHandle(1, new Cheese("stilton", 10)));
    final TupleList index = new AbstractHashTable.SingleIndexTupleList(singleIndex, tuple, "stilton".hashCode(), false);
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    final Cheese stilton2 = new Cheese("stilton", 59);
    final InternalFactHandle h2 = new DefaultFactHandle(2, stilton2);
    RightTuple h1RightTuple = new RightTupleImpl(h1, null);
    RightTuple h2RightTuple = new RightTupleImpl(h2, null);
    // test add
    index.add(h1RightTuple);
    index.add(h2RightTuple);
    assertEquals(h1, index.getFirst().getFactHandle());
    assertEquals(h2, ((RightTuple) index.getFirst().getNext()).getFactHandle());
    // test get
    assertEquals(h1, index.get(h1).getFactHandle());
    assertEquals(h2, index.get(h2).getFactHandle());
    // test removal for combinations
    // remove first
    index.remove(h2RightTuple);
    assertEquals(h1RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // remove second
    index.add(h2RightTuple);
    index.remove(h1RightTuple);
    assertEquals(h2RightTuple.getFactHandle(), index.getFirst().getFactHandle());
    // check index type does not change, as this fact is removed
    stilton1.setType("cheddar");
}
Also used : TupleList(org.drools.core.util.index.TupleList) SingleIndex(org.drools.core.util.AbstractHashTable.SingleIndex) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassFieldReader(org.drools.core.base.ClassFieldReader) Cheese(org.drools.core.test.model.Cheese) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 19 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class MemoryVisitor method checkRightTupleList.

private void checkRightTupleList(final TupleList memory) {
    int count = 0;
    FastIterator rightIt = memory.fastIterator();
    for (Tuple rightTuple = memory.getFirst(); rightTuple != null; rightTuple = (Tuple) rightIt.next(rightTuple)) {
        count++;
    }
    logger.info(indent() + "FactHashTable: " + memory.size() + ":" + count);
    if (memory.size() != count) {
        logger.info(indent() + "error");
    }
}
Also used : FastIterator(org.drools.core.util.FastIterator) Tuple(org.drools.core.spi.Tuple)

Example 20 with Tuple

use of org.drools.core.spi.Tuple in project drools by kiegroup.

the class RuleTerminalNode method cancelMatch.

public void cancelMatch(AgendaItem match, InternalWorkingMemoryActions workingMemory) {
    match.cancel();
    if (match.isQueued()) {
        Tuple leftTuple = match.getTuple();
        if (match.getRuleAgendaItem() != null) {
            // phreak must also remove the LT from the rule network evaluator
            if (leftTuple.getMemory() != null) {
                leftTuple.getMemory().remove(leftTuple);
            }
        }
        RuleExecutor ruleExecutor = ((RuleTerminalNodeLeftTuple) leftTuple).getRuleAgendaItem().getRuleExecutor();
        PhreakRuleTerminalNode.doLeftDelete(ruleExecutor.getPathMemory().getActualAgenda(workingMemory), ruleExecutor, leftTuple);
    }
}
Also used : RuleExecutor(org.drools.core.phreak.RuleExecutor) Tuple(org.drools.core.spi.Tuple)

Aggregations

Tuple (org.drools.core.spi.Tuple)54 InternalFactHandle (org.drools.core.common.InternalFactHandle)17 LeftTuple (org.drools.core.reteoo.LeftTuple)16 RightTuple (org.drools.core.reteoo.RightTuple)14 FastIterator (org.drools.core.util.FastIterator)14 Declaration (org.drools.core.rule.Declaration)9 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 WorkingMemory (org.drools.core.WorkingMemory)7 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)7 Cheese (org.drools.core.test.model.Cheese)7 Test (org.junit.Test)7 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)6 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)6 AccumulateContext (org.drools.core.reteoo.AccumulateNode.AccumulateContext)5 AccumulateMemory (org.drools.core.reteoo.AccumulateNode.AccumulateMemory)5 BetaMemory (org.drools.core.reteoo.BetaMemory)5 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)5 TupleMemory (org.drools.core.reteoo.TupleMemory)5 MethodVisitor (org.mvel2.asm.MethodVisitor)5 FromMemory (org.drools.core.reteoo.FromNode.FromMemory)4