Search in sources :

Example 6 with XmlPackageReader

use of org.jbpm.compiler.xml.compiler.XmlPackageReader in project kogito-runtimes 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);
    assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Example 7 with XmlPackageReader

use of org.jbpm.compiler.xml.compiler.XmlPackageReader in project kogito-runtimes by kiegroup.

the class XmlPackageReaderTest method testAccumulate.

@Test
public void testAccumulate() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseAccumulate.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertThat(packageDescr).isNotNull();
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
    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());
    patternobj = obj.getLhs().getDescrs().get(1);
    assertTrue(patternobj instanceof PatternDescr);
    final PatternDescr patternmax = (PatternDescr) patternobj;
    assertEquals(patternmax.getIdentifier(), "max");
    assertEquals(patternmax.getObjectType(), "Number");
    accumulatedescr = (AccumulateDescr) patternmax.getSource();
    assertTrue(accumulatedescr.isExternalFunction());
    assertEquals("max", accumulatedescr.getFunctions().get(0).getFunction());
    assertNull(accumulatedescr.getInitCode());
    assertNull(accumulatedescr.getActionCode());
    assertNull(accumulatedescr.getResultCode());
    assertNull(accumulatedescr.getReverseCode());
}
Also used : InputStreamReader(java.io.InputStreamReader) 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) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Example 8 with XmlPackageReader

use of org.jbpm.compiler.xml.compiler.XmlPackageReader in project kogito-runtimes by kiegroup.

the class XmlPackageReaderTest method testParseFrom.

@Test
public void testParseFrom() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFrom.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertThat(packageDescr).isNotNull();
    RuleDescr obj = packageDescr.getRules().get(0);
    PatternDescr patterndescr = (PatternDescr) obj.getLhs().getDescrs().get(0);
    FromDescr from = (FromDescr) patterndescr.getSource();
    MVELExprDescr accessordescriptor = (MVELExprDescr) from.getDataSource();
    assertEquals("cheesery.getCheeses(i+4)", accessordescriptor.getExpression());
    assertEquals(patterndescr.getObjectType(), "Cheese");
    assertEquals(patterndescr.getIdentifier(), "cheese");
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Example 9 with XmlPackageReader

use of org.jbpm.compiler.xml.compiler.XmlPackageReader in project kogito-runtimes by kiegroup.

the class XmlPackageReaderTest method testParseImport.

@Test
public void testParseImport() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseImport.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertThat(packageDescr).isNotNull();
    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 functionImport = packageDescr.getFunctionImports();
    assertEquals("org.drools.function", ((FunctionImportDescr) functionImport.get(0)).getTarget());
}
Also used : InputStreamReader(java.io.InputStreamReader) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Example 10 with XmlPackageReader

use of org.jbpm.compiler.xml.compiler.XmlPackageReader in project kogito-runtimes 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);
    assertThat(expectedWithoutHeader).isEqualToIgnoringWhitespace(actual);
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Aggregations

XmlPackageReader (org.jbpm.compiler.xml.compiler.XmlPackageReader)17 InputStreamReader (java.io.InputStreamReader)16 PackageDescr (org.drools.drl.ast.descr.PackageDescr)16 Test (org.junit.jupiter.api.Test)15 List (java.util.List)8 PatternDescr (org.drools.drl.ast.descr.PatternDescr)7 RuleDescr (org.drools.drl.ast.descr.RuleDescr)7 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)4 AndDescr (org.drools.drl.ast.descr.AndDescr)3 FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)3 DrlDumper (org.drools.mvel.DrlDumper)3 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)2 SemanticKnowledgeBuilderConfigurationImpl (org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl)2 ExistsDescr (org.drools.drl.ast.descr.ExistsDescr)1 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)1 ForallDescr (org.drools.drl.ast.descr.ForallDescr)1 FromDescr (org.drools.drl.ast.descr.FromDescr)1 MVELExprDescr (org.drools.drl.ast.descr.MVELExprDescr)1 NotDescr (org.drools.drl.ast.descr.NotDescr)1 QueryDescr (org.drools.drl.ast.descr.QueryDescr)1