Search in sources :

Example 86 with ClassObjectType

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

the class KnowledgeBuilderTest method testTimeWindowBehavior.

@Test
public void testTimeWindowBehavior() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
    typeDeclDescr.addAnnotation("role", "event");
    packageDescr.addTypeDeclaration(typeDeclDescr);
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
    final BehaviorDescr windowDescr = new BehaviorDescr("window");
    windowDescr.setSubType("time");
    windowDescr.setParameters(Collections.singletonList("60000"));
    patternDescr.addBehavior(windowDescr);
    lhs.addDescr(patternDescr);
    ruleDescr.setConsequence("System.out.println( $tick );");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
    final RuleImpl rule = pkg.getRule("rule-1");
    assertNotNull(rule);
    final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
    assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
    final Behavior window = pattern.getBehaviors().get(0);
    assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
    assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
Also used : Pattern(org.drools.core.rule.Pattern) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) ClassObjectType(org.drools.core.base.ClassObjectType) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) StockTick(org.drools.compiler.StockTick) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Behavior(org.drools.core.rule.Behavior) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 87 with ClassObjectType

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

the class PhreakPropagationContext method adaptModificationMaskForObjectType.

public PropagationContext adaptModificationMaskForObjectType(ObjectType type, InternalWorkingMemory workingMemory) {
    if (isAllSetPropertyReactiveMask(originalMask) || originalMask.isSet(PropertySpecificUtil.TRAITABLE_BIT) || !(type instanceof ClassObjectType)) {
        return this;
    }
    ClassObjectType classObjectType = (ClassObjectType) type;
    BitMask cachedMask = classObjectType.getTransformedMask(modifiedClass, originalMask);
    if (cachedMask != null) {
        modificationMask = cachedMask;
        return this;
    }
    modificationMask = originalMask;
    boolean typeBit = modificationMask.isSet(PropertySpecificUtil.TRAITABLE_BIT);
    modificationMask = modificationMask.reset(PropertySpecificUtil.TRAITABLE_BIT);
    Class<?> classType = classObjectType.getClassType();
    String pkgName = classType.getPackage().getName();
    if (classType == modifiedClass || "java.lang".equals(pkgName) || !(classType.isInterface() || modifiedClass.isInterface())) {
        if (typeBit) {
            modificationMask = modificationMask.set(PropertySpecificUtil.TRAITABLE_BIT);
        }
        return this;
    }
    List<String> typeClassProps = getAccessibleProperties(workingMemory, classType, pkgName);
    List<String> modifiedClassProps = getAccessibleProperties(workingMemory, modifiedClass);
    modificationMask = getEmptyPropertyReactiveMask(typeClassProps.size());
    for (int i = 0; i < modifiedClassProps.size(); i++) {
        if (isPropertySetOnMask(originalMask, i)) {
            int posInType = typeClassProps.indexOf(modifiedClassProps.get(i));
            if (posInType >= 0) {
                modificationMask = setPropertyOnMask(modificationMask, posInType);
            }
        }
    }
    if (typeBit) {
        modificationMask = modificationMask.set(PropertySpecificUtil.TRAITABLE_BIT);
    }
    classObjectType.storeTransformedMask(modifiedClass, originalMask, modificationMask);
    return this;
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) BitMask(org.drools.core.util.bitmask.BitMask)

Example 88 with ClassObjectType

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

the class RightTupleIndexHashTableIteratorTest method getConstraint.

protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
    String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
    return new MvelConstraintTestUtil(expression, declaration, extractor);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Declaration(org.drools.core.rule.Declaration) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Example 89 with ClassObjectType

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

the class RightTupleIndexHashTableTest method testResize.

