Search in sources :

Example 1 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class SegmentMemoryPrototypeTest method testEnsureRiaSegmentCreationUsingPrototypes.

@Test
public void testEnsureRiaSegmentCreationUsingPrototypes() {
    // DROOLS-1739
    String str = "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import java.util.List;\n" + "\n" + "rule rule1 when\n" + "    $personE : List()\n" + "    Person( ) from $personE\n" + "then end\n" + "\n" + "rule rule2 when\n" + "    $personE : List()\n" + "    Person( $addresses : addresses ) from $personE\n" + "    $address : Address( ) from $addresses\n" + "    not (Address( this != $address ) from $addresses)\n" + "    String(  )\n" + "then end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSessionConfiguration conf = RuleBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ForceEagerActivationOption.YES);
    KieSession ksession = kbase.newKieSession(conf, null);
    try {
        ksession.insert(asList(new Person()));
        ksession.insert("test");
        assertEquals(1, ksession.fireAllRules());
    } finally {
        ksession.dispose();
        try {
            ksession = kbase.newKieSession(conf, null);
            ksession.insert(asList(new Person()));
            ksession.insert("test");
            assertEquals(1, ksession.fireAllRules());
        } finally {
            ksession.dispose();
        }
    }
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 2 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class RuleEventListenerTest method testRuleEventListener.

@Test
public void testRuleEventListener() {
    String str = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R when\n" + "  $p: Person( $age: age < 20 )\n" + "then\n" + "  modify($p) { setAge( $age + 1 ) };" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    List<String> list = new ArrayList<>();
    ((RuleEventManager) ksession).addEventListener(new RuleEventListener() {

        @Override
        public void onBeforeMatchFire(Match match) {
            list.add("onBeforeMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onAfterMatchFire(Match match) {
            list.add("onAfterMatchFire: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onDeleteMatch(Match match) {
            list.add("onDeleteMatch: " + match.getDeclarationValue("$age"));
        }

        @Override
        public void onUpdateMatch(Match match) {
            list.add("onUpdateMatch: " + match.getDeclarationValue("$age"));
        }
    });
    ksession.insert(new Person("John Smith", 18));
    ksession.fireAllRules();
    List<String> expected = Arrays.asList("onBeforeMatchFire: 18", "onAfterMatchFire: 19", "onUpdateMatch: 19", "onBeforeMatchFire: 19", "onAfterMatchFire: 20", "onDeleteMatch: 20");
    assertEquals(expected, list);
}
Also used : RuleEventManager(org.kie.internal.event.rule.RuleEventManager) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) RuleEventListener(org.kie.internal.event.rule.RuleEventListener) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 3 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class UpdateTest method testJavaModifyBlock.

// this test requires mvel 1.2.19. Leaving it commented until mvel is released.
@Test
public void testJavaModifyBlock() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_JavaModifyBlock.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Person bob = new Person("Bob", 30);
    bob.setStatus("hungry");
    ksession.insert(bob);
    ksession.insert(new Cheese());
    ksession.insert(new Cheese());
    ksession.insert(new OuterClass.InnerClass(1));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals("full", bob.getStatus());
    assertEquals(31, bob.getAge());
    assertEquals(2, ((OuterClass.InnerClass) list.get(1)).getIntAttr());
}
Also used : OuterClass(org.drools.mvel.compiler.OuterClass) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 4 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class UpdateTest method doModifyTest.

private void doModifyTest(final String drlResource) throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, drlResource);
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("results", list);
    final Person bob = new Person("Bob");
    bob.setStatus("hungry");
    final Cheese c = new Cheese();
    ksession.insert(bob);
    ksession.insert(c);
    ksession.fireAllRules();
    assertEquals(10, c.getPrice());
    assertEquals("fine", bob.getStatus());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person)

Example 5 with Person

use of org.drools.mvel.compiler.Person in project drools by kiegroup.

the class UpdateTest method testModifySimple.

@Test
public void testModifySimple() {
    final String str = "package org.drools.mvel.compiler;\n" + "\n" + "rule \"test modify block\"\n" + "when\n" + "    $p: Person( name == \"hungry\" )\n" + "then\n" + "    modify( $p ) { setName(\"fine\") }\n" + "end\n" + "\n" + "rule \"Log\"\n" + "when\n" + "    $o: Object()\n" + "then\n" + "    System.out.println( $o );\n" + "end";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final Person p = new Person("hungry");
    ksession.insert(p);
    ksession.fireAllRules();
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Aggregations

Person (org.drools.mvel.compiler.Person)196 Test (org.junit.Test)185 KieSession (org.kie.api.runtime.KieSession)178 KieBase (org.kie.api.KieBase)171 ArrayList (java.util.ArrayList)98 List (java.util.List)72 Cheese (org.drools.mvel.compiler.Cheese)46 FactHandle (org.kie.api.runtime.rule.FactHandle)38 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)34 Address (org.drools.mvel.compiler.Address)33 FactWithString (org.drools.mvel.integrationtests.facts.FactWithString)24 InternalFactHandle (org.drools.core.common.InternalFactHandle)23 KiePackage (org.kie.api.definition.KiePackage)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)16 IteratorToList (org.drools.mvel.integrationtests.IteratorToList)15 HashMap (java.util.HashMap)12 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)12 Collection (java.util.Collection)11 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)8 AlphaNode (org.drools.core.reteoo.AlphaNode)8