Search in sources :

Example 6 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DescrBuilderTest method testDumperFromPkg.

@Test
public void testDumperFromPkg() {
    // DROOLS-109
    PackageDescr pkg = DescrFactory.newPackage().name("org.test").newRule().name("org.test").lhs().and().or().pattern().id("$x", false).type("Integer").constraint("this > 10").end().pattern().id("$x", false).type("Integer").constraint("this < 20").end().end().pattern().type("Integer").constraint("this == $x").constraint("this == 42").end().end().end().rhs("").end().end().getDescr();
    String drl = new DrlDumper().dump(pkg);
    System.out.println(drl);
    KnowledgeBuilderImpl knowledgeBuilder = (KnowledgeBuilderImpl) KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    System.err.println(knowledgeBuilder.getErrors());
    assertFalse(knowledgeBuilder.getErrors().toString(), knowledgeBuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(knowledgeBuilder.getKnowledgePackages());
    KieSession knowledgeSession = kbase.newKieSession();
    KiePackage rebuiltPkg = knowledgeBuilder.getPackage("org.test");
    org.kie.api.definition.rule.Rule rule = rebuiltPkg.getRules().iterator().next();
    RuleImpl r = ((RuleImpl) rule);
    assertEquals(2, r.getLhs().getChildren().size());
    Iterator<RuleConditionElement> iter = r.getLhs().getChildren().iterator();
    RuleConditionElement arg1 = iter.next();
    assertTrue(arg1 instanceof GroupElement && ((GroupElement) arg1).getType() == GroupElement.Type.OR);
    assertEquals(2, ((GroupElement) arg1).getChildren().size());
    RuleConditionElement arg2 = iter.next();
    assertTrue(arg2 instanceof Pattern);
}
Also used : Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleConditionElement(org.drools.core.rule.RuleConditionElement) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 7 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DescrBuilderTest method testRule.

@Test
public void testRule() throws InstantiationException, IllegalAccessException {
    PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().and().or().pattern("StockTick").constraint("price > 100").end().pattern("StockTick").constraint("price < 10").end().end().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().rhs("    System.out.println(\"foo\");\n").end();
    PackageDescr pkg = packBuilder.getDescr();
    String drl = new DrlDumper().dump(packBuilder.getDescr());
    System.out.println(drl);
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.mvel.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(0, rules);
    ksession = kbase.newKieSession();
    ksession.insert(new StockTick(2, "RHT", 150, 1));
    rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) StockTick(org.drools.mvel.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 8 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DrlxCompilerTest method testSingleFileUnit.

@Test
public void testSingleFileUnit() throws Exception {
    InputStream p = getClass().getClassLoader().getResourceAsStream("drlx1/Example.drlx");
    InputStreamResource r = new InputStreamResource(p);
    DrlxCompiler drlxCompiler = new DrlxCompiler();
    assertTrue("Should not have compiler errors\n" + drlxCompiler.getResults().stream().map(KnowledgeBuilderResult::toString).collect(Collectors.joining("\n")), drlxCompiler.getResults().isEmpty());
    PackageDescr packageDescr = drlxCompiler.toPackageDescr(r);
    System.out.println(new DrlDumper().dump(packageDescr));
}
Also used : InputStream(java.io.InputStream) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Test(org.junit.Test)

Example 9 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DumperTestHelper method dump.

public static String dump(String filename) throws Exception {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final PackageDescr pkgOriginal = parser.parse(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final DrlDumper dumper = new DrlDumper();
    return dumper.dump(pkgOriginal);
}
Also used : InputStreamReader(java.io.InputStreamReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper)

Example 10 with DrlDumper

use of org.drools.mvel.DrlDumper in project drools by kiegroup.

the class DumperTestHelper method DrlFile.

public static void DrlFile(String filename) throws Exception {
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    final PackageDescr pkgOriginal = parser.parse(new InputStreamReader(DumperTestHelper.class.getResourceAsStream(filename)));
    final DrlDumper dumper = new DrlDumper();
    String result1 = dumper.dump(pkgOriginal);
    final PackageDescr pkgDerivated = parser.parse(new StringReader(result1));
    String result2 = dumper.dump(pkgDerivated);
    System.out.println(result1);
    Assertions.assertThat(result1).isEqualToIgnoringWhitespace(result2);
}
Also used : InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper)

Aggregations

DrlDumper (org.drools.mvel.DrlDumper)19 PackageDescr (org.drools.drl.ast.descr.PackageDescr)16 Test (org.junit.Test)16 InputStreamReader (java.io.InputStreamReader)5 KieSession (org.kie.api.runtime.KieSession)5 PackageDescrBuilder (org.drools.drl.ast.dsl.PackageDescrBuilder)4 DrlParser (org.drools.drl.parser.DrlParser)4 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)3 KiePackage (org.kie.api.definition.KiePackage)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)2 InputStreamResource (org.drools.core.io.impl.InputStreamResource)2 Cheese (org.drools.mvel.compiler.Cheese)2 KieBase (org.kie.api.KieBase)2 EntryPoint (org.kie.api.runtime.rule.EntryPoint)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1