public DroolsSession(File saved, String processXML, int initialProcessInstanceId) throws IOException { this.saved = saved; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(new PackageBuilderConfiguration()); kbuilder.add(new ReaderResource(new StringReader(processXML)), ResourceType.DRF); KnowledgeBuilderErrors errors = kbuilder.getErrors(); StringBuilder sb = new StringBuilder(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { sb.append(error.getMessage()).append("\n"); } throw new IllegalArgumentException("Could not parse knowledge:\n" + sb); } Collection<KnowledgePackage> knowledgePackages = kbuilder.getKnowledgePackages(); Process process = knowledgePackages.iterator().next().getProcesses().iterator().next(); processId = process.getId(); kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(knowledgePackages); marshaller = MarshallerFactory.newMarshaller(kbase); SessionConfiguration conf = new SessionConfiguration(); Properties p = new Properties(); p.setProperty( "drools.processInstanceManagerFactory", HudsonProcessInstanceManagerFactory.class.getName()); conf.addProperties(p); Environment env = EnvironmentFactory.newEnvironment(); if (!saved.exists() || saved.length() == 0) { session = kbase.newStatefulKnowledgeSession(conf, env); } else { InputStream is = null; try { is = new FileInputStream(saved); session = marshaller.unmarshall(is, conf, env); } catch (ClassNotFoundException e) { throw new IOException2("Class not found while unmarshalling " + saved.getAbsolutePath(), e); } catch (IOException e) { throw new IOException2("Error while unmarshalling " + saved.getAbsolutePath(), e); } finally { is.close(); } } ((HudsonProcessInstanceManager) ((InternalWorkingMemoryEntryPoint) session) .getInternalWorkingMemory() .getProcessInstanceManager()) .setProcessCounter(initialProcessInstanceId); }
private static KnowledgeBase readKnowledgeBase() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); // Resource resource // =ResourceFactory.newClassPathResource("ruleflow.rf"); DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration(); config.setInputType(DecisionTableInputType.XLS); // kbuilder.add( // ResourceFactory // .newClassPathResource("proposalRules/proposalRule.xls"), // ResourceType.DTABLE, config); kbuilder.add( ResourceFactory.newClassPathResource("comboRules/comboRule-shandong.xls"), ResourceType.DTABLE, config); kbuilder.add( ResourceFactory.newClassPathResource("comboRules/comboRule-shenzhen.xls"), ResourceType.DTABLE, config); kbuilder.add( ResourceFactory.newClassPathResource("comboRules/comboRuleFlow.rf"), ResourceType.DRF, config); kbuilder.add( ResourceFactory.newClassPathResource("specialClausRule/specialClausRule.xls"), ResourceType.DTABLE, config); kbuilder.add( ResourceFactory.newClassPathResource("quickPriceRules/quickPriceRule-shandong.xls"), ResourceType.DTABLE, config); kbuilder.add( ResourceFactory.newClassPathResource("quickPriceRules/quickPriceRule-shenzhen.xls"), ResourceType.DTABLE, config); kbuilder.add( ResourceFactory.newClassPathResource("quickPriceRules/quickPriceRuleFlow.rf"), ResourceType.DRF, config); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { System.err.println("error is :" + error); } throw new IllegalArgumentException("Could not parse knowledge....."); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); System.out.println("[debug]finish read base!"); return kbase; }
private static KnowledgeBase readKnowledgeBase() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); return kbase; }
private Collection<KnowledgePackage> buildKnowledgePackage( Resource resource, ResourceType resourceType) { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(resource, resourceType); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors != null && errors.size() > 0) { for (KnowledgeBuilderError error : errors) { System.err.println("Error: " + error.getMessage()); } Assert.fail("KnowledgeBase did not build"); } Collection<KnowledgePackage> packages = kbuilder.getKnowledgePackages(); return packages; }