Search in sources :

Example 31 with DrlParser

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"));
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 32 with DrlParser

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());
}
Also used : StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 33 with DrlParser

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());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 34 with DrlParser

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());
}
Also used : StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 35 with DrlParser

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()));
}
Also used : DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Aggregations

DrlParser (org.drools.drl.parser.DrlParser)41 PackageDescr (org.drools.drl.ast.descr.PackageDescr)38 Test (org.junit.Test)32 RuleDescr (org.drools.drl.ast.descr.RuleDescr)9 InputStreamReader (java.io.InputStreamReader)8 StringReader (java.io.StringReader)8 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 ParserError (org.drools.drl.parser.ParserError)4 DrlDumper (org.drools.mvel.DrlDumper)4 Reader (java.io.Reader)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 PatternDescr (org.drools.drl.ast.descr.PatternDescr)3 Ignore (org.junit.Ignore)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InputStreamResource (org.drools.core.io.impl.InputStreamResource)2 AttributeDescr (org.drools.drl.ast.descr.AttributeDescr)2 MVELConsequence (org.drools.mvel.expr.MVELConsequence)2