Search in sources :

Example 26 with Command

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

the class UnicodeTest method testQueryCallFromJava.

// test queries in Czech language
@Test
public void testQueryCallFromJava() throws InstantiationException, IllegalAccessException {
    final KieServices kieServices = KieServices.Factory.get();
    final Resource resource = kieServices.getResources().newClassPathResource("unicode.drl", getClass());
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
    final KieSession ksession = kbase.newKieSession();
    final FactType locationType = kbase.getFactType(TestConstants.PACKAGE_FUNCTIONAL, "Umístění");
    // a pear is in the kitchen
    final Object hruška = locationType.newInstance();
    locationType.set(hruška, "věc", "hruška");
    locationType.set(hruška, "místo", "kuchyně");
    // a desk is in the office
    final Object stůl = locationType.newInstance();
    locationType.set(stůl, "věc", "stůl");
    locationType.set(stůl, "místo", "kancelář");
    // a flashlight is on the desk
    final Object svítilna = locationType.newInstance();
    locationType.set(svítilna, "věc", "svítilna");
    locationType.set(svítilna, "místo", "stůl");
    // an envelope is on the desk
    final Object obálka = locationType.newInstance();
    locationType.set(obálka, "věc", "obálka");
    locationType.set(obálka, "místo", "stůl");
    // a key is in the envelope
    final Object klíč = locationType.newInstance();
    locationType.set(klíč, "věc", "klíč");
    locationType.set(klíč, "místo", "obálka");
    // create working memory objects
    final List<Command<?>> commands = new ArrayList<Command<?>>();
    // Location instances
    commands.add(kieServices.getCommands().newInsert(hruška));
    commands.add(kieServices.getCommands().newInsert(stůl));
    commands.add(kieServices.getCommands().newInsert(svítilna));
    commands.add(kieServices.getCommands().newInsert(obálka));
    commands.add(kieServices.getCommands().newInsert(klíč));
    // fire all rules
    final String queryAlias = "obsaženo";
    commands.add(kieServices.getCommands().newQuery(queryAlias, "jeObsažen", new Object[] { Variable.v, "kancelář" }));
    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("stůl")).isTrue();
    Assertions.assertThat(l.contains("svítilna")).isTrue();
    Assertions.assertThat(l.contains("obálka")).isTrue();
    Assertions.assertThat(l.contains("klíč")).isTrue();
}
Also used : ExecutionResults(org.kie.api.runtime.ExecutionResults) Resource(org.kie.api.io.Resource) 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) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 27 with Command

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

the class UnicodeTest method testJapanese.

@Test
public void testJapanese() {
    final KieServices kieServices = KieServices.Factory.get();
    final Resource resource = kieServices.getResources().newClassPathResource("unicode.drl", getClass());
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, resource);
    final KieSession ksession = kbase.newKieSession();
    final List<Command<?>> commands = new ArrayList<Command<?>>();
    List<人> 一覧 = new ArrayList<人>();
    commands.add(kieServices.getCommands().newSetGlobal("一覧", 一覧));
    // let's create person yokozuna
    final 人 横綱 = new 人();
    横綱.set歳(30);
    横綱.setの名前("横綱");
    横綱.set既婚(true);
    commands.add(kieServices.getCommands().newInsert(横綱));
    commands.add(kieServices.getCommands().newFireAllRules("firedRulesCount"));
    final ExecutionResults results = ksession.execute(kieServices.getCommands().newBatchExecution(commands, null));
    Assertions.assertThat(results.getValue("firedRulesCount")).isEqualTo(2);
    Assertions.assertThat(一覧.size()).isEqualTo(1);
    Assertions.assertThat(一覧.iterator().next().getの名前()).isEqualTo("横綱");
}
Also used : Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ExecutionResults(org.kie.api.runtime.ExecutionResults) 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 28 with Command

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

the class MultiRestrictionPatternTest method multiRestriction3.

@Test
public void multiRestriction3() throws Exception {
    List<Command<?>> commands = new ArrayList<Command<?>>();
    Person p = new Person();
    p.setId(3);
    commands.add(getCommands().newInsert(p));
    commands.add(getCommands().newFireAllRules());
    session.execute(getCommands().newBatchExecution(commands, null));
    Assertions.assertThat(firedRules.isRuleFired("and")).isTrue();
}
Also used : Command(org.kie.api.command.Command) ArrayList(java.util.ArrayList) Person(org.drools.testcoverage.common.model.Person) KieSessionTest(org.drools.testcoverage.common.KieSessionTest) Test(org.junit.Test)

Example 29 with Command

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

the class MultipleSalienceUpdateFactTest method test.

