protected KieSession createKnowledgeSession(String processFile) throws Exception { KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); KieFileSystem kfs = ks.newKieFileSystem(); if (processFile != null) { Resource process = ResourceFactory.newClassPathResource(processFile); kfs.write(process); } KieBuilder kb = ks.newKieBuilder(kfs); kb.buildAll(); if (kb.getResults().hasMessages(Level.ERROR)) { throw new RuntimeException("Build Errors:\n" + kb.getResults().toString()); } KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId()); KieBase kbase = kContainer.getKieBase(); Environment env = EnvironmentFactory.newEnvironment(); Properties defaultProps = new Properties(); defaultProps.setProperty( "drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName()); defaultProps.setProperty( "drools.processInstanceManagerFactory", DefaultProcessInstanceManagerFactory.class.getName()); SessionConfiguration conf = new SessionConfiguration(defaultProps); KieSession ksession = (StatefulKnowledgeSession) kbase.newKieSession(conf, env); return ksession; }
/** * Creating a KieBase from the workflow GAV specified in the config. * * @return KieBase for package SRAMPPackage * @throws SrampClientException * @throws SrampAtomException */ public KieContainer getKieContainer(ReleaseId releaseId) throws SrampClientException, SrampAtomException { KieServices ks = KieServices.Factory.get(); KieRepository repo = ks.getRepository(); SrampAtomApiClient client = SrampAtomApiClientFactory.createAtomApiClient(); Governance governance = new Governance(); QueryResultSet results = client .buildQuery(SRAMP_KIE_JAR_QUERY) .parameter(governance.getGovernanceWorkflowGroup()) .parameter(governance.getGovernanceWorkflowName()) .parameter(governance.getGovernanceWorkflowVersion()) .count(1) .query(); if (results.size() > 0) { ArtifactSummary artifactSummery = results.get(0); InputStream is = client.getArtifactContent(artifactSummery); KieModule kModule = repo.addKieModule(ks.getResources().newInputStreamResource(is)); logger.info( Messages.i18n.format( "KieSrampUtil.CreatingKieContainer", artifactSummery)); // $NON-NLS-1$ KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId()); // Creating the KieBase for the SRAMPPackage logger.info( Messages.i18n.format( "KieSrampUtil.FindKieBase", governance.getGovernanceWorkflowPackage())); // $NON-NLS-1$ return kContainer; } else { return null; } }
@Test public void testSignalStartDynamic() throws Exception { KieBase kbase = createKnowledgeBase("BPMN2-SignalStart.bpmn2"); ksession = createKnowledgeSession(kbase); // create KieContainer after session was created to make sure no runtime data // will be used during serialization (deep clone) KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId()); kContainer.getKieBase(); final List<Long> list = new ArrayList<Long>(); ksession.addEventListener( new DefaultProcessEventListener() { public void afterProcessStarted(ProcessStartedEvent event) { logger.info("{}", event.getProcessInstance().getId()); list.add(event.getProcessInstance().getId()); } }); ksession.signalEvent("MySignal", "NewValue"); Thread.sleep(500); assertEquals(1, getNumberOfProcessInstances("Minimal")); // now remove the process from kbase to make sure runtime based listeners are removed from // signal manager kbase.removeProcess("Minimal"); ksession.signalEvent("MySignal", "NewValue"); Thread.sleep(500); // must be still one as the process was removed assertEquals(1, getNumberOfProcessInstances("Minimal")); }
public void go(PrintStream out) { KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); KieModule kModule = kr.addKieModule(ks.getResources().newFileSystemResource(getFile("named-kiesession"))); KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId()); KieSession kSession = kContainer.newKieSession("ksession1"); kSession.setGlobal("out", out); Object msg1 = createMessage(kContainer, "Dave", "Hello, HAL. Do you read me, HAL?"); kSession.insert(msg1); kSession.fireAllRules(); }
/** * This method loads the rule base associated with the Drools event processor. * * @return The knowledge base */ private KieBase loadRuleBase() { String droolsRuleBase = getRuleName() + ".drl"; try { KieServices ks = KieServices.Factory.get(); KieRepository kr = ks.getRepository(); KieModuleModel kmm = ks.newKieModuleModel(); KieBaseModel kbm = kmm.newKieBaseModel(getRuleName()) .setEqualsBehavior(EqualityBehaviorOption.EQUALITY) .setEventProcessingMode(EventProcessingOption.STREAM); kbm.setDefault(true); KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/main/resources/" + kbm.getName() + "/rule1.drl", ks.getResources().newClassPathResource(droolsRuleBase)); kfs.writeKModuleXML(kmm.toXML()); KieBuilder kb = ks.newKieBuilder(kfs); kb.buildAll(); KieContainer container = ks.newKieContainer(kr.getDefaultReleaseId()); KieBase kbase = container.getKieBase(); // TODO: hack it for now, this attribute should be supported in the following drools6 release. System.setProperty("kie.mbean", "enabled"); return kbase; } catch (Throwable e) { LOG.log( Level.SEVERE, MessageFormat.format( java.util.PropertyResourceBundle.getBundle("ep-drools.Messages") .getString("EP-DROOLS-1"), droolsRuleBase, getRuleName()), e); } return (null); }
public Results updateDependencyToVersion(ReleaseId currentReleaseId, ReleaseId newReleaseId) { checkNotClasspathKieProject(); ReleaseId installedReleaseId = getReleaseId(); InternalKieModule currentKM; if (currentReleaseId.getGroupId().equals(installedReleaseId.getGroupId()) && currentReleaseId.getArtifactId().equals(installedReleaseId.getArtifactId())) { // upgrading the kProject itself: taking the kmodule from there currentKM = ((KieModuleKieProject) kProject).getInternalKieModule(); } else { // upgrading a transitive dependency: taking the kmodule from the krepo // if the new and the current release are equal (a snapshot) check if there is an older // version with the same releaseId currentKM = currentReleaseId.equals(newReleaseId) ? (InternalKieModule) ((KieRepositoryImpl) kr).getOldKieModule(currentReleaseId) : (InternalKieModule) kr.getKieModule(currentReleaseId); } return update(currentKM, newReleaseId); }
public void discoverKieModules() { String[] configFiles = { KieModuleModelImpl.KMODULE_JAR_PATH, KieModuleModelImpl.KMODULE_SPRING_JAR_PATH }; for (String configFile : configFiles) { final Enumeration<URL> e; try { e = classLoader.getResources(configFile); } catch (IOException exc) { log.error("Unable to find and build index of " + configFile + "." + exc.getMessage()); return; } // Map of kmodule urls while (e.hasMoreElements()) { URL url = e.nextElement(); notifyKieModuleFound(url); try { InternalKieModule kModule = fetchKModule(url); if (kModule != null) { ReleaseId releaseId = kModule.getReleaseId(); kieModules.put(releaseId, kModule); log.debug("Discovered classpath module " + releaseId.toExternalForm()); kieRepository.addKieModule(kModule); } } catch (Exception exc) { log.error( "Unable to build index of kmodule.xml url=" + url.toExternalForm() + "\n" + exc.getMessage()); } } } }
private Results update(final InternalKieModule currentKM, final ReleaseId newReleaseId) { final InternalKieModule newKM = (InternalKieModule) kr.getKieModule(newReleaseId); ChangeSetBuilder csb = new ChangeSetBuilder(); final KieJarChangeSet cs = csb.build(currentKM, newKM); final List<String> modifiedClasses = getModifiedClasses(cs); final List<String> dslFiles = getUnchangedDslFiles(newKM, cs); ((KieModuleKieProject) kProject).updateToModule(newKM); final ResultsImpl results = new ResultsImpl(); List<String> kbasesToRemove = new ArrayList<String>(); for (Entry<String, KieBase> kBaseEntry : kBases.entrySet()) { String kbaseName = kBaseEntry.getKey(); final KieBaseModel kieBaseModel = kProject.getKieBaseModel(kbaseName); // if a kbase no longer exists, just remove it from the cache if (kieBaseModel == null) { // have to save for later removal to avoid iteration errors kbasesToRemove.add(kbaseName); } else { final InternalKnowledgeBase kBase = (InternalKnowledgeBase) kBaseEntry.getValue(); boolean locked = kBase.tryLock(); if (locked) { try { updateKBase( kBase, currentKM, newReleaseId, newKM, cs, modifiedClasses, dslFiles, results, kieBaseModel); } finally { kBase.unlock(); } } else { kBase.enqueueModification( new Runnable() { @Override public void run() { updateKBase( kBase, currentKM, newReleaseId, newKM, cs, modifiedClasses, dslFiles, results, kieBaseModel); } }); } } } for (String kbaseToRemove : kbasesToRemove) { kBases.remove(kbaseToRemove); } for (Iterator<Entry<String, KieSession>> it = this.kSessions.entrySet().iterator(); it.hasNext(); ) { Entry<String, KieSession> ksession = it.next(); if (kProject.getKieSessionModel(ksession.getKey()) == null) { // remove sessions that no longer exist it.remove(); } } for (Iterator<Entry<String, StatelessKieSession>> it = this.statelessKSessions.entrySet().iterator(); it.hasNext(); ) { Entry<String, StatelessKieSession> ksession = it.next(); if (kProject.getKieSessionModel(ksession.getKey()) == null) { // remove sessions that no longer exist it.remove(); } } return results; }