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ř");
}
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);
}
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);
}
}
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));
}
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();
}
Aggregations