use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class LiteralTest method testLiteral.
@Test
public void testLiteral() {
final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Cheesery.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List list;\n" + "global Cheesery cheesery;\n" + "\n" + "rule \"literal test rule\"\n" + " when\n" + " Cheese( $x: type, type == \"stilton\" )\n" + " then\n" + " list.add( $x );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("literal-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession();
try {
final List list = new ArrayList();
session.setGlobal("list", list);
final Cheese stilton = new Cheese("stilton", 5);
session.insert(stilton);
session.fireAllRules();
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 LiteralTest method testLiteralWithEscapes.
@Test
public void testLiteralWithEscapes() {
final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "\n" + "rule \"literal test rule\"\n" + " when\n" + " Cheese( $x: type, type == \"s\\tti\\\"lto\\nn\" )\n" + " then\n" + " list.add( $x );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("literal-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession();
try {
final List list = new ArrayList();
session.setGlobal("list", list);
final String expected = "s\tti\"lto\nn";
final Cheese stilton = new Cheese(expected, 5);
session.insert(stilton);
final int fired = session.fireAllRules();
assertEquals(1, fired);
assertEquals(expected, ((List) session.getGlobal("list")).get(0));
} finally {
session.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class BindTest method testAutomaticBindings.
@Test
public void testAutomaticBindings() {
final String drl = "package org.drools.compiler.integrationtests.drl;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "rule \"test auto bindings 1\"\n" + "when\n" + " $p : Person();\n" + " $c : Cheese( type == $p.likes, price == $c.price )\n" + "then\n" + " results.add( $p );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("bind-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final List list = new ArrayList();
ksession.setGlobal("results", list);
final Person bob = new Person("bob");
bob.setLikes("stilton");
final Cheese stilton = new Cheese("stilton", 12);
ksession.insert(bob);
ksession.insert(stilton);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(bob, list.get(0));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class AlphaTest method testNPEOnMVELAlphaPredicates.
@Test
public void testNPEOnMVELAlphaPredicates() {
final String drl = "package org.drools.compiler\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Person.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "rule \"test NPE on mvel predicate\"\n" + "when\n" + " $p : Person( cheese.type != null )\n" + " $q : Cheese( ) from $p.cheese\n" + "then\n" + " results.add( $q );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("alpha-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession();
try {
final List list = new ArrayList();
session.setGlobal("results", list);
final Cheese cheese = new Cheese("stilton", 10);
final Cheesery cheesery = new Cheesery();
cheesery.addCheese(cheese);
final Person bob = new Person("bob", "stilton");
final Cheese cheese2 = new Cheese();
bob.setCheese(cheese2);
final FactHandle p = session.insert(bob);
session.insert(cheesery);
session.fireAllRules();
assertEquals("should not have fired", 0, list.size());
cheese2.setType("stilton");
session.update(p, bob);
session.fireAllRules();
assertEquals(1, list.size());
} finally {
session.dispose();
}
}
use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.
the class RuleTemplateTest method testSampleCheese.
@Test
public void testSampleCheese() {
// first we compile the decision table into a whole lot of rules.
final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
final KieServices kieServices = KieServices.Factory.get();
final Resource table = kieServices.getResources().newClassPathResource("sample_cheese.xls", getClass());
final Resource template = kieServices.getResources().newClassPathResource("sample_cheese.drt", getClass());
String drl = null;
try {
// the data we are interested in starts at row 2, column 2 (i.e. B2)
drl = converter.compile(table.getInputStream(), template.getInputStream(), 2, 2);
} catch (IOException e) {
throw new IllegalArgumentException("Could not read spreadsheet or rules stream.", e);
}
// compile the drl
final Resource drlResource = kieServices.getResources().newReaderResource(new StringReader(drl));
drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
final Collection<KiePackage> pkgs = kbase.getKiePackages();
Assertions.assertThat(pkgs.size()).isEqualTo(2);
final ArrayList<String> names = new ArrayList<String>();
for (KiePackage kp : pkgs) {
names.add(kp.getName());
}
Assertions.assertThat(names.contains(TestConstants.PACKAGE_FUNCTIONAL)).isTrue();
Assertions.assertThat(names.contains(TestConstants.PACKAGE_TESTCOVERAGE_MODEL)).isTrue();
final KiePackage kiePackage = (KiePackage) pkgs.toArray()[names.indexOf(TestConstants.PACKAGE_FUNCTIONAL)];
Assertions.assertThat(kiePackage.getRules().size()).isEqualTo(2);
final KieSession ksession = kbase.newKieSession();
ksession.insert(new Cheese("stilton", 42));
ksession.insert(new Person("michael", "stilton", 42));
final List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
LOGGER.debug(list.toString());
ksession.dispose();
}
Aggregations