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);
}
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);
}
use of org.drools.core.rule.PredicateConstraint in project drools by kiegroup.
the class FieldConstraintTest method testPredicateConstraint.
/**
* <pre>
*
* (Cheese (price ?price1 )
* (Cheese (price ?price2&:(= ?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));
}
Aggregations