Search in sources :

Example 26 with Cheese

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();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 27 with Cheese

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();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.testcoverage.common.model.Cheese) Test(org.junit.Test)

Example 28 with Cheese

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();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 29 with Cheese

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();
    }
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.testcoverage.common.model.Cheese) Cheesery(org.drools.testcoverage.common.model.Cheesery) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 30 with Cheese

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();
}
Also used : ExternalSpreadsheetCompiler(org.drools.decisiontable.ExternalSpreadsheetCompiler) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) Cheese(org.drools.testcoverage.common.model.Cheese) IOException(java.io.IOException) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Aggregations

Cheese (org.drools.testcoverage.common.model.Cheese)97 KieBase (org.kie.api.KieBase)92 KieSession (org.kie.api.runtime.KieSession)88 Test (org.junit.Test)85 ArrayList (java.util.ArrayList)65 Person (org.drools.testcoverage.common.model.Person)47 List (java.util.List)33 FactHandle (org.kie.api.runtime.rule.FactHandle)22 Cheesery (org.drools.testcoverage.common.model.Cheesery)11 Arrays.asList (java.util.Arrays.asList)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 BigDecimal (java.math.BigDecimal)3 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)3 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2