Search in sources :

Example 91 with ClassObjectType

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

the class RightTupleIndexHashTableTest method testEmptyIterator.

@Test
public void testEmptyIterator() {
    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);
    final Cheese stilton = new Cheese("stilton", 55);
    final InternalFactHandle stiltonHandle = new DefaultFactHandle(2, stilton);
    assertNull(map.getFirst(new LeftTupleImpl(stiltonHandle, null, true)));
}
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) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 92 with ClassObjectType

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

the class FieldConstraintTest method testPredicateConstraint.

/**
 * <pre>
 *
 *                (Cheese (price ?price1 )
 *                (Cheese (price ?price2&amp;:(= ?price2 (* 2 ?price1) )
 *
 * </pre>
 */
@Test
public void testPredicateConstraint() {
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final InternalReadAccessor priceExtractor = store.getReader(Cheese.class, "price");
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price1Declaration = new Declaration("price1", priceExtractor, pattern);
    pattern = new Pattern(1, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value form
    final Declaration price2Declaration = new Declaration("price2", priceExtractor, pattern);
    final PredicateExpression evaluator = new PredicateExpression() {

        private static final long serialVersionUID = 510l;

        public boolean evaluate(InternalFactHandle handle, Tuple tuple, Declaration[] previousDeclarations, Declaration[] localDeclarations, WorkingMemory workingMemory, Object context) {
            int price1 = previousDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, tuple.getObject(previousDeclarations[0]));
            int price2 = localDeclarations[0].getIntValue((InternalWorkingMemory) workingMemory, handle.getObject());
            return (price2 == (price1 * 2));
        }

        public Object createContext() {
            return null;
        }

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

        public void writeExternal(ObjectOutput out) throws IOException {
        }
    };
    final PredicateConstraint constraint1 = new PredicateConstraint(evaluator, new Declaration[] { price1Declaration }, new Declaration[] { price2Declaration });
    final Cheese cheddar0 = new Cheese("cheddar", 5);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar0);
    LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
    final Cheese cheddar1 = new Cheese("cheddar", 10);
    final InternalFactHandle f1 = (InternalFactHandle) ksession.insert(cheddar1);
    tuple = new LeftTupleImpl(tuple, new RightTupleImpl(f1, null), null, true);
    final PredicateContextEntry context = (PredicateContextEntry) constraint1.createContextEntry();
    context.updateFromTuple(ksession, tuple);
    assertTrue(constraint1.isAllowedCachedLeft(context, f1));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) Cheese(org.drools.core.test.model.Cheese) PredicateExpression(org.drools.core.spi.PredicateExpression) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateContextEntry(org.drools.core.rule.PredicateConstraint.PredicateContextEntry) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) ObjectInput(java.io.ObjectInput) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Example 93 with ClassObjectType

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

the class GroupElementTest method testDeclarationOrdering.

@Test
public void testDeclarationOrdering() {
    final GroupElement and1 = GroupElementFactory.newAndInstance();
    final Pattern pattern1 = new Pattern(0, new ClassObjectType(Person.class), "x");
    and1.addChild(pattern1);
    final Pattern pattern2 = new Pattern(2, new ClassObjectType(Person.class), "y");
    and1.addChild(pattern2);
    Declaration x1 = (Declaration) and1.getInnerDeclarations().get("x");
    Declaration y1 = (Declaration) and1.getInnerDeclarations().get("y");
    Declaration z1 = (Declaration) and1.getInnerDeclarations().get("z");
    assertNotNull(x1);
    assertNotNull(y1);
    assertNull(z1);
    assertEquals(2, and1.getChildren().size());
    assertSame(pattern1, and1.getChildren().get(0));
    assertSame(pattern2, and1.getChildren().get(1));
    final GroupElement and2 = GroupElementFactory.newAndInstance();
    and2.addChild(and1);
    final Pattern pattern3 = new Pattern(3, new ClassObjectType(Person.class), "x");
    and2.addChild(pattern3);
    and2.pack();
    assertEquals(3, and2.getChildren().size());
    assertSame(pattern1, and2.getChildren().get(0));
    assertSame(pattern2, and2.getChildren().get(1));
    assertSame(pattern3, and2.getChildren().get(2));
    Declaration x2 = (Declaration) and2.getInnerDeclarations().get("x");
    Declaration y2 = (Declaration) and2.getInnerDeclarations().get("y");
    Declaration z2 = (Declaration) and2.getInnerDeclarations().get("z");
    assertNotNull(x2);
    assertNotNull(y2);
    assertNull(z2);
    assertNotSame(x1, x2);
    assertSame(x2, pattern3.getDeclaration());
    assertSame(y1, y2);
    assertSame(z1, z2);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) Person(org.drools.core.test.model.Person) Test(org.junit.Test)

