use of org.drools.core.rule.Forall in project drools by kiegroup.
the class ForallBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final ForallDescr forallDescr = (ForallDescr) descr;
final PatternBuilder patternBuilder = (PatternBuilder) context.getDialect().getBuilder(PatternDescr.class);
final Pattern basePattern = (Pattern) patternBuilder.build(context, forallDescr.getBasePattern());
if (basePattern == null) {
return null;
}
final Forall forall = new Forall(basePattern);
// adding the newly created forall CE to the build stack
// this is necessary in case of local declaration usage
context.getDeclarationResolver().pushOnBuildStack(forall);
for (BaseDescr baseDescr : forallDescr.getRemainingPatterns()) {
final Pattern anotherPattern = (Pattern) patternBuilder.build(context, (PatternDescr) baseDescr);
forall.addRemainingPattern(anotherPattern);
}
if (forallDescr.getDescrs().size() == 1) {
// An optimization for unlinking, where we allow unlinking if the resulting 'not' node has no constraints
// we need to record this here, due to getRemainingPatterns injecting "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
// which we wish to ignore
PatternDescr p = (PatternDescr) forallDescr.getDescrs().get(0);
if (p.getConstraint().getDescrs().isEmpty()) {
forall.setEmptyBetaConstraints(true);
}
}
// poping the forall
context.getDeclarationResolver().popBuildStack();
return forall;
}
use of org.drools.core.rule.Forall in project drools by kiegroup.
the class ForallBuilder method build.
/**
* @inheritDoc
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
final Forall forall = (Forall) rce;
context.pushRuleComponent(forall);
// forall can be translated into
// not( basePattern and not( <remaining_patterns>+ ) )
// so we just do that:
final GroupElement and = GroupElementFactory.newAndInstance();
and.addChild(forall.getBasePattern());
final GroupElement not2 = GroupElementFactory.newNotInstance();
if (forall.getRemainingPatterns().size() == 1) {
if (forall.isEmptyBetaConstraints()) {
// The reason why this is here is because forall can inject a
// "this == " + BASE_IDENTIFIER $__forallBaseIdentifier
// Which we don't want to actually count in the case of forall node linking
context.setEmptyForAllBetaConstraints();
}
not2.addChild(forall.getRemainingPatterns().get(0));
and.addChild(not2);
} else if (forall.getRemainingPatterns().size() > 1) {
final GroupElement and2 = GroupElementFactory.newAndInstance();
for (Pattern pattern : forall.getRemainingPatterns()) {
and2.addChild(pattern);
}
not2.addChild(and2);
and.addChild(not2);
}
final GroupElement not = GroupElementFactory.newNotInstance();
not.addChild(and);
// get builder for the CEs
final ReteooComponentBuilder builder = utils.getBuilderFor(not);
// builds the CEs
builder.build(context, utils, not);
context.popRuleComponent();
}
Aggregations