Search in sources :

Example 41 with KieHelper

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

the class QueryTest method testQueryWithClassArg.

@Test
public void testQueryWithClassArg() {
    // DROOLS-590
    String drl = "global java.util.List list; " + "" + "declare Foo end " + "" + "query bar( Class $c ) " + "  Class( this.getName() == $c.getName() ) " + "end " + "query bar2( Class $c ) " + "  Class( this == $c ) " + "end " + "" + "rule Init when then insert( Foo.class ); end " + "" + "rule React1 " + "when " + "  bar( Foo.class ; ) " + "then " + "  list.add( 'aa' ); " + "end  " + "rule React2 " + "when\n" + "  bar2( Foo.class ; ) " + "then " + "  list.add( 'bb' ); " + "end";
    List list = new ArrayList();
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieSession ks = helper.build().newKieSession();
    ks.setGlobal("list", list);
    ks.fireAllRules();
    assertEquals(Arrays.asList("aa", "bb"), list);
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 42 with KieHelper

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

the class RuleEventListenerTest method testRuleEventListener.

@Test
public void testRuleEventListener() {
    String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R when\n" + "  $p: Person( $age: age < 20 )\n" + "then\n" + "  modify($p) { setAge( $age + 1 ) };" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<>();
    ((RuleEventManager) ksession).addEventListener(new RuleEventListener() {

        @Override
        public void onBeforeMatchFire(Match match) {
            list.add("onBeforeMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onAfterMatchFire(Match match) {
            list.add("onAfterMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onDeleteMatch(Match match) {
            list.add("onDeleteMatch: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onUpdateMatch(Match match) {
            list.add("onUpdateMatch: " + match.getDeclarationValue("$age"));
        }
    });
    ksession.insert(new Person("John Smith", 18));
    ksession.fireAllRules();
    List<String> expected = Arrays.asList("onBeforeMatchFire: 18", "onAfterMatchFire: 19", "onUpdateMatch: 19", "onBeforeMatchFire: 19", "onAfterMatchFire: 20", "onDeleteMatch: 20");
    assertEquals(expected, list);
}
Also used : RuleEventManager(org.kie.internal.event.rule.RuleEventManager) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 43 with KieHelper

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

the class RuleExecutionTest method testAll.

@Test
public void testAll() throws Exception {
    String str = "global java.util.List list\n" + "rule R1 when\n" + "    $i : Integer()\n" + "    $s : String( this == $i.toString() )\n" + "then\n" + "    list.add( $i );\n" + "end\n" + "\n" + "rule R2 @All when\n" + "    $i : Integer()\n" + "    $s : Long( intValue == $i )\n" + "then\n" + "    insert( \"\" + $i );\n" + "    list.add( -$i );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert(1);
    ksession.insert(2);
    ksession.insert(3);
    ksession.insert(1L);
    ksession.insert(2L);
    ksession.insert(3L);
    ksession.fireAllRules();
    assertEquals(asList(-1, -2, -3, 1, 2, 3), list);
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 44 with KieHelper

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

the class RuleExecutionTest method testOnDeleteMatchConsequence.

@Test
public void testOnDeleteMatchConsequence() throws Exception {
    String str = "import " + Person.class.getCanonicalName() + ";\n" + "rule R1 when\n" + "    $p : Person( age > 30 )\n" + "then\n" + "    $p.setStatus(\"in\");\n" + "then[$onDeleteMatch$]\n" + "    $p.setStatus(\"out\");\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession();
    Person mario = new Person("Mario", 40);
    FactHandle fact = ksession.insert(mario);
    ksession.fireAllRules();
    assertEquals("in", mario.getStatus());
    ksession.delete(fact);
    ksession.fireAllRules();
    assertEquals("out", mario.getStatus());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 45 with KieHelper

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

the class OOPathOnGraphTest method testOOPathOnGraphWithReactiveContentModification.

@Test
public void testOOPathOnGraphWithReactiveContentModification() {
    String drl = "import org.drools.compiler.oopath.graph.*;\n" + "import " + Library.class.getCanonicalName() + ";\n" + "import " + Book.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "\n" + "rule R when\n" + "  Vertex( it instanceof Library, $a : /outVs/outVs/it#Person[ age > 25 ] )\n" + "then\n" + "  list.add( $a.getName() );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    Vertex<Library> library = getGraph();
    ksession.insert(library);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("Mario"));
    list.clear();
    Person raoul = (Person) library.getOutVs().get(0).getOutVs().get(0).getIt();
    assertEquals("Raoul", raoul.getName());
    raoul.setAge(raoul.getAge() + 1);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("Raoul"));
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) 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