Example 94 with ClassObjectType

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

the class LogicTransformerTest method testNotExistsTransformation.

/**
 * Exists inside a not is redundant and should be eliminated
 *
 * (Not (exists (A) ) )
 *
 * <pre>
 *             Not
 *              |
 *            Exists
 *              |
 *             And
 *             / \
 *            a   b
 * </pre>
 *
 * Should become:
 *
 * <pre>
 *             Not
 *              |
 *             And
 *             / \
 *            a   b
 * </pre>
 */
@Test
public void testNotExistsTransformation() throws InvalidPatternException {
    final ObjectType type = new ClassObjectType(String.class);
    final Pattern a = new Pattern(0, type, "a");
    final Pattern b = new Pattern(1, type, "b");
    final GroupElement not = GroupElementFactory.newNotInstance();
    final GroupElement exists = GroupElementFactory.newExistsInstance();
    final GroupElement and = GroupElementFactory.newAndInstance();
    not.addChild(exists);
    exists.addChild(and);
    and.addChild(a);
    and.addChild(b);
    GroupElement[] transformed = LogicTransformer.getInstance().transform(not, Collections.EMPTY_MAP);
    GroupElement wrapper = transformed[0];
    GroupElement notR = (GroupElement) wrapper.getChildren().get(0);
    assertTrue(notR.isNot());
    assertEquals(1, notR.getChildren().size());
    assertTrue(notR.getChildren().get(0) instanceof GroupElement);
    GroupElement andR = (GroupElement) notR.getChildren().get(0);
    assertTrue(andR.isAnd());
    assertEquals(2, andR.getChildren().size());
    assertTrue(andR.getChildren().get(0) instanceof Pattern);
    assertTrue(andR.getChildren().get(1) instanceof Pattern);
    final Pattern a1 = (Pattern) andR.getChildren().get(0);
    final Pattern b1 = (Pattern) andR.getChildren().get(1);
    assertEquals(a, a1);
    assertEquals(b, b1);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Test(org.junit.Test)

Example 95 with ClassObjectType

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

the class LogicTransformerTest method testCloneable.

@Test
public void testCloneable() {
    final ObjectType type = new ClassObjectType(String.class);
    final Pattern a = new Pattern(0, type, "a");
    final Pattern b = new Pattern(1, type, "b");
    final Pattern c = new Pattern(2, type, "c");
    final Pattern d = new Pattern(3, type, "d");
    final Pattern e = new Pattern(4, type, "e");
    final Pattern f = new Pattern(5, type, "f");
    final Pattern g = new Pattern(6, type, "g");
    final Pattern h = new Pattern(7, type, "h");
    // Test against a known false tree
    final GroupElement and = GroupElementFactory.newAndInstance();
    and.addChild(a);
    and.addChild(b);
    final GroupElement or = GroupElementFactory.newOrInstance();
    or.addChild(c);
    or.addChild(d);
    and.addChild(or);
    final GroupElement and2 = GroupElementFactory.newAndInstance();
    and2.addChild(e);
    and2.addChild(f);
    or.addChild(and2);
    final GroupElement not = GroupElementFactory.newNotInstance();
    and.addChild(not);
    final GroupElement or2 = GroupElementFactory.newOrInstance();
    not.addChild(or2);
    or2.addChild(g);
    or2.addChild(h);
    final GroupElement cloned = (GroupElement) and.clone();
    assertEquals(and, cloned);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) 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