@Test
public void testResize() 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(16, 0.75f, new FieldIndex[] { fieldIndex }, false);
    assertEquals(0, map.size());
    final Cheese stilton1 = new Cheese("stilton", 35);
    map.add(newRightTuple(1, stilton1));
    final Cheese stilton2 = new Cheese("stilton", 81);
    map.add(newRightTuple(2, stilton2));
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    map.add(newRightTuple(3, cheddar1));
    final Cheese cheddar2 = new Cheese("cheddar", 38);
    map.add(newRightTuple(4, cheddar2));
    final Cheese brie = new Cheese("brie", 293);
    map.add(newRightTuple(5, brie));
    final Cheese mozerella = new Cheese("mozerella", 15);
    map.add(newRightTuple(6, mozerella));
    final Cheese dolcelatte = new Cheese("dolcelatte", 284);
    map.add(newRightTuple(7, dolcelatte));
    final Cheese camembert1 = new Cheese("camembert", 924);
    map.add(newRightTuple(8, camembert1));
    final Cheese camembert2 = new Cheese("camembert", 765);
    map.add(newRightTuple(9, camembert2));
    final Cheese redLeicestor = new Cheese("red leicestor", 23);
    map.add(newRightTuple(10, redLeicestor));
    final Cheese wensleydale = new Cheese("wensleydale", 20);
    map.add(newRightTuple(11, wensleydale));
    final Cheese edam = new Cheese("edam", 12);
    map.add(newRightTuple(12, edam));
    final Cheese goude1 = new Cheese("goude", 93);
    map.add(newRightTuple(13, goude1));
    final Cheese goude2 = new Cheese("goude", 88);
    map.add(newRightTuple(14, goude2));
    final Cheese gruyere = new Cheese("gruyere", 82);
    map.add(newRightTuple(15, gruyere));
    final Cheese emmental = new Cheese("emmental", 98);
    map.add(newRightTuple(16, emmental));
    // At this point we have 16 facts but only 12 different types of cheeses
    // so no table resize and thus its size is 16
    assertEquals(16, map.size());
    Entry[] table = map.getTable();
    assertEquals(16, table.length);
    final Cheese feta = new Cheese("feta", 48);
    map.add(newRightTuple(2, feta));
    // This adds our 13th type of cheese. The map is set with an initial capacity of 16 and
    // a threshold of 75%, that after 12 it should resize the map to 32.
    assertEquals(17, map.size());
    table = map.getTable();
    assertEquals(32, table.length);
    final Cheese haloumi = new Cheese("haloumi", 48);
    map.add(newRightTuple(2, haloumi));
    final Cheese chevre = new Cheese("chevre", 48);
    map.add(newRightTuple(2, chevre));
}
Also used : Pattern(org.drools.core.rule.Pattern) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Cheese(org.drools.core.test.model.Cheese) Declaration(org.drools.core.rule.Declaration) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) Test(org.junit.Test)

Example 90 with ClassObjectType

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

the class RightTupleIndexHashTableTest method testRemove.

@Test
public void testRemove() 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);
    RightTuple stiltonRightTuple1 = new RightTupleImpl(stiltonHandle1, null);
    map.add(stiltonRightTuple1);
    final Cheese cheddar1 = new Cheese("cheddar", 35);
    final InternalFactHandle cheddarHandle1 = new DefaultFactHandle(2, cheddar1);
    RightTuple cheddarRightTuple1 = new RightTupleImpl(cheddarHandle1, null);
    map.add(cheddarRightTuple1);
    final Cheese stilton2 = new Cheese("stilton", 81);
    final InternalFactHandle stiltonHandle2 = new DefaultFactHandle(3, stilton2);
    RightTuple stiltonRightTuple2 = new RightTupleImpl(stiltonHandle2, null);
    map.add(stiltonRightTuple2);
    assertEquals(3, map.size());
    assertEquals(2, tablePopulationSize(map));
    // cheddar is in its own bucket, which should be removed once empty. We cannot have
    // empty FieldIndexEntries in the Map, as they get their value  from the first FactEntry.
    map.remove(cheddarRightTuple1);
    assertEquals(2, map.size());
    assertEquals(1, tablePopulationSize(map));
    // We remove t he stiltonHandle2, but there is still  one more stilton, so size  should be the same
    map.remove(stiltonRightTuple2);
    assertEquals(1, map.size());
    assertEquals(1, tablePopulationSize(map));
    // No more stiltons, so the table should be empty
    map.remove(stiltonRightTuple1);
    assertEquals(0, map.size());
    assertEquals(0, tablePopulationSize(map));
}
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) 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) 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