Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Updates that allow the serialization test to complete successfully, w…
Browse files Browse the repository at this point in the history
…ithout resorting to any "workarounds"
  • Loading branch information
lanceleverich committed Jul 8, 2015
1 parent c134188 commit bbb6b09
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 50 deletions.
Expand Up @@ -16,7 +16,8 @@

package org.drools.expectations;


import static org.kie.api.runtime.EnvironmentName.GLOBALS;
import static org.kie.api.runtime.EnvironmentName.CALENDARS;
import it.unibo.deis.lia.org.drools.expectations.ECEHelper;
import it.unibo.deis.lia.org.drools.expectations.model.Expectation;
import it.unibo.deis.lia.org.drools.expectations.model.ExpectationContext;
Expand All @@ -26,6 +27,8 @@
import org.drools.core.ClockType;
import org.drools.core.WorkingMemory;
import org.drools.core.common.DefaultFactHandle;
import org.drools.core.common.DroolsObjectInputStream;
import org.drools.core.common.DroolsObjectOutputStream;
import org.drools.core.common.EventFactHandle;
import org.drools.core.marshalling.impl.ClassObjectMarshallingStrategyAcceptor;
import org.drools.core.marshalling.impl.IdentityPlaceholderResolverStrategy;
Expand All @@ -39,12 +42,11 @@
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.definition.KiePackage;
import org.kie.api.definition.type.FactType;
import org.kie.api.marshalling.KieMarshallers;
import org.kie.api.marshalling.Marshaller;
import org.kie.api.marshalling.ObjectMarshallingStrategy;
import org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
import org.kie.api.runtime.*;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.api.time.SessionClock;
import org.kie.internal.KnowledgeBase;
Expand All @@ -69,7 +71,9 @@
public class ExpTestBase {

protected SessionClock clock;
private Marshaller marshaller;
private KieSessionConfiguration sessionConfig;
private Environment sessionEnvironment;
private KieBase kieBase;

public class TestConsequenceHandler implements ConsequenceExceptionHandler {

Expand All @@ -85,34 +89,48 @@ public void handleException(Activation activation, WorkingMemory workingMemory,
}

public KieSession buildKnowledgeSession( byte[] source ) {
KieSession kieSession = new ECEHelper().addECEContent( new String( source ) ).newECESession( false );
marshaller = KieServices.Factory.get().getMarshallers().newMarshaller(kieSession.getKieBase());
KieSession kieSession = new ECEHelper().addECEContent(new String(source)).newECESession(false);
kieBase = kieSession.getKieBase();
sessionConfig = kieSession.getSessionConfiguration();
sessionEnvironment = kieSession.getEnvironment();
clock = kieSession.getSessionClock();
assertEquals( 0, kieSession.getObjects().size() );
return kieSession;
}

private Marshaller createMarshaller(KieBase kBase) {
KieMarshallers marshallers = KieServices.Factory.get().getMarshallers();
ObjectMarshallingStrategyAcceptor acceptor = marshallers.newClassFilterAcceptor(new String[]{"*.*"});
ObjectMarshallingStrategy strategy = marshallers.newSerializeMarshallingStrategy(acceptor);
return marshallers.newMarshaller(kBase,new ObjectMarshallingStrategy[] {strategy});
}

public ByteArrayOutputStream marshallSession(KieSession session) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

KieBase kBase = session.getKieBase();
try {
KieServices.Factory
.get()
.getMarshallers()
.newMarshaller(session.getKieBase()).marshall(baos, session);
DroolsObjectOutputStream stream = new DroolsObjectOutputStream(baos);
stream.writeObject(kBase);
stream.writeObject(session.getGlobals());
stream.writeObject(session.getCalendars());
createMarshaller(kBase).marshall(stream, session);
} catch (IOException e) {
e.printStackTrace();
}
return baos;
}

public KieSession unmarshallSession(KieBase kbase, ByteArrayInputStream bais) {
public KieSession unmarshallSession(ByteArrayInputStream bais) {
KieSession kSession = null;
try {
kSession = KieServices.Factory
.get()
.getMarshallers()
.newMarshaller(kbase).unmarshall(bais);
DroolsObjectInputStream stream = new DroolsObjectInputStream(bais);
KieBase base = (KieBase)stream.readObject();
Globals globals = (Globals)stream.readObject();
Calendars calendars = (Calendars)stream.readObject();
sessionEnvironment.set(GLOBALS, globals);
sessionEnvironment.set(CALENDARS, calendars);
kSession = createMarshaller(base).unmarshall(stream, sessionConfig, sessionEnvironment);
clock = kSession.getSessionClock();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
Expand All @@ -138,6 +156,7 @@ public static String reportWMObjects( KieSession session ) {

}
String ans = " ================ WM " + session.getObjects().size() + " ==============\n";
ans += "Session Clock Current Time: "+session.getSessionClock().getCurrentTime()+"\n";
while (! queue.isEmpty())
ans += queue.poll();
ans += " ================ END WM ===========\n";
Expand Down Expand Up @@ -219,7 +238,6 @@ protected Object newReading(KieSession kSession, String probe, Double value ) {
return null;
}


protected int countMeta( String type, KieSession ksession ) {
Class klass = null;
try {
Expand Down
Expand Up @@ -39,7 +39,6 @@
public class ExpectationTest extends ExpTestBase {

@Test
@Ignore
public void testMarshall() {

String src = "" +
Expand Down Expand Up @@ -92,52 +91,52 @@ public void testMarshall() {

sleep( 150 );

System.out.println( reportWMObjects( kSession ) );

kSession.insert( newMessage( kSession, "Peter", "John", "Hello back", "Y3" ) );
kSession.fireAllRules();

System.out.println( reportWMObjects( kSession ) );

assertFalse( list.contains( "F1Y3" ) );
assertEquals( 2, countMeta( Fulfill.class.getName(), kSession ) );
assertEquals( 1, countMeta( Expectation.class.getName(), kSession ) );
assertEquals( 0, countMeta( Pending.class.getName(), kSession ) );
assertEquals( 0, countMeta( Viol.class.getName(), kSession ) );
ByteArrayOutputStream baos = this.marshallSession(kSession);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
kSession.dispose();
KieSession kieSession = this.unmarshallSession(bais);

sleep( 10 );
// Note: We have to re-establish our connection with the list that is in the reconstituted session
list = (List)kieSession.getGlobal("list");
System.out.println( reportWMObjects(kieSession) );

kSession.insert( newMessage( kSession, "John", "Peter", "Hello", "X2" ) );
kieSession.insert( newMessage( kieSession, "Peter", "John", "Hello back", "Y3" ) );

System.out.println( reportWMObjects( kSession ) );
System.out.println( reportWMObjects( kieSession ) );

kSession.fireAllRules();
assertFalse( list.contains( "F1Y3" ) );
assertEquals( 2, countMeta( Fulfill.class.getName(), kieSession ) );
assertEquals( 1, countMeta( Expectation.class.getName(), kieSession ) );
assertEquals(0, countMeta(Pending.class.getName(), kieSession));
assertEquals(0, countMeta(Viol.class.getName(), kieSession));

System.out.println( reportWMObjects( kSession ) );
sleep(10);

assertEquals( Arrays.asList( "F1Y", "F1Y2" ), list );
assertEquals( 2, countMeta( Fulfill.class.getName(), kSession ) );
assertEquals( 1, countMeta( Pending.class.getName(), kSession ) );
assertEquals( 0, countMeta( Viol.class.getName(), kSession ) );

ByteArrayOutputStream baos = this.marshallSession(kSession);
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
kSession.dispose();
kieSession.insert(newMessage(kieSession, "John", "Peter", "Hello", "X2"));
kieSession.fireAllRules();
System.out.println(reportWMObjects(kieSession));

kSession = this.unmarshallSession(kSession.getKieBase(),bais);
sleep( 110 );
assertEquals(Arrays.asList("F1Y", "F1Y2"), list);
assertEquals(2, countMeta(Fulfill.class.getName(), kieSession));
assertEquals(1, countMeta(Pending.class.getName(), kieSession));
assertEquals(0, countMeta(Viol.class.getName(), kieSession));

kSession.addEventListener(new org.kie.api.event.rule.DebugAgendaEventListener());
kSession.fireAllRules();
System.out.println(reportWMObjects(kSession));
sleep(110);

// assertTrue( list.contains( "V1" ) );
kieSession.fireAllRules();
System.out.println(reportWMObjects(kieSession));

assertTrue(list.contains("V1"));

assertEquals( 2, countMeta( Fulfill.class.getName(), kSession ) );
assertEquals( 0, countMeta( Pending.class.getName(), kSession ) );
assertEquals( 1, countMeta( Viol.class.getName(), kSession ) );
assertEquals( 2 ,countMeta( Expectation.class.getName(), kSession ) );

assertEquals(2, countMeta(Fulfill.class.getName(), kieSession));
assertEquals(0, countMeta(Pending.class.getName(), kieSession));
assertEquals(1, countMeta(Viol.class.getName(), kieSession));
assertEquals(2, countMeta(Expectation.class.getName(), kieSession));

}

Expand Down

0 comments on commit bbb6b09

Please sign in to comment.