use of org.drools.mvel.compiler.FactA in project drools by kiegroup.
the class MarshallingTest method testMultiRuleMultiJoinNodePatternsWithHalt.
@Test
public void testMultiRuleMultiJoinNodePatternsWithHalt() throws Exception {
String rule1 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 1\"\n" + "when\n" + " $c : Cheese( ) \n" + " $p : Person( cheese == $c ) \n" + "then\n" + " list.add( $p );\n" + "end";
String rule2 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "import " + Cell.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 2\"\n" + "when\n" + " $c : Cheese( ) \n" + " $p : Person( cheese == $c ) \n" + " $x : Cell( value == $p.age ) \n" + "then\n" + " list.add( $x );\n" + "end";
String rule3 = "package org.drools.compiler.test;\n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + FactB.class.getCanonicalName() + "\n" + "import " + FactC.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule \"Rule 3\"\n" + "when\n" + " $a : FactA( field2 > 10 ) \n" + " $b : FactB( f2 >= $a.field2 ) \n" + " $p : Person( name == \"darth vadar\" ) \n" + " $c : FactC( f2 >= $b.f2 ) \n" + "then\n" + " list.add( $c );\n" + " drools.halt();\n" + "end";
KieBase kBase = loadKnowledgeBaseFromString(rule1, rule2, rule3);
KieSession session = kBase.newKieSession();
List list = new ArrayList();
session.setGlobal("list", list);
Cheese stilton = new Cheese("stilton", 25);
Cheese brie = new Cheese("brie", 49);
Person bobba = new Person("bobba fet", 30);
bobba.setCheese(stilton);
Person vadar = new Person("darth vadar", 38);
Person c3po = new Person("c3p0", 17);
c3po.setCheese(stilton);
Person r2d2 = new Person("r2d2", 58);
r2d2.setCheese(brie);
session.insert(stilton);
session.insert(bobba);
session.insert(vadar);
session.insert(brie);
session.insert(c3po);
session.insert(r2d2);
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
list = (List) session.getGlobal("list");
assertEquals(3, list.size());
assertTrue(list.contains(r2d2));
assertTrue(list.contains(c3po));
assertTrue(list.contains(bobba));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
Cell cell30 = new Cell(30);
session.insert(cell30);
Cell cell58 = new Cell(58);
session.insert(cell58);
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
assertEquals(5, list.size());
assertTrue(list.contains(cell30));
assertTrue(list.contains(cell58));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.insert(new FactA(15));
session.insert(new FactB(20));
FactC factC27 = new FactC(27);
session.insert(factC27);
FactC factC52 = new FactC(52);
session.insert(factC52);
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
session.fireAllRules();
assertEquals(7, list.size());
assertTrue(list.contains(factC52));
assertTrue(list.contains(factC27));
}
use of org.drools.mvel.compiler.FactA in project drools by kiegroup.
the class MVELTest method testTypeCoercionFloatCompareToDouble.
@Test
public void testTypeCoercionFloatCompareToDouble() {
String str = "package com.sample\n" + "import " + FactA.class.getCanonicalName() + ";\n" + "rule R1\n" + "dialect \"mvel\"\n" + "when\n" + " $f : FactA(field3 == 15.1)\n" + "then\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
FactA f = new FactA();
f.setField3(new Float(15.1f));
ksession.insert(f);
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.mvel.compiler.FactA in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRules2.
@Test(timeout = 10000)
public void testDynamicRules2() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration);
KieSession session = kbase.newKieSession();
// Assert some simple facts
final FactA a = new FactA("hello", new Integer(1), new Float(3.14));
final FactB b = new FactB("hello", new Integer(2), new Float(6.28));
session.insert(a);
session.insert(b);
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicRules2.drl").getKiePackages();
kbase.addPackages(kpkgs);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
}
use of org.drools.mvel.compiler.FactA in project drools by kiegroup.
the class ExecutionFlowControlTest method testEnabledExpressionWithOr.
@Test
public void testEnabledExpressionWithOr() throws Exception {
String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + Foo.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "rule r1 salience(f1.field2) enabled(f1.field2 >= 20)\n" + "when\n" + " foo: Foo()\n" + " ( Pet() and f1 : FactA( field1 == 'f1') ) or \n" + " f1 : FactA(field1 == 'f2') \n" + "then\n" + " list.add( f1 );\n" + " foo.setId( 'xxx' );\n" + "end\n" + "\n";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, text);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Foo(null, null));
ksession.insert(new Pet(null));
FactA fact1 = new FactA();
fact1.setField1("f1");
fact1.setField2(10);
FactA fact2 = new FactA();
fact2.setField1("f1");
fact2.setField2(30);
FactA fact3 = new FactA();
fact3.setField1("f2");
fact3.setField2(20);
ksession.insert(fact1);
ksession.insert(fact2);
ksession.insert(fact3);
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals(fact2, list.get(0));
assertEquals(fact3, list.get(1));
}
Aggregations