use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class AbstractASMEvalBuilder method buildEval.
private RuleConditionElement buildEval(RuleBuildContext context, EvalDescr evalDescr, AnalysisResult analysis, Declaration[] declarations) {
String className = "eval" + context.getNextId();
evalDescr.setClassMethodName(className);
Arrays.sort(declarations, RuleTerminalNode.SortDeclarations.instance);
EvalCondition eval = new EvalCondition(declarations);
Map<String, Object> vars = createVariableContext(className, (String) evalDescr.getContent(), context, declarations, null, analysis.getBoundIdentifiers().getGlobals());
generateMethodTemplate("evalMethod", context, vars);
byte[] bytecode = createEvalBytecode(context, vars);
registerInvokerBytecode(context, vars, bytecode, eval);
return eval;
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class PhreakEvalNode method doLeftInserts.
public void doLeftInserts(EvalConditionNode evalNode, EvalMemory em, LeftTupleSink sink, InternalWorkingMemory wm, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples) {
EvalCondition condition = evalNode.getCondition();
for (LeftTuple leftTuple = srcLeftTuples.getInsertFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
final boolean allowed = condition.isAllowed(leftTuple, wm, em.context);
if (allowed) {
boolean useLeftMemory = RuleNetworkEvaluator.useLeftMemory(evalNode, leftTuple);
trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), useLeftMemory));
}
leftTuple.clearStaged();
leftTuple = next;
}
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class JavaDialectBinaryEqualityTest method test1.
@Test
public void test1() {
KiePackage pkg1 = getKnowledgePackage1();
KiePackage pkg2 = getKnowledgePackage1();
KiePackage pkg3 = getKnowledgePackage2();
RuleImpl rule1 = ((InternalKnowledgePackage) pkg1).getRule("rule1");
RuleImpl rule2 = ((InternalKnowledgePackage) pkg2).getRule("rule1");
RuleImpl rule3 = ((InternalKnowledgePackage) pkg3).getRule("rule1");
// test return value
Pattern p1 = (Pattern) rule1.getLhs().getChildren().get(0);
Constraint rvc1 = p1.getConstraints().get(0);
Pattern p2 = (Pattern) rule2.getLhs().getChildren().get(0);
Constraint rvc2 = p2.getConstraints().get(0);
assertNotSame(rvc1, rvc2);
assertEquals(rvc1, rvc2);
Pattern p3 = (Pattern) rule3.getLhs().getChildren().get(0);
Constraint rvc3 = p3.getConstraints().get(0);
assertNotSame(rvc1, rvc3);
assertThat(rvc1, not(equalTo(rvc3)));
// test inline eval
PredicateConstraint pc1 = getPredicateConstraint(p1);
PredicateExpression pe1 = (PredicateExpression) pc1.getPredicateExpression();
PredicateConstraint pc2 = getPredicateConstraint(p2);
PredicateExpression pe2 = (PredicateExpression) pc2.getPredicateExpression();
assertNotSame(pe1, pe2);
assertEquals(pe1, pe2);
PredicateConstraint pc3 = getPredicateConstraint(p3);
PredicateExpression pe3 = (PredicateExpression) pc3.getPredicateExpression();
assertNotSame(pe1, pe3);
assertThat(pe1, not(equalTo(pe3)));
// test eval
EvalCondition ec1 = (EvalCondition) rule1.getLhs().getChildren().get(1);
EvalExpression ee1 = (EvalExpression) ec1.getEvalExpression();
EvalCondition ec2 = (EvalCondition) rule2.getLhs().getChildren().get(1);
EvalExpression ee2 = (EvalExpression) ec2.getEvalExpression();
assertNotSame(ee1, ee2);
assertEquals(ee1, ee2);
EvalCondition ec3 = (EvalCondition) rule3.getLhs().getChildren().get(1);
EvalExpression ee3 = (EvalExpression) ec3.getEvalExpression();
assertNotSame(ee1, ee3);
assertThat(ee1, not(equalTo(ee3)));
// test consequence
assertNotSame(rule1.getConsequence(), rule2.getConsequence());
assertEquals(rule1.getConsequence(), rule2.getConsequence());
assertNotSame(rule1.getConsequence(), rule3.getConsequence());
assertThat(rule1.getConsequence(), not(equalTo(rule3.getConsequence())));
// check LHS equals
assertNotSame(rule1.getLhs(), rule2.getLhs());
assertEquals(rule1.getLhs(), rule2.getLhs());
assertNotSame(rule1.getLhs(), rule3.getLhs());
assertThat(rule1.getLhs(), not(equalTo(rule3.getLhs())));
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class PhreakEvalNode method doLeftUpdates.
public void doLeftUpdates(EvalConditionNode evalNode, EvalMemory em, LeftTupleSink sink, ReteEvaluator reteEvaluator, TupleSets<LeftTuple> srcLeftTuples, TupleSets<LeftTuple> trgLeftTuples, TupleSets<LeftTuple> stagedLeftTuples) {
EvalCondition condition = evalNode.getCondition();
for (LeftTuple leftTuple = srcLeftTuples.getUpdateFirst(); leftTuple != null; ) {
LeftTuple next = leftTuple.getStagedNext();
boolean wasPropagated = leftTuple.getFirstChild() != null && leftTuple.getContextObject() != EVAL_LEFT_TUPLE_DELETED;
boolean allowed = condition.isAllowed(leftTuple, reteEvaluator, em.context);
if (allowed) {
leftTuple.setContextObject(null);
if (wasPropagated) {
// update
LeftTuple childLeftTuple = leftTuple.getFirstChild();
childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
normalizeStagedTuples(stagedLeftTuples, childLeftTuple);
trgLeftTuples.addUpdate(childLeftTuple);
} else {
// assert
trgLeftTuples.addInsert(sink.createLeftTuple(leftTuple, sink, leftTuple.getPropagationContext(), true));
}
} else {
if (wasPropagated) {
// retract
leftTuple.setContextObject(EVAL_LEFT_TUPLE_DELETED);
LeftTuple childLeftTuple = leftTuple.getFirstChild();
childLeftTuple.setPropagationContext(leftTuple.getPropagationContext());
RuleNetworkEvaluator.unlinkAndDeleteChildLeftTuple(childLeftTuple, trgLeftTuples, stagedLeftTuples);
}
// else do nothing
}
leftTuple.clearStaged();
leftTuple = next;
}
}
use of org.drools.core.rule.EvalCondition in project drools by kiegroup.
the class MVELEvalBuilderTest method testSimpleExpression.
@Test
public void testSimpleExpression() {
InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("pkg1");
final RuleDescr ruleDescr = new RuleDescr("rule 1");
KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl(pkg);
final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
final InternalReadAccessor extractor = store.getReader(Cheese.class, "price");
final Pattern pattern = new Pattern(0, new ClassObjectType(int.class));
final Declaration declaration = new Declaration("a", extractor, pattern);
final Map map = new HashMap();
map.put("a", declaration);
declarationResolver.setDeclarations(map);
context.setDeclarationResolver(declarationResolver);
final EvalDescr evalDescr = new EvalDescr();
evalDescr.setContent("a == 10");
final MVELEvalBuilder builder = new MVELEvalBuilder();
final EvalCondition eval = (EvalCondition) builder.build(context, evalDescr);
((MVELEvalExpression) eval.getEvalExpression()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
org.drools.core.reteoo.MockLeftTupleSink sink = new MockLeftTupleSink(buildContext);
MockTupleSource source = new MockTupleSource(1, buildContext);
source.setObjectCount(1);
sink.setLeftTupleSource(source);
final Cheese cheddar = new Cheese("cheddar", 10);
final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
final LeftTupleImpl tuple = new LeftTupleImpl(f0, sink, true);
f0.removeLeftTuple(tuple);
Object evalContext = eval.createContext();
assertTrue(eval.isAllowed(tuple, ksession, evalContext));
cheddar.setPrice(9);
ksession.update(f0, cheddar);
assertFalse(eval.isAllowed(tuple, ksession, evalContext));
}
Aggregations