Search in sources :

Example 96 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class XmlPackageReaderTest method testParseForall.

@Test
public void testParseForall() throws Exception {
    final XmlPackageReader xmlPackageReader = getXmReader();
    xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseForall.xml")));
    final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
    assertNotNull(packageDescr);
    RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
    ForallDescr forall = (ForallDescr) obj.getLhs().getDescrs().get(0);
    List forallPaterns = forall.getDescrs();
    PatternDescr pattarnState = (PatternDescr) forallPaterns.get(0);
    PatternDescr personState = (PatternDescr) forallPaterns.get(1);
    PatternDescr cheeseState = (PatternDescr) forallPaterns.get(2);
    assertEquals(pattarnState.getObjectType(), "State");
    assertEquals(personState.getObjectType(), "Person");
    assertEquals(cheeseState.getObjectType(), "Cheese");
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) List(java.util.List) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) ForallDescr(org.drools.compiler.lang.descr.ForallDescr) Test(org.junit.Test)

Example 97 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools 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();
    assertNotNull(packageDescr);
    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.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 98 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools 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();
    assertNotNull(packageDescr);
    assertEquals("com.sample", packageDescr.getName());
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 99 with PackageDescr

use of org.drools.compiler.lang.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);
}
Also used : InputStreamReader(java.io.InputStreamReader) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 100 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class DumperTestHelper method XmlFile.

public static void XmlFile(String filename) throws Exception {
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    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);
    Assertions.assertThat(buffer).isEqualToIgnoringWhitespace(result);
    assertNotNull(result);
}
Also used : InputStreamReader(java.io.InputStreamReader) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlDumper(org.drools.compiler.compiler.xml.XmlDumper) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)239 Test (org.junit.Test)192 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)44 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InputStreamReader (java.io.InputStreamReader)25 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)22 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14