use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseRhs.
@Test
public void testParseRhs() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseRhs.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 RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
assertEquals("my rule", ruleDescr.getName());
final String consequence = (String) ruleDescr.getConsequence();
assertNotNull(consequence);
assertEquals("System.out.println( \"hello\" );", consequence.trim());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseCollect.
@Test
public void testParseCollect() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseCollect.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
String expected = StringUtils.readFileAsString(new InputStreamReader(getClass().getResourceAsStream("test_ParseCollect.drl")));
String expectedWithoutHeader = removeLicenseHeader(expected);
String actual = new DrlDumper().dump(packageDescr);
Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testAccumulateMultiPattern.
@Test
public void testAccumulateMultiPattern() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseAccumulate.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(1);
Object patternobj = obj.getLhs().getDescrs().get(0);
assertTrue(patternobj instanceof PatternDescr);
final PatternDescr patterncheese = (PatternDescr) patternobj;
assertEquals(patterncheese.getIdentifier(), "cheese");
assertEquals(patterncheese.getObjectType(), "Cheese");
AccumulateDescr accumulatedescr = (AccumulateDescr) patterncheese.getSource();
assertEquals("total += $cheese.getPrice();", accumulatedescr.getActionCode());
assertEquals("int total = 0;", accumulatedescr.getInitCode());
assertEquals("new Integer( total ) );", accumulatedescr.getResultCode());
AndDescr anddescr = (AndDescr) accumulatedescr.getInput();
List descrlist = anddescr.getDescrs();
PatternDescr[] listpattern = (PatternDescr[]) descrlist.toArray(new PatternDescr[descrlist.size()]);
assertEquals(listpattern[0].getObjectType(), "Milk");
assertEquals(listpattern[1].getObjectType(), "Cup");
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class DescrBuilderTest method testDumperTimer.
@Test
public void testDumperTimer() {
PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").attribute("timer").value("cron:0/5 * * * * ?").end().lhs().end().rhs("").end().end().getDescr();
String drl = new DrlDumper().dump(pkg);
assertTrue(drl.contains("timer (cron:0/5 * * * * ?)"));
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class DescrBuilderTest method testDumperPositional.
@Test
public void testDumperPositional() {
PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().pattern().type("Integer").constraint("this > 10", true).constraint("this > 11", true).constraint("this > 12", false).constraint("this > 13", false).end().end().rhs("").end().end().getDescr();
String drl = new DrlDumper().dump(pkg);
assertTrue(drl.contains("Integer( this > 10, this > 11; this > 12, this > 13 )"));
}
Aggregations