Search in sources :

Example 76 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class PropertyReactivityTest method testUpdateOnNonExistingProperty.

@Test
public void testUpdateOnNonExistingProperty() {
    // DROOLS-2170
    final String str1 = "import " + Klass.class.getCanonicalName() + "\n" + "rule R when\n" + "  Klass( b == 2 )\n" + "then\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(str1, ResourceType.DRL).build().newKieSession();
    final Klass bean = new Klass(1, 2, 3, 4, 5, 6);
    final FactHandle fh = ksession.insert(bean);
    ksession.fireAllRules();
    try {
        ksession.update(fh, bean, "z");
        fail("Trying to update not existing property must fail");
    } catch (Exception e) {
        assertTrue(e.getMessage().contains(Klass.class.getName()));
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 77 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class PropertyReactivityTest method testWatchFieldInExternalNotPattern.

@Test
public void testWatchFieldInExternalNotPattern() {
    // DROOLS-1445
    final String drl = "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + "    $p1 : Person( name == \"Mario\" )\n" + "    not( Person( age < $p1.age ) )\n" + "then\n" + "    list.add(\"t0\");\n" + "end\n" + "rule Z when\n" + "    $p1 : Person( name == \"Mario\" ) \n" + "then\n" + "    modify($p1) { setAge(35); } \n" + "end\n";
    // making the default explicit:
    final KieSession ksession = new KieHelper(PropertySpecificOption.ALWAYS).addContent(drl, ResourceType.DRL).build().newKieSession();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    final Person mario = new Person("Mario", 40);
    final Person mark = new Person("Mark", 37);
    final FactHandle fh_mario = ksession.insert(mario);
    ksession.insert(mark);
    final int x = ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("t0", list.get(0));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 78 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class PropertyReactivityTest method testPropReactiveUpdate.

@Test
public void testPropReactiveUpdate() {
    // DROOLS-1275
    final String str1 = "import " + Klass.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "rule R when\n" + "  Klass( b == 2 )\n" + "then\n" + "  list.add(\"fired\");\n" + "end\n";
    final KieSession ksession = new KieHelper().addContent(str1, ResourceType.DRL).build().newKieSession();
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    final Klass bean = new Klass(1, 2, 3, 4, 5, 6);
    final FactHandle fh = ksession.insert(bean);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ((StatefulKnowledgeSessionImpl) ksession).update(fh, bean, "a", "d");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ((StatefulKnowledgeSessionImpl) ksession).update(fh, bean, "c", "b");
    ksession.fireAllRules();
    assertEquals(2, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 79 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class JBRULESTest method testJBRULES3323.

@Test
public void testJBRULES3323() throws Exception {
    // adding rules. it is important to add both since they reciprocate
    final StringBuilder rule = new StringBuilder();
    rule.append("package de.orbitx.accumulatetesettest;\n");
    rule.append("import java.util.Set;\n");
    rule.append("import java.util.HashSet;\n");
    rule.append("import org.drools.compiler.Foo;\n");
    rule.append("import org.drools.compiler.Bar;\n");
    rule.append("rule \"Sub optimal foo parallelism - this rule is causing NPE upon reverse\"\n");
    rule.append("when\n");
    rule.append("$foo : Foo($leftId : id, $leftBar : bar != null)\n");
    rule.append("$fooSet : Set()\n");
    rule.append("from accumulate ( Foo(id > $leftId, bar != null && != $leftBar, $bar : bar),\n");
    rule.append("collectSet( $bar ) )\n");
    rule.append("then\n");
    rule.append("//System.out.println(\"ok\");\n");
    rule.append("end\n");
    // building stuff
    final KieBase kbase = loadKnowledgeBaseFromString(rule.toString());
    final KieSession ksession = createKnowledgeSession(kbase);
    // adding test data
    final Bar[] barList = new Bar[3];
    for (int i = 0; i < barList.length; i++) {
        barList[i] = new Bar(String.valueOf(i));
    }
    final Foo[] fooList = new Foo[4];
    for (int i = 0; i < fooList.length; i++) {
        fooList[i] = new Foo(String.valueOf(i), i == 3 ? barList[2] : barList[i]);
    }
    for (final Foo foo : fooList) {
        ksession.insert(foo);
    }
    // the NPE is caused by exactly this sequence. of course there are more sequences but this
    // appears to be the most short one
    final int[] magicFoos = new int[] { 3, 3, 1, 1, 0, 0, 2, 2, 1, 1, 0, 0, 3, 3, 2, 2, 3, 1, 1 };
    final int[] magicBars = new int[] { 1, 2, 0, 1, 1, 0, 1, 2, 2, 1, 2, 0, 0, 2, 0, 2, 0, 0, 1 };
    // upon final rule firing an NPE will be thrown in org.drools.core.rule.Accumulate
    for (int i = 0; i < magicFoos.length; i++) {
        final Foo tehFoo = fooList[magicFoos[i]];
        final FactHandle fooFactHandle = ksession.getFactHandle(tehFoo);
        tehFoo.setBar(barList[magicBars[i]]);
        ksession.update(fooFactHandle, tehFoo);
        ksession.fireAllRules();
    }
    ksession.dispose();
}
Also used : Bar(org.drools.compiler.Bar) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) Foo(org.drools.compiler.Foo) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 80 with FactHandle

use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.

the class ExtendsTest method testInheritAnnotationsInOtherPackage.

@Test
public void testInheritAnnotationsInOtherPackage() throws Exception {
    String s1 = "package org.drools.compiler.test.pack1;\n" + "global java.util.List list;" + "\n" + "declare Event\n" + "@role(event)" + "  id    : int\n" + "end\n" + "\n" + "rule \"X\"\n" + "when\n" + "  $s1 : Event()\n" + "then\n" + "  System.out.println( $s1 );\n" + "  list.add( $s1.getId() );\n " + "end";
    String s2 = "package org.drools.compiler.test.pack2;\n" + "\n" + "import org.drools.compiler.test.pack1.Event;\n" + "global java.util.List list;" + "\n" + "declare Event end\n" + "\n" + "declare SubEvent extends Event\n" + "end\n" + "\n" + "rule \"Init\"\n" + "when\n" + "then\n" + "  list.add( 0 );\n" + "  insert( new SubEvent( 1 ) );\n" + "  insert( new SubEvent( 2 ) );\n" + "end\n" + "\n" + "rule \"Seq\"\n" + "when\n" + "  $s1 : SubEvent( id == 1 )\n" + "  $s2 : SubEvent( id == 2, this after[0,10s] $s1 )\n" + "then\n" + "  list.add( 3 );\n" + "  System.out.println( $s1 + \" after \" + $s1 );\n" + "end \n" + "\n" + "rule \"Seq 2 \"\n" + "when\n" + "  $s1 : Event( id == 1 )\n" + "  $s2 : Event( id == 2, this after[0,10s] $s1 )\n" + "then\n" + "  list.add( 4 );\n" + "  System.out.println( $s1 + \" after II \" + $s1 );\n" + "end";
    KieSession kSession = new KieHelper().addContent(s1, ResourceType.DRL).addContent(s2, ResourceType.DRL).build().newKieSession();
    List list = new ArrayList();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    for (Object o : kSession.getObjects()) {
        FactHandle h = kSession.getFactHandle(o);
        assertTrue(h instanceof EventFactHandle);
    }
    System.out.println(list);
    assertEquals(5, list.size());
    assertTrue(list.contains(0));
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
    assertTrue(list.contains(3));
    assertTrue(list.contains(4));
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Test(org.junit.Test)

Aggregations

FactHandle (org.kie.api.runtime.rule.FactHandle)650 Test (org.junit.Test)547 KieSession (org.kie.api.runtime.KieSession)506 KieBase (org.kie.api.KieBase)304 ArrayList (java.util.ArrayList)298 InternalFactHandle (org.drools.core.common.InternalFactHandle)192 List (java.util.List)165 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)119 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)66 KieHelper (org.kie.internal.utils.KieHelper)50 Person (org.drools.modelcompiler.domain.Person)46 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)43 HashMap (java.util.HashMap)41 Person (org.drools.compiler.Person)41 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)39 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 Person (org.drools.mvel.compiler.Person)38 Ignore (org.junit.Ignore)36 Match (org.kie.api.runtime.rule.Match)36 Collection (java.util.Collection)34