@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("GetGlobalCommand result is null!", result); Object global = result.getValue("cheeseCountry"); assertNotNull("Retrieved global fact is null!", global); assertEquals("Retrieved global is not equal to 'France'.", "France", global ); }
cmds.add(new InsertObjectCommand(new String("Hi!"), "handle")); BatchExecutionCommand batchCmd = CommandFactory.newBatchExecution(cmds, "kresults"); ExecuteCommand execCmd = new ExecuteCommand(batchCmd,true); assertNotNull(results.getFactHandle("handle")); assertTrue(((DefaultFactHandle)results.getFactHandle("handle")).isDisconnected()); batchCmd = CommandFactory.newBatchExecution(cmds, "kresults"); execCmd = new ExecuteCommand(batchCmd); assertNotNull(results.getFactHandle("handle")); assertFalse(((DefaultFactHandle)results.getFactHandle("handle")).isDisconnected());
FactHandle handle_1 = ksession.insert( expected_1 ); FactHandle handle_2 = ksession.insert( expected_2 ); ksession.fireAllRules(); Object object = ksession.getObject(handle_1); commands.add(CommandFactory.newGetObjects("out_list")); Command cmds = CommandFactory.newBatchExecution( commands ); ExecutionResults result = (ExecutionResults) ksession.execute( cmds ); assertNotNull("GetObjectsCommand result is null!", result); List<Object> objectList = (List) result.getValue("out_list"); assertTrue("Retrieved object list is null or empty!", objectList != null && ! objectList.isEmpty());
@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("GetObjectCommand result is null!", result); assertEquals( expected_1, result.getValue("out_1") ); assertEquals( expected_2, result.getValue("out_2") ); }
@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); }
@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("Batch execution result is null!", result); Object firedObject = result.getValue("fired"); assertTrue("Retrieved object is null or incorrect!", firedObject != null && firedObject instanceof Integer); 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)); }
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(); Collection<? extends Object> factList = ksession.getObjects(); assertTrue("Expected " + expectedList.size() + " objects but retrieved " + factList.size(), factList.size() == expectedList.size() ); for( Object fact : factList ) {
@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("Expected " + expectedList.size() + " objects but retrieved " + factList.size(), factList.size() == expectedList.size() ); for( Object fact : factList ) { if( expectedList.contains(fact) ) { expectedList.remove(fact); } } assertTrue("Retrieved object list did not contain expected objects.", expectedList.isEmpty() ); }
List<Object> facts = getInputList(inputMessage, operation, runtime); for (Object fact : facts) { session.insert(fact); session.fireAllRules(); } else if (ExchangePattern.IN_OUT.equals(exchangePattern)) { KieCommands cmds = KieServices.Factory.get().getCommands(); Map<String, Object> inouts = getInputOutputMap(inputMessage, operation, runtime); for (Entry<String, Object> inout : inouts.entrySet()) { batch.add(cmds.newInsert(inout.getValue(), inout.getKey())); batch.add(cmds.newInsertElements(facts)); batch.add(cmds.newFireAllRules()); BatchExecutionCommand exec = cmds.newBatchExecution(batch, KnowledgeConstants.RESULT); ExecutionResults results = session.execute(exec); for (String id : inouts.keySet()) { expressionVariables.put(id, results.getValue(id));
@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); }
@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("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() ); }
@Test public void testFactHandleReturn() throws Exception { String str = ""; str += "package org.kie \n"; str += "import org.kie.camel.embedded.camel.testdomain.Cheese \n"; str += "global java.util.List list1 \n"; str += "rule rule1 \n"; str += " when \n"; str += " $c : Cheese() \n"; str += " \n"; str += " then \n"; str += " System.err.println(42); \n"; str += "end\n"; KieSession ksession = getKieSession(ResourceFactory.newByteArrayResource(str.getBytes())); setExec(ksession); FactHandle fh = ksession.insert(new Person("mic", 42)); List<FactHandle> list = new ArrayList<FactHandle>(); list.add(fh); ksession.setGlobal("list1", list); String outXml = execContent("testFactHandleReturn.in.1"); assertXMLEqual(getContent("testFactHandleReturn.expected.1", fh.toExternalForm()), outXml); ExecutionResults result = unmarshalOutXml(outXml, ExecutionResults.class); List outList = (List)result.getValue("out-list"); assertEquals(1, outList.size()); assertEquals(fh.toExternalForm(), ((FactHandle)outList.get(0)).toExternalForm()); assertNotSame(fh, outList.get(0)); }
protected void handleStateless(WorkItem workItem, String kieSessionName, Map<String, Object> parameters, Map<String, Object> results) { logger.debug("Evaluating rules in stateless session with name {}", kieSessionName); StatelessKieSession kieSession = kieContainer.newStatelessKieSession(kieSessionName); List<Command<?>> commands = new ArrayList<Command<?>>(); for (Entry<String, Object> entry : parameters.entrySet()) { String inputKey = workItem.getId() + "_" + entry.getKey(); commands.add(commandsFactory.newInsert(entry.getValue(), inputKey, true, null)); } commands.add(commandsFactory.newFireAllRules("Fired")); BatchExecutionCommand executionCommand = commandsFactory.newBatchExecution(commands); ExecutionResults executionResults = kieSession.execute(executionCommand); logger.debug("{} rules fired", executionResults.getValue("Fired")); for (Entry<String, Object> entry : parameters.entrySet()) { String inputKey = workItem.getId() + "_" + entry.getKey(); String key = entry.getKey().replaceAll(workItem.getId() + "_", ""); results.put(key, executionResults.getValue(inputKey)); } }
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.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 );
@Test public void testSessionBatchExecutionCommand() throws Exception { Person john = new Person(); john.setName("John Smith"); List<Command> commands = new ArrayList<Command>(); commands.add(CommandFactory.newInsert(john, "john")); BatchExecutionCommand batchExecutionCommand = CommandFactory.newBatchExecution(commands); ExecutionResults response = (ExecutionResults)template.requestBody("direct:test-with-session", batchExecutionCommand); assertTrue("Expected valid ExecutionResults object", response != null); assertTrue("ExecutionResults missing expected fact", response.getFactHandle("john") != null); assertTrue("ExecutionResults missing expected fact", response.getFactHandle("john") instanceof FactHandle); }
@Test public void testSessionInsert() throws Exception { String cmd = ""; cmd += "<batch-execution lookup=\"ksession1\">\n"; cmd += " <insert out-identifier=\"salaboy\">\n"; cmd += " <org.kie.pipeline.camel.Person>\n"; cmd += " <name>salaboy</name>\n"; cmd += " </org.kie.pipeline.camel.Person>\n"; cmd += " </insert>\n"; cmd += " <fire-all-rules/>\n"; cmd += "</batch-execution>\n"; String outXml = new String((byte[])template.requestBody("direct:test-with-session", cmd)); ExecutionResults result = (ExecutionResults) BatchExecutionHelper.newXStreamMarshaller().fromXML(outXml); Person person = (Person)result.getValue("salaboy"); assertEquals("salaboy", person.getName()); String expectedXml = ""; expectedXml += "<?xml version='1.0' encoding='UTF-8'?><execution-results>"; expectedXml += "<result identifier=\"salaboy\">"; expectedXml += "<org.kie.pipeline.camel.Person>"; expectedXml += "<name>salaboy</name>"; expectedXml += "</org.kie.pipeline.camel.Person>"; expectedXml += "</result>"; expectedXml += "<fact-handle identifier=\"salaboy\" external-form=\"" + ((InternalFactHandle)result.getFactHandle("salaboy")).toExternalForm() + "\"/>"; expectedXml += "</execution-results>"; assertXMLEqual(expectedXml, outXml); }
List list1 = (List)result.getValue("list1"); assertEquals(2, list1.size()); assertTrue(list1.contains(new Cheese("stilton", 35))); assertTrue(list1.contains(new Cheese("stilton", 30))); List myFacts = (List)result.getValue("myfacts"); assertEquals(2, list1.size()); assertTrue(myFacts.contains(new Cheese("stilton", 35))); assertTrue(myFacts.contains(new Cheese("stilton", 30))); List<FactHandle> factHandles = (List<FactHandle>)result.getFactHandle("myfacts"); List list = new ArrayList(); list.add(ksession.getObject(((InternalFactHandle)factHandles.get(0)))); list.add(ksession.getObject(((InternalFactHandle)factHandles.get(1)))); assertTrue(list.contains(new Cheese("stilton", 35))); assertTrue(list.contains(new Cheese("stilton", 30)));
@Override protected void configureDroolsContext(javax.naming.Context jndiContext) { Person me = new Person(); me.setName("Hadrian"); KieSession ksession = registerKnowledgeRuntime("ksession1", null); InsertObjectCommand cmd = new InsertObjectCommand(me); cmd.setOutIdentifier("camel-rider"); cmd.setReturnObject(false); BatchExecutionCommandImpl script = new BatchExecutionCommandImpl(Arrays.asList(new Command<?>[] {cmd})); ExecutionResults results = ksession.execute(script); handle = ((FactHandle)results.getFactHandle("camel-rider")).toExternalForm(); }
@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() ); }
@Test public void testSessionInsert() throws Exception { Person person = new Person(); person.setName("Mauricio"); InsertObjectCommand cmd = (InsertObjectCommand) CommandFactory.newInsert(person, "salaboy"); ExecutionResults response = (ExecutionResults)template.requestBody("direct:test-with-session", cmd); assertTrue("Expected valid ExecutionResults object", response != null); assertTrue("ExecutionResults missing expected fact", response.getFactHandle("salaboy") != null); assertTrue("ExecutionResults missing expected fact", response.getFactHandle("salaboy") instanceof FactHandle); }