Search in sources :

Example 16 with Address

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();
}
Also used : Address(org.drools.mvel.compiler.Address) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 17 with Address

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());
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 18 with Address

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();
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 19 with Address

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();
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 20 with Address

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();
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

Address (org.drools.mvel.compiler.Address)29 Person (org.drools.mvel.compiler.Person)27 Test (org.junit.Test)27 KieSession (org.kie.api.runtime.KieSession)26 KieBase (org.kie.api.KieBase)25 ArrayList (java.util.ArrayList)12 List (java.util.List)11 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)8 AlphaNode (org.drools.core.reteoo.AlphaNode)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)4 MVELConstraint (org.drools.mvel.MVELConstraint)4 MVELObjectClassFieldReader (org.drools.mvel.extractors.MVELObjectClassFieldReader)4 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)2 Cheese (org.drools.mvel.compiler.Cheese)2 KiePackage (org.kie.api.definition.KiePackage)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2