Search in sources :

Example 26 with KnowledgeBase

use of org.drools.KnowledgeBase in project jBPM5-Developer-Guide by Salaboy.

the class MyFirstjBPM5ProcessTest method test.

@Test
public void test() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("firstDummyProcess.bpmn"), ResourceType.BPMN2);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        fail(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    // Uncomment to see all the logs
    // KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
    // We have our Session, now let's play with our Dummy Process
    ProcessInstance processInstance = ksession.createProcessInstance("DummyProcess", null);
    assertEquals(processInstance.getState(), ProcessInstance.STATE_PENDING);
    ksession.startProcessInstance(processInstance.getId());
    assertEquals(processInstance.getState(), ProcessInstance.STATE_COMPLETED);
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ProcessInstance(org.drools.runtime.process.ProcessInstance) ClassPathResource(org.drools.io.impl.ClassPathResource) Test(org.junit.Test)

Example 27 with KnowledgeBase

use of org.drools.KnowledgeBase 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);
    }
}
Also used : KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) DeploymentManager(org.activiti.engine.impl.persistence.deploy.DeploymentManager) ResourceEntity(org.activiti.engine.impl.persistence.entity.ResourceEntity) Resource(org.drools.io.Resource)

Example 28 with KnowledgeBase

use of org.drools.KnowledgeBase in project Activiti by Activiti.

the class BusinessRuleTaskActivityBehavior method execute.

public void execute(ActivityExecution execution) throws Exception {
    ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) execution.getEngineServices().getProcessEngineConfiguration();
    ProcessDefinition processDefinition = processEngineConfiguration.getDeploymentManager().findDeployedProcessDefinitionById(execution.getProcessDefinitionId());
    String deploymentId = processDefinition.getDeploymentId();
    KnowledgeBase knowledgeBase = RulesHelper.findKnowledgeBaseByDeploymentId(deploymentId);
    StatefulKnowledgeSession ksession = knowledgeBase.newStatefulKnowledgeSession();
    if (variablesInputExpressions != null) {
        Iterator<Expression> itVariable = variablesInputExpressions.iterator();
        while (itVariable.hasNext()) {
            Expression variable = itVariable.next();
            ksession.insert(variable.getValue(execution));
        }
    }
    if (!rulesExpressions.isEmpty()) {
        RulesAgendaFilter filter = new RulesAgendaFilter();
        Iterator<Expression> itRuleNames = rulesExpressions.iterator();
        while (itRuleNames.hasNext()) {
            Expression ruleName = itRuleNames.next();
            filter.addSuffic(ruleName.getValue(execution).toString());
        }
        filter.setAccept(!exclude);
        ksession.fireAllRules(filter);
    } else {
        ksession.fireAllRules();
    }
    Collection<Object> ruleOutputObjects = ksession.getObjects();
    if (ruleOutputObjects != null && !ruleOutputObjects.isEmpty()) {
        Collection<Object> outputVariables = new ArrayList<Object>();
        for (Object object : ruleOutputObjects) {
            outputVariables.add(object);
        }
        execution.setVariable(resultVariable, outputVariables);
    }
    ksession.dispose();
    leave(execution);
}
Also used : KnowledgeBase(org.drools.KnowledgeBase) Expression(org.activiti.engine.delegate.Expression) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ProcessDefinition(org.activiti.engine.repository.ProcessDefinition) RulesAgendaFilter(org.activiti.engine.impl.rules.RulesAgendaFilter) ProcessEngineConfigurationImpl(org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl)

Example 29 with KnowledgeBase

use of org.drools.KnowledgeBase 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;
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) KnowledgeBase(org.drools.KnowledgeBase) File(java.io.File)

Example 30 with KnowledgeBase

use of org.drools.KnowledgeBase in project jBPM5-Developer-Guide by Salaboy.

the class GenericWebServiceTaskTest method initializeSession.

private void initializeSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("HospitalInvokeInsuranceScenarioV3-data.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();
        }
    });
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) ClassPathResource(org.drools.io.impl.ClassPathResource)

Aggregations

KnowledgeBase (org.drools.KnowledgeBase)52 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)48 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)47 ClassPathResource (org.drools.io.impl.ClassPathResource)45 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)28 HashMap (java.util.HashMap)20 ProcessInstance (org.drools.runtime.process.ProcessInstance)20 Person (com.salaboy.model.Person)16 Test (org.junit.Test)13 DefaultProcessEventListener (org.drools.event.process.DefaultProcessEventListener)11 DefaultAgendaEventListener (org.drools.event.rule.DefaultAgendaEventListener)11 KnowledgeBuilderErrors (org.drools.builder.KnowledgeBuilderErrors)9 QueryResults (org.drools.runtime.rule.QueryResults)9 Resources (com.salaboy.model.Resources)6 EntityManagerFactory (javax.persistence.EntityManagerFactory)6 ProcessNodeLeftEvent (org.drools.event.process.ProcessNodeLeftEvent)6 Customer (com.salaboy.model.Customer)5 RatesToday (com.salaboy.model.RatesToday)5 KnowledgeBaseConfiguration (org.drools.KnowledgeBaseConfiguration)5 FactHandle (org.drools.runtime.rule.FactHandle)5