Search in sources :

Example 86 with Person

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

the class DynamicRulesTest method testDynamicRuleRemovalsSubNetworkAndNot.

@Test(timeout = 10000)
public void testDynamicRuleRemovalsSubNetworkAndNot() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithNotSubnetwork.drl");
    KieSession ksession = kbase.newKieSession();
    final AgendaEventListener alistener = mock(AgendaEventListener.class);
    ksession.addEventListener(alistener);
    // pattern does not match, so do not activate
    ksession.insert(new Person("toni"));
    ksession.fireAllRules();
    verify(alistener, never()).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    // pattern matches, so create activation
    ksession.insert(new Person("bob"));
    ksession.fireAllRules();
    verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    // already active, so no new activation should be created
    ksession.insert(new Person("mark"));
    ksession.fireAllRules();
    verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    kbase.removeKiePackage("org.drools.mvel.compiler");
    assertEquals(0, kbase.getKiePackages().size());
    // lets re-compile and add it again
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithNotSubnetwork.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ksession.fireAllRules();
    // rule should be reactivated, since data is still in the session
    verify(alistener, times(2)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 87 with Person

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

the class ExecutionFlowControlTest method testSalienceExpression.

@Test
public void testSalienceExpression() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "test_salienceExpressionRule.drl");
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final PersonInterface person10 = new Person("bob", "cheese", 10);
    ksession.insert(person10);
    final PersonInterface person20 = new Person("mic", "cheese", 20);
    ksession.insert(person20);
    ksession.fireAllRules();
    assertEquals("Two rules should have been fired", 2, list.size());
    assertEquals("Rule 3 should have been fired first", "Rule 3", list.get(0));
    assertEquals("Rule 2 should have been fired second", "Rule 2", list.get(1));
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 88 with Person

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

the class FirstOrderLogicTest method testFromWithOr.

@Test
public void testFromWithOr() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_FromWithOr.drl");
    KieSession session = kbase.newKieSession();
    final List<Address> results = new ArrayList<Address>();
    session.setGlobal("results", results);
    Address a1 = new Address();
    a1.setZipCode("12345");
    Address a2 = new Address();
    a2.setZipCode("54321");
    Address a3 = new Address();
    a3.setZipCode("99999");
    Person p = new Person();
    p.addAddress(a1);
    p.addAddress(a2);
    p.addAddress(a3);
    session.insert(p);
    session.fireAllRules();
    assertEquals(2, results.size());
    assertTrue(results.contains(a1));
    assertTrue(results.contains(a2));
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 89 with Person

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

the class FirstOrderLogicTest method testCollectWithNestedFromWithParams.

@Test
public void testCollectWithNestedFromWithParams() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_CollectWithNestedFrom.drl");
    KieSession workingMemory = kbase.newKieSession();
    final List results = new ArrayList();
    workingMemory.setGlobal("results", results);
    final Person bob = new Person("bob", "stilton");
    Cheesery cheesery = new Cheesery();
    cheesery.addCheese(new Cheese("stilton", 10));
    cheesery.addCheese(new Cheese("brie", 20));
    cheesery.addCheese(new Cheese("muzzarela", 8));
    cheesery.addCheese(new Cheese("stilton", 5));
    cheesery.addCheese(new Cheese("provolone", 1));
    workingMemory.insert(bob);
    workingMemory.insert(cheesery);
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    List cheeses = (List) results.get(0);
    assertEquals(2, cheeses.size());
    assertEquals(bob.getLikes(), ((Cheese) cheeses.get(0)).getType());
    assertEquals(bob.getLikes(), ((Cheese) cheeses.get(1)).getType());
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) Cheese(org.drools.mvel.compiler.Cheese) Cheesery(org.drools.mvel.compiler.Cheesery) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 90 with Person

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

the class FirstOrderLogicTest method testCollectFromMVELAfterOr.

@Test
public void testCollectFromMVELAfterOr() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_CollectFromMVELAfterOr.drl");
    KieSession wm = kbase.newKieSession();
    List results = new ArrayList();
    wm.setGlobal("results", results);
    Person jill = new Person("jill");
    Person bob = new Person("bob");
    List addresses = new ArrayList();
    addresses.add(new Address("a"));
    addresses.add(new Address("b"));
    addresses.add(new Address("c"));
    bob.setAddresses(addresses);
    wm.insert(jill);
    wm.insert(bob);
    wm = SerializationHelper.getSerialisedStatefulKnowledgeSession(wm, true);
    results = (List) wm.getGlobal("results");
    wm.fireAllRules();
    assertEquals(2, results.size());
    assertEquals(3, ((Collection) results.get(0)).size());
}
Also used : Address(org.drools.mvel.compiler.Address) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) 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