use of org.drools.builder.KnowledgeBuilder in project Activiti by Activiti.
the class RulesDeployer method deploy.
public void deploy(DeploymentEntity deployment, Map<String, Object> deploymentSettings) {
log.debug("Processing deployment {}", deployment.getName());
KnowledgeBuilder knowledgeBuilder = null;
DeploymentManager deploymentManager = Context.getProcessEngineConfiguration().getDeploymentManager();
Map<String, ResourceEntity> resources = deployment.getResources();
for (String resourceName : resources.keySet()) {
log.info("Processing resource {}", resourceName);
if (resourceName.endsWith(".drl")) {
// is only parsing .drls sufficient? what about other rule dsl's? (@see ResourceType)
if (knowledgeBuilder == null) {
knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
}
ResourceEntity resourceEntity = resources.get(resourceName);
byte[] resourceBytes = resourceEntity.getBytes();
Resource droolsResource = ResourceFactory.newByteArrayResource(resourceBytes);
knowledgeBuilder.add(droolsResource, ResourceType.DRL);
}
}
if (knowledgeBuilder != null) {
KnowledgeBase knowledgeBase = knowledgeBuilder.newKnowledgeBase();
deploymentManager.getKnowledgeBaseCache().add(deployment.getId(), knowledgeBase);
}
}
use of org.drools.builder.KnowledgeBuilder in project sling by apache.
the class AbstractUsingBundleListMojo method createKnowledgeBase.
private KnowledgeBase createKnowledgeBase(File[] files) throws MojoExecutionException {
KnowledgeBuilder builder = KnowledgeBuilderFactory.newKnowledgeBuilder();
builder.add(ResourceFactory.newClassPathResource("drools-globals.drl", getClass()), ResourceType.DRL);
for (File file : files) {
getLog().info("Parsing rule file " + file.getAbsolutePath());
builder.add(ResourceFactory.newFileResource(file), ResourceType.DRL);
}
if (builder.hasErrors()) {
getLog().error("Rule errors:");
for (KnowledgeBuilderError error : builder.getErrors()) {
getLog().error(error.toString());
}
throw new MojoExecutionException("Unable to create rules. See log for details.");
}
KnowledgeBase base = KnowledgeBaseFactory.newKnowledgeBase();
base.addKnowledgePackages(builder.getKnowledgePackages());
return base;
}
use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class EmergencyBedRequestBaseTest method initializeSession.
/**
* Compiles the resources indicated by the concrete implementation of
* this class (using {@link #getResources()} abstract method.
* If there is any compilation error an {@link IllegalStateException} is
* thrown.
* After the resources are compiled, a kbase is created and populated with
* the resulting knowledge package/s.
* From this kbase, a new StatefulKnowledgeSession is created. The session
* is configured with 2 listeners:
* 1.- An Agenda event listener that will fireAllRules() every time an
* activation happens
* 2.- A Process event listener that will insert the process instance as
* a Fact once a process is started.
*/
private void initializeSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
for (Map.Entry<Resource, ResourceType> entry : this.getResources().entrySet()) {
kbuilder.add(entry.getKey(), entry.getValue());
}
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(">>> Error:" + error.getMessage());
}
throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
session.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
session.fireAllRules();
}
});
session.addEventListener(new DefaultProcessEventListener() {
@Override
public void beforeProcessStarted(ProcessStartedEvent event) {
session.insert(event.getProcessInstance());
}
});
}
use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class AsyncWorkItemWaitForCompletionTest method initializeSession.
private void initializeSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
// We are using a proces containing a single Task. The task is configured
// to wait until the completion of the external system. A specialized
// callback (CompleteWorkItemCallback) is used to complete the work item
// after the command was executed.
kbuilder.add(new ClassPathResource("async-work-item-wait.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(">>> Error:" + error.getMessage());
}
throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
session.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
session.fireAllRules();
}
});
// Registers the session in SessionStoreUtil. The callback we are using
// for the process' Task will retrieve this session later and completes
// the work item using it.
SessionStoreUtil.sessionCache.put("sessionId=" + session.getId(), session);
}
use of org.drools.builder.KnowledgeBuilder in project jBPM5-Developer-Guide by Salaboy.
the class HospitalInsuranceProcessExecutorTest method initializeSession.
private void initializeSession() {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ClassPathResource("InsuranceProcessV2.bpmn"), ResourceType.BPMN2);
if (kbuilder.hasErrors()) {
KnowledgeBuilderErrors errors = kbuilder.getErrors();
for (KnowledgeBuilderError error : errors) {
System.out.println(">>> Error:" + error.getMessage());
}
throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
session = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLoggerFactory.newConsoleLogger(session);
session.addEventListener(new DefaultAgendaEventListener() {
@Override
public void afterRuleFlowGroupActivated(org.drools.event.rule.RuleFlowGroupActivatedEvent event) {
session.fireAllRules();
}
});
// Registers the same generic handler for all the tasks. The handler will
// schedule commands into the Executor Service. Each task in the process
// is configured to use a different command.
AsyncGenericWorkItemHandler genericHandler = new AsyncGenericWorkItemHandler(executor, session.getId());
session.getWorkItemManager().registerWorkItemHandler("Gather Patient Data", genericHandler);
session.getWorkItemManager().registerWorkItemHandler("Insurance Service", genericHandler);
session.getWorkItemManager().registerWorkItemHandler("External Insurance Company Service", genericHandler);
session.getWorkItemManager().registerWorkItemHandler("Rates Service", genericHandler);
session.getWorkItemManager().registerWorkItemHandler("Invoice Service", genericHandler);
}
Aggregations