Search in sources :

Example 76 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class Misc2Test method testKieBuilderWithClassLoader.

@Test
public void testKieBuilderWithClassLoader() {
    // DROOLS-763
    String drl = "import com.billasurf.Person\n" + "\n" + "global java.util.List list\n" + "\n" + "rule R1 when\n" + "    $i : Integer()\n" + "then\n" + "    Person p = new Person();\n" + "    p.setAge($i);\n" + "    insert(p);\n" + "end\n" + "\n" + "rule R2 when\n" + "    $p : Person()\n" + "then\n" + "    list.add($p.getAge());\n" + "end\n";
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { this.getClass().getResource("/billasurf.jar") });
    KieSession ksession = new KieHelper().setClassLoader(urlClassLoader).addContent(drl, ResourceType.DRL).build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert(18);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(18, (int) list.get(0));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 77 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class Misc2Test method testClassAccumulator.

@Test
public void testClassAccumulator() {
    // DROOLS-626
    String drl = "global java.util.List list\n" + "declare InitClass\n" + "  clazz: Class\n" + "end\n" + "\n" + "rule \"init\" when\n" + "then\n" + "  insert( new InitClass( String.class ) );\n" + "  insert( new InitClass( Integer.class ) );\n" + "end\n" + "\n" + "rule \"make init classes\"\n" + "when\n" + "  accumulate( InitClass( $clazz; ), $classes: collectList( $clazz ) )\n" + "then\n" + "  list.addAll($classes);\n" + "end ";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<Class> list = new ArrayList<Class>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    System.out.println(list);
    assertTrue(list.containsAll(asList(String.class, Integer.class)));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 78 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class Misc2Test method testNotSubnetwork.

@Test
public void testNotSubnetwork() {
    // DROOLS-623
    String drl = "import " + TypeCC.class.getCanonicalName() + ";\n" + "import " + TypeDD.class.getCanonicalName() + ";\n" + "  \n" + "rule R1 \n" + "when  \n" + "   $dd : TypeDD( value < 1 )\n" + "then  \n" + "	System.out.println(\"Rule R1 Fired\");\n" + "	modify($dd) { setValue(1); }\n" + "end  \n" + "  \n" + "rule R2 when  \n" + "   String( )  \n" + "   $cc : TypeCC( value < 1 )\n" + "   not(  \n" + "	   $cc_not : TypeCC( )  \n" + "	   and  \n" + "	   $dd_not : TypeDD( value==0 )  \n" + "   )  \n" + "then  \n" + "   System.out.println(\"Rule R2 Fired\");\n" + "   modify($cc) { setValue($cc.getValue()+1); }\n" + "end; ";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    ksession.insert("1");
    ksession.insert("2");
    TypeCC cc = new TypeCC();
    ksession.insert(cc);
    ksession.insert(new TypeDD());
    ksession.fireAllRules();
    System.out.println("Rule R2 is fired count - " + cc.getValue());
    assertEquals("Rule 2 should be fired once as we have firing rule as one of criteria checking rule only fire once", 1, cc.getValue());
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 79 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class Misc2Test method testFromEPDontRequireLeftInput.

@Test
public void testFromEPDontRequireLeftInput() {
    // DROOLS-1014
    String drl = "rule R when\n" + "    $s1 : String() from entry-point \"xxx\"\n" + "then\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    Rete rete = ((KnowledgeBaseImpl) kbase).getRete();
    LeftInputAdapterNode liaNode = null;
    for (ObjectTypeNode otn : rete.getObjectTypeNodes()) {
        Class<?> otnType = ((ClassObjectType) otn.getObjectType()).getClassType();
        if (String.class == otnType) {
            assertEquals(1, otn.getObjectSinkPropagator().size());
        } else if (InitialFact.class.isAssignableFrom(otnType)) {
            assertEquals(0, otn.getObjectSinkPropagator().size());
        } else {
            fail("There shouldn't be other OTNs");
        }
    }
}
Also used : Rete(org.drools.core.reteoo.Rete) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) InitialFact(org.drools.core.InitialFact) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) LeftInputAdapterNode(org.drools.core.reteoo.LeftInputAdapterNode) Test(org.junit.Test)

Example 80 with KieHelper

use of org.kie.internal.utils.KieHelper in project drools by kiegroup.

the class Misc2Test method testSubnetworkAccumulate.

@Test
public void testSubnetworkAccumulate() {
    String drl = "import " + StringWrapper.class.getCanonicalName() + ";\n" + "global StringBuilder sb;" + "rule R when\n" + "  $s : String()\n" + "  Number( $i : intValue ) from accumulate ($sw : StringWrapper( $value : value ) " + "                                       and eval( $sw.contains($s) ), " + "                                 sum($value) )\n" + "then\n" + "  sb.append($i);\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    StringBuilder sb = new StringBuilder();
    ksession.setGlobal("sb", sb);
    ksession.insert("test");
    StringWrapper sw = new StringWrapper();
    FactHandle swFH = ksession.insert(sw);
    ksession.fireAllRules();
    sw.setWrapped("test");
    ksession.update(swFH, sw);
    ksession.fireAllRules();
    sw.setWrapped(null);
    ksession.update(swFH, sw);
    ksession.fireAllRules();
    sw.setWrapped("test");
    ksession.update(swFH, sw);
    ksession.fireAllRules();
    sw.setWrapped(null);
    ksession.update(swFH, sw);
    ksession.fireAllRules();
    sw.setWrapped("test");
    ksession.update(swFH, sw);
    ksession.fireAllRules();
    assertEquals("040404", sb.toString());
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Aggregations

KieHelper (org.kie.internal.utils.KieHelper)467 Test (org.junit.Test)427 KieSession (org.kie.api.runtime.KieSession)355 ArrayList (java.util.ArrayList)194 KieBase (org.kie.api.KieBase)152 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)77 Person (org.drools.compiler.Person)61 FactHandle (org.kie.api.runtime.rule.FactHandle)55 List (java.util.List)44 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)41 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)38 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)37 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)34 Man (org.drools.compiler.oopath.model.Man)29 Child (org.drools.compiler.oopath.model.Child)24 Woman (org.drools.compiler.oopath.model.Woman)23 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)18 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18