use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class DeclarationScopeResolver method getDeclarations.
/**
* Return all declarations scoped to the current
* RuleConditionElement in the build stack
*/
public Map<String, Declaration> getDeclarations(RuleImpl rule, String consequenceName) {
Map<String, Declaration> declarations = new HashMap<String, Declaration>();
for (RuleConditionElement aBuildStack : this.buildStack) {
// if we are inside of an OR we don't want each previous stack entry added because we can't see those variables
if (aBuildStack instanceof GroupElement && ((GroupElement) aBuildStack).getType() == GroupElement.Type.OR) {
continue;
}
// this may be optimized in the future to only re-add elements at
// scope breaks, like "NOT" and "EXISTS"
Map<String, Declaration> innerDeclarations = aBuildStack instanceof GroupElement ? ((GroupElement) aBuildStack).getInnerDeclarations(consequenceName) : aBuildStack.getInnerDeclarations();
declarations.putAll(innerDeclarations);
}
if (null != rule.getParent()) {
return getAllExtendedDeclaration(rule.getParent(), declarations);
}
return declarations;
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class DeclarationScopeResolver method isDuplicated.
public boolean isDuplicated(RuleImpl rule, final String name, final String type) {
if (this.globalMap.containsKey((name))) {
return true;
}
for (int i = this.buildStack.size() - 1; i >= 0; i--) {
final RuleConditionElement rce = buildStack.get(i);
final Declaration declaration = rce.resolveDeclaration(name);
if (declaration != null) {
// if it is an OR and it is duplicated, we can stop looking for duplication now
// as it is a separate logical branch
boolean inOr = ((rce instanceof GroupElement) && ((GroupElement) rce).isOr());
if (!inOr || type == null) {
return !inOr;
}
return !declaration.getDeclarationClass().getName().equals(type);
}
}
// look at parent rules
if (rule != null && rule.getParent() != null) {
// recursive algorithm for each parent
// -> lhs.getInnerDeclarations()
Declaration parentDeclaration = getExtendedDeclaration(rule.getParent(), name);
return null != parentDeclaration;
}
return false;
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class AccumulateBuilder method build.
/**
* @inheritDoc
*/
public void build(final BuildContext context, final BuildUtils utils, final RuleConditionElement rce) {
final Accumulate accumulate = (Accumulate) rce;
boolean existSubNetwort = false;
context.pushRuleComponent(accumulate);
final List<BetaNodeFieldConstraint> resultBetaConstraints = context.getBetaconstraints();
final List<AlphaNodeFieldConstraint> resultAlphaConstraints = context.getAlphaConstraints();
RuleConditionElement source = accumulate.getSource();
if (source instanceof GroupElement) {
GroupElement ge = (GroupElement) source;
if (ge.isAnd() && ge.getChildren().size() == 1) {
source = ge.getChildren().get(0);
}
}
// get builder for the pattern
final ReteooComponentBuilder builder = utils.getBuilderFor(source);
// save tuple source and current pattern offset for later if needed
LeftTupleSource tupleSource = context.getTupleSource();
final int currentPatternIndex = context.getCurrentPatternOffset();
// builds the source pattern
builder.build(context, utils, source);
// if object source is null, then we need to adapt tuple source into a subnetwork
if (context.getObjectSource() == null) {
// attach right input adapter node to convert tuple source into an object source
RightInputAdapterNode riaNode = context.getComponentFactory().getNodeFactoryService().buildRightInputNode(context.getNextId(), context.getTupleSource(), tupleSource, context);
// attach right input adapter node to convert tuple source into an object source
context.setObjectSource(utils.attachNode(context, riaNode));
// restore tuple source from before the start of the sub network
context.setTupleSource(tupleSource);
// create a tuple start equals constraint and set it in the context
final TupleStartEqualsConstraint constraint = TupleStartEqualsConstraint.getInstance();
final List<BetaNodeFieldConstraint> betaConstraints = new ArrayList<BetaNodeFieldConstraint>();
betaConstraints.add(constraint);
context.setBetaconstraints(betaConstraints);
existSubNetwort = true;
}
NodeFactory nfactory = context.getComponentFactory().getNodeFactoryService();
final BetaConstraints resultsBinder = utils.createBetaNodeConstraint(context, resultBetaConstraints, true);
final BetaConstraints sourceBinder = utils.createBetaNodeConstraint(context, context.getBetaconstraints(), false);
AccumulateNode accNode = nfactory.buildAccumulateNode(context.getNextId(), context.getTupleSource(), context.getObjectSource(), resultAlphaConstraints.toArray(new AlphaNodeFieldConstraint[resultAlphaConstraints.size()]), sourceBinder, resultsBinder, accumulate, existSubNetwort, context);
context.setTupleSource(utils.attachNode(context, accNode));
// source pattern was bound, so nulling context
context.setObjectSource(null);
context.setCurrentPatternOffset(currentPatternIndex);
context.popRuleComponent();
}
use of org.drools.core.rule.RuleConditionElement in project drools by kiegroup.
the class DescrBuilderTest method testDumperFromPkg.
@Test
public void testDumperFromPkg() {
// DROOLS-109
PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
String drl = new DrlDumper().dump(pkg);
System.out.println(drl);
KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
System.err.println(knowledgeBuilder.getErrors());
assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
KieSession knowledgeSession = kbase.newKieSession();
KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
RuleImpl r = ((RuleImpl) rule);
assertEquals(2, r.getLhs().getChildren().size());
Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
RuleConditionElement arg1 = iter.next();
assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
assertEquals(2, ((GroupElement) arg1).getChildren().size());
RuleConditionElement arg2 = iter.next();
assertTrue(arg2 instanceof Pattern);
}
Aggregations