Search in sources :

Example 56 with ClassObjectType

use of org.drools.core.base.ClassObjectType 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 57 with ClassObjectType

use of org.drools.core.base.ClassObjectType 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 58 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class PatternBuilder method attachObjectTypeNode.

private void attachObjectTypeNode(final BuildContext context, final BuildUtils utils, final Pattern pattern) {
    boolean objectMemory = context.isObjectTypeNodeMemoryEnabled();
    ObjectType objectType = pattern.getObjectType();
    if (pattern.getObjectType() instanceof ClassObjectType) {
        // Is this the query node, if so we don't want any memory
        if (DroolsQuery.class == ((ClassObjectType) pattern.getObjectType()).getClassType()) {
            context.setTupleMemoryEnabled(false);
            context.setObjectTypeNodeMemoryEnabled(false);
        }
    }
    ObjectTypeNode otn = context.getComponentFactory().getNodeFactoryService().buildObjectTypeNode(context.getNextId(), (EntryPointNode) context.getObjectSource(), objectType, context);
    if (objectType.isEvent() && EventProcessingOption.STREAM.equals(context.getKnowledgeBase().getConfiguration().getEventProcessingMode())) {
        ExpirationSpec expirationSpec = getExpirationForType(context, objectType);
        if (expirationSpec.offset != NEVER_EXPIRES && expirationSpec.hard) {
            // hard expiration is set, so use it
            otn.setExpirationOffset(expirationSpec.offset);
        } else {
            // otherwise calculate it based on behaviours and temporal constraints
            long offset = NEVER_EXPIRES;
            for (Behavior behavior : pattern.getBehaviors()) {
                if (behavior.getExpirationOffset() != NEVER_EXPIRES) {
                    offset = Math.max(behavior.getExpirationOffset(), offset);
                }
            }
            // if there's no implicit expiration uses the (eventually set) soft one
            if (offset == NEVER_EXPIRES && !expirationSpec.hard) {
                offset = expirationSpec.offset;
            }
            long distance = context.getExpirationOffset(pattern);
            if (distance == NEVER_EXPIRES) {
                // it means the rules have no temporal constraints, or
                // the constraints require events to be hold forever. In this
                // case, we allow type declarations to override the implicit
                // expiration offset by defining an expiration policy with the
                // @expires tag
                otn.setExpirationOffset(offset);
            } else {
                otn.setExpirationOffset(Math.max(distance, offset));
            }
        }
    }
    context.setObjectSource(utils.attachNode(context, otn));
    context.setObjectTypeNodeMemoryEnabled(objectMemory);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Behavior(org.drools.core.rule.Behavior)

Example 59 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class XpathBackReference method getDeclaration.

public Declaration getDeclaration(Pattern pattern, String id) {
    if (!id.startsWith(BACK_REFERENCE_HEAD)) {
        return null;
    }
    Declaration declaration = declarations.get(id);
    if (declaration != null) {
        return declaration;
    }
    int backRefPos = Integer.parseInt(id.substring(XpathBackReference.BACK_REFERENCE_HEAD.length()));
    int relativeOffset = backReferenceClasses.size() - 1 - backRefPos;
    declaration = new Declaration(id, new PatternExtractor(new ClassObjectType(backReferenceClasses.get(backRefPos))), new RelativePattern(pattern, relativeOffset), true);
    if (declarations == Collections.EMPTY_MAP) {
        declarations = new HashMap<String, Declaration>();
    }
    declarations.put(id, declaration);
    return declaration;
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) PatternExtractor(org.drools.core.spi.PatternExtractor)

Example 60 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class DeclarationTest method testDeclaration.

@Test
public void testDeclaration() {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(5, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value from
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    assertEquals("typeOfCheese", declaration.getIdentifier());
    assertSame(String.class, declaration.getDeclarationClass());
    assertSame(extractor, declaration.getExtractor());
    assertEquals(5, declaration.getPattern().getOffset());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Cheese(org.drools.core.test.model.Cheese) Test(org.junit.Test)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)123 Test (org.junit.Test)76 Pattern (org.drools.core.rule.Pattern)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)34 KieBase (org.kie.api.KieBase)32 Declaration (org.drools.core.rule.Declaration)28 ObjectType (org.drools.core.spi.ObjectType)28 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 BetaNode (org.drools.core.reteoo.BetaNode)17 AlphaNode (org.drools.core.reteoo.AlphaNode)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)13 Cheese (org.drools.core.test.model.Cheese)13 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)12