Search in sources :

Example 31 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class FactHandleMarshallingTest method createWorkingMemory.

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

Example 32 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ProtobufInputMarshaller method readSession.

public static ReadSessionResult readSession(ProtobufMarshallerReaderContext context, int id, Environment environment, SessionConfiguration config, KieSessionInitializer initializer) throws IOException, ClassNotFoundException {
    ProtobufMessages.KnowledgeSession _session = loadAndParseSession(context);
    StatefulKnowledgeSessionImpl session = createAndInitializeSession(context, id, environment, config, _session);
    // Initialize the session before unmarshalling data
    if (initializer != null) {
        initializer.init(session);
    }
    return new ReadSessionResult(readSession(_session, session, session.getAgenda(), context), _session);
}
Also used : StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl)

Example 33 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class ProtobufInputMarshaller method createAndInitializeSession.

private static StatefulKnowledgeSessionImpl createAndInitializeSession(ProtobufMarshallerReaderContext context, int id, Environment environment, SessionConfiguration config, ProtobufMessages.KnowledgeSession _session) throws IOException {
    FactHandleFactory handleFactory = context.getKnowledgeBase().newFactHandleFactory(_session.getRuleData().getLastId(), _session.getRuleData().getLastRecency());
    InternalAgenda agenda = RuntimeComponentFactory.get().getAgendaFactory().createAgenda(context.getKnowledgeBase(), false);
    StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) PhreakWorkingMemoryFactory.getInstance().createWorkingMemory(id, context.getKnowledgeBase(), handleFactory, // pCTx starts at 1, as InitialFact is 0
    1, config, agenda, environment);
    agenda.setWorkingMemory(session);
    readAgenda(context, _session.getRuleData(), agenda);
    return session;
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) FactHandleFactory(org.drools.core.spi.FactHandleFactory)

Example 34 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class QueryTest method testDroolsQueryCleanup.

@Test
public void testDroolsQueryCleanup() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_QueryMemoryLeak.drl");
    KieSession ksession = kbase.newKieSession();
    String workerId = "B1234";
    Worker worker = new Worker();
    worker.setId(workerId);
    FactHandle handle = ksession.insert(worker);
    ksession.fireAllRules();
    assertNotNull(handle);
    Object retractedWorker = null;
    for (int i = 0; i < 100; i++) {
        retractedWorker = (Object) ksession.getQueryResults("getWorker", new Object[] { workerId });
    }
    assertNotNull(retractedWorker);
    StatefulKnowledgeSessionImpl sessionImpl = (StatefulKnowledgeSessionImpl) ksession;
    Collection<EntryPointNode> entryPointNodes = sessionImpl.getKnowledgeBase().getRete().getEntryPointNodes().values();
    EntryPointNode defaultEntryPointNode = null;
    for (EntryPointNode epNode : entryPointNodes) {
        if (epNode.getEntryPoint().getEntryPointId().equals("DEFAULT")) {
            defaultEntryPointNode = epNode;
            break;
        }
    }
    assertNotNull(defaultEntryPointNode);
    Map<ObjectType, ObjectTypeNode> obnodes = defaultEntryPointNode.getObjectTypeNodes();
    ObjectType key = new ClassObjectType(DroolsQuery.class);
    ObjectTypeNode droolsQueryNode = obnodes.get(key);
    Iterator<InternalFactHandle> it = ((ObjectTypeNodeMemory) sessionImpl.getNodeMemory(droolsQueryNode)).iterator();
    assertFalse(it.hasNext());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) ObjectTypeNodeMemory(org.drools.core.reteoo.ObjectTypeNode.ObjectTypeNodeMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KieBase(org.kie.api.KieBase) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) Worker(org.drools.mvel.compiler.Worker) KieSession(org.kie.api.runtime.KieSession) DomainObject(org.drools.mvel.compiler.DomainObject) InsertedObject(org.drools.mvel.compiler.InsertedObject) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 35 with StatefulKnowledgeSessionImpl

use of org.drools.kiesession.session.StatefulKnowledgeSessionImpl in project drools by kiegroup.

the class BayesRuntimeTest method testBayesRuntimeManager.

@Test
public void testBayesRuntimeManager() throws Exception {
    KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
    kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
    InternalKnowledgeBase kbase = getKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newKieSession();
    BayesRuntime bayesRuntime = ksession.getKieRuntime(BayesRuntime.class);
    BayesInstance<Garden> instance = bayesRuntime.createInstance(Garden.class);
    assertNotNull(instance);
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) BayesRuntime(org.drools.beliefs.bayes.runtime.BayesRuntime) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)66 Test (org.junit.Test)57 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)34 ArrayList (java.util.ArrayList)15 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)15 KieBase (org.kie.api.KieBase)15 ClassObjectType (org.drools.core.base.ClassObjectType)14 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)14 BetaMemory (org.drools.core.reteoo.BetaMemory)14 List (java.util.List)13 InternalFactHandle (org.drools.core.common.InternalFactHandle)13 KieSession (org.kie.api.runtime.KieSession)12 FactHandle (org.kie.api.runtime.rule.FactHandle)11 MockObjectSink (org.drools.core.reteoo.MockObjectSink)10 BuildContext (org.drools.core.reteoo.builder.BuildContext)10 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)8 HashMap (java.util.HashMap)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 PathMemory (org.drools.core.reteoo.PathMemory)7 Rete (org.drools.core.reteoo.Rete)7