use of org.drools.compiler.Cheese in project drools by kiegroup.
the class FirstOrderLogicTest method testForallSinglePatternWithExists.
@Test
public void testForallSinglePatternWithExists() throws Exception {
KieBase kbase = loadKnowledgeBase("test_ForallSinglePatternWithExists.drl");
KieSession workingMemory = createKnowledgeSession(kbase);
final List list = new ArrayList();
workingMemory.setGlobal("results", list);
workingMemory.insert(new Cheese("stilton", 10));
workingMemory.insert(new Cheese("brie", 10));
workingMemory.insert(new Cheese("brie", 10));
workingMemory.insert(new Order(1, "bob"));
workingMemory.insert(new Person("bob", "stilton", 10));
workingMemory.insert(new Person("mark", "stilton"));
workingMemory.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.compiler.Cheese in project drools by kiegroup.
the class FirstOrderLogicTest method testCollectModifyAlphaRestriction.
@Test
public void testCollectModifyAlphaRestriction() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CollectAlphaRestriction.drl");
KieSession wm = createKnowledgeSession(kbase);
final List results = new ArrayList();
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 FactHandle[] cheeseHandles = new FactHandle[cheese.length];
for (int i = 0; i < cheese.length; i++) {
cheeseHandles[i] = (FactHandle) wm.insert(cheese[i]);
}
// ---------------- 1st scenario
int fireCount = 0;
wm.fireAllRules();
assertEquals(++fireCount, results.size());
assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setType("brie");
wm.update(cheeseHandles[index], cheese[index]);
wm.fireAllRules();
assertEquals(++fireCount, results.size());
assertEquals(2, ((Collection) results.get(fireCount - 1)).size());
assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
// ---------------- 3rd scenario
wm.retract(cheeseHandles[2]);
wm.fireAllRules();
assertEquals(++fireCount, results.size());
assertEquals(1, ((Collection) results.get(fireCount - 1)).size());
assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
}
use of org.drools.compiler.Cheese in project drools by kiegroup.
the class FirstOrderLogicTest method testCollectModify.
@Test
public void testCollectModify() throws Exception {
KieBase kbase = loadKnowledgeBase("test_Collect.drl");
KieSession wm = createKnowledgeSession(kbase);
List results = new ArrayList();
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] = (FactHandle) wm.insert(cheese[i]);
}
final FactHandle bobHandle = (FactHandle) wm.insert(bob);
// ---------------- 1st scenario
int fireCount = 0;
wm.fireAllRules();
assertEquals(++fireCount, results.size());
assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setPrice(9);
wm.update(cheeseHandles[index], cheese[index]);
wm.fireAllRules();
assertEquals(++fireCount, results.size());
assertEquals(3, ((Collection) results.get(fireCount - 1)).size());
assertEquals(ArrayList.class.getName(), results.get(fireCount - 1).getClass().getName());
// ---------------- 3rd scenario
bob.setLikes("brie");
wm.update(bobHandle, bob);
wm.fireAllRules();
assertEquals(fireCount, results.size());
// ---------------- 4th scenario
wm.retract(cheeseHandles[3]);
wm.fireAllRules();
// should not have fired as per constraint
assertEquals(fireCount, results.size());
}
use of org.drools.compiler.Cheese in project drools by kiegroup.
the class FirstOrderLogicTest method testForallSinglePattern.
@Test
public void testForallSinglePattern() throws Exception {
KieBase kbase = loadKnowledgeBase("test_ForallSinglePattern.drl");
KieSession workingMemory = createKnowledgeSession(kbase);
final List list = new ArrayList();
workingMemory.setGlobal("results", list);
int fired = 0;
// no cheeses, so should fire
workingMemory.fireAllRules();
assertEquals(++fired, list.size());
// only stilton, so should not fire again
FactHandle stilton1 = (FactHandle) workingMemory.insert(new Cheese("stilton", 10));
workingMemory.fireAllRules();
assertEquals(fired, list.size());
// only stilton, so should not fire again
FactHandle stilton2 = (FactHandle) workingMemory.insert(new Cheese("stilton", 11));
workingMemory.fireAllRules();
assertEquals(fired, list.size());
// still only stilton, so should not fire
workingMemory.retract(stilton1);
workingMemory.fireAllRules();
assertEquals(fired, list.size());
// there is a brie, so should not fire
FactHandle brie = (FactHandle) workingMemory.insert(new Cheese("brie", 10));
workingMemory.fireAllRules();
assertEquals(fired, list.size());
// no brie anymore, so should fire
workingMemory.retract(brie);
workingMemory.fireAllRules();
assertEquals(++fired, list.size());
// no more cheese, but since it already fired, should not fire again
workingMemory.retract(stilton2);
workingMemory.fireAllRules();
assertEquals(fired, list.size());
}
use of org.drools.compiler.Cheese in project drools by kiegroup.
the class FirstOrderLogicTest method testCollectAfterOrCE.
@Test
public void testCollectAfterOrCE() throws Exception {
Collection<KiePackage> pkgs = loadKnowledgePackages("test_OrCEFollowedByCollect.drl");
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
KieSession session = createKnowledgeSession(kbase);
// Set up facts
final Cheesery bonFromage = new Cheesery();
bonFromage.addCheese(new Cheese("cheddar"));
bonFromage.addCheese(new Cheese("cheddar"));
session.insert(bonFromage);
int rules = session.fireAllRules();
assertEquals(2, rules);
// Serialize and test again
pkgs = SerializationHelper.serializeObject(pkgs);
kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
session = createKnowledgeSession(kbase);
session.insert(bonFromage);
rules = session.fireAllRules();
assertEquals(2, rules);
}
Aggregations