Search in sources :

Example 21 with Command

use of org.kie.api.command.Command in project drools by kiegroup.

the class UnicodeTest method testCzechXLSDecisionTable.

@Test
public void testCzechXLSDecisionTable() throws FileNotFoundException {
    final KieServices kieServices = KieServices.Factory.get();
    final Resource resource = kieServices.getResources().newClassPathResource("unicode.xls", getClass());
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
    final KieSession ksession = kbase.newKieSession();
    final List<Command<?>> commands = new ArrayList<Command<?>>();
    final List<Člověk> dospělí = new ArrayList<Člověk>();
    commands.add(kieServices.getCommands().newSetGlobal("dospělí", dospělí));
    final Člověk Řehoř = new Člověk();
    Řehoř.setVěk(30);
    Řehoř.setJméno("Řehoř");
    commands.add(kieServices.getCommands().newInsert(Řehoř));
    commands.add(kieServices.getCommands().newFireAllRules());
    ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
    Assertions.assertThat(kbase.getRule(TestConstants.PACKAGE_FUNCTIONAL, "přidej k dospělým")).isNotNull();
    Assertions.assertThat(dospělí.size()).isEqualTo(1);
    Assertions.assertThat(dospělí.iterator().next().getJméno()).isEqualTo("Řehoř");
}
Also used : Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 22 with Command

use of org.kie.api.command.Command in project drools by kiegroup.

the class PropertyListenerTest method runTest.

