Search in sources :

Example 46 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource 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.compiler.lang.descr.PackageDescr) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 47 with ByteArrayResource

use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.

the class DescrBuilderTest method testAccumulate.

@Test
public void testAccumulate() throws InstantiationException, IllegalAccessException {
    PackageDescrBuilder packBuilder = DescrFactory.newPackage().newGlobal().identifier("list").type(List.class.getName()).end().name("org.drools.compiler").newRule().name("r1").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot );").end().newRule().name("r2").attribute("dialect", "mvel").lhs().pattern().id("$tot", true).type(Double.class.getName()).end().accumulate().source().pattern().id("$i", false).type(Integer.class.getName()).end().end().function("sum", "$tot", true, "$i").constraint("$tot > 15").end().end().rhs("list.add( $tot * 2 );").end();
    String drl = new DrlDumper().dump(packBuilder.getDescr());
    System.out.println(drl);
    KnowledgeBuilder knowledgeBuilder = 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();
    List list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.insert(3);
    knowledgeSession.insert(39);
    knowledgeSession.insert(24.0);
    knowledgeSession.insert(42.0);
    knowledgeSession.fireAllRules();
    assertEquals(Arrays.asList(42.0, 84.0), list);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) DrlDumper(org.drools.compiler.lang.DrlDumper) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)47 Test (org.junit.Test)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)43 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 KieSession (org.kie.api.runtime.KieSession)20 ArrayList (java.util.ArrayList)14 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)14 List (java.util.List)10 FactType (org.kie.api.definition.type.FactType)9 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)5 FactHandle (org.kie.api.runtime.rule.FactHandle)4 Person (org.drools.compiler.Person)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 Arrays.asList (java.util.Arrays.asList)2 BitSet (java.util.BitSet)2 LinkedList (java.util.LinkedList)2 DrlDumper (org.drools.compiler.lang.DrlDumper)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)2 KieBase (org.kie.api.KieBase)2