use of org.drools.testcoverage.common.model.Cheesery in project drools by kiegroup.
the class FromTest method testFromDeclarationWithWorkingMemoryLogger.
/**
* JBRULES-1415 Certain uses of from causes NullPointerException in WorkingMemoryLogger
*/
@Test
public void testFromDeclarationWithWorkingMemoryLogger() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "rule \"Test Rule\"\n" + "when\n" + " $cheesery : Cheesery()\n" + " Cheese( $type : type) from $cheesery.cheeses\n" + "then\n" + " list.add( $type );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("from-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession();
try {
final List list = new ArrayList();
session.setGlobal("list", list);
final Cheesery cheesery = new Cheesery();
cheesery.addCheese(new Cheese("stilton", 22));
session.insert(cheesery);
session.fireAllRules();
assertEquals(1, ((List) session.getGlobal("list")).size());
assertEquals("stilton", ((List) session.getGlobal("list")).get(0));
} finally {
session.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheesery in project drools by kiegroup.
the class FromTest method testMultipleFroms.
@Test
public void testMultipleFroms() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import java.util.List;\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global Cheesery cheesery;\n" + "global java.util.List results;\n" + "\n" + "rule MyRule\n" + " dialect \"java\"\n" + "when\n" + " $i : List() from collect(Cheese() from cheesery.getCheeses())\n" + " $k : List() from collect(Cheese() from cheesery.getCheeses())\n" + "then\n" + " results.add( $i );\n" + " results.add( $k );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("from-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final List results = new ArrayList();
ksession.setGlobal("results", results);
final Cheesery cheesery = new Cheesery();
cheesery.addCheese(new Cheese("stilton", 15));
cheesery.addCheese(new Cheese("brie", 10));
ksession.setGlobal("cheesery", cheesery);
ksession.fireAllRules();
assertEquals(2, results.size());
assertEquals(2, ((List) results.get(0)).size());
assertEquals(2, ((List) results.get(1)).size());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheesery 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.Cheesery in project drools by kiegroup.
the class AccumulateTest method testAccumulateModify.
@Test(timeout = 10000)
public void testAccumulateModify() {
final String drl = "package org.drools.compiler.integrationtests;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Constraints everywhere\" salience 80\n" + " when\n" + " $person : Person( $likes : likes )\n" + " $cheesery : Cheesery( totalAmount > 20 )\n" + " from accumulate( $cheese : Cheese( type == $likes ),\n" + " init( Cheesery cheesery = new Cheesery(); ),\n" + " action( cheesery.addCheese( $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 org.kie.api.runtime.rule.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.Cheesery in project drools by kiegroup.
the class ImportsTest method testImportStaticClass.
@Test
public void testImportStaticClass() {
final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "\n" + "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "import " + Cheesery.Maturity.class.getCanonicalName() + ";\n" + "global java.util.List list\n" + "\n" + "rule \"status, int based enum\"\n" + " salience 10\n" + " when\n" + " p : Cheesery(status == Cheesery.SELLING_CHEESE, maturity == Maturity.OLD)\n" + " then\n" + " list.add( p );\n" + "\n" + "end \n" + "\n" + "rule \"maturity, object based enum\"\n" + " when\n" + " p : Cheesery(status == Cheesery.MAKING_CHEESE, maturity == Maturity.YOUNG)\n" + " then\n" + " list.add( p );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("imports-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession();
try {
final List list = new ArrayList();
session.setGlobal("list", list);
final Cheesery cheesery1 = new Cheesery();
cheesery1.setStatus(Cheesery.SELLING_CHEESE);
cheesery1.setMaturity(Cheesery.Maturity.OLD);
session.insert(cheesery1);
final Cheesery cheesery2 = new Cheesery();
cheesery2.setStatus(Cheesery.MAKING_CHEESE);
cheesery2.setMaturity(Cheesery.Maturity.YOUNG);
session.insert(cheesery2);
session.fireAllRules();
assertEquals(2, list.size());
assertEquals(cheesery1, list.get(0));
assertEquals(cheesery2, list.get(1));
} finally {
session.dispose();
}
}
Aggregations