Search in sources :

Example 1 with XmlPackageReader

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

the class DumperTestHelper method XmlFile.

public static void XmlFile(String filename) throws Exception {
    SemanticKnowledgeBuilderConfigurationImpl conf = new SemanticKnowledgeBuilderConfigurationImpl();
    XmlPackageReader xmlPackageReader = new XmlPackageReader(conf.getSemanticModules());
    xmlPackageReader.getParser().setClassLoader(DumperTestHelper.class.getClassLoader());
    xmlPackageReader.read(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final PackageDescr pkgOriginal = xmlPackageReader.getPackageDescr();
    final XmlDumper dumper = new XmlDumper();
    final String result = dumper.dump(pkgOriginal);
    String buffer = readFile(filename);
    System.out.println(buffer);
    System.out.println(result);
    assertThat(buffer).isEqualToIgnoringWhitespace(result);
    assertThat(result).isNotNull();
}
Also used : SemanticKnowledgeBuilderConfigurationImpl(org.jbpm.compiler.xml.compiler.SemanticKnowledgeBuilderConfigurationImpl) InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlDumper(org.jbpm.compiler.xml.compiler.XmlDumper) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader)

Example 2 with XmlPackageReader

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

the class XmlPackageReaderTest method testParsePackageName.

@Test
public void testParsePackageName() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParsePackageName.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertThat(packageDescr).isNotNull();
    assertEquals("com.sample", packageDescr.getName());
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Example 3 with XmlPackageReader

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

the class XmlPackageReaderTest method testParseSimpleRule.

@Test
public void testParseSimpleRule() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_SimpleRule1.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.List", ((ImportDescr) imports.get(0)).getTarget());
    assertEquals("org.drools.mvel.compiler.Person", ((ImportDescr) imports.get(1)).getTarget());
    RuleDescr ruleDescr = (RuleDescr) packageDescr.getRules().get(0);
    assertEquals("simple_rule1", ruleDescr.getName());
    AndDescr lhs = ruleDescr.getLhs();
    PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    ExprConstraintDescr expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == \"darth\"", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(1);
    assertEquals("simple_rule2", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || == -3.5", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(2);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("age == 35 || (!= 7.0 && != -70)", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(3);
    assertEquals("simple_rule3", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("name == $s", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(4);
    assertEquals("simple_rule4", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || (age == 35 || (!= 7.0 && != -70))", expr.getExpression());
    ruleDescr = (RuleDescr) packageDescr.getRules().get(5);
    assertEquals("simple_rule5", ruleDescr.getName());
    lhs = ruleDescr.getLhs();
    patternDescr = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("Person", patternDescr.getObjectType());
    expr = (ExprConstraintDescr) ((AndDescr) patternDescr.getConstraint()).getDescrs().get(0);
    assertEquals("(name == $s) || ((age != 34) && (age != 37) && (name != \"yoda\"))", expr.getExpression());
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) XmlPackageReader(org.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Example 4 with XmlPackageReader

use of org.jbpm.compiler.xml.compiler.XmlPackageReader in project kogito-runtimes 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();
    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 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.jbpm.compiler.xml.compiler.XmlPackageReader) Test(org.junit.jupiter.api.Test)

Example 5 with XmlPackageReader

use of org.jbpm.compiler.xml.compiler.XmlPackageReader in project kogito-runtimes 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();
    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 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.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