use of org.drools.compiler.Cheesery 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);
}
use of org.drools.compiler.Cheesery 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());
}
use of org.drools.compiler.Cheesery in project drools by kiegroup.
the class AlphaTest method testNPEOnMVELAlphaPredicates.
@Test
public void testNPEOnMVELAlphaPredicates() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_NPEOnMVELPredicate.drl"));
final KieSession session = createKnowledgeSession(kbase);
final List list = new ArrayList();
session.setGlobal("results", list);
final Cheese cheese = new Cheese("stilton", 10);
final Cheesery cheesery = new Cheesery();
cheesery.addCheese(cheese);
final Person bob = new Person("bob", "stilton");
final Cheese cheese2 = new Cheese();
bob.setCheese(cheese2);
final FactHandle p = session.insert(bob);
final FactHandle c = session.insert(cheesery);
session.fireAllRules();
assertEquals("should not have fired", 0, list.size());
cheese2.setType("stilton");
session.update(p, bob);
session.fireAllRules();
assertEquals(1, list.size());
}
use of org.drools.compiler.Cheesery in project drools by kiegroup.
the class FirstOrderLogicTest method testCollectWithNestedFromWithParams.
@Test
public void testCollectWithNestedFromWithParams() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CollectWithNestedFrom.drl");
KieSession workingMemory = createKnowledgeSession(kbase);
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());
}
use of org.drools.compiler.Cheesery in project drools by kiegroup.
the class FirstOrderLogicTest method testFromInsideNotAndExists.
@Test
public void testFromInsideNotAndExists() throws Exception {
KieBase kbase = loadKnowledgeBase("test_FromInsideNotAndExists.drl");
KieSession workingMemory = createKnowledgeSession(kbase);
final List list = new ArrayList();
workingMemory.setGlobal("results", list);
final Cheese cheddar = new Cheese("cheddar", 7);
final Cheese provolone = new Cheese("provolone", 5);
final Cheesery cheesery = new Cheesery();
cheesery.addCheese(cheddar);
cheesery.addCheese(provolone);
FactHandle handle = (FactHandle) workingMemory.insert(cheesery);
workingMemory.fireAllRules();
assertEquals(0, list.size());
cheesery.addCheese(new Cheese("stilton", 10));
cheesery.removeCheese(cheddar);
workingMemory.update(handle, cheesery);
workingMemory.fireAllRules();
assertEquals(2, list.size());
}
Aggregations