Search in sources :

Example 1 with Resource

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

the class MultiSessionsPatternsTest method createProcessInteractionKnowledgeSession.

/**
     * Creates a new ksession containing a single rules resource: 
     * 'interaction-rules.drl'.
     * This method uses {@link #createKnowledgeSession(java.lang.String, java.util.Map)}
     * in order to create the ksession.
     * @param key The key used to register the kbase created with the resources
     * used by this method.
     * @param em The entity manager registered as a global in the created
     * session.
     * @return a new ksession containing a single process definition.
     */
private StatefulKnowledgeSession createProcessInteractionKnowledgeSession(String key, EntityManager em) {
    Map<Resource, ResourceType> resources = new HashMap<Resource, ResourceType>();
    resources.put(ResourceFactory.newClassPathResource("interaction-rules.drl"), ResourceType.DRL);
    //creates the session
    StatefulKnowledgeSession ksession = this.createKnowledgeSession(key, resources);
    //register globals
    ksession.setGlobal("em", em);
    ksession.setGlobal("ksessionSupport", this);
    return ksession;
}
Also used : HashMap(java.util.HashMap) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) Resource(org.drools.io.Resource) ResourceType(org.drools.builder.ResourceType)

Example 2 with Resource

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

the class SessionsPatternsTestsBase method createKnowledgeSession.

/**
     * Creates a new Knowledge Base with the passed resources and returns a fresh
     * ksession from it. This method register the created kbase in {@link #kbases}
     * with the key passed as parameter. The returned session is configured as
     * persistent.
     * @param key the key used to register the generated kbase in {@link #kbases}.
     * @param resources The resources to be placed inside the generated kbase.
     * @return 
     */
protected StatefulKnowledgeSession createKnowledgeSession(String key, Map<Resource, ResourceType> resources) {
    //Creates a new kbuilder
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    //Adds al the given resources
    for (Map.Entry<Resource, ResourceType> entry : resources.entrySet()) {
        kbuilder.add(entry.getKey(), entry.getValue());
    }
    //If there is any compilation error then fail!
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        Assert.fail(">>> Knowledge couldn't be parsed! ");
    }
    //Creates a new kbase
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    //Add the generated knowledge packages from kbuilder.
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    //Register the kbase in this.kbases
    kbases.put(key, kbase);
    //Creates a Persistence Knowledge Session
    System.out.println(" >>> Let's create a Persistent Knowledge Session");
    final StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, createEnvironment());
    return ksession;
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) StatefulKnowledgeSession(org.drools.runtime.StatefulKnowledgeSession) Resource(org.drools.io.Resource) ResourceType(org.drools.builder.ResourceType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Resource

use of org.drools.io.Resource 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());
        }
    });
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilderErrors(org.drools.builder.KnowledgeBuilderErrors) Resource(org.drools.io.Resource) ProcessStartedEvent(org.drools.event.process.ProcessStartedEvent) DefaultAgendaEventListener(org.drools.event.rule.DefaultAgendaEventListener) ResourceType(org.drools.builder.ResourceType) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) DefaultProcessEventListener(org.drools.event.process.DefaultProcessEventListener) Map(java.util.Map)

Example 4 with Resource

use of org.drools.io.Resource 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)

Aggregations

Resource (org.drools.io.Resource)4 KnowledgeBase (org.drools.KnowledgeBase)3 KnowledgeBuilder (org.drools.builder.KnowledgeBuilder)3 ResourceType (org.drools.builder.ResourceType)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 KnowledgeBuilderError (org.drools.builder.KnowledgeBuilderError)2 StatefulKnowledgeSession (org.drools.runtime.StatefulKnowledgeSession)2 DeploymentManager (org.activiti.engine.impl.persistence.deploy.DeploymentManager)1 ResourceEntity (org.activiti.engine.impl.persistence.entity.ResourceEntity)1 KnowledgeBuilderErrors (org.drools.builder.KnowledgeBuilderErrors)1 DefaultProcessEventListener (org.drools.event.process.DefaultProcessEventListener)1 ProcessStartedEvent (org.drools.event.process.ProcessStartedEvent)1 DefaultAgendaEventListener (org.drools.event.rule.DefaultAgendaEventListener)1