Search in sources :

Example 46 with KnowledgeBase

use of org.drools.KnowledgeBase 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 47 with KnowledgeBase

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

the class DefineCarPriceWorkItemHandler method createStatelessSession.

private StatelessKnowledgeSession createStatelessSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("good_old_patterns/car-pricing-rules.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase.newStatelessKnowledgeSession();
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) ClassPathResource(org.drools.io.impl.ClassPathResource)

Example 48 with KnowledgeBase

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

the class RankCarWorkItemHandler method createStatelessSession.

private StatelessKnowledgeSession createStatelessSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("good_old_patterns/car-ranking-rules.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase.newStatelessKnowledgeSession();
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) ClassPathResource(org.drools.io.impl.ClassPathResource)

Example 49 with KnowledgeBase

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

the class RulesHelper method createStatelessSession.

private static StatelessKnowledgeSession createStatelessSession(String rules) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource(rules), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.out.println(">>> Error:" + error.getMessage());
        }
        throw new IllegalStateException(">>> Knowledge couldn't be parsed! ");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase.newStatelessKnowledgeSession();
}
Also used : KnowledgeBuilderError(org.drools.builder.KnowledgeBuilderError) KnowledgeBuilder(org.drools.builder.KnowledgeBuilder) KnowledgeBase(org.drools.KnowledgeBase) ClassPathResource(org.drools.io.impl.ClassPathResource)

Example 50 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)

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