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());
}
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);
}
}
}
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());
}
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());
}
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());
}
Aggregations