Search in sources :

Example 1 with FactWithList

use of org.drools.mvel.integrationtests.facts.FactWithList in project drools by kiegroup.

the class Misc2Test method testCollectWithEagerActivation.

@Test
public void testCollectWithEagerActivation() {
    // DROOLS-4468
    String drl = "import java.util.ArrayList\n" + "import " + FactWithList.class.getCanonicalName() + "\n" + "import " + FactWithString.class.getCanonicalName() + "\n" + "\n" + "dialect \"mvel\"\n" + "global java.util.List list; \n" + "\n" + " rule \"Init\"\n" + " when\n" + "     $fl: FactWithList(items.size()==0)\n" + " then\n" + "     $fl.getItems().add(\"A\");\n" + "     $fl.getItems().add(\"B\");\n" + "     update($fl);\n" + " end\n" + "\n" + " rule \"R1\"\n" + " when\n" + "     $fl: FactWithList($itemList : items)\n" + "     $l: java.util.List(size > 0) from collect(FactWithString($itemList contains stringValue));\n" + " then\n" + "      list.add(\"R1\"); \n" + " end\n" + "\n" + " rule \"R2\"\n" + " when\n" + "     $fl: FactWithList($itemList : items)\n" + "     not( FactWithString($itemList contains stringValue) )\n" + " then\n" + "      list.add(\"R2\"); \n" + " end";
    KieSessionConfiguration config = KieServices.Factory.get().newKieSessionConfiguration(null);
    config.setOption(ForceEagerActivationOption.YES);
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession(config, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new FactWithString("A"));
    ksession.insert(new FactWithList());
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("R1"));
}
Also used : FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) KieBase(org.kie.api.KieBase) FactWithList(org.drools.mvel.integrationtests.facts.FactWithList) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) FactWithList(org.drools.mvel.integrationtests.facts.FactWithList) List(java.util.List) FactWithString(org.drools.mvel.integrationtests.facts.FactWithString) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 FactWithList (org.drools.mvel.integrationtests.facts.FactWithList)1 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1 KieSession (org.kie.api.runtime.KieSession)1 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)1 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)1