@Test
public void runTest() {
    final KieServices kieServices = KieServices.Factory.get();
    final Resource drlResource = kieServices.getResources().newClassPathResource("propertyListenerTest.drl", getClass());
    final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModule(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
    final KieSession session = kieBase.newKieSession();
    final ArrayList<Person> people = new ArrayList<Person>();
    people.add(new Person("Test 1"));
    people.add(new Person("Test 2"));
    LOGGER.info("== Listeners attached before rules ==");
    for (Person person : people) {
        for (PropertyChangeListener listener : person.getBoundSupport().getPropertyChangeListeners()) {
            LOGGER.info("Listener attached of type: " + listener);
        }
    }
    // call rules
    final List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(kieServices.getCommands().newInsert(people));
    commands.add(kieServices.getCommands().newFireAllRules());
    session.execute(kieServices.getCommands().newBatchExecution(commands, null));
    session.dispose();
    LOGGER.info("Session disposed");
    LOGGER.info("== Listeners attached after rules (should be none) ==");
    printAndAssertListenersFromPeople(people);
    // update name to trigger update
    people.get(0).setName("Test 3");
    LOGGER.info("== Listeners attached after updating name (should be none) ==");
    printAndAssertListenersFromPeople(people);
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 23 with Command

use of org.kie.api.command.Command in project drools by kiegroup.

the class StarImportTest method starImportedFactAlsoDeclaredInDRL.

/**
 * Tests that rule fires if supplied with a fact that is imported using
 * "star" import.
 *
 * See BZ 973264.
 */
@Test
public void starImportedFactAlsoDeclaredInDRL() throws Exception {
    session.setGlobal("LOGGER", LOGGER);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    session.addEventListener(ael);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(getCommands().newInsert(new TestEvent(1, "event 1", 0)));
    commands.add(getCommands().newFireAllRules());
    session.execute(getCommands().newBatchExecution(commands, null));
    // the rule should have fired exactly once
    try {
        verify(ael, times(1)).afterMatchFired(any(AfterMatchFiredEvent.class));
    } catch (WantedButNotInvoked e) {
        Assertions.fail("The rule does not fire. For more information see BZ 973264", e);
    }
}
Also used : Command(org.kie.api.command.Command) TestEvent(org.drools.testcoverage.common.model.TestEvent) ArrayList(java.util.ArrayList) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) WantedButNotInvoked(org.mockito.exceptions.verification.WantedButNotInvoked) KieSessionTest(org.drools.testcoverage.common.KieSessionTest) Test(org.junit.Test)

Example 24 with Command

use of org.kie.api.command.Command 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 25 with Command

use of org.kie.api.command.Command in project drools by kiegroup.

the class QueryTest method testRecursiveQueryWithBatchCommand.

@Test
public void testRecursiveQueryWithBatchCommand() throws Exception {
    String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + "query isContainedIn(String x, String y)\n" + "    Location (x, y;)\n" + "    or\n" + "    ( Location (z, y;) and ?isContainedIn(x, z;))\n" + "end\n" + "declare Location\n" + "    thing : String\n" + "    location : String\n" + "end";
    KieServices kieServices = KieServices.Factory.get();
    KieSession ksession = getKieSession(str);
    FactType locationType = ksession.getKieBase().getFactType("org.test", "Location");
    // a pear is in the kitchen
    final Object pear = locationType.newInstance();
    locationType.set(pear, "thing", "pear");
    locationType.set(pear, "location", "kitchen");
    // a desk is in the office
    final Object desk = locationType.newInstance();
    locationType.set(desk, "thing", "desk");
    locationType.set(desk, "location", "office");
    // a flashlight is on the desk
    final Object flashlight = locationType.newInstance();
    locationType.set(flashlight, "thing", "flashlight");
    locationType.set(flashlight, "location", "desk");
    // an envelope is on the desk
    final Object envelope = locationType.newInstance();
    locationType.set(envelope, "thing", "envelope");
    locationType.set(envelope, "location", "desk");
    // a key is in the envelope
    final Object key = locationType.newInstance();
    locationType.set(key, "thing", "key");
    locationType.set(key, "location", "envelope");
    // create working memory objects
    final List<Command<?>> commands = new ArrayList<Command<?>>();
    // Location instances
    commands.add(kieServices.getCommands().newInsert(pear));
    commands.add(kieServices.getCommands().newInsert(desk));
    commands.add(kieServices.getCommands().newInsert(flashlight));
    commands.add(kieServices.getCommands().newInsert(envelope));
    commands.add(kieServices.getCommands().newInsert(key));
    // fire all rules
    final String queryAlias = "myQuery";
    commands.add(kieServices.getCommands().newQuery(queryAlias, "isContainedIn", new Object[] { Variable.v, "office" }));
    final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
    final QueryResults qResults = (QueryResults) results.getValue(queryAlias);
    final List<String> l = new ArrayList<String>();
    for (QueryResultsRow r : qResults) {
        l.add((String) r.get("x"));
    }
    // items in the office should be the following
    Assertions.assertThat(l.size()).isEqualTo(4);
    Assertions.assertThat(l.contains("desk")).isTrue();
    Assertions.assertThat(l.contains("flashlight")).isTrue();
    Assertions.assertThat(l.contains("envelope")).isTrue();
    Assertions.assertThat(l.contains("key")).isTrue();
}
Also used : ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) QueryResults(org.kie.api.runtime.rule.QueryResults) FactType(org.kie.api.definition.type.FactType) Command(org.kie.api.command.Command) QueryResultsRow(org.kie.api.runtime.rule.QueryResultsRow) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Aggregations

Command (org.kie.api.command.Command)38 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)34 ExecutionResults (org.kie.api.runtime.ExecutionResults)18 KieSession (org.kie.api.runtime.KieSession)14 Cheese (org.drools.compiler.Cheese)10 KieBase (org.kie.api.KieBase)9 KieServices (org.kie.api.KieServices)9 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)7 Resource (org.kie.api.io.Resource)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 List (java.util.List)5 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)5 KieSessionTest (org.drools.testcoverage.common.KieSessionTest)4 FactType (org.kie.api.definition.type.FactType)3 FactHandle (org.kie.api.runtime.rule.FactHandle)3 QueryResults (org.kie.api.runtime.rule.QueryResults)3 BatchExecutionCommandImpl (org.drools.core.command.runtime.BatchExecutionCommandImpl)2