org.kie.api.runtime.ExecutionResults Java Examples
The following examples show how to use
org.kie.api.runtime.ExecutionResults.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MoreBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 7 votes |
@Test public void testQuery() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/simple_query_test.drl"), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(kbuilder.getKnowledgePackages()); ksession = createKnowledgeSession(kbase); 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(result, "Batch execution result is null!"); Object queryResultsObject = result.getValue("numStinkyCheeses"); assertTrue(queryResultsObject != null && queryResultsObject instanceof QueryResults, "Retrieved object is null or incorrect!"); assertEquals( 1, ((QueryResults) queryResultsObject).size() ); }
Example #2
Source File: SimpleBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testGetGlobalCommand() throws Exception { ksession.insert(new Integer(5)); ksession.insert(new Integer(7)); ksession.fireAllRules(); ksession.setGlobal("globalCheeseCountry", "France"); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newGetGlobal( "globalCheeseCountry", "cheeseCountry" )); Command cmds = CommandFactory.newBatchExecution( commands ); ExecutionResults result = (ExecutionResults) ksession.execute( cmds ); assertNotNull(result, "GetGlobalCommand result is null!"); Object global = result.getValue("cheeseCountry"); assertNotNull(global, "Retrieved global fact is null!"); assertEquals("France", global, "Retrieved global is not equal to 'France'."); }
Example #3
Source File: SimpleRuleBean.java From servicemix with Apache License 2.0 | 6 votes |
/** * */ public void start() throws Exception { for (int i = 0; i < 10; i++) { Customer customer = customer(); logger.info("------------------- START ------------------\n" + " KieSession fireAllRules. {}", customer); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsert(customer, "customer")); commands.add(CommandFactory.newFireAllRules("num-rules-fired")); ExecutionResults results = ksession.execute(CommandFactory .newBatchExecution(commands)); int fired = Integer.parseInt(results.getValue("num-rules-fired") .toString()); customer = (Customer)results.getValue("customer"); logger.info("After rule rules-fired={} {} \n" + "------------------- STOP ---------------------", fired, customer); } }
Example #4
Source File: FoodRecommendationServiceImpl.java From drools-workshop with Apache License 2.0 | 6 votes |
@Override public Meal recommendMeal(Person person) throws BusinessException { StatelessKieSession kieSession = kContainer.newStatelessKieSession(); InsertObjectCommand insertObjectCommand = new InsertObjectCommand(person); FireAllRulesCommand fireAllRulesCommand = new FireAllRulesCommand(); QueryCommand queryCommand = new QueryCommand("results", "getMeal", (Object[]) null); List<GenericCommand<?>> commands = new ArrayList<GenericCommand<?>>(); commands.add(insertObjectCommand); commands.add(fireAllRulesCommand); commands.add(queryCommand); BatchExecutionCommand batch = new BatchExecutionCommandImpl(commands); ExecutionResults results = kieSession.execute(batch); QueryResults queryResults = (QueryResults) results.getValue("results"); Iterator<QueryResultsRow> iterator = queryResults.iterator(); while (iterator.hasNext()) { Meal meal = (Meal) iterator.next().get("$m"); if (meal != null) { System.out.println("Meal : " + meal); return meal; } } return null; }
Example #5
Source File: ExecuteCommand.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public ExecutionResults execute(Context context) { KieSession ksession = ((RegistryContext) context).lookup( KieSession.class ); ExecutionResults kresults = ksession.execute(this.command); if ( this.outIdentifier != null ) { ((RegistryContext) context).lookup( ExecutionResultImpl.class ).setResult( this.outIdentifier, kresults ); } if (disconnected) { ExecutionResultImpl disconnectedResults = new ExecutionResultImpl(); HashMap<String, Object> disconnectedHandles = new HashMap<String, Object>(); for (String key : kresults.getIdentifiers()) { FactHandle handle = (FactHandle) kresults.getFactHandle(key); if (handle != null) { DefaultFactHandle disconnectedHandle = ((DefaultFactHandle) handle).clone(); disconnectedHandle.disconnect(); disconnectedHandles.put(key, disconnectedHandle); } } disconnectedResults.setFactHandles(disconnectedHandles); disconnectedResults.setResults((HashMap)((ExecutionResultImpl)kresults).getResults()); return disconnectedResults; } return kresults; }
Example #6
Source File: SimpleBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testGetObjectCommand() throws Exception { String expected_1 = "expected_1"; String expected_2 = "expected_2"; FactHandle handle_1 = ksession.insert( expected_1 ); FactHandle handle_2 = ksession.insert( expected_2 ); ksession.fireAllRules(); Object fact = ksession.getObject(handle_1); assertNotNull(fact); assertEquals(expected_1, fact); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newGetObject(handle_1, "out_1")); commands.add(CommandFactory.newGetObject(handle_2, "out_2")); Command cmds = CommandFactory.newBatchExecution( commands ); ExecutionResults result = (ExecutionResults) ksession.execute( cmds ); assertNotNull(result, "GetObjectCommand result is null!"); assertEquals(expected_1, result.getValue("out_1")); assertEquals(expected_2, result.getValue("out_2")); }
Example #7
Source File: StatelessSessionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testInsertObject() throws Exception { String str = ""; str += "package org.kie \n"; str += "import org.drools.compiler.Cheese \n"; str += "rule rule1 \n"; str += " when \n"; str += " $c : Cheese() \n"; str += " \n"; str += " then \n"; str += " $c.setPrice( 30 ); \n"; str += "end\n"; Cheese stilton = new Cheese( "stilton", 5 ); final StatelessKieSession ksession = getSession2( ResourceFactory.newByteArrayResource( str.getBytes() ) ); final ExecutableCommand cmd = (ExecutableCommand) CommandFactory.newInsert( stilton, "outStilton" ); final BatchExecutionCommandImpl batch = new BatchExecutionCommandImpl( Arrays.asList( new ExecutableCommand<?>[] { cmd } ) ); final ExecutionResults result = ( ExecutionResults ) ksession.execute( batch ); stilton = ( Cheese ) result.getValue( "outStilton" ); assertEquals( 30, stilton.getPrice() ); }
Example #8
Source File: SimpleBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testSetGlobalCommand() throws Exception { ksession.insert(new Integer(5)); ksession.insert(new Integer(7)); ksession.fireAllRules(); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newSetGlobal( "globalCheeseCountry", "France", true )); Command cmds = CommandFactory.newBatchExecution( commands ); ExecutionResults result = (ExecutionResults) ksession.execute( cmds ); assertNotNull(result); Object global = result.getValue("globalCheeseCountry"); assertNotNull(global); assertEquals("France", global); }
Example #9
Source File: FireAllRulesCommandTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void infiniteLoopTerminatesAtMaxTest() { String str = ""; str += "package org.drools.compiler.integrationtests \n"; str += "import " + Cheese.class.getCanonicalName() + " \n"; str += "rule StringRule \n"; str += " when \n"; str += " $c : Cheese() \n"; str += " then \n"; str += " update($c); \n"; str += "end \n"; StatelessKieSession ksession = getSession(str); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsert(new Cheese("stilton"))); FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10); farc.setOutIdentifier("num-rules-fired"); commands.add(farc); ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands)); int fired = Integer.parseInt(results.getValue("num-rules-fired").toString()); assertEquals(10, fired); }
Example #10
Source File: FireAllRulesCommandTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void oneRuleFiredWithDefinedMaxTest() { String str = ""; str += "package org.drools.compiler.integrationtests \n"; str += "import " + Cheese.class.getCanonicalName() + " \n"; str += "rule StringRule \n"; str += " when \n"; str += " $c : Cheese() \n"; str += " then \n"; str += " System.out.println($c); \n"; str += "end \n"; StatelessKieSession ksession = getSession(str); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsert(new Cheese("stilton"))); FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10); farc.setOutIdentifier("num-rules-fired"); commands.add(farc); ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands)); int fired = Integer.parseInt(results.getValue("num-rules-fired").toString()); assertEquals(1, fired); }
Example #11
Source File: FireAllRulesCommandTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void zeroRulesFiredTest() { String str = ""; str += "package org.drools.compiler.integrationtests \n"; str += "import " + Cheese.class.getCanonicalName() + " \n"; str += "rule StringRule \n"; str += " when \n"; str += " $c : Cheese() \n"; str += " then \n"; str += " System.out.println($c); \n"; str += "end \n"; StatelessKieSession ksession = getSession(str); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsert("not cheese")); commands.add(CommandFactory.newFireAllRules("num-rules-fired")); ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands)); int fired = Integer.parseInt(results.getValue("num-rules-fired").toString()); assertEquals(0, fired); }
Example #12
Source File: FireAllRulesCommandTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void oneRuleFiredTest() { String str = ""; str += "package org.drools.compiler.integrationtests \n"; str += "import " + Cheese.class.getCanonicalName() + " \n"; str += "rule StringRule \n"; str += " when \n"; str += " $c : Cheese() \n"; str += " then \n"; str += " System.out.println($c); \n"; str += "end \n"; StatelessKieSession ksession = getSession(str); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsert(new Cheese("stilton"))); commands.add(CommandFactory.newFireAllRules("num-rules-fired")); ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands)); int fired = Integer.parseInt(results.getValue("num-rules-fired").toString()); assertEquals(1, fired); }
Example #13
Source File: SimpleBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testInsertObjectCommand() throws Exception { String expected_1 = "expected_1"; String expected_2 = "expected_2"; List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsert(expected_1, "out_1")); commands.add(CommandFactory.newInsert(expected_2, "out_2")); Command cmds = CommandFactory.newBatchExecution( commands ); ExecutionResults result = (ExecutionResults) ksession.execute( cmds ); Object fact_1 = result.getValue("out_1"); assertNotNull(fact_1); Object fact_2 = result.getValue("out_2"); assertNotNull(fact_2); ksession.fireAllRules(); Object [] expectedArr = {expected_1, expected_2}; List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr)); Collection<? extends Object> factList = ksession.getObjects(); assertTrue(factList.size() == expectedList.size(), "Expected " + expectedList.size() + " objects but retrieved " + factList.size()); for( Object fact : factList ) { if( expectedList.contains(fact) ) { expectedList.remove(fact); } } assertTrue(expectedList.isEmpty(), "Retrieved object list did not contain expected objects."); }
Example #14
Source File: SimpleBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testInsertElementsCommand() throws Exception { String expected_1 = "expected_1"; String expected_2 = "expected_2"; Object [] expectedArr = {expected_1, expected_2}; Collection<Object> factCollection = Arrays.asList(expectedArr); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsertElements(factCollection, "out_list", true, null)); Command cmds = CommandFactory.newBatchExecution( commands ); ExecutionResults result = (ExecutionResults) ksession.execute( cmds ); Collection<? extends Object> outList = (Collection<? extends Object>) result.getValue("out_list"); assertNotNull(outList); ksession.fireAllRules(); List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr)); Collection<? extends Object> factList = ksession.getObjects(); assertTrue(factList.size() == expectedList.size(), "Expected " + expectedList.size() + " objects but retrieved " + factList.size()); for( Object fact : factList ) { if( expectedList.contains(fact) ) { expectedList.remove(fact); } } assertTrue(expectedList.isEmpty(), "Retrieved object list did not contain expected objects."); }
Example #15
Source File: MoreBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testFireAllRules() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/drl/test_ImportFunctions.drl"), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addPackages(kbuilder.getKnowledgePackages()); ksession = createKnowledgeSession(kbase); final Cheese cheese = new Cheese("stilton", 15); ksession.insert(cheese); List<?> list = new ArrayList(); ksession.setGlobal("list", list); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newFireAllRules("fired")); Command<?> cmds = CommandFactory.newBatchExecution(commands); ExecutionResults result = (ExecutionResults) ksession.execute(cmds); assertNotNull(result, "Batch execution result is null!"); Object firedObject = result.getValue("fired"); assertTrue( firedObject != null && firedObject instanceof Integer, "Retrieved object is null or incorrect!"); assertEquals(4, firedObject); list = (List<?>) ksession.getGlobal("list"); assertEquals(4, list.size()); assertEquals("rule1", list.get(0)); assertEquals("rule2", list.get(1)); assertEquals("rule3", list.get(2)); assertEquals("rule4", list.get(3)); }
Example #16
Source File: SimpleBatchExecutionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings({ "rawtypes", "unchecked" }) public void testGetObjectsCommand() throws Exception { String expected_1 = "expected_1"; String expected_2 = "expected_2"; FactHandle handle_1 = ksession.insert( expected_1 ); FactHandle handle_2 = ksession.insert( expected_2 ); ksession.fireAllRules(); Object object = ksession.getObject(handle_1); assertNotNull(object); assertEquals(expected_1, object); object = ksession.getObject(handle_2); assertNotNull(object); assertEquals(expected_2, object); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newGetObjects("out_list")); Command cmds = CommandFactory.newBatchExecution( commands ); ExecutionResults result = (ExecutionResults) ksession.execute( cmds ); assertNotNull(result, "GetObjectsCommand result is null!"); List<Object> objectList = (List) result.getValue("out_list"); boolean b = objectList != null && ! objectList.isEmpty(); assertTrue(b, "Retrieved object list is null or empty!"); Collection<? extends Object> factList = ksession.getObjects(); Object [] expectedArr = {expected_1, expected_2}; List<Object> expectedList = new ArrayList<Object>(Arrays.asList(expectedArr)); assertTrue(factList.size() == expectedList.size(), "Expected " + expectedList.size() + " objects but retrieved " + factList.size()); for( Object fact : factList ) { if( expectedList.contains(fact) ) { expectedList.remove(fact); } } assertTrue(expectedList.isEmpty(), "Retrieved object list did not contain expected objects."); }
Example #17
Source File: LibraryClient.java From rhpam-7-openshift-image with Apache License 2.0 | 5 votes |
Suggestion getSuggestion(String keyword) { SuggestionRequest suggestionRequest = new SuggestionRequest(); suggestionRequest.setKeyword(keyword); suggestionRequest.setKeyword("Zombie"); List<Command<?>> cmds = new ArrayList<Command<?>>(); cmds.add(commands.newInsert(suggestionRequest)); cmds.add(commands.newFireAllRules()); cmds.add(commands.newQuery("suggestion", "get suggestion")); BatchExecutionCommand batch = commands.newBatchExecution(cmds, "LibraryRuleSession"); ExecutionResults execResults; if (appcfg.getKieSession() != null) { execResults = appcfg.getKieSession().execute(batch); } else { ServiceResponse<ExecutionResults> serviceResponse = appcfg.getRuleServicesClient().executeCommandsWithResults("rhpam-kieserver-library", batch); //logger.info(String.valueOf(serviceResponse)); execResults = serviceResponse.getResult(); } QueryResults queryResults = (QueryResults) execResults.getValue("suggestion"); if (queryResults != null) { for (QueryResultsRow queryResult : queryResults) { SuggestionResponse suggestionResponse = (SuggestionResponse) queryResult.get("suggestionResponse"); if (suggestionResponse != null) { return suggestionResponse.getSuggestion(); } } } return null; }
Example #18
Source File: BusinessRulesBean.java From iot-ocp with Apache License 2.0 | 5 votes |
public Measure processRules(@Body Measure measure) { KieServicesConfiguration config = KieServicesFactory.newRestConfiguration( kieHost, kieUser, kiePassword); Set<Class<?>> jaxBClasses = new HashSet<Class<?>>(); jaxBClasses.add(Measure.class); config.addJaxbClasses(jaxBClasses); config.setMarshallingFormat(MarshallingFormat.JAXB); RuleServicesClient client = KieServicesFactory.newKieServicesClient(config) .getServicesClient(RuleServicesClient.class); List<Command<?>> cmds = new ArrayList<Command<?>>(); KieCommands commands = KieServices.Factory.get().getCommands(); cmds.add(commands.newInsert(measure)); GetObjectsCommand getObjectsCommand = new GetObjectsCommand(); getObjectsCommand.setOutIdentifier("objects"); cmds.add(commands.newFireAllRules()); cmds.add(getObjectsCommand); BatchExecutionCommand myCommands = CommandFactory.newBatchExecution(cmds, "DecisionTableKS"); ServiceResponse<ExecutionResults> response = client.executeCommandsWithResults("iot-ocp-businessrules-service", myCommands); List responseList = (List) response.getResult().getValue("objects"); Measure responseMeasure = (Measure) responseList.get(0); return responseMeasure; }
Example #19
Source File: FireAllRulesCommandTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void fiveRulesFiredTest() { String str = ""; str += "package org.drools.compiler.integrationtests \n"; str += "import " + Cheese.class.getCanonicalName() + " \n"; str += "rule StringRule \n"; str += " when \n"; str += " $c : Cheese() \n"; str += " then \n"; str += " System.out.println($c); \n"; str += "end \n"; StatelessKieSession ksession = getSession(str); List<Command<?>> commands = new ArrayList<Command<?>>(); commands.add(CommandFactory.newInsert(new Cheese("stilton"))); commands.add(CommandFactory.newInsert(new Cheese("gruyere"))); commands.add(CommandFactory.newInsert(new Cheese("cheddar"))); commands.add(CommandFactory.newInsert(new Cheese("stinky"))); commands.add(CommandFactory.newInsert(new Cheese("limburger"))); commands.add(CommandFactory.newFireAllRules("num-rules-fired")); ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands)); int fired = Integer.parseInt(results.getValue("num-rules-fired").toString()); assertEquals(5, fired); }
Example #20
Source File: Utils.java From servicemix with Apache License 2.0 | 5 votes |
/** * Create commands for Drools engine. * * @param body {@link org.apache.camel.Exchange} */ public Command<ExecutionResults> insertAndFireAll(final Customer body) { Command<?> insert = CommandFactory.newInsert(body, "customer"); @SuppressWarnings("unchecked") Command<ExecutionResults> batch = CommandFactory .newBatchExecution(Arrays.asList(insert)); return batch; }
Example #21
Source File: BatchExecutionCommandImpl.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public ExecutionResults execute(Context context) { for ( Command command : commands ) { ((ExecutableCommand<?>) command).execute( context ); } return null; }
Example #22
Source File: DroolsStatelessSession.java From Decision with Apache License 2.0 | 4 votes |
public Results fireRules(List data) { Results res = new Results(); KieCommands commands = kServices.getCommands(); List<Command> cmds = new ArrayList<Command>(); cmds.add(commands.newInsertElements(data)); cmds.add(CommandFactory.newFireAllRules()); cmds.add(CommandFactory.newQuery(QUERY_RESULT, QUERY_NAME)); ExecutionResults er = session.execute(commands.newBatchExecution(cmds)); this.addResults(res.getResults(), er, QUERY_RESULT); return res; }
Example #23
Source File: DroolsStatelessSession.java From Decision with Apache License 2.0 | 3 votes |
private void addResults(List r, ExecutionResults er, String name){ Iterator<QueryResultsRow> rows = ((QueryResults) er.getValue(name)).iterator(); while (rows.hasNext()) { QueryResultsRow row = rows.next(); r.add( row.get(name)); } }