use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class FromTest method testBasicFrom.
@Test
public void testBasicFrom() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "import java.util.List;\n" + "\n" + "global List list1;\n" + "global List list2;\n" + "global List list3;\n" + "global Cheesery cheesery;\n" + "\n" + "rule \"test from using a global\"\n" + " when\n" + " $cheese : Cheese() from cheesery.getCheeses()\n" + " then\n" + " list1.add( $cheese );\n" + "end\n" + "\n" + "\n" + "rule \"test from using a declaration\"\n" + " when\n" + " $ch : Cheesery()\n" + " $cheese : Cheese() from $ch.getCheeses()\n" + " then\n" + " list2.add( $cheese );\n" + "end\n" + "\n" + "\n" + "rule \"test from with filter\"\n" + " when\n" + " $cheese : Cheese(type == \"stilton\" ) from cheesery.getCheeses()\n" + " then\n" + " list3.add( $cheese );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("from-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final List list1 = new ArrayList();
ksession.setGlobal("list1", list1);
final List list2 = new ArrayList();
ksession.setGlobal("list2", list2);
final List list3 = new ArrayList();
ksession.setGlobal("list3", list3);
final Cheesery cheesery = new Cheesery();
final Cheese stilton = new Cheese("stilton", 12);
final Cheese cheddar = new Cheese("cheddar", 15);
cheesery.addCheese(stilton);
cheesery.addCheese(cheddar);
ksession.setGlobal("cheesery", cheesery);
ksession.insert(cheesery);
final Person p = new Person("stilton");
ksession.insert(p);
ksession.fireAllRules();
ksession.fireAllRules();
// from using a global
assertEquals(2, ((List) ksession.getGlobal("list1")).size());
assertEquals(cheddar, ((List) ksession.getGlobal("list1")).get(0));
assertEquals(stilton, ((List) ksession.getGlobal("list1")).get(1));
// from using a declaration
assertEquals(2, ((List) ksession.getGlobal("list2")).size());
assertEquals(cheddar, ((List) ksession.getGlobal("list2")).get(0));
assertEquals(stilton, ((List) ksession.getGlobal("list2")).get(1));
// from using a declaration
assertEquals(1, ((List) ksession.getGlobal("list3")).size());
assertEquals(stilton, ((List) ksession.getGlobal("list3")).get(0));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class FromTest method testFromNodeWithMultipleBetas.
@Test
public void testFromNodeWithMultipleBetas() {
final String drl = "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Address.class.getCanonicalName() + ";\n" + "rule R1 when\n" + " $p : Person( $name : name, $addresses : addresses )\n" + " $c : Cheese( $type: type == $name )\n" + " $a : Address( street == $type, city == $name ) from $addresses\n" + "then\n" + "end\n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("from-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final Person p = new Person("x");
p.addAddress(new Address("x", 1, "x"));
p.addAddress(new Address("y", 2, "y"));
ksession.insert(p);
ksession.insert(new Cheese("x"));
ksession.fireAllRules();
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method testStandardDeviationDouble.
@Test
public void testStandardDeviationDouble() {
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 : standardDeviation($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.89, cheeseInsertsFunction(kieBase, 4, 4, 3, 2, 2), 0.01);
assertEquals(1.10, cheeseInsertsFunction(kieBase, 5, 3, 3, 2, 2), 0.01);
assertEquals(1.67, cheeseInsertsFunction(kieBase, 5, 5, 2, 2, 1), 0.01);
assertEquals(1.67, cheeseInsertsFunction(kieBase, 6, 3, 3, 2, 1), 0.01);
assertEquals(2.10, cheeseInsertsFunction(kieBase, 6, 5, 2, 1, 1), 0.01);
assertEquals(4.00, cheeseInsertsFunction(kieBase, 11, 1, 1, 1, 1), 0.01);
assertEquals(6.00, cheeseInsertsFunction(kieBase, 15, 0, 0, 0, 0), 0.01);
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method testAccumulateReverseModifyMVEL.
@Test(timeout = 10000)
public void testAccumulateReverseModifyMVEL() {
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" + " $cheesery : Cheesery( totalAmount > 20 )\n" + " from accumulate( $cheese : Cheese( type == $likes ),\n" + " init( cheesery = new Cheesery(); ),\n" + " action( cheesery.addCheese( $cheese ); ),\n" + " reverse( cheesery.removeCheese( $cheese ); ),\n" + " result( cheesery ) );\n" + " then\n" + " results.add( $cheesery );\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, ((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());
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method execTestAccumulateSum.
private void execTestAccumulateSum(final String fileName) {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, fileName);
final KieSession session = kbase.newKieSession();
try {
final DataSet data = new DataSet();
data.results = new ArrayList<>();
session.setGlobal("results", data.results);
data.cheese = new Cheese[] { new Cheese("stilton", 8, 0), new Cheese("stilton", 10, 1), new Cheese("stilton", 9, 2), new Cheese("brie", 11, 3), new Cheese("brie", 4, 4), new Cheese("provolone", 8, 5) };
data.bob = new Person("Bob", "stilton");
data.cheeseHandles = new FactHandle[data.cheese.length];
for (int i = 0; i < data.cheese.length; i++) {
data.cheeseHandles[i] = session.insert(data.cheese[i]);
}
data.bobHandle = session.insert(data.bob);
// ---------------- 1st scenario
session.fireAllRules();
assertEquals(1, data.results.size());
assertEquals(27, ((Number) data.results.get(data.results.size() - 1)).intValue());
updateReferences(session, data);
// ---------------- 2nd scenario
final int index = 1;
data.cheese[index].setPrice(3);
session.update(data.cheeseHandles[index], data.cheese[index]);
final int count = session.fireAllRules();
assertEquals(1, count);
assertEquals(2, data.results.size());
assertEquals(20, ((Number) data.results.get(data.results.size() - 1)).intValue());
// ---------------- 3rd scenario
data.bob.setLikes("brie");
session.update(data.bobHandle, data.bob);
session.fireAllRules();
assertEquals(3, data.results.size());
assertEquals(15, ((Number) data.results.get(data.results.size() - 1)).intValue());
// ---------------- 4th scenario
session.delete(data.cheeseHandles[3]);
session.fireAllRules();
// should not have fired as per constraint
assertEquals(3, data.results.size());
} finally {
session.dispose();
}
}
Aggregations