@Test
public void test() {
    session.setGlobal("LOGGER", LOGGER);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    Person person = new Person("PAUL");
    ListHolder listHolder = new ListHolder();
    List<String> list = Arrays.asList("eins", "zwei", "drei");
    listHolder.setList(list);
    commands.add(getCommands().newInsert(person));
    commands.add(getCommands().newInsert(listHolder));
    commands.add(getCommands().newFireAllRules());
    session.execute(getCommands().newBatchExecution(commands, null));
    Assertions.assertThat(firedRules.isRuleFired("PERSON_PAUL")).isTrue();
    Assertions.assertThat(firedRules.isRuleFired("PERSON_PETER")).isTrue();
}
Also used : Command(org.kie.api.command.Command) ArrayList(java.util.ArrayList) Person(org.drools.testcoverage.common.model.Person) ListHolder(org.drools.testcoverage.common.model.ListHolder) KieSessionTest(org.drools.testcoverage.common.KieSessionTest) Test(org.junit.Test)

Example 30 with Command

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

the class SeveralKieSessionsTest method performTest.

/**
 * Performs the actual test on given KieSession.
 */
private void performTest(final KieSession ksession) throws Exception {
    final KieBase kbase = ksession.getKieBase();
    // fact types
    FactType manType = kbase.getFactType(PACKAGE, "Man");
    FactType womanType = kbase.getFactType(PACKAGE, "Woman");
    FactType parentType = kbase.getFactType(PACKAGE, "Parent");
    // ksession.addEventListener(arg0)
    // create working memory objects
    List<Command<?>> commands = new ArrayList<Command<?>>();
    ListHolder listHolder = new ListHolder();
    commands.add(CommandFactory.newInsert(listHolder));
    // parents
    Object parent1 = parentType.newInstance();
    parentType.set(parent1, "parent", "Eva");
    parentType.set(parent1, "child", "Abel");
    commands.add(CommandFactory.newInsert(parent1));
    Object parent2 = parentType.newInstance();
    parentType.set(parent2, "parent", "Eva");
    parentType.set(parent2, "child", "Kain");
    commands.add(CommandFactory.newInsert(parent2));
    Object parent3 = parentType.newInstance();
    parentType.set(parent3, "parent", "Adam");
    parentType.set(parent3, "child", "Abel");
    commands.add(CommandFactory.newInsert(parent3));
    Object parent4 = parentType.newInstance();
    parentType.set(parent4, "parent", "Adam");
    parentType.set(parent4, "child", "Kain");
    commands.add(CommandFactory.newInsert(parent4));
    Object parent5 = parentType.newInstance();
    parentType.set(parent5, "parent", "Abel");
    parentType.set(parent5, "child", "Josef");
    commands.add(CommandFactory.newInsert(parent5));
    // persons
    Object adam = manType.newInstance();
    manType.set(adam, "name", "Adam");
    commands.add(CommandFactory.newInsert(adam));
    Object eva = womanType.newInstance();
    womanType.set(eva, "name", "Eva");
    womanType.set(eva, "age", 101);
    commands.add(CommandFactory.newInsert(eva));
    Object abel = manType.newInstance();
    manType.set(abel, "name", "Abel");
    commands.add(CommandFactory.newInsert(abel));
    Object kain = manType.newInstance();
    manType.set(kain, "name", "Kain");
    commands.add(CommandFactory.newInsert(kain));
    Object josef = manType.newInstance();
    manType.set(josef, "name", "Josef");
    commands.add(CommandFactory.newInsert(josef));
    // fire all rules
    commands.add(CommandFactory.newFireAllRules());
    ksession.execute(CommandFactory.newBatchExecution(commands));
    // asserts
    List<String> manList = listHolder.getManList();
    assertEquals(manList.size(), 4);
    assertTrue(manList.contains("Adam"));
    assertTrue(manList.contains("Kain"));
    assertTrue(manList.contains("Abel"));
    assertTrue(manList.contains("Josef"));
    List<String> personList = listHolder.getPersonList();
    assertEquals(personList.size(), 5);
    assertTrue(personList.contains("Adam"));
    assertTrue(personList.contains("Kain"));
    assertTrue(personList.contains("Abel"));
    assertTrue(personList.contains("Josef"));
    assertTrue(personList.contains("Eva"));
    List<String> parentList = listHolder.getParentList();
    assertEquals(parentList.size(), 5);
    assertTrue(parentList.contains("Adam"));
    assertTrue(parentList.contains("Eva"));
    assertTrue(parentList.contains("Abel"));
    List<String> motherList = listHolder.getMotherList();
    assertEquals(motherList.size(), 2);
    assertTrue(motherList.contains("Eva"));
    List<String> fatherList = listHolder.getFatherList();
    assertEquals(fatherList.size(), 3);
    assertTrue(fatherList.contains("Adam"));
    assertTrue(fatherList.contains("Abel"));
    assertFalse(fatherList.contains("Eva"));
    assertFalse(fatherList.contains("Kain"));
    assertFalse(fatherList.contains("Josef"));
    List<String> grandparentList = listHolder.getGrandparentList();
    assertEquals(grandparentList.size(), 2);
    assertTrue(grandparentList.contains("Eva"));
    assertTrue(grandparentList.contains("Adam"));
    assertTrue(listHolder.isGrandmaBlessedAgeTriggered());
}
Also used : Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) FactType(org.kie.api.definition.type.FactType)

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