Search in sources :

Example 1 with SessionConfiguration

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

the class StatelessKnowledgeSessionImpl method newWorkingMemory.

public StatefulKnowledgeSession newWorkingMemory() {
    this.kBase.readLock();
    try {
        StatefulKnowledgeSession ksession = (StatefulKnowledgeSession) wmFactory.createWorkingMemory(this.kBase.nextWorkingMemoryCounter(), this.kBase, (SessionConfiguration) this.conf, this.environment);
        StatefulKnowledgeSessionImpl ksessionImpl = (StatefulKnowledgeSessionImpl) ksession;
        ((Globals) ksessionImpl.getGlobalResolver()).setDelegate(this.sessionGlobals);
        registerListeners(ksessionImpl);
        for (Map.Entry<String, Channel> entry : this.channels.entrySet()) {
            ksession.registerChannel(entry.getKey(), entry.getValue());
        }
        wmCreated.incrementAndGet();
        return ksession;
    } finally {
        this.kBase.readUnlock();
    }
}
Also used : Globals(org.kie.api.runtime.Globals) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) Channel(org.kie.api.runtime.Channel) HashMap(java.util.HashMap) Map(java.util.Map) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 2 with SessionConfiguration

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

the class FactHandleMarshallingTest method createWorkingMemory.

private StatefulKnowledgeSessionImpl createWorkingMemory(InternalKnowledgeBase kBase) {
    // WorkingMemoryEntryPoint
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    SessionConfiguration sessionConf = ((SessionConfiguration) ksconf);
    StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase, true, sessionConf, EnvironmentFactory.newEnvironment());
    return wm;
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 3 with SessionConfiguration

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

the class MarshallingTest method testScheduledActivation.

@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
    KnowledgeBaseImpl knowledgeBase = (KnowledgeBaseImpl) KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgePackageImpl impl = new KnowledgePackageImpl("test");
    BuildContext buildContext = new BuildContext(knowledgeBase);
    // simple rule that fires after 10 seconds
    final RuleImpl rule = new RuleImpl("test-rule");
    new RuleTerminalNode(1, new MockTupleSource(2), rule, rule.getLhs(), 0, buildContext);
    final List<String> fired = new ArrayList<String>();
    rule.setConsequence(new Consequence() {

        public void evaluate(KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) throws Exception {
            fired.add("a");
        }

        public String getName() {
            return "default";
        }
    });
    rule.setTimer(new DurationTimer(10000));
    rule.setPackage("test");
    impl.addRule(rule);
    knowledgeBase.addPackages(Collections.singleton(impl));
    SessionConfiguration config = SessionConfiguration.newInstance();
    config.setClockType(ClockType.PSEUDO_CLOCK);
    KieSession ksession = knowledgeBase.newKieSession(config, KieServices.get().newEnvironment());
    PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
    ksession.insert("cheese");
    assertTrue(fired.isEmpty());
    // marshall, then unmarshall session
    readWrite(knowledgeBase, ksession, config);
    // the activations should fire after 10 seconds
    assertTrue(fired.isEmpty());
    scheduler.advanceTime(12, TimeUnit.SECONDS);
    assertFalse(fired.isEmpty());
}
Also used : Marshaller(org.kie.api.marshalling.Marshaller) WorkingMemory(org.drools.core.WorkingMemory) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) DurationTimer(org.drools.core.time.impl.DurationTimer) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with SessionConfiguration

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

the class BayesBeliefSystemTest method getSession.

protected KieSession getSession(String ruleFile) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(ResourceFactory.newClassPathResource(ruleFile), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
        fail();
    }
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addPackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
    KieSession kSession = kBase.newKieSession(ksConf, null);
    return kSession;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 5 with SessionConfiguration

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

the class AbductionTest method getSessionFromString.

protected KieSession getSessionFromString(String drlString, KieBaseConfiguration kbConf) {
    KieHelper kieHelper = new KieHelper();
    kieHelper.addContent(drlString, ResourceType.DRL);
    Results res = kieHelper.verify();
    if (res.hasMessages(Message.Level.ERROR)) {
        fail(res.getMessages(Message.Level.ERROR).toString());
    }
    if (kbConf == null) {
        kbConf = KieServices.Factory.get().newKieBaseConfiguration();
    }
    kbConf.setOption(EqualityBehaviorOption.EQUALITY);
    KieBase kieBase = kieHelper.build(kbConf);
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
    return kieBase.newKieSession(ksConf, null);
}
Also used : QueryResults(org.kie.api.runtime.rule.QueryResults) FlatQueryResults(org.drools.core.runtime.rule.impl.FlatQueryResults) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Aggregations

SessionConfiguration (org.drools.core.SessionConfiguration)23 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)17 KieSession (org.kie.api.runtime.KieSession)12 Test (org.junit.Test)11 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 KieBase (org.kie.api.KieBase)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)6 Properties (java.util.Properties)3 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)3 TimerService (org.drools.core.time.TimerService)3 Trigger (org.drools.core.time.Trigger)3 ArrayList (java.util.ArrayList)2 WorkingMemory (org.drools.core.WorkingMemory)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)2 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)2 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)2 Ignore (org.junit.Ignore)2 KieHelper (org.kie.internal.utils.KieHelper)2 IOException (java.io.IOException)1