use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class JoinNodeRangeIndexingTest method testCoercionStringVsIntWithMap.
@Test
public void testCoercionStringVsIntWithMap() {
final String drl = "package org.drools.compiler.integrationtests;\n" + "import " + Map.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R\n" + " when\n" + " $map : Map()" + " p : Cheese(type < $map.get(\"key\"))\n" + " then\n" + " list.add( p );\n" + "end";
// Integer is coerced to String (thus, String comparison)
final KieBase kbase = getKieBaseWithRangeIndexOption(drl);
// We don't index this case
assertIndexedFalse(kbase, Cheese.class);
final KieSession ksession = kbase.newKieSession();
try {
final List<Cheese> list = new ArrayList<>();
ksession.setGlobal("list", list);
final Cheese cheese1 = new Cheese("1");
ksession.insert(cheese1);
final Cheese cheese5 = new Cheese("5");
ksession.insert(cheese5);
final Cheese cheese10 = new Cheese("10");
ksession.insert(cheese10);
Map<String, Object> map = new HashMap<>();
map.put("key", 5);
ksession.insert(map);
ksession.fireAllRules();
// If we do String comparison, cheese10 is also contained
Assertions.assertThat(list).containsExactly(cheese1);
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class RuleTemplateTest method testGuidedRuleTemplate.
@Test
public void testGuidedRuleTemplate() throws Exception {
final String resourceName = "cheese.template";
final KieResources kieResources = KieServices.get().getResources();
final Resource resource = kieResources.newClassPathResource(resourceName, RuleTemplateTest.class);
resource.setResourceType(ResourceType.TEMPLATE);
final KieBase kBase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
final KieSession kSession = kBase.newKieSession();
final Cheese cheese = new Cheese();
cheese.setPrice(90);
final Customer petr = new Customer(0, "Peter");
final Customer john = new Customer(1, "John");
kSession.insert(cheese);
kSession.insert(petr);
kSession.insert(john);
Assertions.assertThat(kSession.fireAllRules()).as("One rule should be fired").isEqualTo(1);
final Collection messages = kSession.getObjects(object -> object instanceof Message);
Assertions.assertThat(messages).hasSize(1);
Assertions.assertThat(messages).hasOnlyOneElementSatisfying(message -> ((Message) message).getMessage().compareTo("Peter satisfied"));
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AndTest method testExplicitAnd.
@Test
public void testExplicitAnd() {
final String drl = "package HelloWorld\n" + " \n" + "import " + Message.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + " ;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Hello World\"\n" + " when\n" + " Message() and Cheese()\n" + " then\n" + " list.add(\"hola\");\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("and-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Message("hola"));
ksession.fireAllRules();
assertEquals(0, list.size());
ksession.insert(new Cheese("brie", 33));
ksession.fireAllRules();
assertEquals(1, ((List) ksession.getGlobal("list")).size());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese 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.Cheese 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();
}
}
Aggregations