Search in sources :

Example 16 with GlobalDescr

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

the class KnowledgeBuilderTest method testErrors.

@Test
public void testErrors() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    fieldBindingDescr = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    pattern.addConstraint(new ExprConstraintDescr("price == x"));
    // There is no m this should produce errors.
    ruleDescr.setConsequence("update(m);");
    builder.addPackage(packageDescr);
    assertTrue(builder.getErrors().getErrors().length > 0);
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 17 with GlobalDescr

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

the class XmlPackageReaderTest method testParseFunction.

@Test
public void testParseFunction() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFunction.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 18 with GlobalDescr

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

the class XmlPackageReaderTest method testParseQuery.

@Test
public void testParseQuery() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseQuery.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
    final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
    final List names = functionDescr.getParameterNames();
    assertEquals("foo", names.get(0));
    assertEquals("bada", names.get(1));
    final List types = functionDescr.getParameterTypes();
    assertEquals("Bar", types.get(0));
    assertEquals("Bing", types.get(1));
    assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
    final QueryDescr queryDescr = (QueryDescr) packageDescr.getRules().get(0);
    assertEquals("my query", queryDescr.getName());
    final AndDescr lhs = queryDescr.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    final PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Foo", patternDescr.getObjectType());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) QueryDescr(org.drools.drl.ast.descr.QueryDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) FunctionDescr(org.drools.drl.ast.descr.FunctionDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 19 with GlobalDescr

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

the class XmlPackageReaderTest method testParseGlobal.

@Test
public void testParseGlobal() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseGlobal.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
    final List imports = packageDescr.getImports();
    assertEquals(2, imports.size());
    assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
    final List globals = packageDescr.getGlobals();
    assertEquals(2, globals.size());
    final GlobalDescr x = (GlobalDescr) globals.get(0);
    final GlobalDescr yada = (GlobalDescr) globals.get(1);
    assertEquals("com.sample.X", x.getType());
    assertEquals("x", x.getIdentifier());
    assertEquals("com.sample.Yada", yada.getType());
    assertEquals("yada", yada.getIdentifier());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) InputStreamReader(java.io.InputStreamReader) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 20 with GlobalDescr

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

the class KnowledgeBuilderTest method testPredicate.

@Test
public void testPredicate() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    final BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    final BindingDescr fieldBindingDescr2 = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr2);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    final ExprConstraintDescr predicate = new ExprConstraintDescr("eval(( ( Integer )map.get( new Integer(x) )).intValue() == y)");
    pattern.addConstraint(predicate);
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)24 PackageDescr (org.drools.drl.ast.descr.PackageDescr)14 RuleDescr (org.drools.drl.ast.descr.RuleDescr)13 Test (org.junit.Test)12 AndDescr (org.drools.drl.ast.descr.AndDescr)10 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 PatternDescr (org.drools.drl.ast.descr.PatternDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 BindingDescr (org.drools.drl.ast.descr.BindingDescr)6 Cheese (org.drools.mvel.compiler.Cheese)6 List (java.util.List)5 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)5 FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)5 InputStreamReader (java.io.InputStreamReader)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)4 ImportDescr (org.drools.drl.ast.descr.ImportDescr)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Iterator (java.util.Iterator)2