Search in sources :

Example 1 with PersistenceContextManager

use of org.drools.persistence.api.PersistenceContextManager in project drools by kiegroup.

the class JPAWorkItemManager method getPersistenceContext.

private PersistenceContext getPersistenceContext() {
    Environment env = this.kruntime.getEnvironment();
    PersistenceContext context = ((PersistenceContextManager) env.get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER)).getCommandScopedPersistenceContext();
    return context;
}
Also used : Environment(org.kie.api.runtime.Environment) PersistenceContext(org.drools.persistence.api.PersistenceContext) PersistenceContextManager(org.drools.persistence.api.PersistenceContextManager)

Example 2 with PersistenceContextManager

use of org.drools.persistence.api.PersistenceContextManager in project drools by kiegroup.

the class ReloadSessionTest method reloadKnowledgeSessionTest.

@Test
public void reloadKnowledgeSessionTest() {
    // Initialize drools environment stuff
    Environment env = createEnvironment();
    KieBase kbase = initializeKnowledgeBase(simpleRule);
    StatefulKnowledgeSession commandKSession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    assertTrue("There should be NO facts present in a new (empty) knowledge session.", commandKSession.getFactHandles().isEmpty());
    // Persist a facthandle to the database
    Integer integerFact = (new Random()).nextInt(Integer.MAX_VALUE - 1) + 1;
    commandKSession.insert(integerFact);
    // At this point in the code, the fact has been persisted to the database
    // (within a transaction via the PersistableRunner)
    Collection<FactHandle> factHandles = commandKSession.getFactHandles();
    assertTrue("At least one fact should have been inserted by the ksession.insert() method above.", !factHandles.isEmpty());
    FactHandle origFactHandle = factHandles.iterator().next();
    assertTrue("The stored fact should contain the same number as the value inserted (but does not).", Integer.parseInt(((DefaultFactHandle) origFactHandle).getObject().toString()) == integerFact.intValue());
    // Save the sessionInfo id in order to retrieve it later
    long sessionInfoId = commandKSession.getIdentifier();
    // Clean up the session, environment, etc.
    PersistenceContextManager pcm = (PersistenceContextManager) commandKSession.getEnvironment().get(EnvironmentName.PERSISTENCE_CONTEXT_MANAGER);
    commandKSession.dispose();
    pcm.dispose();
    emf.close();
    // Reload session from the database
    emf = Persistence.createEntityManagerFactory(DROOLS_PERSISTENCE_UNIT_NAME);
    context.put(ENTITY_MANAGER_FACTORY, emf);
    env = createEnvironment();
    // Re-initialize the knowledge session:
    StatefulKnowledgeSession newCommandKSession = JPAKnowledgeService.loadStatefulKnowledgeSession(sessionInfoId, kbase, null, env);
    // Test that the session has been successfully reinitialized
    factHandles = newCommandKSession.getFactHandles();
    assertTrue("At least one fact should have been persisted by the ksession.insert above.", !factHandles.isEmpty() && factHandles.size() == 1);
    FactHandle retrievedFactHandle = factHandles.iterator().next();
    assertTrue("If the retrieved and original FactHandle object are the same, then the knowledge session has NOT been reloaded!", origFactHandle != retrievedFactHandle);
    assertTrue("The retrieved fact should contain the same info as the original (but does not).", Integer.parseInt(((DefaultFactHandle) retrievedFactHandle).getObject().toString()) == integerFact.intValue());
    // Test to see if the (retrieved) facts can be processed
    ArrayList<Object> list = new ArrayList<Object>();
    newCommandKSession.setGlobal("list", list);
    newCommandKSession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) PersistenceContextManager(org.drools.persistence.api.PersistenceContextManager) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Random(java.util.Random) KieBase(org.kie.api.KieBase) Environment(org.kie.api.runtime.Environment) Test(org.junit.Test)

Aggregations

PersistenceContextManager (org.drools.persistence.api.PersistenceContextManager)2 Environment (org.kie.api.runtime.Environment)2 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)1 PersistenceContext (org.drools.persistence.api.PersistenceContext)1 Test (org.junit.Test)1 KieBase (org.kie.api.KieBase)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)1