Search in sources :

Example 1 with AssertBehaviour

use of org.drools.core.RuleBaseConfiguration.AssertBehaviour in project opennms by OpenNMS.

the class DroolsNorthbounder method initializeDroolsEngine.

/**
 * Initialize drools engine.
 *
 * @throws Exception the exception
 */
private void initializeDroolsEngine() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kFileSystem = ks.newKieFileSystem();
    for (String ruleFile : m_engine.getRuleFiles()) {
        LOG.debug("Loading rules file: {}", ruleFile);
        kFileSystem.write("src/main/resources/" + ruleFile, ks.getResources().newFileSystemResource(new File(ruleFile)));
    }
    KieBuilder kbuilder = ks.newKieBuilder(kFileSystem);
    kbuilder.buildAll();
    if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        LOG.warn("Unable to initialize Drools engine: {}", kbuilder.getResults().getMessages(Level.ERROR));
        throw new IllegalStateException("Unable to initialize Drools engine: " + kbuilder.getResults().getMessages(Level.ERROR));
    }
    KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    AssertBehaviour behaviour = AssertBehaviour.determineAssertBehaviour(m_engine.getAssertBehaviour());
    RuleBaseConfiguration ruleBaseConfig = new RuleBaseConfiguration();
    ruleBaseConfig.setAssertBehaviour(behaviour);
    ruleBaseConfig.setEventProcessingMode(EventProcessingOption.STREAM);
    m_kieBase = kContainer.newKieBase(ruleBaseConfig);
    m_kieSession = m_kieBase.newKieSession();
    m_kieSession.setGlobal("engine", this);
    unmarshallStateFromDisk(true);
    ApplicationContext ctx = m_context;
    if (m_engine.getAppContext() != null) {
        ctx = new FileSystemXmlApplicationContext(new String[] { m_engine.getAppContext() }, m_context);
    }
    for (Global global : m_engine.getGlobals()) {
        m_kieSession.setGlobal(global.getName(), global.constructValue(ctx));
    }
    new Thread(() -> {
        Logging.putPrefix(getName());
        LOG.debug("Starting task thread for {}", getName());
        m_kieSession.fireUntilHalt();
        LOG.debug("Stopping task thread for {}", getName());
    }, "FireTask-" + getName()).start();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) FileSystemXmlApplicationContext(org.springframework.context.support.FileSystemXmlApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AssertBehaviour(org.drools.core.RuleBaseConfiguration.AssertBehaviour) KieBuilder(org.kie.api.builder.KieBuilder) File(java.io.File) KieContainer(org.kie.api.runtime.KieContainer)

Example 2 with AssertBehaviour

use of org.drools.core.RuleBaseConfiguration.AssertBehaviour in project opennms by OpenNMS.

the class DroolsCorrelationEngine method initialize.

/**
 * <p>initialize</p>
 *
 * @throws java.lang.Exception if any.
 */
public void initialize() throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kFileSystem = ks.newKieFileSystem();
    loadRules(kFileSystem);
    KieBuilder kbuilder = ks.newKieBuilder(kFileSystem);
    kbuilder.buildAll();
    if (kbuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
        LOG.warn("Unable to initialize Drools engine: {}", kbuilder.getResults().getMessages(Level.ERROR));
        throw new IllegalStateException("Unable to initialize Drools engine: " + kbuilder.getResults().getMessages(Level.ERROR));
    }
    KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    AssertBehaviour behaviour = AssertBehaviour.determineAssertBehaviour(m_assertBehaviour);
    RuleBaseConfiguration ruleBaseConfig = new RuleBaseConfiguration();
    ruleBaseConfig.setAssertBehaviour(behaviour);
    EventProcessingOption eventProcessingOption = EventProcessingOption.CLOUD;
    if (m_eventProcessingMode != null && m_eventProcessingMode.toLowerCase().equals("stream")) {
        eventProcessingOption = EventProcessingOption.STREAM;
        m_isStreaming = true;
    }
    ruleBaseConfig.setEventProcessingMode(eventProcessingOption);
    m_kieBase = kContainer.newKieBase(ruleBaseConfig);
    m_kieSession = m_kieBase.newKieSession();
    m_kieSession.setGlobal("engine", this);
    for (final Map.Entry<String, Object> entry : m_globals.entrySet()) {
        m_kieSession.setGlobal(entry.getKey(), entry.getValue());
    }
    if (m_persistState != null && m_persistState) {
        unmarshallStateFromDisk(true);
    }
    if (m_isStreaming) {
        new Thread(() -> {
            Logging.putPrefix(getClass().getSimpleName() + '-' + getName());
            m_kieSession.fireUntilHalt();
        }, "FireTask").start();
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) EventProcessingOption(org.kie.api.conf.EventProcessingOption) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) AssertBehaviour(org.drools.core.RuleBaseConfiguration.AssertBehaviour) KieBuilder(org.kie.api.builder.KieBuilder) HashMap(java.util.HashMap) Map(java.util.Map) KieContainer(org.kie.api.runtime.KieContainer)

Aggregations

RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)2 AssertBehaviour (org.drools.core.RuleBaseConfiguration.AssertBehaviour)2 KieServices (org.kie.api.KieServices)2 KieBuilder (org.kie.api.builder.KieBuilder)2 KieFileSystem (org.kie.api.builder.KieFileSystem)2 KieContainer (org.kie.api.runtime.KieContainer)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EventProcessingOption (org.kie.api.conf.EventProcessingOption)1 ApplicationContext (org.springframework.context.ApplicationContext)1 FileSystemXmlApplicationContext (org.springframework.context.support.FileSystemXmlApplicationContext)1