use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd3.
/*
* Works Fine if both the scenarios mentioned above are skipped.
*/
@Test
public void testSerializeAdd3() throws Exception {
// Create a rulebase, a session, and test it
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
List results = new ArrayList();
KieSession session = kBase.newKieSession();
session.setGlobal("results", results);
InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(1, results.size());
assertEquals(stilton1.getObject(), results.get(0));
// serialize session
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamic add pkgs
kpkgs = loadKnowledgePackages("test_Dynamic3_0.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(4, results.size());
assertEquals(stilton2.getObject(), results.get(1));
assertTrue(results.contains(bob2.getObject()));
assertTrue(results.contains(bob1.getObject()));
// serialize session
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamic add pkgs
kpkgs = loadKnowledgePackages("test_Dynamic1_2.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 40));
InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 40));
InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 40));
InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 40));
InternalFactHandle addr1 = (InternalFactHandle) session.insert(new Address("bangalore"));
InternalFactHandle addr2 = (InternalFactHandle) session.insert(new Address("India"));
session.fireAllRules();
assertEquals(9, results.size());
assertEquals(stilton3.getObject(), results.get(4));
assertEquals(bob4.getObject(), results.get(5));
assertEquals(bob3.getObject(), results.get(6));
assertTrue(results.contains(addr2.getObject()));
assertTrue(results.contains(addr1.getObject()));
// serialize session
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 50));
InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 50));
InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 50));
InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 50));
InternalFactHandle addr3 = (InternalFactHandle) session.insert(new Address("Tripura"));
InternalFactHandle addr4 = (InternalFactHandle) session.insert(new Address("Agartala"));
session.fireAllRules();
assertEquals(14, results.size());
assertEquals(stilton4.getObject(), results.get(9));
assertEquals(bob6.getObject(), results.get(10));
assertEquals(bob5.getObject(), results.get(11));
assertTrue(results.contains(addr4.getObject()));
assertTrue(results.contains(addr3.getObject()));
// serialize session
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.dispose();
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class PropertyReactivityTest method testModifyWithGetter.
@Test
public void testModifyWithGetter() {
final String rule1 = "package foo.bar\n" + "import " + Person.class.getName() + "\n" + "declare Person @propertyReactive end\n" + "rule x\n" + " when\n" + " $p : Person( address != null ) @watch(!address) \n" + " then\n" + " modify($p){getAddress().setStreet(\"foo\");}\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, rule1);
KieSession ksession = kbase.newKieSession();
final Person p = new Person();
p.setAddress(new Address());
ksession.insert(p);
final int fired = ksession.fireAllRules(10);
assertEquals(1, fired);
assertEquals("foo", p.getAddress().getStreet());
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testNullSafeWithMethod.
@Test
public void testNullSafeWithMethod() {
// DROOLS-4095
String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + " $street : String()\n" + " Person( getAddress()!.street == $street ) \n" + "then\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario", 38));
ksession.insert("Main Street");
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street"));
ksession.insert(mark);
Person edson = new Person("Edson", 34);
edson.setAddress(new Address(null));
ksession.insert(edson);
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testNullSafeNullComparison2.
@Test
public void testNullSafeNullComparison2() {
String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + " $street : String()\n" + " Person( address!.street == $street ) \n" + "then\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario", 38));
ksession.insert("Main Street");
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street"));
ksession.insert(mark);
Person edson = new Person("Edson", 34);
edson.setAddress(new Address(null));
ksession.insert(edson);
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
use of org.drools.mvel.compiler.Address in project drools by kiegroup.
the class NullSafeDereferencingTest method testDoubleNullSafe.
@Test
public void testDoubleNullSafe() {
String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + " Person( address!.street!.length > 15 ) \n" + "then\n" + "end";
KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario", 38));
Person mark = new Person("Mark", 37);
mark.setAddress(new Address("Main Street"));
ksession.insert(mark);
Person edson = new Person("Edson", 34);
edson.setAddress(new Address(null));
ksession.insert(edson);
Person alex = new Person("Alex", 34);
alex.setAddress(new Address("The Main Very Big Street"));
ksession.insert(alex);
assertEquals(1, ksession.fireAllRules());
ksession.dispose();
}
Aggregations