use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class OrTest method testOrCE.
@Test
public void testOrCE() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "rule \"test or CE\"\n" + "when\n" + " $c : Cheese( type == \"stilton\" )\n" + " or\n" + " (\n" + " $c2 : Cheese( type == \"brie\" )\n" + " and\n" + " (\n" + " $p : Person( likes == \"stilton\" )\n" + " or\n" + " $p : Person( name == \"bob\" )\n" + " )\n" + " ) \n" + "then\n" + " results.add(\" OK \" );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("or-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final List list = new ArrayList();
ksession.setGlobal("results", list);
ksession.insert(new Cheese("brie", 10));
ksession.insert(new Person("bob"));
ksession.fireAllRules();
assertEquals("should have fired once", 1, list.size());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class ExistsTest method testNodeSharingNotExists.
@Test
public void testNodeSharingNotExists() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "rule rule1\n" + "when\n" + " not Cheese( type == Cheese.STILTON )\n" + "then\n" + " results.add( \"rule1\" );\n" + "end\n" + "\n" + "rule rule2\n" + "when\n" + " exists Cheese( type == Cheese.STILTON )\n" + "then\n" + " results.add( \"rule2\" );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("exists-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final List list = new ArrayList();
ksession.setGlobal("results", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("rule1", list.get(0));
ksession.insert(new Cheese("stilton", 10));
ksession.fireAllRules();
assertEquals(2, list.size());
assertEquals("rule2", list.get(1));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class FromTest method testFromSharingWithAccumulate.
@Test
public void testFromSharingWithAccumulate() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "\n" + "import java.util.List;\n" + "import java.util.ArrayList;\n" + "import " + Cheesery.class.getCanonicalName() + " ;\n" + "import " + Cheese.class.getCanonicalName() + " ;\n" + "\n" + "global java.util.List output1;\n" + "global java.util.List output2;\n" + "\n" + "rule R1\n" + " when\n" + " $cheesery : Cheesery()\n" + " $list : List( ) from accumulate( $cheese : Cheese( ) from $cheesery.getCheeses(),\n" + " init( List l = new ArrayList(); ),\n" + " action( l.add( $cheese ); )\n" + " result( l ) )\n" + " then\n" + " output1.add( $list );\n" + "end\n" + "rule R2\n" + " when\n" + " $cheesery : Cheesery()\n" + " $list : List( ) from accumulate( $cheese : Cheese( ) from $cheesery.getCheeses(),\n" + " init( List l = new ArrayList(); ),\n" + " action( l.add( $cheese ); )\n" + " result( l ) )\n" + " then\n" + " output2.add( $list );\n" + "end\n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("from-test", kieBaseTestConfiguration, drl);
EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNode(EntryPointId.DEFAULT);
ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Cheesery.class));
Sink[] otnSinks = otn.getSinks();
assertEquals(1, otnSinks.length);
LeftInputAdapterNode lia = (LeftInputAdapterNode) otnSinks[0];
Sink[] liaSinks = lia.getSinks();
// there must be only 1 shared from node
assertEquals(1, Stream.of(liaSinks).filter(sink -> sink instanceof FromNode).count());
final KieSession ksession = kbase.newKieSession();
try {
final List<?> output1 = new ArrayList<>();
ksession.setGlobal("output1", output1);
final List<?> output2 = new ArrayList<>();
ksession.setGlobal("output2", output2);
final Cheesery cheesery = new Cheesery();
cheesery.addCheese(new Cheese("stilton", 8));
cheesery.addCheese(new Cheese("provolone", 8));
final FactHandle cheeseryHandle = ksession.insert(cheesery);
ksession.fireAllRules();
assertEquals(1, output1.size());
assertEquals(2, ((List) output1.get(0)).size());
assertEquals(1, output2.size());
assertEquals(2, ((List) output2.get(0)).size());
output1.clear();
output2.clear();
ksession.update(cheeseryHandle, cheesery);
ksession.fireAllRules();
assertEquals(1, output1.size());
assertEquals(2, ((List) output1.get(0)).size());
assertEquals(1, output2.size());
assertEquals(2, ((List) output2.get(0)).size());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class EvalTest method testPredicateException.
@Test
public void testPredicateException() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "function boolean throwException(Object object) {\n" + " throw new RuntimeException( \"this should throw an exception\" );\n" + "}\n" + "\n" + "rule \"Throw Predicate Exception\"\n" + " when\n" + " Cheese( type1:type, eval( throwException( type1 ) ) )\n" + " then\n" + "\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("eval-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final Cheese brie = new Cheese("brie", 12);
try {
ksession.insert(brie);
ksession.fireAllRules();
fail("Should throw an Exception from the Predicate");
} catch (final Exception e) {
Throwable cause = e.getCause();
if (cause instanceof InvocationTargetException) {
cause = ((InvocationTargetException) cause).getTargetException();
}
assertTrue(cause.getMessage().contains("this should throw an exception"));
}
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class RemoveRuleTest method testRuleRemovalWithJoinedRootPattern.
@Test
public void testRuleRemovalWithJoinedRootPattern() {
String str = "";
str += "package org.drools.compiler \n";
str += "import " + Person.class.getCanonicalName() + ";\n";
str += "import " + Cheese.class.getCanonicalName() + ";\n";
str += "rule rule1 \n";
str += "when \n";
str += " String() \n";
str += " Person() \n";
str += "then \n";
str += "end \n";
str += "rule rule2 \n";
str += "when \n";
str += " String() \n";
str += " Cheese() \n";
str += "then \n";
str += "end \n";
final KieServices kieServices = KieServices.get();
final ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-remove-rule-with-joined-root-pattern", "1.0");
KieUtil.getKieModuleFromDrls(releaseId, kieBaseTestConfiguration, str);
final KieContainer kieContainer = kieServices.newKieContainer(releaseId);
final KieBase kbase = kieContainer.getKieBase();
final KieSession ksession = kbase.newKieSession();
final DefaultFactHandle handle = (DefaultFactHandle) ksession.insert("hello");
ksession.fireAllRules();
LeftTuple leftTuple = handle.getFirstLeftTuple();
assertNotNull(leftTuple);
assertNotNull(leftTuple.getPeer());
kbase.removeRule("org.drools.compiler", "rule2");
leftTuple = handle.getFirstLeftTuple();
assertNotNull(leftTuple);
assertNull(leftTuple.getHandleNext());
}
Aggregations