use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class ExtendsTest method testDeclareInheritance.
@Test
public void testDeclareInheritance() throws Exception {
String s1 = "package org.drools;\n" + "import org.drools.compiler.integrationtests.ExtendsTest.*;\n" + "\n" + "declare A \n" + " @role( event )" + " @typesafe( false )\n" + "end\n" + "" + "declare C @role( event ) @typesafe( false ) end \n" + "" + "rule R \n" + "when " + " $x : C( this.x == 1 ) \n" + "then\n" + " System.out.println( $x ); \n" + "end\n" + "";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
KieSession knowledgeSession = knowledgeBase.newKieSession();
FactHandle h = knowledgeSession.insert(new X());
assertTrue(((InternalFactHandle) h).isEvent());
}
use of org.kie.api.runtime.rule.FactHandle in project drools by kiegroup.
the class FirstOrderLogicTest method testOr.
@Test
public void testOr() throws Exception {
KieBase kbase = loadKnowledgeBase("test_OrNesting.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 Cheese brie = new Cheese("brie", 15);
final Person mark = new Person("mark", "stilton");
FactHandle ch = (FactHandle) workingMemory.insert(cheddar);
FactHandle ph = (FactHandle) workingMemory.insert(provolone);
FactHandle bh = (FactHandle) workingMemory.insert(brie);
FactHandle markh = (FactHandle) workingMemory.insert(mark);
workingMemory.fireAllRules();
assertEquals(1, list.size());
}
use of org.kie.api.runtime.rule.FactHandle 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.kie.api.runtime.rule.FactHandle 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.kie.api.runtime.rule.FactHandle 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());
}
Aggregations