Search in sources :

Example 66 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testGlobal1.

@Test
public void testGlobal1() throws Exception {
    final String source = "package foo.bar.baz\n" + "import com.foo.Bar\n" + "global java.util.List<java.util.Map<String,Integer>> aList;\n" + "global Integer aNumber";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertEquals("foo.bar.baz", pkg.getName());
    assertEquals(1, pkg.getImports().size());
    ImportDescr impdescr = pkg.getImports().get(0);
    assertEquals("com.foo.Bar", impdescr.getTarget());
    assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
    assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
    assertEquals(2, pkg.getGlobals().size());
    GlobalDescr global = pkg.getGlobals().get(0);
    assertEquals("java.util.List<java.util.Map<String,Integer>>", global.getType());
    assertEquals("aList", global.getIdentifier());
    assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
    assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
    global = pkg.getGlobals().get(1);
    assertEquals("Integer", global.getType());
    assertEquals("aNumber", global.getIdentifier());
    assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
    assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 67 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testAccumulateMultipleFunctionsConstraint.

@Test
public void testAccumulateMultipleFunctionsConstraint() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulateMultipleFunctionsConstraint.drl");
    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());
    assertEquals(2, out.getConstraint().getDescrs().size());
    assertEquals("$a1 > 10 && $M1 <= 100", out.getConstraint().getDescrs().get(0).toString());
    assertEquals("$m1 == 5", out.getConstraint().getDescrs().get(1).toString());
    AccumulateDescr accum = (AccumulateDescr) out.getSource();
    assertTrue(accum.isExternalFunction());
    List<AccumulateDescr.AccumulateFunctionCallDescr> functions = accum.getFunctions();
    assertEquals(3, functions.size());
    assertEquals("average", functions.get(0).getFunction());
    assertEquals("$a1", functions.get(0).getBind());
    assertEquals("$price", functions.get(0).getParams()[0]);
    assertEquals("min", functions.get(1).getFunction());
    assertEquals("$m1", functions.get(1).getBind());
    assertEquals("$price", functions.get(1).getParams()[0]);
    assertEquals("max", functions.get(2).getFunction());
    assertEquals("$M1", functions.get(2).getBind());
    assertEquals("$price", functions.get(2).getParams()[0]);
    final PatternDescr pattern = (PatternDescr) accum.getInputPattern();
    assertEquals("Cheese", pattern.getObjectType());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Example 68 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testRuleOldSyntax2.

@Test
public void testRuleOldSyntax2() throws Exception {
    final String source = "rule \"Test\" when ( $r :LiteralRestriction( operator == Operator.EQUAL ) ) then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("Test", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    PatternDescr patternDescr = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("$r", patternDescr.getIdentifier());
    assertEquals(1, patternDescr.getDescrs().size());
    ExprConstraintDescr fieldConstraintDescr = (ExprConstraintDescr) patternDescr.getDescrs().get(0);
    assertEquals("operator == Operator.EQUAL", fieldConstraintDescr.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 69 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testFunctions.

@Test
public void testFunctions() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "functions.drl");
    assertEquals(2, pkg.getRules().size());
    final List<FunctionDescr> functions = pkg.getFunctions();
    assertEquals(2, functions.size());
    FunctionDescr func = functions.get(0);
    assertEquals("functionA", func.getName());
    assertEquals("String", func.getReturnType());
    assertEquals(2, func.getParameterNames().size());
    assertEquals(2, func.getParameterTypes().size());
    assertEquals(19, func.getLine());
    assertEquals(0, func.getColumn());
    assertEquals("String", func.getParameterTypes().get(0));
    assertEquals("s", func.getParameterNames().get(0));
    assertEquals("Integer", func.getParameterTypes().get(1));
    assertEquals("i", func.getParameterNames().get(1));
    assertEqualsIgnoreWhitespace("foo();", func.getBody());
    func = functions.get(1);
    assertEquals("functionB", func.getName());
    assertEqualsIgnoreWhitespace("bar();", func.getText());
}
Also used : PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) Test(org.junit.Test)

Example 70 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class RuleParserTest method testBasicBinding.

// @Test public void testExpanderUnExpandableErrorLines() throws Exception {
// 
// //stubb expander
// final ExpanderResolver res = new ExpanderResolver() {
// public Expander get(String name,
// String config) {
// return new Expander() {
// public String expand(String scope,
// String pattern) {
// if ( pattern.startsWith( "Good" ) ) {
// return pattern;
// } else {
// throw new IllegalArgumentException( "whoops" );
// }
// 
// }
// };
// }
// };
// 
// final DRLParser parser = parseResource( "expander_line_errors.dslr" );
// parser.setExpanderResolver( res );
// parser.compilationUnit();
// assertTrue( parser.hasErrors() );
// 
// final List messages = parser.getErrorMessages();
// assertEquals( messages.size(),
// parser.getErrors().size() );
// 
// assertEquals( 4,
// parser.getErrors().size() );
// assertEquals( ExpanderException.class,
// parser.getErrors().get( 0 ).getClass() );
// assertEquals( 8,
// ((RecognitionException) parser.getErrors().get( 0 )).line );
// assertEquals( 10,
// ((RecognitionException) parser.getErrors().get( 1 )).line );
// assertEquals( 12,
// ((RecognitionException) parser.getErrors().get( 2 )).line );
// assertEquals( 13,
// ((RecognitionException) parser.getErrors().get( 3 )).line );
// 
// final PackageDescr pack = parser.getPackageDescr();
// assertNotNull( pack );
// 
// final ExpanderException ex = (ExpanderException) parser.getErrors().get(
// 0 );
// assertTrue( ex.getMessage().indexOf( "whoops" ) > -1 );
// 
// }
@Test
public void testBasicBinding() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "basic_binding.drl");
    final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get(0);
    final AndDescr lhs = ruleDescr.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    final PatternDescr cheese = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", cheese.getObjectType());
    assertEquals(1, cheese.getConstraint().getDescrs().size());
    final ExprConstraintDescr fieldBinding = (ExprConstraintDescr) cheese.getDescrs().get(0);
    assertEquals("$type:type", fieldBinding.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.drl.ast.descr.PackageDescr)262 Test (org.junit.Test)210 RuleDescr (org.drools.drl.ast.descr.RuleDescr)104 PatternDescr (org.drools.drl.ast.descr.PatternDescr)85 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)52 AndDescr (org.drools.drl.ast.descr.AndDescr)39 DrlParser (org.drools.drl.parser.DrlParser)39 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 InputStreamReader (java.io.InputStreamReader)23 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)23 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)23 KieSession (org.kie.api.runtime.KieSession)19 List (java.util.List)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)17 KiePackage (org.kie.api.definition.KiePackage)17 DrlDumper (org.drools.mvel.DrlDumper)16 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15