Search in sources :

Example 96 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class AccumulateTest method testAccumulateReverseModify2.

@Test(timeout = 10000)
public void testAccumulateReverseModify2() throws Exception {
    // read in the source
    KieSession wm = getKieSessionFromResources("test_AccumulateReverseModify2.drl");
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) };
    final Person bob = new Person("Bob", "stilton");
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = wm.insert(cheese[i]);
    }
    final FactHandle bobHandle = wm.insert(bob);
    // ---------------- 1st scenario
    wm.fireAllRules();
    // no fire, as per rule constraints
    assertEquals(0, results.size());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(9);
    wm.update(cheeseHandles[index], cheese[index]);
    wm.fireAllRules();
    // 1 fire
    assertEquals(1, results.size());
    assertEquals(24, ((Number) results.get(results.size() - 1)).intValue());
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    cheese[3].setPrice(20);
    wm.update(cheeseHandles[3], cheese[3]);
    wm.fireAllRules();
    // 2 fires
    assertEquals(2, results.size());
    assertEquals(36, ((Number) results.get(results.size() - 1)).intValue());
    // ---------------- 4th scenario
    wm.delete(cheeseHandles[3]);
    wm.fireAllRules();
    // should not have fired as per constraint
    assertEquals(2, results.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 97 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class AccumulateTest method updateReferences.

private void updateReferences(final KieSession session, final DataSet data) {
    data.results = (List<?>) session.getGlobal("results");
    for (Iterator<?> it = session.getObjects().iterator(); it.hasNext(); ) {
        Object next = it.next();
        if (next instanceof Cheese) {
            Cheese c = (Cheese) next;
            data.cheese[c.getOldPrice()] = c;
            data.cheeseHandles[c.getOldPrice()] = session.getFactHandle(c);
            assertNotNull(data.cheeseHandles[c.getOldPrice()]);
        } else if (next instanceof Person) {
            data.bob = (Person) next;
            data.bobHandle = session.getFactHandle(data.bob);
        }
    }
}
Also used : Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person)

Example 98 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class AccumulateTest method execTestAccumulateCount.

public void execTestAccumulateCount(String fileName) throws Exception {
    // read in the source
    KieSession wm = getKieSessionFromResources(fileName);
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 8), new Cheese("stilton", 10), new Cheese("stilton", 9), new Cheese("brie", 4), new Cheese("brie", 1), new Cheese("provolone", 8) };
    final Person bob = new Person("Bob", "stilton");
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = wm.insert(cheese[i]);
    }
    final FactHandle bobHandle = wm.insert(bob);
    // ---------------- 1st scenario
    wm.fireAllRules();
    // no fire, as per rule constraints
    assertEquals(1, results.size());
    assertEquals(3, ((Number) results.get(results.size() - 1)).intValue());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(3);
    wm.update(cheeseHandles[index], cheese[index]);
    wm.fireAllRules();
    // 1 fire
    assertEquals(2, results.size());
    assertEquals(3, ((Number) results.get(results.size() - 1)).intValue());
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    wm.fireAllRules();
    // 2 fires
    assertEquals(3, results.size());
    assertEquals(2, ((Number) results.get(results.size() - 1)).intValue());
    // ---------------- 4th scenario
    wm.delete(cheeseHandles[3]);
    wm.fireAllRules();
    // should not have fired as per constraint
    assertEquals(3, results.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person)

Example 99 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class AccumulateTest method testAccumulateModifyMVEL.

@Test(timeout = 10000)
public void testAccumulateModifyMVEL() throws Exception {
    // read in the source
    KieSession wm = getKieSessionFromResources("test_AccumulateModifyMVEL.drl");
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 2), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 16), new Cheese("provolone", 8) };
    final Person bob = new Person("Bob", "stilton");
    final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
    for (int i = 0; i < cheese.length; i++) {
        cheeseHandles[i] = wm.insert(cheese[i]);
    }
    final org.kie.api.runtime.rule.FactHandle bobHandle = wm.insert(bob);
    // ---------------- 1st scenario
    wm.fireAllRules();
    // no fire, as per rule constraints
    assertEquals(0, results.size());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setPrice(9);
    wm.update(cheeseHandles[index], cheese[index]);
    wm.fireAllRules();
    // 1 fire
    assertEquals(1, results.size());
    assertEquals(24, ((Cheesery) results.get(results.size() - 1)).getTotalAmount());
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    wm.fireAllRules();
    // 2 fires
    assertEquals(2, results.size());
    assertEquals(31, ((Cheesery) results.get(results.size() - 1)).getTotalAmount());
    // ---------------- 4th scenario
    wm.delete(cheeseHandles[3]);
    wm.fireAllRules();
    // should not have fired as per constraint
    assertEquals(2, results.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) Cheese(org.drools.compiler.Cheese) FactHandle(org.kie.api.runtime.rule.FactHandle) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 100 with Cheese

use of org.drools.compiler.Cheese in project drools by kiegroup.

the class AccumulateTest method testAccumulateWithFromChaining.

@Test(timeout = 10000)
public void testAccumulateWithFromChaining() throws Exception {
    // read in the source
    KieSession wm = getKieSessionFromResources("test_AccumulateWithFromChaining.drl");
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 8), new Cheese("stilton", 10), new Cheese("stilton", 9), new Cheese("brie", 4), new Cheese("brie", 1), new Cheese("provolone", 8) };
    Cheesery cheesery = new Cheesery();
    for (int i = 0; i < cheese.length; i++) {
        cheesery.addCheese(cheese[i]);
    }
    FactHandle cheeseryHandle = wm.insert(cheesery);
    final Person bob = new Person("Bob", "stilton");
    final FactHandle bobHandle = wm.insert(bob);
    // ---------------- 1st scenario
    wm.fireAllRules();
    // one fire, as per rule constraints
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(results.size() - 1)).size());
    // ---------------- 2nd scenario
    final int index = 1;
    cheese[index].setType("brie");
    wm.update(cheeseryHandle, cheesery);
    wm.fireAllRules();
    // no fire
    assertEquals(1, results.size());
    System.out.println(results);
    // ---------------- 3rd scenario
    bob.setLikes("brie");
    wm.update(bobHandle, bob);
    wm.fireAllRules();
    // 2 fires
    assertEquals(2, results.size());
    assertEquals(3, ((List) results.get(results.size() - 1)).size());
    // ---------------- 4th scenario
    cheesery.getCheeses().remove(cheese[3]);
    wm.update(cheeseryHandle, cheesery);
    wm.fireAllRules();
    // should not have fired as per constraint
    assertEquals(2, results.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Cheesery(org.drools.compiler.Cheesery) Person(org.drools.compiler.Person) Test(org.junit.Test)

Aggregations

Cheese (org.drools.compiler.Cheese)222 Test (org.junit.Test)204 KieSession (org.kie.api.runtime.KieSession)195 ArrayList (java.util.ArrayList)152 KieBase (org.kie.api.KieBase)138 List (java.util.List)100 Person (org.drools.compiler.Person)92 FactHandle (org.kie.api.runtime.rule.FactHandle)53 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)36 KiePackage (org.kie.api.definition.KiePackage)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 InternalFactHandle (org.drools.core.common.InternalFactHandle)20 Cheesery (org.drools.compiler.Cheesery)14 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)14 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)13 HashMap (java.util.HashMap)10 Command (org.kie.api.command.Command)8 ExecutionResults (org.kie.api.runtime.ExecutionResults)8 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)7