use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class FactHandleTest method testFactHandleSequence.
@Test
public void testFactHandleSequence() throws Exception {
String drlString = "package org.jboss.brms\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "rule \"FactHandleId\"\n" + " when\n" + " $c : Cheese()\n" + " then\n" + " // do something;\n" + "end";
KieBase kBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drlString);
List<Long> factHandleIDs = new ArrayList<>();
KieSession kieSession = kBase.newKieSession();
kieSession.addEventListener(createCollectEventListener(factHandleIDs));
kieSession.insert(new Cheese("mozzarella"));
kieSession.insert(new Cheese("pecorino"));
kieSession.fireAllRules();
kieSession.dispose();
// This should reset Fact Handle IDs
kieSession = kBase.newKieSession();
kieSession.addEventListener(createCollectEventListener(factHandleIDs));
kieSession.insert(new Cheese("parmigiano"));
kieSession.fireAllRules();
Assertions.assertThat(factHandleIDs).containsExactly(1L, 2L, 1L);
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AlphaNetworkModifyTest method testModifyWithLiaToAcc.
@Test
public void testModifyWithLiaToAcc() {
// technically you can't have a modify with InitialFactImpl
// But added test for completeness
final String drl = "package org.simple \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Cheese.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule x1 \n" + "when \n" + " Object() from accumulate( $p : Person() and Cheese(), collectList( $p ) )\n" + " Person() \n" + "then \n" + "end \n" + "rule x2 \n" + "when \n" + " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n" + " Person() \n" + "then \n" + "end \n" + "rule x3 \n" + "when \n" + " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n" + " Person() \n" + "then \n" + "end \n" + "rule x4 \n" + "when \n" + " Object() from accumulate( $ch : Cheese(), collectList( $ch ) )\n" + " Person() \n" + "then \n" + "end \n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("alpha-network-modify-test", kieBaseTestConfiguration, drl);
final KieSession wm = kbase.newKieSession();
try {
wm.fireAllRules();
final ObjectTypeNode otnInit = getObjectTypeNode(kbase, "InitialFactImpl");
assertNotNull(otnInit);
final LeftInputAdapterNode liaNode = (LeftInputAdapterNode) otnInit.getObjectSinkPropagator().getSinks()[0];
final LeftTupleSink[] sinks = liaNode.getSinkPropagator().getSinks();
assertEquals(0, sinks[0].getLeftInputOtnId().getId());
assertEquals(1, sinks[1].getLeftInputOtnId().getId());
assertEquals(2, sinks[2].getLeftInputOtnId().getId());
final ObjectTypeNode otnPerson = getObjectTypeNode(kbase, "Person");
final ObjectTypeNode otnCheese = getObjectTypeNode(kbase, "Cheese");
assertNotNull(otnPerson);
assertNotNull(otnCheese);
assertEquals(0, otnPerson.getOtnIdCounter());
assertEquals(0, otnCheese.getOtnIdCounter());
wm.insert(new Person());
wm.insert(new Cheese());
wm.fireAllRules();
assertEquals(3, otnPerson.getOtnIdCounter());
assertEquals(2, otnCheese.getOtnIdCounter());
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method testAccumulateReverseModify2.
@Test(timeout = 10000)
public void testAccumulateReverseModify2() {
final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Constraints everywhere\" salience 80\n" + " when\n" + " $person : Person( $likes : likes )\n" + " $total : Number( intValue > 20 )\n" + " from accumulate( Cheese( type == $likes, $p : price ),\n" + " init( int total = 0; ),\n" + " action( total += $p; ),\n" + " reverse( total -= $p; ),\n" + " result( new Integer( total ) ) )\n" + " then\n" + " results.add( $total );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("accumulate-test", kieBaseTestConfiguration, drl);
final KieSession wm = kbase.newKieSession();
try {
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 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());
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method testAccumulateReverseModifyMVEL2.
@Test(timeout = 10000)
public void testAccumulateReverseModifyMVEL2() {
final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Constraints everywhere\" salience 80\n" + " dialect \"mvel\"\n" + " when\n" + " $person : Person( $likes : likes )\n" + " $total : Number( intValue > 20 )\n" + " from accumulate( $cheese : Cheese( type == $likes, $p : price ),\n" + " init( int total = 0; ),\n" + " action( total += $p; ),\n" + " reverse( total -= $p; ),\n" + " result( new Integer( total ) ) )\n" + " then\n" + " results.add( $total );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("accumulate-test", kieBaseTestConfiguration, drl);
final KieSession wm = kbase.newKieSession();
try {
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 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);
wm.fireAllRules();
// 2 fires
assertEquals(2, results.size());
assertEquals(31, ((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());
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method testVarianceDouble.
@Test
public void testVarianceDouble() {
final String drl = "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R when\n" + " accumulate(\n" + " Cheese($price : price);\n" + " $result : variance($price)\n" + " )\n" + "then\n" + " list.add($result);\n" + "end";
final KieBase kieBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("accumulate-test", kieBaseTestConfiguration, drl);
assertEquals(0.00, cheeseInsertsFunction(kieBase, 3, 3, 3, 3, 3), 0.01);
assertEquals(0.80, cheeseInsertsFunction(kieBase, 4, 4, 3, 2, 2), 0.01);
assertEquals(1.20, cheeseInsertsFunction(kieBase, 5, 3, 3, 2, 2), 0.01);
assertEquals(2.80, cheeseInsertsFunction(kieBase, 5, 5, 2, 2, 1), 0.01);
assertEquals(2.80, cheeseInsertsFunction(kieBase, 6, 3, 3, 2, 1), 0.01);
assertEquals(4.40, cheeseInsertsFunction(kieBase, 6, 5, 2, 1, 1), 0.01);
assertEquals(16.00, cheeseInsertsFunction(kieBase, 11, 1, 1, 1, 1), 0.01);
assertEquals(36.00, cheeseInsertsFunction(kieBase, 15, 0, 0, 0, 0), 0.01);
}
Aggregations