use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class RuleBuilderTest method testBuild.
@Test
public void testBuild() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(new PackageDescr("org.drools"));
InternalKnowledgePackage pkg = kBuilder.getPackage("org.drools");
final PackageDescr pkgDescr = parser.parse(new InputStreamReader(getClass().getResourceAsStream("nestedConditionalElements.drl")));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
pkg.addGlobal("results", List.class);
final RuleDescr ruleDescr = pkgDescr.getRules().get(0);
final String ruleClassName = "RuleClassName.java";
ruleDescr.setClassName(ruleClassName);
ruleDescr.addAttribute(new AttributeDescr("dialect", "java"));
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
final RuleImpl rule = kBuilder.getPackage("org.drools.mvel.compiler").getRule("test nested CEs");
assertEquals("There should be 2 rule level declarations", 2, rule.getDeclarations().size());
// second GE should be a not
final GroupElement not = (GroupElement) rule.getLhs().getChildren().get(1);
assertTrue(not.isNot());
// not has no outer declarations
assertTrue(not.getOuterDeclarations().isEmpty());
assertEquals(1, not.getInnerDeclarations().size());
assertTrue(not.getInnerDeclarations().keySet().contains("$state"));
// second not
final GroupElement not2 = (GroupElement) ((GroupElement) not.getChildren().get(0)).getChildren().get(1);
assertTrue(not2.isNot());
// not has no outer declarations
assertTrue(not2.getOuterDeclarations().isEmpty());
assertEquals(1, not2.getInnerDeclarations().size());
assertTrue(not2.getInnerDeclarations().keySet().contains("$likes"));
}
use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class RuleParserTest method testPackageWithError2.
@Test
public void testPackageWithError2() throws Exception {
final String source = "package 12 12312 231";
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final PackageDescr pkg = parser.parse(true, new StringReader(source));
assertTrue(parser.hasErrors());
assertEquals("", pkg.getName());
}
use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class RuleParserTest method testExpanderMultipleConstraintsFlush.
@Test
public void testExpanderMultipleConstraintsFlush() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
// this is similar to the other test, but it requires a flush to add the
// constraints
final PackageDescr pkg = parser.parse(this.getReader("expander_multiple_constraints_flush.dslr"), this.getReader("multiple_constraints.dsl"));
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final 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());
assertNotNull((String) rule.getConsequence());
}
use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class RuleParserTest method testPackageWithError.
@Test
public void testPackageWithError() throws Exception {
final String source = "package 12 foo.bar.baz";
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final PackageDescr pkg = parser.parse(true, new StringReader(source));
assertTrue(parser.hasErrors());
assertEquals("foo.bar.baz", pkg.getName());
}
use of org.drools.drl.parser.DrlParser in project drools by kiegroup.
the class DRLIncompleteCodeTest method testIncompleteCode3.
@Test
public void testIncompleteCode3() throws DroolsParserException, RecognitionException {
String input = "rule MyRule when Class ( property > somevalue ) then end query MyQuery Class ( property == collection ) end ";
DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
PackageDescr descr = parser.parse(true, input);
assertNotNull(descr);
assertEquals("MyRule", descr.getRules().get(0).getName());
assertNotNull(descr);
assertEquals("MyQuery", descr.getRules().get(1).getName());
assertEquals(Location.LOCATION_RHS, getLastIntegerValue(parser.getEditorSentences().get(0).getContent()));
}
Aggregations