Search in sources :

Example 6 with Person

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

the class UpdateTest method noDormantCheckOnModifies.

@Test
public void noDormantCheckOnModifies() throws Exception {
    // Test case for BZ 862325
    final String str = "package org.drools.mvel.compiler;\n" + " rule R1\n" + "    salience 10\n" + "    when\n" + "        $c : Cheese( price == 10 ) \n" + "        $p : Person( ) \n" + "    then \n" + "        modify($c) { setPrice( 5 ) }\n" + "        modify($p) { setAge( 20 ) }\n" + "end\n" + "rule R2\n" + "    when\n" + "        $p : Person( )" + "    then \n" + "        // noop\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.insert(new Person("Bob", 19));
    ksession.insert(new Cheese("brie", 10));
    ksession.fireAllRules();
    // both rules should fire exactly once
    verify(ael, times(2)).afterMatchFired(any(org.kie.api.event.rule.AfterMatchFiredEvent.class));
    // no cancellations should have happened
    verify(ael, never()).matchCancelled(any(org.kie.api.event.rule.MatchCancelledEvent.class));
}
Also used : Cheese(org.drools.mvel.compiler.Cheese) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 7 with Person

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

the class SequentialTest method testSalience.

@Test
public void testSalience() throws Exception {
    final KieModule kieModule = KieUtil.getKieModuleFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "simpleSalience.drl");
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.execute(new Person("pob"));
    assertEquals(3, list.size());
    assertEquals("rule 3", list.get(0));
    assertEquals("rule 2", list.get(1));
    assertEquals("rule 1", list.get(2));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) KieModule(org.kie.api.builder.KieModule) DynamicRulesTest(org.drools.mvel.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 8 with Person

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

the class SequentialTest method testBasicOperation.

@Test
public void testBasicOperation() throws Exception {
    final KieModule kieModule = KieUtil.getKieModuleFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "simpleSequential.drl");
    final KieBase kbase = KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p1 = new Person("p1", "stilton");
    final Person p2 = new Person("p2", "cheddar");
    final Person p3 = new Person("p3", "stilton");
    final Cheese stilton = new Cheese("stilton", 15);
    final Cheese cheddar = new Cheese("cheddar", 15);
    ksession.execute(CommandFactory.newInsertElements(Arrays.asList(new Object[] { p1, stilton, p2, cheddar, p3 })));
    assertEquals(3, list.size());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) KieModule(org.kie.api.builder.KieModule) DynamicRulesTest(org.drools.mvel.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 9 with Person

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

the class SequentialTest method testSequentialWithRulebaseUpdate.

// JBRULES-1567 - ArrayIndexOutOfBoundsException in sequential execution after calling RuleBase.addPackage(..)
@Test
public void testSequentialWithRulebaseUpdate() throws Exception {
    final KieModule kieModule = KieUtil.getKieModuleFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "simpleSalience.drl");
    final InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
    StatelessKieSession ksession = kbase.newStatelessKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.execute(new Person("pob"));
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", DynamicRulesTest.class, kieBaseTestConfiguration, "test_Dynamic3.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ksession = kbase.newStatelessKieSession();
    ksession.setGlobal("list", list);
    Person person = new Person("bop");
    ksession.execute(person);
    assertEquals(7, list.size());
    assertEquals("rule 3", list.get(0));
    assertEquals("rule 2", list.get(1));
    assertEquals("rule 1", list.get(2));
    assertEquals("rule 3", list.get(3));
    assertEquals("rule 2", list.get(4));
    assertEquals("rule 1", list.get(5));
    assertEquals(person, list.get(6));
}
Also used : DynamicRulesTest(org.drools.mvel.integrationtests.DynamicRulesTest) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) KieModule(org.kie.api.builder.KieModule) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) DynamicRulesTest(org.drools.mvel.integrationtests.DynamicRulesTest) Test(org.junit.Test)

Example 10 with Person

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

the class FieldAccessTest method testGenericsOption.

@Test
public void testGenericsOption() throws Exception {
    // JBRULES-3579
    final String str = "import org.drools.mvel.compiler.*;\n" + "rule R1 when\n" + "   $c : Cheese( $type: type )\n" + "   $p : Person( $name : name, addressOption.get.street == $type )\n" + "then\n" + "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final Person p = new Person("x");
    p.setAddress(new Address("x", "x", "x"));
    ksession.insert(p);
    ksession.insert(new Cheese("x"));
    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) Cheese(org.drools.mvel.compiler.Cheese) 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