Search in sources :

Example 11 with PredicateConstraint

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

the class PatternBuilder method buildEval.

protected Constraint buildEval(final RuleBuildContext context, final Pattern pattern, final PredicateDescr predicateDescr, final Map<String, OperatorDescr> aliases, final String expr, final boolean isEvalExpression) {
    AnalysisResult analysis = buildAnalysis(context, pattern, predicateDescr, aliases);
    if (analysis == null) {
        // something bad happened
        return null;
    }
    Declaration[][] usedDeclarations = getUsedDeclarations(context, pattern, analysis);
    Declaration[] previousDeclarations = usedDeclarations[0];
    Declaration[] localDeclarations = usedDeclarations[1];
    BoundIdentifiers usedIdentifiers = analysis.getBoundIdentifiers();
    Arrays.sort(previousDeclarations, SortDeclarations.instance);
    Arrays.sort(localDeclarations, SortDeclarations.instance);
    boolean isJavaEval = isEvalExpression && context.getDialect().isJava();
    if (isJavaEval) {
        final PredicateConstraint predicateConstraint = new PredicateConstraint(null, previousDeclarations, localDeclarations);
        final PredicateBuilder builder = context.getDialect().getPredicateBuilder();
        builder.build(context, usedIdentifiers, previousDeclarations, localDeclarations, predicateConstraint, predicateDescr, analysis);
        return predicateConstraint;
    }
    String[] requiredGlobals = usedIdentifiers.getGlobals().keySet().toArray(new String[usedIdentifiers.getGlobals().size()]);
    Declaration[] mvelDeclarations = new Declaration[previousDeclarations.length + localDeclarations.length + requiredGlobals.length];
    int i = 0;
    for (Declaration d : previousDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (Declaration d : localDeclarations) {
        mvelDeclarations[i++] = d;
    }
    for (String global : requiredGlobals) {
        mvelDeclarations[i++] = context.getDeclarationResolver().getDeclaration(global);
    }
    boolean isDynamic = pattern.getObjectType().isTemplate() || (!((ClassObjectType) pattern.getObjectType()).getClassType().isArray() && !context.getKnowledgeBuilder().getTypeDeclaration(pattern.getObjectType()).isTypesafe());
    return getConstraintBuilder().buildMvelConstraint(context.getPkg().getName(), expr, mvelDeclarations, getOperators(usedIdentifiers.getOperators()), context, previousDeclarations, localDeclarations, predicateDescr, analysis, isDynamic);
}
Also used : TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) PredicateConstraint(org.drools.core.rule.PredicateConstraint) AnalysisResult(org.drools.compiler.compiler.AnalysisResult) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers)

Example 12 with PredicateConstraint

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

the class CompositeObjectSinkAdapterTest method testAddOneAlphaNotHashable.

@Test
public void testAddOneAlphaNotHashable() {
    final AlphaNode al = createAlphaNode(new PredicateConstraint(null, null));
    ad.addObjectSink(al);
    sinksAre(al);
    otherSinksAre(al);
}
Also used : PredicateConstraint(org.drools.core.rule.PredicateConstraint) AlphaNode(org.drools.core.reteoo.AlphaNode) Test(org.junit.Test)

Example 13 with PredicateConstraint

use of org.drools.core.rule.PredicateConstraint 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 = 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, ReteEvaluator reteEvaluator, Object context) {
            int price1 = previousDeclarations[0].getIntValue(reteEvaluator, tuple.getObject(previousDeclarations[0]));
            int price2 = localDeclarations[0].getIntValue(reteEvaluator, 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 : ReteEvaluator(org.drools.core.common.ReteEvaluator) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectOutput(java.io.ObjectOutput) Cheese(org.drools.mvel.model.Cheese) PredicateExpression(org.drools.core.spi.PredicateExpression) RightTupleImpl(org.drools.core.reteoo.RightTupleImpl) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) PredicateContextEntry(org.drools.core.rule.PredicateConstraint.PredicateContextEntry) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Declaration(org.drools.core.rule.Declaration) ObjectInput(java.io.ObjectInput) PredicateConstraint(org.drools.core.rule.PredicateConstraint) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Tuple(org.drools.core.spi.Tuple) Test(org.junit.Test)

Aggregations

PredicateConstraint (org.drools.core.rule.PredicateConstraint)13 Test (org.junit.Test)12 PredicateExpression (org.drools.core.spi.PredicateExpression)7 Pattern (org.drools.core.rule.Pattern)5 AlphaNode (org.drools.core.reteoo.AlphaNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 CompiledInvoker (org.drools.core.spi.CompiledInvoker)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)3 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)3 Constraint (org.drools.core.spi.Constraint)3 StringReader (java.io.StringReader)2 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 BetaNode (org.drools.core.reteoo.BetaNode)2 Declaration (org.drools.core.rule.Declaration)2 EvalCondition (org.drools.core.rule.EvalCondition)2 EvalExpression (org.drools.core.spi.EvalExpression)2 Person (org.drools.mvel.compiler.Person)2 ObjectInput (java.io.ObjectInput)1