Search in sources :

Example 6 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class FieldConstraintTest method testPrimitiveLiteralConstraint.

/**
 * <pre>
 *
 *                Cheese( price == 5 )
 *
 * </pre>
 */
@Test
public void testPrimitiveLiteralConstraint() {
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    ;
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final ClassFieldReader extractor = store.getReader(Cheese.class, "price");
    AlphaNodeFieldConstraint constraint = ConstraintTestUtil.createCheesePriceEqualsConstraint(extractor, 5, useLambdaConstraint);
    final Cheese cheddar = new Cheese("cheddar", 5);
    final InternalFactHandle cheddarHandle = (InternalFactHandle) ksession.insert(cheddar);
    // check constraint
    assertTrue(constraint.isAllowed(cheddarHandle, ksession));
    final Cheese stilton = new Cheese("stilton", 10);
    final InternalFactHandle stiltonHandle = (InternalFactHandle) ksession.insert(stilton);
    // check constraint
    assertFalse(constraint.isAllowed(stiltonHandle, ksession));
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Cheese(org.drools.mvel.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class BaseBetaConstraintsTest method getCheeseTypeConstraint.

protected BetaNodeFieldConstraint getCheeseTypeConstraint(final String identifier, Operator operator) {
    if (useLambdaConstraint) {
        Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
        Predicate1<Cheese> predicate;
        if (operator == Operator.BuiltInOperator.EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.NOT_EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> !EvaluationUtil.areNullSafeEquals(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.GREATER.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.greaterThan(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.greaterOrEqual(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.LESS.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.lessThan(_this.getType(), identifier));
        } else if (operator == Operator.BuiltInOperator.GREATER_OR_EQUAL.getOperator()) {
            predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.lessOrEqual(_this.getType(), identifier));
        } else {
            throw new RuntimeException(operator + " is not supported");
        }
        return LambdaConstraintTestUtil.createLambdaConstraint1(Cheese.class, pattern, predicate, null);
    } else {
        ClassFieldAccessorStore store = new ClassFieldAccessorStore();
        store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
        store.setEagerWire(true);
        InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
        Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(Cheese.class)));
        String expression = "type " + operator.getOperatorString() + " " + identifier;
        return new MVELConstraintTestUtil(expression, operator.getOperatorString(), declaration, extractor);
    }
}
Also used : LinkedList(org.drools.core.util.LinkedList) ClassObjectType(org.drools.core.base.ClassObjectType) BetaConstraints(org.drools.core.common.BetaConstraints) BetaNodeFieldConstraint(org.drools.core.spi.BetaNodeFieldConstraint) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) EvaluatorRegistry(org.drools.compiler.builder.impl.EvaluatorRegistry) Pattern(org.drools.core.rule.Pattern) RunWith(org.junit.runner.RunWith) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) BetaMemory(org.drools.core.reteoo.BetaMemory) ArrayList(java.util.ArrayList) ConstraintType(org.drools.core.util.index.IndexUtil.ConstraintType) Assert.assertSame(org.junit.Assert.assertSame) Declaration(org.drools.core.rule.Declaration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) Index(org.drools.core.util.AbstractHashTable.Index) LinkedListEntry(org.drools.core.util.LinkedListEntry) TupleList(org.drools.core.util.index.TupleList) Parameterized(org.junit.runners.Parameterized) TupleIndexHashTable(org.drools.core.util.index.TupleIndexHashTable) Collection(java.util.Collection) Operator(org.drools.drl.parser.impl.Operator) Assert.assertTrue(org.junit.Assert.assertTrue) FieldIndex(org.drools.core.util.AbstractHashTable.FieldIndex) Predicate1(org.drools.model.functions.Predicate1) EvaluationUtil(org.drools.modelcompiler.util.EvaluationUtil) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) Cheese(org.drools.mvel.model.Cheese) Assert.assertEquals(org.junit.Assert.assertEquals) NodeTypeEnums(org.drools.core.reteoo.NodeTypeEnums) IndexableConstraint(org.drools.core.rule.IndexableConstraint) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.mvel.model.Cheese) ClassFieldAccessorStore(org.drools.mvel.accessors.ClassFieldAccessorStore) ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Predicate1(org.drools.model.functions.Predicate1) Declaration(org.drools.core.rule.Declaration)

