Search in sources :

Example 1 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)

Example 2 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class FactHandleTest method testFactHandleSequence.

@Test
public void testFactHandleSequence() throws Exception {
    String drlString = "package org.jboss.brms\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "rule \"FactHandleId\"\n" + "    when\n" + "        $c : Cheese()\n" + "    then\n" + "        // do something;\n" + "end";
    KieBase kBase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drlString);
    List<Long> factHandleIDs = new ArrayList<>();
    KieSession kieSession = kBase.newKieSession();
    kieSession.addEventListener(createCollectEventListener(factHandleIDs));
    kieSession.insert(new Cheese("mozzarella"));
    kieSession.insert(new Cheese("pecorino"));
    kieSession.fireAllRules();
    kieSession.dispose();
    // This should reset Fact Handle IDs
    kieSession = kBase.newKieSession();
    kieSession.addEventListener(createCollectEventListener(factHandleIDs));
    kieSession.insert(new Cheese("parmigiano"));
    kieSession.fireAllRules();
    Assertions.assertThat(factHandleIDs).containsExactly(1L, 2L, 1L);
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Test(org.junit.Test)

Example 3 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class ActivationTest method noDormantCheckOnModifies.

/**
 * Tests improper deactivation of already activated rule on the agenda. See
 * BZ 862325.
 */
@Test
public void noDormantCheckOnModifies() throws Exception {
    AgendaEventListener ael = mock(AgendaEventListener.class);
    session.addEventListener(ael);
    session.setGlobal("LOGGER", LOGGER);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(getCommands().newInsert(new Person("Bob", 19)));
    commands.add(getCommands().newInsert(new Cheese("brie", 10)));
    commands.add(getCommands().newFireAllRules());
    session.execute(getCommands().newBatchExecution(commands, null));
    // both rules should fire exactly once
    verify(ael, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
    // no cancellations should have happened
    verify(ael, never()).matchCancelled(any(MatchCancelledEvent.class));
}
Also used : Command(org.kie.api.command.Command) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieSessionTest(org.drools.testcoverage.common.KieSessionTest) Test(org.junit.Test)

Example 4 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class AccumulateMvelDialectTest method testMVELAccumulate2WM.

// See https://issues.jboss.org/browse/DROOLS-2733
@Test(timeout = 10000)
public void testMVELAccumulate2WM() {
    final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("accumulate-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_AccumulateMVEL.drl");
    final KieSession wm1 = kbase.newKieSession();
    try {
        final List<?> results1 = new ArrayList<>();
        wm1.setGlobal("results", results1);
        final List<?> results2 = new ArrayList<>();
        final KieSession wm2 = kbase.newKieSession();
        try {
            wm2.setGlobal("results", results2);
            wm1.insert(new Person("Bob", "stilton", 20));
            wm1.insert(new Person("Mark", "provolone"));
            wm2.insert(new Person("Bob", "stilton", 20));
            wm2.insert(new Person("Mark", "provolone"));
            wm1.insert(new Cheese("stilton", 10));
            wm1.insert(new Cheese("brie", 5));
            wm2.insert(new Cheese("stilton", 10));
            wm1.insert(new Cheese("provolone", 150));
            wm2.insert(new Cheese("brie", 5));
            wm2.insert(new Cheese("provolone", 150));
            wm1.fireAllRules();
            wm2.fireAllRules();
        } finally {
            wm2.dispose();
        }
        assertEquals(165, results1.get(0));
        assertEquals(10, results1.get(1));
        assertEquals(150, results1.get(2));
        assertEquals(10, results1.get(3));
        assertEquals(210, results1.get(4));
        assertEquals(165, results2.get(0));
        assertEquals(10, results2.get(1));
        assertEquals(150, results2.get(2));
        assertEquals(10, results2.get(3));
        assertEquals(210, results2.get(4));
    } finally {
        wm1.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.testcoverage.common.model.Cheese) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 5 with Cheese

use of org.drools.testcoverage.common.model.Cheese in project drools by kiegroup.

the class JoinNodeRangeIndexingTest method testCoercionBigDecimalVsInt.

@Test
public void testCoercionBigDecimalVsInt() {
    final String drl = "package org.drools.compiler.integrationtests;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "import " + Primitives.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "rule R\n" + "    when\n" + "        Cheese($price : price)\n" + "        p : Primitives(bigDecimal < $price)\n" + "    then\n" + "        list.add( p );\n" + "end";
    // Integer is coerced to BigDecimal
    final KieBase kbase = getKieBaseWithRangeIndexOption(drl);
    assertIndexedTrue(kbase, Primitives.class);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List<Primitives> list = new ArrayList<>();
        ksession.setGlobal("list", list);
        final Primitives bd42 = new Primitives();
        bd42.setBigDecimal(new BigDecimal("42"));
        ksession.insert(bd42);
        final Primitives bd43 = new Primitives();
        bd43.setBigDecimal(new BigDecimal("43"));
        ksession.insert(bd43);
        ksession.insert(new Cheese("gorgonzola", 43));
        ksession.fireAllRules();
        Assertions.assertThat(list).containsExactly(bd42);
    } 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) Primitives(org.drools.testcoverage.common.model.Primitives) BigDecimal(java.math.BigDecimal) 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