use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class AccumulateInlineVisitor method findBoundIdentifier.
private Optional<String> findBoundIdentifier(AccumulateDescr descr, BaseDescr input) {
BlockStmt actionBlock = parseBlockAddSemicolon(descr.getActionCode());
Collection<String> allNamesInActionBlock = collectNamesInBlock(actionBlock, context);
if (allNamesInActionBlock.size() == 1) {
return Optional.of(allNamesInActionBlock.iterator().next());
}
if (input instanceof PatternDescr) {
return Optional.of(((PatternDescr) input).getIdentifier());
}
return ((AndDescr) input).getDescrs().stream().filter(b -> ((PatternDescr) b).getAllBoundIdentifiers().containsAll(allNamesInActionBlock)).findFirst().map(binding -> ((PatternDescr) binding).getIdentifier());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class JavaAccumulateBuilder method build.
public RuleConditionElement build(final RuleBuildContext context, final BaseDescr descr, final Pattern prefixPattern) {
final AccumulateDescr accumDescr = (AccumulateDescr) descr;
if (!accumDescr.hasValidInput()) {
return null;
}
// build source
BaseDescr input = accumDescr.getInput();
if (input instanceof AndDescr && ((AndDescr) input).getDescrs().size() == 1) {
input = ((AndDescr) input).getDescrs().get(0);
}
final RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder(input.getClass());
final RuleConditionElement source = builder.build(context, input);
if (source == null) {
return null;
}
final boolean readLocalsFromTuple = PackageBuilderUtil.isReadLocalsFromTuple(context, accumDescr, source);
Map<String, Declaration> declsInScope = context.getDeclarationResolver().getDeclarations(context.getRule());
if (prefixPattern != null && prefixPattern.getDeclaration() != null) {
declsInScope.remove(prefixPattern.getDeclaration().getIdentifier());
}
Map<String, Class<?>> declCls = DeclarationScopeResolver.getDeclarationClasses(declsInScope);
Accumulate accumulate;
if (accumDescr.isExternalFunction()) {
// if it uses 1+ external function, build methods for them
accumulate = buildExternalFunctionCall(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
} else {
// if it uses inline code, build the class for it
accumulate = buildInlineAccumulate(context, accumDescr, source, declsInScope, declCls, readLocalsFromTuple);
}
return accumulate;
}
use of org.drools.drl.ast.descr.AndDescr 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.drl.ast.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testLiteralBoolAndNegativeNumbersRule.
@Test
public void testLiteralBoolAndNegativeNumbersRule() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "literal_bool_and_negative.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertNotNull(rule);
assertEquals("simple_rule", rule.getName());
assertNotNull(rule.getLhs());
assertEqualsIgnoreWhitespace("cons();", (String) rule.getConsequence());
final AndDescr lhs = rule.getLhs();
assertEquals(3, lhs.getDescrs().size());
PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
assertEquals(1, pattern.getConstraint().getDescrs().size());
AndDescr fieldAnd = (AndDescr) pattern.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertEquals("bar == false", fld.getExpression());
pattern = (PatternDescr) lhs.getDescrs().get(1);
assertEquals(1, pattern.getConstraint().getDescrs().size());
fieldAnd = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertEquals("boo > -42", fld.getText());
pattern = (PatternDescr) lhs.getDescrs().get(2);
assertEquals(1, pattern.getConstraint().getDescrs().size());
fieldAnd = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertEquals("boo > -42.42", fld.getText());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class RuleParserTest method testOrWithSpecialBind.
@Test
public void testOrWithSpecialBind() throws Exception {
String source = "rule \"A and (B or C or D)\" \n" + " when \n" + " pdo1 : ParametricDataObject( paramID == 101, stringValue == \"1000\" ) and \n" + " pdo2 :(ParametricDataObject( paramID == 101, stringValue == \"1001\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1002\" ) or \n" + " ParametricDataObject( paramID == 101, stringValue == \"1003\" )) \n" + " then \n" + " System.out.println( \"Rule: A and (B or C or D) Fired. pdo1: \" + pdo1 + \" pdo2: \"+ pdo2); \n" + "end\n";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = pkg.getRules().get(0);
AndDescr lhs = rule.getLhs();
assertEquals(2, lhs.getDescrs().size());
PatternDescr pdo1 = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("pdo1", pdo1.getIdentifier());
OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
assertEquals(3, or.getDescrs().size());
for (BaseDescr pdo2 : or.getDescrs()) {
assertEquals("pdo2", ((PatternDescr) pdo2).getIdentifier());
}
}
Aggregations