/** * Reading rules from Guvnor * * @return KnowledgeBase - the Guvnor knowledge Base */ private static KnowledgeBase readRemoteKnowledgeBase() { KnowledgeAgentConfiguration kaconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration(); kaconf.setProperty("drools.agent.scanDirectories", "false"); KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("test agent", kaconf); kagent.applyChangeSet(ResourceFactory.newClassPathResource("guvnor.xml")); return kagent.getKnowledgeBase(); }
public void testDroolsGeneration(int i) throws Exception { String filename = (i % 2 == 0) ? "/drools/stress1.drl" : "/drools/stress2.drl"; InputStreamReader reader = new InputStreamReader(DroolsKnowledgeBaseTest.class.getResourceAsStream(filename)); KnowledgeAgentConfiguration aconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration(); aconf.setProperty("drools.agent.newInstance", "false"); aconf.setProperty("drools.agent.monitorChangeSetEvents", "false"); aconf.setProperty("drools.agent.scanResources", "false"); KnowledgeBuilder builder = compileRules(reader); KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("Success Correspondence Agent", aconf); KnowledgeBase kbase = kagent.getKnowledgeBase(); kbase.addKnowledgePackages(builder.getKnowledgePackages()); // kagent.monitorResourceChangeEvents(false); Rule rule1 = kbase.getRule("com.rosettastone.succor", "Equal rule 1"); Rule rule5 = kbase.getRule("com.rosettastone.succor", "Equal rule 5"); Rule rule6 = kbase.getRule("com.rosettastone.succor", "Equal rule 6"); StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession(); if (i % 2 == 0) { Assert.assertNotNull(rule1); Assert.assertNotNull(rule5); Assert.assertNull(rule6); testRulesFirst(session); } else { Assert.assertNull(rule1); Assert.assertNotNull(rule5); Assert.assertNotNull(rule6); testRulesSecond(session); } }
protected void addResource(StatefulKnowledgeSession kSession, Resource res) { KnowledgeBase kbase = kSession.getKnowledgeBase(); ChangeSetImpl cs = new ChangeSetImpl(); cs.setResourcesAdded(Arrays.asList(res)); KnowledgeAgentConfiguration kaConfig = KnowledgeAgentFactory.newKnowledgeAgentConfiguration(); kaConfig.setProperty("drools.agent.newInstance", "false"); KnowledgeAgent kAgent = KnowledgeAgentFactory.newKnowledgeAgent(" adder ", kbase, kaConfig); kAgent.applyChangeSet(cs); }
/** * Check if all fields are initialized properly. * * <p>In this class, this method will check if the servicePeriodSplitDates contains null. * * @throws OPMConfigurationException if any needed field is not initialized properly. * @since 1.1 OPM - Rules Engine - Scenarios Conversion 2 - Deduction Update Assembly */ @Override @PostConstruct public void checkConfiguration() { super.checkConfiguration(); if (this.entityManager == null) { throw new OPMConfigurationException("entityManager cannot be null."); } if (servicePeriodSplitDates != null) { for (Date date : servicePeriodSplitDates) { if (date == null) { throw new OPMConfigurationException( "Items in servicePeriodSplitDates list cannot be null."); } } } if (this.deductionTableTemplate == null) { throw new OPMConfigurationException("deductionTableTemplate cannot be null."); } try { // Generate deduction table this.generateDeductionTable(); // Initialize knowledge agent KnowledgeAgentConfiguration config = KnowledgeAgentFactory.newKnowledgeAgentConfiguration(); // We have to set the newInstance property to false so that the knowledge base changes can be // reflected config.setProperty("drools.agent.newInstance", "false"); KnowledgeAgent knowledgeAgent = KnowledgeAgentFactory.newKnowledgeAgent("deductionKnowledgeAgent", config); // Substitute the classpath resource location with the file system resource location String changeSet = ServiceHelper.inputStreamToString( ResourceFactory.newClassPathResource("deduction-change-set.xml").getInputStream()); changeSet = changeSet.replace( "classpath:rules/deduction_table.xls", "file:" + this.deductionTableTemplate.getDecisionTableFile()); knowledgeAgent.applyChangeSet( ResourceFactory.newByteArrayResource(changeSet.getBytes("utf-8"))); this.setKnowledgeAgent(knowledgeAgent); } catch (IOException e) { throw new OPMConfigurationException("Failed to initialize KnowledgeAgent."); } }