use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.
the class XmlPackageReaderTest method testParseRule.
@Test
public void testParseRule() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseRule.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
String expected = StringUtils.readFileAsString(new InputStreamReader(getClass().getResourceAsStream("test_ParseRule.drl")));
// remove license header as that one is not stored in the XML
String expectedWithoutHeader = removeLicenseHeader(expected);
System.out.println(expectedWithoutHeader);
String actual = new DrlDumper().dump(packageDescr);
Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
use of org.drools.compiler.compiler.xml.XmlPackageReader in project drools by kiegroup.
the class XmlPackageReaderTest method testParseLhs.
@Test
public void testParseLhs() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseLhs.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
String expected = StringUtils.readFileAsString(new InputStreamReader(getClass().getResourceAsStream("test_ParseLhs.drl")));
String expectedWithoutHeader = removeLicenseHeader(expected);
String actual = new DrlDumper().dump(packageDescr);
Assertions.assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
use of org.drools.compiler.compiler.xml.XmlPackageReader 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.compiler.compiler.xml.XmlPackageReader 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.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());
}
use of org.drools.compiler.compiler.xml.XmlPackageReader 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.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());
}
Aggregations