use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class ConditionalBranchBuilder method getLastPattern.
private Pattern getLastPattern(RuleBuildContext context) {
GroupElement ge = (GroupElement) context.getDeclarationResolver().peekBuildStack();
Pattern lastPattern = getLastPattern(ge.getChildren());
if (lastPattern == null) {
RuleImpl parent = context.getRule().getParent();
if (parent != null) {
lastPattern = getLastPattern(parent.getLhs().getChildren());
}
}
return lastPattern;
}
use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class KiePackagesBuilder method populateLHS.
private void populateLHS(RuleContext ctx, KnowledgePackageImpl pkg, View view) {
GroupElement lhs = ctx.getRule().getLhs();
if (ctx.getRule().getRuleUnitClassName() != null) {
lhs.addChild(addPatternForVariable(ctx, lhs, getUnitVariable(ctx, pkg, view)));
}
addSubConditions(ctx, lhs, view.getSubConditions());
if (requiresLeftActivation(lhs)) {
lhs.addChild(0, new Pattern(0, ClassObjectType.InitialFact_ObjectType));
}
}
use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class RuleBuilder method build.
/**
* Build the give rule into the
*/
public static void build(final RuleBuildContext context) {
RuleDescr ruleDescr = context.getRuleDescr();
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(ruleDescr.getLhs().getClass());
if (builder != null) {
// this is established during pre-processing, if it's query
Pattern prefixPattern = context.getPrefixPattern();
final GroupElement ce = (GroupElement) builder.build(context, getLhsForRuleUnit(context.getRule(), ruleDescr.getLhs()), prefixPattern);
context.getRule().setLhs(ce);
} else {
throw new RuntimeException("BUG: builder not found for descriptor class " + ruleDescr.getLhs().getClass());
}
// build all the rule's attributes
// must be after building LHS because some attributes require bindings from the LHS
buildAttributes(context);
// generate the main rule from the previously generated s.
if (!(ruleDescr instanceof QueryDescr)) {
// do not build the consequence if we have a query
ConsequenceBuilder consequenceBuilder = context.getDialect().getConsequenceBuilder();
consequenceBuilder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
for (String name : ruleDescr.getNamedConsequences().keySet()) {
consequenceBuilder.build(context, name);
}
}
}
use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class RuleUnlinkingTest method createNetworkNode.
private NetworkNode createNetworkNode(int id, int type, LeftTupleSource leftTupleSource, RuleImpl rule) {
MockObjectSource mockObjectSource = new MockObjectSource(8);
LeftTupleSink networkNode = null;
switch(type) {
case JOIN_NODE:
{
networkNode = new JoinNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
break;
}
case EXISTS_NODE:
{
networkNode = new ExistsNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
break;
}
case NOT_NODE:
{
networkNode = new NotNode(id, leftTupleSource, mockObjectSource, new EmptyBetaConstraints(), buildContext);
break;
}
case RULE_TERMINAL_NODE:
{
networkNode = new RuleTerminalNode(id, leftTupleSource, rule, new GroupElement(Type.AND), 0, buildContext);
break;
}
}
mockObjectSource.attach();
if (NodeTypeEnums.isLeftTupleSource(networkNode)) {
((LeftTupleSource) networkNode).attach(buildContext);
} else {
((RuleTerminalNode) networkNode).attach(buildContext);
}
return networkNode;
}
use of org.drools.core.rule.GroupElement in project drools by kiegroup.
the class BuildUtilsTest method testCalculateTemporalDistance.
/**
* Test method for {@link org.drools.core.reteoo.builder.BuildUtils#calculateTemporalDistance(org.drools.core.rule.GroupElement)}.
*/
@Test
public void testCalculateTemporalDistance() {
// input is here just for "documentation" purposes
Interval[][] input = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 4), new Interval(MIN, MAX), new Interval(MIN, MAX) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(MIN, MAX), new Interval(1, 2), new Interval(MIN, MAX) }, { new Interval(-4, 3), new Interval(MIN, MAX), new Interval(0, 0), new Interval(2, 3), new Interval(MIN, MAX) }, { new Interval(MIN, MAX), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(MIN, MAX), new Interval(-10, -1), new Interval(0, 0) } };
Interval[][] expected = new Interval[][] { { new Interval(0, 0), new Interval(-2, 2), new Interval(-3, 2), new Interval(-1, 4), new Interval(0, 14) }, { new Interval(-2, 2), new Interval(0, 0), new Interval(-2, 0), new Interval(1, 2), new Interval(2, 12) }, { new Interval(-2, 3), new Interval(0, 2), new Interval(0, 0), new Interval(2, 3), new Interval(3, 13) }, { new Interval(-4, 1), new Interval(-2, -1), new Interval(-3, -2), new Interval(0, 0), new Interval(1, 10) }, { new Interval(-14, 0), new Interval(-12, -2), new Interval(-13, -3), new Interval(-10, -1), new Interval(0, 0) } };
AfterEvaluatorDefinition evals = new AfterEvaluatorDefinition();
ClassObjectType ot = new ClassObjectType(StockTick.class, true);
Pattern a = new Pattern(0, ot, "$a");
Pattern b = new Pattern(1, ot, "$b");
b.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-2,2"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern c = new Pattern(2, ot, "$c");
c.addConstraint(new EvaluatorConstraint(new Declaration[] { a.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "-3,4"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern d = new Pattern(3, ot, "$d");
d.addConstraint(new EvaluatorConstraint(new Declaration[] { b.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,2"), new SelfReferenceClassFieldReader(StockTick.class)));
d.addConstraint(new EvaluatorConstraint(new Declaration[] { c.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "2,3"), new SelfReferenceClassFieldReader(StockTick.class)));
Pattern e = new Pattern(4, ot, "$e");
e.addConstraint(new EvaluatorConstraint(new Declaration[] { d.getDeclaration() }, evals.getEvaluator(ValueType.OBJECT_TYPE, AfterEvaluatorDefinition.AFTER, "1,10"), new SelfReferenceClassFieldReader(StockTick.class)));
GroupElement not = new GroupElement(Type.NOT);
not.addChild(e);
GroupElement and = new GroupElement(Type.AND);
and.addChild(a);
and.addChild(b);
and.addChild(c);
and.addChild(d);
and.addChild(not);
TemporalDependencyMatrix matrix = utils.calculateTemporalDistance(and);
// printMatrix( matrix.getMatrix() );
assertEqualsMatrix(expected, matrix.getMatrix());
assertEquals(15, matrix.getExpirationOffset(a));
assertEquals(11, matrix.getExpirationOffset(d));
assertEquals(1, matrix.getExpirationOffset(e));
}
Aggregations