Example 8 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method keyForCheeseCharObjectType.

private HashKey keyForCheeseCharObjectType(char c) {
    Cheese cheese = new Cheese();
    cheese.setCharObjectType(c);
    HashKey hashKey = new CompositeObjectSinkAdapter.HashKey();
    hashKey.setValue(extractor.getIndex(), cheese, extractor);
    return hashKey;
}
Also used : Cheese(org.drools.mvel.model.Cheese) HashKey(org.drools.core.reteoo.CompositeObjectSinkAdapter.HashKey)

Example 9 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testAddThreeAlphasVerifyRangeQuery.

@Test
public void testAddThreeAlphasVerifyRangeQuery() {
    extractor = store.getReader(Cheese.class, "price");
    final AlphaNode al1 = createAlphaNode(cheesePriceGreaterThan(10));
    ad.addObjectSink(al1);
    final AlphaNode al2 = createAlphaNode(cheesePriceGreaterThan(20));
    ad.addObjectSink(al2);
    final AlphaNode al3 = createAlphaNode(cheesePriceGreaterThan(30));
    ad.addObjectSink(al3);
    // test propagation
    CompositeObjectSinkAdapter.FieldIndex fieldIndex = ad.getRangeIndexedFieldIndexes().get(0);
    Cheese cheese = new Cheese();
    cheese.setPrice(25);
    Collection<AlphaNode> matchingAlphaNodes = ad.getRangeIndexMap().get(fieldIndex).getMatchingAlphaNodes(cheese);
    assertEquals(2, matchingAlphaNodes.size());
    assertTrue(matchingAlphaNodes.contains(al1));
    assertTrue(matchingAlphaNodes.contains(al2));
    // should not find this one
    cheese.setPrice(5);
    matchingAlphaNodes = ad.getRangeIndexMap().get(fieldIndex).getMatchingAlphaNodes(cheese);
    assertTrue(matchingAlphaNodes.isEmpty());
}
Also used : Cheese(org.drools.mvel.model.Cheese) CompositeObjectSinkAdapter(org.drools.core.reteoo.CompositeObjectSinkAdapter) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 10 with Cheese

use of org.drools.mvel.model.Cheese in project drools by kiegroup.

the class LambdaConstraintTestUtil method createCheeseTypeEqualsConstraint.

public static LambdaConstraint createCheeseTypeEqualsConstraint(final String rightValue, int indexId) {
    // Typical LambdaConstraint used in drools-test-coverage. (type == "xxx")
    Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
    Predicate1<Cheese> predicate = new Predicate1.Impl<Cheese>(_this -> EvaluationUtil.areNullSafeEquals(_this.getType(), rightValue));
    AlphaIndexImpl<Cheese, String> index = new AlphaIndexImpl<Cheese, String>(String.class, org.drools.model.Index.ConstraintType.EQUAL, indexId, _this -> _this.getType(), rightValue);
    return createLambdaConstraint1(Cheese.class, pattern, predicate, index);
}
Also used : Pattern(org.drools.core.rule.Pattern) DeclarationImpl(org.drools.model.impl.DeclarationImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) AlphaIndexImpl(org.drools.model.index.AlphaIndexImpl) ClassObjectType(org.drools.core.base.ClassObjectType) Cheese(org.drools.mvel.model.Cheese)

Aggregations

Cheese (org.drools.mvel.model.Cheese)16 Test (org.junit.Test)8 ClassObjectType (org.drools.core.base.ClassObjectType)7 Pattern (org.drools.core.rule.Pattern)7 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)6 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)6 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)6 AlphaNode (org.drools.core.reteoo.AlphaNode)5 DeclarationImpl (org.drools.model.impl.DeclarationImpl)5 AlphaIndexImpl (org.drools.model.index.AlphaIndexImpl)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)3 PropagationContextFactory (org.drools.core.common.PropagationContextFactory)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 MockObjectSink (org.drools.core.reteoo.MockObjectSink)3 BuildContext (org.drools.core.reteoo.builder.BuildContext)3 PropagationContext (org.drools.core.spi.PropagationContext)3 MockObjectSource (org.drools.mvel.model.MockObjectSource)3 HashKey (org.drools.core.reteoo.CompositeObjectSinkAdapter.HashKey)2 Declaration (org.drools.core.rule.Declaration)2