use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testFromWithInlineListIndex.
@Test
public void testFromWithInlineListIndex() throws Exception {
String source = "rule XYZ \n" + " when \n" + " o: Order( ) \n" + " Number( ) from [1, 2, 3][1] \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n" + " o.addError(\"Invalid customer id\"); \n" + "end \n";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("XYZ", rule.getName());
assertFalse(parser.hasErrors());
PatternDescr number = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("[1, 2, 3][1]", ((FromDescr) number.getSource()).getDataSource().toString());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testExpanderMultipleConstraints.
@Test
public void testExpanderMultipleConstraints() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final PackageDescr pkg = parser.parse(this.getReader("expander_multiple_constraints.dslr"), this.getReader("multiple_constraints.dsl"));
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(2, rule.getLhs().getDescrs().size());
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertEquals(2, pattern.getConstraint().getDescrs().size());
assertEquals("age < 42", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0)).getExpression());
assertEquals("location==atlanta", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1)).getExpression());
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Bar", pattern.getObjectType());
assertNotNull((String) rule.getConsequence());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testNestedCEs.
@Test
public void testNestedCEs() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "nested_conditional_elements.drl");
assertNotNull(rule);
final AndDescr root = rule.getLhs();
final NotDescr not1 = (NotDescr) root.getDescrs().get(0);
final AndDescr and1 = (AndDescr) not1.getDescrs().get(0);
final PatternDescr state = (PatternDescr) and1.getDescrs().get(0);
final NotDescr not2 = (NotDescr) and1.getDescrs().get(1);
final AndDescr and2 = (AndDescr) not2.getDescrs().get(0);
final PatternDescr person = (PatternDescr) and2.getDescrs().get(0);
final PatternDescr cheese = (PatternDescr) and2.getDescrs().get(1);
final PatternDescr person2 = (PatternDescr) root.getDescrs().get(1);
final OrDescr or = (OrDescr) root.getDescrs().get(2);
final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(0);
final PatternDescr cheese3 = (PatternDescr) or.getDescrs().get(1);
assertEquals(state.getObjectType(), "State");
assertEquals(person.getObjectType(), "Person");
assertEquals(cheese.getObjectType(), "Cheese");
assertEquals(person2.getObjectType(), "Person");
assertEquals(cheese2.getObjectType(), "Cheese");
assertEquals(cheese3.getObjectType(), "Cheese");
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testForallWithFrom.
@Test
public void testForallWithFrom() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "forallwithfrom.drl");
assertEquals(1, pkg.getRules().size());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final ForallDescr forall = (ForallDescr) rule.getLhs().getDescrs().get(0);
assertEquals(2, forall.getDescrs().size());
final PatternDescr pattern = forall.getBasePattern();
assertEquals("Person", pattern.getObjectType());
assertEquals("$village", ((FromDescr) pattern.getSource()).getDataSource().toString());
final List<BaseDescr> remaining = forall.getRemainingPatterns();
assertEquals(1, remaining.size());
final PatternDescr cheese = (PatternDescr) remaining.get(0);
assertEquals("Cheese", cheese.getObjectType());
assertEquals("$cheesery", ((FromDescr) cheese.getSource()).getDataSource().toString());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class RuleParserTest method testImportAccumulate.
@Test
public void testImportAccumulate() throws Exception {
String drl = "package org.drools.mvel.compiler\n" + "import acc foo.Bar baz\n" + "import accumulate foo.Bar2 baz2\n" + "rule \"Accumulate 1\"\n" + "when\n" + " acc( Cheese( $price : price ),\n" + " $v1 : baz( $price ), \n" + " $v2 : baz2( $price ) )\n" + "then\n" + "end\n";
final PackageDescr pkg = (PackageDescr) parse("compilationUnit", drl);
assertEquals(2, pkg.getAccumulateImports().size());
AccumulateImportDescr imp = (AccumulateImportDescr) pkg.getAccumulateImports().get(0);
assertEquals("foo.Bar", imp.getTarget());
assertEquals("baz", imp.getFunctionName());
imp = (AccumulateImportDescr) pkg.getAccumulateImports().get(1);
assertEquals("foo.Bar2", imp.getTarget());
assertEquals("baz2", imp.getFunctionName());
assertEquals(1, pkg.getRules().size());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
PatternDescr out = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Object", out.getObjectType());
AccumulateDescr accum = (AccumulateDescr) out.getSource();
assertTrue(accum.isExternalFunction());
List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
assertEquals(2, functions.size());
assertEquals("baz", functions.get(0).getFunction());
assertEquals("$v1", functions.get(0).getBind());
assertEquals("$price", functions.get(0).getParams()[0]);
assertEquals("baz2", functions.get(1).getFunction());
assertEquals("$v2", functions.get(1).getBind());
assertEquals("$price", functions.get(1).getParams()[0]);
final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
assertEquals("Cheese", pattern.getObjectType());
}
Aggregations