use of org.drools.core.rule.GroupElement 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.GroupElement 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.GroupElement in project drools by kiegroup.
the class RuleBuilderTest method testBuildBigIntegerLiteralConstraint.
@Test
public void testBuildBigIntegerLiteralConstraint() throws Exception {
final PackageDescr pkgDescr = new PackageDescr("org.drools");
final RuleDescr ruleDescr = new RuleDescr("Test Rule");
AndDescr andDescr = new AndDescr();
PatternDescr patDescr = new PatternDescr("java.math.BigInteger", "$bd");
ExprConstraintDescr fcd = new ExprConstraintDescr("this==10");
patDescr.addConstraint(fcd);
andDescr.addDescr(patDescr);
ruleDescr.setLhs(andDescr);
ruleDescr.setConsequence("");
pkgDescr.addRule(ruleDescr);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
final RuleImpl rule = kBuilder.getPackages()[0].getRule("Test Rule");
final GroupElement and = rule.getLhs();
final Pattern pat = (Pattern) and.getChildren().get(0);
if (pat.getConstraints().get(0) instanceof MvelConstraint) {
final MvelConstraint fc = (MvelConstraint) pat.getConstraints().get(0);
assertTrue("Wrong class. Expected java.math.BigInteger. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigInteger);
}
}
use of org.drools.core.rule.GroupElement 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