use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class RuleFlowGroupTest method testRuleFlowGroupWithLockOnActivate.
@Test
public void testRuleFlowGroupWithLockOnActivate() {
// JBRULES-3590
final String drl = "import " + Person.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "rule R1\n" + "ruleflow-group \"group1\"\n" + "lock-on-active true\n" + "when\n" + " $p : Person()\n" + "then\n" + " $p.setName(\"John\");\n" + " update ($p);\n" + "end\n" + "rule R2\n" + "ruleflow-group \"group1\"\n" + "lock-on-active true\n" + "when\n" + " $p : Person( name == null )\n" + " forall ( Cheese ( type == \"cheddar\" ))\n" + "then\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("ruleflowgroup-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
ksession.insert(new Person());
ksession.insert(new Cheese("gorgonzola"));
((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("group1");
assertEquals(1, ksession.fireAllRules());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class ExceptionTest method testReturnValueException.
@Test
public void testReturnValueException() {
final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + TestException.class.getCanonicalName() + ";\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "function String throwException( ) {\n" + " throw new TestException( \"this should throw an exception\" );\n" + "}\n" + "\n" + "rule \"Throw ReturnValue Exception\"\n" + " when\n" + " Cheese( type == ( throwException( ) ) )\n" + " then\n" + "\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("exception-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final Cheese brie = new Cheese("brie", 12);
Assertions.assertThatThrownBy(() -> {
ksession.insert(brie);
ksession.fireAllRules();
}).hasRootCauseInstanceOf(TestException.class);
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class OrTest method testOrWithReturnValueRestriction.
@Test
public void testOrWithReturnValueRestriction() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + " \n" + "rule \"r1\"\n" + "when\n" + " Cheese( type == \"brie\", $price : price )\n" + " Cheese( type == \"stilton\", price == 10 || == ( $price % 10 ) )\n" + "then\n" + " // noop\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("or-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
ksession.insert(new Cheese("brie", 18));
ksession.insert(new Cheese("stilton", 8));
ksession.insert(new Cheese("brie", 28));
final int fired = ksession.fireAllRules();
assertEquals(2, fired);
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class OrTest method testOr.
@Test
public void testOr() {
final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "\n" + "rule \"literal test rule\"\n" + " when\n" + " Cheese(type == \"stilton\" ) or Cheese(type == \"cheddar\")\n" + " then\n" + " list.add(\"got cheese\");\n" + "end \n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("or-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession();
try {
final List list = new ArrayList();
session.setGlobal("list", list);
final Cheese cheddar = new Cheese("cheddar", 5);
final FactHandle h = session.insert(cheddar);
session.fireAllRules();
// just one added
assertEquals("got cheese", list.get(0));
assertEquals(1, list.size());
session.delete(h);
session.fireAllRules();
// still just one
assertEquals(1, list.size());
session.insert(new Cheese("stilton", 5));
session.fireAllRules();
// now have one more
assertEquals(2, ((List) session.getGlobal("list")).size());
} finally {
session.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class OrTest method testOrWithBinding.
@Test
public void testOrWithBinding() {
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" + "\n" + "rule \"MyRule\"\n" + " when\n" + " c : (Cheese( type == \"stilton\") or\n" + " Cheese( type == \"brie\" ) or\n" + " Cheese( type == \"muzzarella\" ) )\n" + " p : Person()\n" + " then\n" + " results.add(c);\n" + " results.add(p);\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);
final Person hola = new Person("hola");
ksession.insert(hola);
ksession.fireAllRules();
assertEquals(0, list.size());
final Cheese brie = new Cheese("brie");
ksession.insert(brie);
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.contains(hola));
assertTrue(list.contains(brie));
} finally {
ksession.dispose();
}
}
Aggregations