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()));
}
}
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));
}
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());
}
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();
}
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));
}
Aggregations