use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithFromChaining.
@Test(timeout = 10000)
public void testAccumulateWithFromChaining() {
final String drl = "package org.drools.compiler\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\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 \"Accumulate with From Chaining\" salience 80\n" + " when\n" + " $cheesery : Cheesery()\n" + " $person : Person( $likes : likes )\n" + " $list : List( size > 2 )\n" + " from accumulate( $cheese : Cheese( type == $likes ) from $cheesery.getCheeses(),\n" + " init( List l = new ArrayList(); ),\n" + " action( l.add( $cheese ); )\n" + " result( l ) )\n" + " then\n" + " results.add( $list );\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", 8), new Cheese("stilton", 10), new Cheese("stilton", 9), new Cheese("brie", 4), new Cheese("brie", 1), new Cheese("provolone", 8) };
final Cheesery cheesery = new Cheesery();
for (final Cheese aCheese : cheese) {
cheesery.addCheese(aCheese);
}
final 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());
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method testAccumulateWithPreviouslyBoundVariables.
@Test(timeout = 10000)
public void testAccumulateWithPreviouslyBoundVariables() {
final String drl = "package org.drools.compiler\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Accumulate with bound var \" salience 100\n" + " when\n" + " Cheese( type == \"stilton\", $price : price )\n" + " $totalAmount : Number() from accumulate( $c : Cheese( type == \"brie\" ),\n" + " init( int total = 0; ),\n" + " action( total += $c.getPrice() + $price; ),\n" + " reverse( total -= $c.getPrice() + $price; ),\n" + " result( new Integer( total ) ) );\n" + " then\n" + " //System.out.println(\"Total amount = US$ \"+$totalAmount );\n" + " results.add($totalAmount);\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);
wm.insert(new Cheese("stilton", 10));
wm.insert(new Cheese("brie", 5));
wm.insert(new Cheese("provolone", 150));
wm.insert(new Cheese("brie", 20));
wm.fireAllRules();
assertEquals(1, results.size());
assertEquals(45, results.get(0));
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateTest method updateReferences.
private void updateReferences(final KieSession session, final DataSet data) {
data.results = (List<?>) session.getGlobal("results");
for (final Object next : session.getObjects()) {
if (next instanceof Cheese) {
final Cheese c = (Cheese) next;
data.cheese[c.getOldPrice()] = c;
data.cheeseHandles[c.getOldPrice()] = session.getFactHandle(c);
assertNotNull(data.cheeseHandles[c.getOldPrice()]);
} else if (next instanceof Person) {
data.bob = (Person) next;
data.bobHandle = session.getFactHandle(data.bob);
}
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AccumulateMvelDialectTest method testMVELAccumulate2WM.
// See https://issues.jboss.org/browse/DROOLS-2733
@Test(timeout = 10000)
public void testMVELAccumulate2WM() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_AccumulateMVEL.drl");
final KieSession wm1 = kbase.newKieSession();
try {
final List<?> results1 = new ArrayList<>();
wm1.setGlobal("results", results1);
final List<?> results2 = new ArrayList<>();
final KieSession wm2 = kbase.newKieSession();
try {
wm2.setGlobal("results", results2);
wm1.insert(new Person("Bob", "stilton", 20));
wm1.insert(new Person("Mark", "provolone"));
wm2.insert(new Person("Bob", "stilton", 20));
wm2.insert(new Person("Mark", "provolone"));
wm1.insert(new Cheese("stilton", 10));
wm1.insert(new Cheese("brie", 5));
wm2.insert(new Cheese("stilton", 10));
wm1.insert(new Cheese("provolone", 150));
wm2.insert(new Cheese("brie", 5));
wm2.insert(new Cheese("provolone", 150));
wm1.fireAllRules();
wm2.fireAllRules();
} finally {
wm2.dispose();
}
assertEquals(165, results1.get(0));
assertEquals(10, results1.get(1));
assertEquals(150, results1.get(2));
assertEquals(10, results1.get(3));
assertEquals(210, results1.get(4));
assertEquals(165, results2.get(0));
assertEquals(10, results2.get(1));
assertEquals(150, results2.get(2));
assertEquals(10, results2.get(3));
assertEquals(210, results2.get(4));
} finally {
wm1.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class CommandsTest method testSessionTimeCommands.
@Test
public void testSessionTimeCommands() {
final String drl = "package org.drools.compiler.integrationtests \n" + "import " + Cheese.class.getCanonicalName() + " \n" + "rule StringRule \n" + "when \n" + " $c : Cheese() \n" + "then \n" + "end \n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession kSession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final KieCommands kieCommands = KieServices.get().getCommands();
assertEquals(0L, (long) kSession.execute(kieCommands.newGetSessionTime()));
assertEquals(2000L, (long) kSession.execute(kieCommands.newAdvanceSessionTime(2, TimeUnit.SECONDS)));
assertEquals(2000L, (long) kSession.execute(kieCommands.newGetSessionTime()));
} finally {
kSession.dispose();
}
}
Aggregations