Search in sources :

Example 36 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testTwoEqualEntries.

@Test
public void testTwoEqualEntries() throws Exception {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    assertEquals(0, map.size());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle stiltonHandle1 = new DefaultFactHandle(1, stilton1);
    map.add(new RightTupleImpl(stiltonHandle1, null));
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
    map.add(new RightTupleImpl(cheddarHandle1, null));
    final Cheese stilton2 = new Cheese("stilton", 81);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
    map.add(new RightTupleImpl(stiltonHandle2, null));
    assertEquals(3, map.size());
    assertEquals(2, tablePopulationSize(map));
    // Check they are correctly chained to the same FieldIndexEntry
    final Cheese stilton3 = new Cheese("stilton", 89);
    final InternalFactHandle stiltonHandle3 = new DefaultFactHandle(4, stilton2);
    final Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle3, null, true));
    assertSame(stiltonHandle1, tuple.getFactHandle());
    assertSame(stiltonHandle2, ((RightTuple) tuple.getNext()).getFactHandle());
}
Also used : Pattern(org.drools.core.rule.Pattern) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Cheese(org.drools.core.test.model.Cheese) Declaration(org.drools.core.rule.Declaration) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 37 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class RightTupleIndexHashTableTest method testTwoDifferentEntries.

@Test
public void testTwoDifferentEntries() throws Exception {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    final FieldIndex fieldIndex = new FieldIndex(extractor, declaration, MvelConstraint.INDEX_EVALUATOR);
    final TupleIndexHashTable map = new TupleIndexHashTable(new FieldIndex[] { fieldIndex }, false);
    assertEquals(0, map.size());
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle stiltonHandle1 = new DefaultFactHandle(1, stilton1);
    map.add(new RightTupleImpl(stiltonHandle1, null));
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
    map.add(new RightTupleImpl(cheddarHandle1, null));
    assertEquals(2, map.size());
    assertEquals(2, tablePopulationSize(map));
    final Cheese stilton2 = new Cheese("stilton", 77);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(2, stilton2);
    Tuple tuple = map.getFirst(new LeftTupleImpl(stiltonHandle2, null, true));
    assertSame(stiltonHandle1, tuple.getFactHandle());
    assertNull(tuple.getNext());
    final Cheese cheddar2 = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle2 = new DefaultFactHandle(2, cheddar2);
    tuple = map.getFirst(new LeftTupleImpl(cheddarHandle2, null, true));
    assertSame(cheddarHandle1, tuple.getFactHandle());
    assertNull(tuple.getNext());
}
Also used : Pattern(org.drools.core.rule.Pattern) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Cheese(org.drools.core.test.model.Cheese) Declaration(org.drools.core.rule.Declaration) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) InternalFactHandle(org.drools.core.common.InternalFactHandle) RightTuple(org.drools.core.reteoo.RightTuple) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 38 with Declaration

use of org.drools.core.rule.Declaration 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 39 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class EvaluatorHelper method valuesAsMap.

public static Map<String, Object> valuesAsMap(Object object, InternalWorkingMemory workingMemory, Tuple tuple, Declaration[] declarations) {
    if (declarations.length == 0) {
        return null;
    }
    Map<String, Object> map = new HashMap<String, Object>();
    for (Declaration declaration : declarations) {
        if (tuple == null) {
            map.put(declaration.getBindingName(), declaration.getExtractor().getValue(workingMemory, object));
        } else {
            Object fact = tuple.getObject(declaration);
            map.put(declaration.getBindingName(), declaration.getExtractor().getValue(workingMemory, fact != null ? fact : object));
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Declaration(org.drools.core.rule.Declaration)

Example 40 with Declaration

use of org.drools.core.rule.Declaration in project drools by kiegroup.

the class RowAdapter method get.

public Object get(String identifier) {
    if (factHandles == null) {
        this.factHandles = this.tuple.toFactHandles();
    }
    Declaration declr = this.rule.getDeclaration(identifier);
    if (declr == null) {
        throw new RuntimeException("The identifier '" + identifier + "' does not exist as a bound variable for this query");
    }
    InternalFactHandle factHandle = getFactHandle(declr);
    return declr.getValue(null, factHandle.getObject());
}
Also used : Declaration(org.drools.core.rule.Declaration) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Aggregations

Declaration (org.drools.core.rule.Declaration)115 Pattern (org.drools.core.rule.Pattern)42 ClassObjectType (org.drools.core.base.ClassObjectType)29 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)27 TypeDeclaration (org.drools.core.rule.TypeDeclaration)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)22 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)17 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)17 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)16 Test (org.junit.Test)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)14 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)12 WorkingMemory (org.drools.core.WorkingMemory)11 MVELCompilationUnit (org.drools.core.base.mvel.MVELCompilationUnit)11 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 Consequence (org.drools.core.spi.Consequence)10 Constraint (org.drools.core.spi.Constraint)10 AnalysisResult (org.drools.compiler.compiler.AnalysisResult)9