private Vector<Action> loadActions() { DEBUG.message("load actions"); Action action; Vector<Action> actionsList = new Vector<Action>(); for (QueryResultsRow result : ksession.getQueryResults(BaguetteSOLO.CONSULT_ACTIONS)) { action = (Action) result.get("action"); // get the Action object action.setRobot(this); // link it to the current robot actionsList.add(action); ksession.retract(result.getFactHandle("action")); // clears the fact from the active memory } return actionsList; }
protected Collection<ReportItem> evaluate(Object... elements) { StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); Collection<ReportItem> reportItems = new ArrayList<ReportItem>(); List<Command<?>> commands = new ArrayList<Command<?>>(); for (Object o : elements) { commands.add(CommandFactory.newInsert(o)); } commands.add(CommandFactory.newFireAllRules()); commands.add(CommandFactory.newQuery("reports", "getReports")); ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands)); // System.out.println(results.getIdentifiers()); QueryResults queryResults = (QueryResults) results.getValue("reports"); for (QueryResultsRow result : queryResults) { reportItems.add((ReportItem) result.get("item")); } return reportItems; }
@Test public void testQuery() throws Exception { String str = ""; str += "package org.drools.test \n"; str += "import org.drools.Cheese \n"; str += "query cheeses \n"; str += " stilton : Cheese(type == 'stilton') \n"; str += " cheddar : Cheese(type == 'cheddar', price == stilton.price) \n"; str += "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { fail(kbuilder.getErrors().toString()); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); kbase = SerializationHelper.serializeObject(kbase); StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession(); Cheese stilton1 = new Cheese("stilton", 1); Cheese cheddar1 = new Cheese("cheddar", 1); Cheese stilton2 = new Cheese("stilton", 2); Cheese cheddar2 = new Cheese("cheddar", 2); Cheese stilton3 = new Cheese("stilton", 3); Cheese cheddar3 = new Cheese("cheddar", 3); 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); 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")); ExecutionResults batchResult = (ExecutionResults) ksession.execute(CommandFactory.newBatchExecution(cmds)); org.drools.runtime.rule.QueryResults results = (org.drools.runtime.rule.QueryResults) batchResult.getValue("cheeses"); assertEquals(3, results.size()); assertEquals(2, results.getIdentifiers().length); Set newSet = new HashSet(); for (org.drools.runtime.rule.QueryResultsRow result : results) { list = new ArrayList(); list.add(result.get("stilton")); list.add(result.get("cheddar")); newSet.add(list); } assertEquals(set, newSet); }