Search in sources :

Example 16 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class RuleBaseConfigurationTest method testSystemProperties.

@Test
public void testSystemProperties() {
    RuleBaseConfiguration cfg = new RuleBaseConfiguration();
    assertEquals(AssertBehaviour.IDENTITY, cfg.getAssertBehaviour());
    System.setProperty("drools.equalityBehavior", "EQUALITY");
    cfg = new RuleBaseConfiguration();
    assertEquals(AssertBehaviour.EQUALITY, cfg.getAssertBehaviour());
    System.getProperties().remove("drools.equalityBehavior");
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) Test(org.junit.Test)

Example 17 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration 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)

Example 18 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testTemporalOperators.

@Test(timeout = 10000)
public void testTemporalOperators() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new StockTick(1, "A", 10, 1000));
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 19 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testTemporalOperators2.

@Test(timeout = 10000)
public void testTemporalOperators2() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators2.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    EntryPoint ep = ksession.getEntryPoint("X");
    clock.advanceTime(1000, TimeUnit.SECONDS);
    ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    int rules = ksession.fireAllRules();
// assertEquals( 2,
// rules );
// assertEquals( 1, list.size() );
// StockTick[] stocks = ( StockTick[] ) list.get(0);
// assertSame( tick4, stocks[0]);
// assertSame( tick2, stocks[1]);
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 20 with RuleBaseConfiguration

use of org.drools.core.RuleBaseConfiguration in project drools by kiegroup.

the class Misc2Test method testLegacySalienceResolver.

@Test
public void testLegacySalienceResolver() {
    // DROOLS-159
    String drl = "package org.drools.test; \n" + "" + "global java.util.List list; \n " + "" + "rule X salience 10 \n" + "then\n" + " list.add( 1 ); \n" + "end\n" + "" + "rule Y salience 5 \n" + "then\n" + " list.add( 2 ); \n" + "end\n" + "";
    KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kb.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
    assertFalse(kb.hasErrors());
    KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    ((RuleBaseConfiguration) kbconf).setConflictResolver(SalienceConflictResolver.getInstance());
    InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
    knowledgeBase.addPackages(kb.getKnowledgePackages());
    KieSession knowledgeSession = knowledgeBase.newKieSession();
    List list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.fireAllRules();
    assertEquals(Arrays.asList(1, 2), list);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

RuleBaseConfiguration (org.drools.core.RuleBaseConfiguration)28 Test (org.junit.Test)14 KieSession (org.kie.api.runtime.KieSession)8 ArrayList (java.util.ArrayList)7 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)6 List (java.util.List)5 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)5 BuildContext (org.drools.core.reteoo.builder.BuildContext)5 BetaNodeFieldConstraint (org.drools.core.spi.BetaNodeFieldConstraint)5 KieBase (org.kie.api.KieBase)5 Properties (java.util.Properties)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 MVELDialectRuntimeData (org.drools.core.rule.MVELDialectRuntimeData)4 StockTick (org.drools.compiler.StockTick)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)3 BetaMemory (org.drools.core.reteoo.BetaMemory)3 IndexableConstraint (org.drools.core.rule.IndexableConstraint)3 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)3