Search in sources :

Example 36 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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<>();
    final 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 37 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults 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<>();
    // 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<>();
    for (final 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 38 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class RuleStatelessScenarioExecutableBuilderTest method generateCommands.

@Test
public void generateCommands() {
    FactMappingValue emptyFMV = new FactMappingValue(FactIdentifier.EMPTY, ExpressionIdentifier.DESCRIPTION, null);
    RuleStatelessScenarioExecutableBuilder builder = new RuleStatelessScenarioExecutableBuilder(null, null);
    Command<ExecutionResults> batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertFalse(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertFalse(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
    builder.setActiveAgendaGroup("test");
    batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertFalse(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
    builder.insert(new Object());
    batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertTrue(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertFalse(verifyCommand(batchCommand, ValidateFactCommand.class));
    builder.addInternalCondition(String.class, obj -> null, new ScenarioResult(emptyFMV, null));
    batchCommand = builder.generateCommands(null);
    assertTrue(verifyCommand(batchCommand, AddCoverageListenerCommand.class));
    assertTrue(verifyCommand(batchCommand, FireAllRulesCommand.class));
    assertTrue(verifyCommand(batchCommand, AgendaGroupSetFocusCommand.class));
    assertTrue(verifyCommand(batchCommand, InsertElementsCommand.class));
    assertTrue(verifyCommand(batchCommand, ValidateFactCommand.class));
}
Also used : InsertElementsCommand(org.drools.core.command.runtime.rule.InsertElementsCommand) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) ExecutionResults(org.kie.api.runtime.ExecutionResults) FireAllRulesCommand(org.drools.core.command.runtime.rule.FireAllRulesCommand) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) AgendaGroupSetFocusCommand(org.drools.core.command.runtime.rule.AgendaGroupSetFocusCommand) Test(org.junit.Test)

Example 39 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class MoreBatchExecutionTest method testQuery.

@Test
public void testQuery() {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(this.getClass(), kieBaseTestConfiguration, "org/drools/mvel/integrationtests/simple_query_test.drl");
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new Cheese("stinky", 5));
    ksession.insert(new Cheese("smelly", 7));
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("Batch execution result is null!", result);
    Object queryResultsObject = result.getValue("numStinkyCheeses");
    assertTrue("Retrieved object is null or incorrect!", queryResultsObject != null && queryResultsObject instanceof QueryResults);
    assertEquals(1, ((QueryResults) queryResultsObject).size());
}
Also used : Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 40 with ExecutionResults

use of org.kie.api.runtime.ExecutionResults in project drools by kiegroup.

the class StatelessSessionTest method testQuery.

@Test
public void testQuery() throws Exception {
    String str = "";
    str += "package org.kie.test  \n";
    str += "import org.drools.mvel.compiler.Cheese \n";
    str += "query cheeses \n";
    str += "    stilton : Cheese(type == 'stilton') \n";
    str += "    cheddar : Cheese(type == 'cheddar', price == stilton.price) \n";
    str += "end\n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    final StatelessKieSession ksession = kbase.newStatelessKieSession();
    final Cheese stilton1 = new Cheese("stilton", 1);
    final Cheese cheddar1 = new Cheese("cheddar", 1);
    final Cheese stilton2 = new Cheese("stilton", 2);
    final Cheese cheddar2 = new Cheese("cheddar", 2);
    final Cheese stilton3 = new Cheese("stilton", 3);
    final Cheese cheddar3 = new Cheese("cheddar", 3);
    final Set set = new HashSet();
    List list = new ArrayList();
    list.add(stilton1);
    list.add(cheddar1);
    set.add(list);
    list = new ArrayList();
    list.add(stilton2);
    list.add(cheddar2);
    set.add(list);
    list = new ArrayList();
    list.add(stilton3);
    list.add(cheddar3);
    set.add(list);
    final List<Command> cmds = new ArrayList<Command>();
    cmds.add(CommandFactory.newInsert(stilton1));
    cmds.add(CommandFactory.newInsert(stilton2));
    cmds.add(CommandFactory.newInsert(stilton3));
    cmds.add(CommandFactory.newInsert(cheddar1));
    cmds.add(CommandFactory.newInsert(cheddar2));
    cmds.add(CommandFactory.newInsert(cheddar3));
    cmds.add(CommandFactory.newQuery("cheeses", "cheeses"));
    final ExecutionResults batchResult = (ExecutionResults) ksession.execute(CommandFactory.newBatchExecution(cmds));
    final org.kie.api.runtime.rule.QueryResults results = (org.kie.api.runtime.rule.QueryResults) batchResult.getValue("cheeses");
    assertEquals(3, results.size());
    assertEquals(2, results.getIdentifiers().length);
    final Set newSet = new HashSet();
    for (final org.kie.api.runtime.rule.QueryResultsRow result : results) {
        list = new ArrayList();
        list.add(result.get("stilton"));
        list.add(result.get("cheddar"));
        newSet.add(list);
    }
    assertEquals(set, newSet);
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) Cheese(org.drools.mvel.compiler.Cheese) ExecutableCommand(org.kie.api.command.ExecutableCommand) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ExecutionResults (org.kie.api.runtime.ExecutionResults)41 Test (org.junit.Test)39 ArrayList (java.util.ArrayList)37 Command (org.kie.api.command.Command)35 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)17 FireAllRulesCommand (org.drools.core.command.runtime.rule.FireAllRulesCommand)11 Cheese (org.drools.compiler.Cheese)10 Cheese (org.drools.mvel.compiler.Cheese)10 List (java.util.List)9 KieSession (org.kie.api.runtime.KieSession)8 KieBase (org.kie.api.KieBase)7 FactHandle (org.kie.api.runtime.rule.FactHandle)5 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)4 QueryResults (org.kie.api.runtime.rule.QueryResults)4 ExecutableCommand (org.drools.core.command.impl.ExecutableCommand)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)3 KieServices (org.kie.api.KieServices)3 ExecutableCommand (org.kie.api.command.ExecutableCommand)3 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)3