[jboss-svn-commits] JBL Code SVN: r25868 - in labs/jbossrules/trunk: drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance and 4 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Mar 28 09:43:30 EDT 2009
Author: KrisVerlaenen
Date: 2009-03-28 09:43:30 -0400 (Sat, 28 Mar 2009)
New Revision: 25868
Added:
labs/jbossrules/trunk/drools-persistence-jpa/src/test/resources/VariablesProcess.rf
Modified:
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/AbstractProcessInstanceMarshaller.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/InputMarshaller.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/OutputMarshaller.java
labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/RuleFlowProcessInstanceMarshaller.java
labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java
labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SessionInfo.java
labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SingleSessionCommandService.java
labs/jbossrules/trunk/drools-persistence-jpa/src/test/java/org/drools/persistence/session/PersistentStatefulSessionTest.java
labs/jbossrules/trunk/drools-process/drools-osworkflow/src/main/java/org/drools/osworkflow/persistence/marshaller/OSWorkflowProcessInstanceMarshaller.java
Log:
JBRULES-2042: Problems with persistence of processes
- fixed various persistence problems
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/AbstractProcessInstanceMarshaller.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/AbstractProcessInstanceMarshaller.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/AbstractProcessInstanceMarshaller.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -12,11 +12,12 @@
import org.drools.common.InternalRuleBase;
import org.drools.common.InternalWorkingMemory;
+import org.drools.process.core.Context;
+import org.drools.process.core.Process;
import org.drools.process.core.context.swimlane.SwimlaneContext;
import org.drools.process.core.context.variable.VariableScope;
import org.drools.process.instance.context.swimlane.SwimlaneContextInstance;
import org.drools.process.instance.context.variable.VariableScopeInstance;
-import org.drools.process.instance.impl.ProcessInstanceImpl;
import org.drools.ruleflow.instance.RuleFlowProcessInstance;
import org.drools.runtime.process.NodeInstance;
import org.drools.runtime.process.NodeInstanceContainer;
@@ -34,7 +35,13 @@
import org.drools.workflow.instance.node.TimerNodeInstance;
import org.drools.workflow.instance.node.WorkItemNodeInstance;
-/* Author: mfossati, salaboy */
+/**
+ * Default implementation of a process instance marshaller.
+ *
+ * @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
+ * @author mfossati
+ * @author salaboy
+ */
public abstract class AbstractProcessInstanceMarshaller implements
ProcessInstanceMarshaller {
@@ -198,70 +205,72 @@
}
stream.writeShort( PersisterEnums.END );
} else {
- // TODO ForEachNodeInstance
- // TODO timer manager
throw new IllegalArgumentException( "Unknown node instance type: " + nodeInstance );
}
}
// Input methods
- public ProcessInstance readProcessInstance(MarshallerReaderContext context)
- throws IOException {
+ public ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
+ InternalRuleBase ruleBase = context.ruleBase;
+ InternalWorkingMemory wm = context.wm;
- RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
- processInstance.setId( stream.readLong() );
- String processId = stream.readUTF();
- processInstance.setProcessId( processId );
- if ( ruleBase != null ) {
- processInstance.setProcess( ruleBase.getProcess( processId ) );
- }
- processInstance.setState( stream.readInt() );
- long nodeInstanceCounter = stream.readLong();
- processInstance.setWorkingMemory( wm );
+ WorkflowProcessInstanceImpl processInstance = createProcessInstance();
+ processInstance.setId(stream.readLong());
+ String processId = stream.readUTF();
+ processInstance.setProcessId(processId);
+ Process process = ruleBase.getProcess(processId);
+ if (ruleBase != null) {
+ processInstance.setProcess(process);
+ }
+ processInstance.setState(stream.readInt());
+ long nodeInstanceCounter = stream.readLong();
+ processInstance.setWorkingMemory(wm);
- int nbVariables = stream.readInt();
- if ( nbVariables > 0 ) {
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance( VariableScope.VARIABLE_SCOPE );
- for ( int i = 0; i < nbVariables; i++ ) {
- String name = stream.readUTF();
- try {
- Object value = stream.readObject();
- variableScopeInstance.setVariable( name,
- value );
- } catch ( ClassNotFoundException e ) {
- throw new IllegalArgumentException( "Could not reload variable " + name );
- }
- }
- }
+ int nbVariables = stream.readInt();
+ if (nbVariables > 0) {
+ Context variableScope = process
+ .getDefaultContext(VariableScope.VARIABLE_SCOPE);
+ VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
+ processInstance.getContextInstance(variableScope);
+ for (int i = 0; i < nbVariables; i++) {
+ String name = stream.readUTF();
+ try {
+ Object value = stream.readObject();
+ variableScopeInstance.setVariable(name, value);
+ } catch (ClassNotFoundException e) {
+ throw new IllegalArgumentException(
+ "Could not reload variable " + name);
+ }
+ }
+ }
- int nbSwimlanes = stream.readInt();
- if ( nbSwimlanes > 0 ) {
- SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance( SwimlaneContext.SWIMLANE_SCOPE );
- for ( int i = 0; i < nbSwimlanes; i++ ) {
- String name = stream.readUTF();
- String value = stream.readUTF();
- swimlaneContextInstance.setActorId( name,
- value );
- }
- }
+ int nbSwimlanes = stream.readInt();
+ if (nbSwimlanes > 0) {
+ Context swimlaneContext = process.getDefaultContext(SwimlaneContext.SWIMLANE_SCOPE);
+ SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance)
+ processInstance.getContextInstance(swimlaneContext);
+ for (int i = 0; i < nbSwimlanes; i++) {
+ String name = stream.readUTF();
+ String value = stream.readUTF();
+ swimlaneContextInstance.setActorId(name, value);
+ }
+ }
- while ( stream.readShort() == PersisterEnums.NODE_INSTANCE ) {
- readNodeInstance( context,
- processInstance,
- processInstance );
- }
+ while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
+ readNodeInstance(context, processInstance, processInstance);
+ }
- processInstance.internalSetNodeInstanceCounter( nodeInstanceCounter );
- if ( wm != null ) {
- processInstance.reconnect();
- }
- return processInstance;
+ processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
+ if (wm != null) {
+ processInstance.reconnect();
+ }
+ return processInstance;
}
-
+
+ protected abstract WorkflowProcessInstanceImpl createProcessInstance();
+
public NodeInstance readNodeInstance(MarshallerReaderContext context,
NodeInstanceContainer nodeInstanceContainer,
WorkflowProcessInstance processInstance) throws IOException {
@@ -277,6 +286,42 @@
nodeInstance.setProcessInstance(processInstance);
nodeInstance.setId(id);
+ switch ( nodeType ) {
+ case PersisterEnums.COMPOSITE_NODE_INSTANCE :
+ int nbVariables = stream.readInt();
+ if ( nbVariables > 0 ) {
+ Context variableScope = ((org.drools.process.core.Process)
+ processInstance.getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
+ VariableScopeInstance variableScopeInstance = (VariableScopeInstance)
+ ((CompositeContextNodeInstance) nodeInstance).getContextInstance(variableScope);
+ for ( int i = 0; i < nbVariables; i++ ) {
+ String name = stream.readUTF();
+ try {
+ Object value = stream.readObject();
+ variableScopeInstance.setVariable( name,
+ value );
+ } catch ( ClassNotFoundException e ) {
+ throw new IllegalArgumentException( "Could not reload variable " + name );
+ }
+ }
+ }
+ while ( stream.readShort() == PersisterEnums.NODE_INSTANCE ) {
+ readNodeInstance( context,
+ (CompositeContextNodeInstance) nodeInstance,
+ processInstance );
+ }
+ break;
+ case PersisterEnums.FOR_EACH_NODE_INSTANCE :
+ while ( stream.readShort() == PersisterEnums.NODE_INSTANCE ) {
+ readNodeInstance( context,
+ (ForEachNodeInstance) nodeInstance,
+ processInstance );
+ }
+ break;
+ default :
+ // do nothing
+ }
+
return nodeInstance;
}
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/InputMarshaller.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/InputMarshaller.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/InputMarshaller.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -790,166 +790,11 @@
public static void readProcessInstances(MarshallerReaderContext context) throws IOException {
ObjectInputStream stream = context.stream;
while ( stream.readShort() == PersisterEnums.PROCESS_INSTANCE ) {
- readProcessInstance( context );
+ String processType = stream.readUTF();
+ ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType).readProcessInstance(context);
}
}
- public static ProcessInstance readProcessInstance(MarshallerReaderContext context) throws IOException {
- ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
-
- RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
- processInstance.setId( stream.readLong() );
- String processId = stream.readUTF();
- processInstance.setProcessId( processId );
- if ( ruleBase != null ) {
- processInstance.setProcess( ruleBase.getProcess( processId ) );
- }
- processInstance.setState( stream.readInt() );
- long nodeInstanceCounter = stream.readLong();
- processInstance.setWorkingMemory( wm );
-
- int nbVariables = stream.readInt();
- if ( nbVariables > 0 ) {
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance( VariableScope.VARIABLE_SCOPE );
- for ( int i = 0; i < nbVariables; i++ ) {
- String name = stream.readUTF();
- try {
- Object value = stream.readObject();
- variableScopeInstance.setVariable( name,
- value );
- } catch ( ClassNotFoundException e ) {
- throw new IllegalArgumentException( "Could not reload variable " + name );
- }
- }
- }
-
- int nbSwimlanes = stream.readInt();
- if ( nbSwimlanes > 0 ) {
- SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance( SwimlaneContext.SWIMLANE_SCOPE );
- for ( int i = 0; i < nbSwimlanes; i++ ) {
- String name = stream.readUTF();
- String value = stream.readUTF();
- swimlaneContextInstance.setActorId( name,
- value );
- }
- }
-
- while ( stream.readShort() == PersisterEnums.NODE_INSTANCE ) {
- readNodeInstance( context,
- processInstance,
- processInstance );
- }
-
- processInstance.internalSetNodeInstanceCounter( nodeInstanceCounter );
- if ( wm != null ) {
- processInstance.reconnect();
- }
- return processInstance;
- }
-
- public static NodeInstance readNodeInstance(MarshallerReaderContext context,
- NodeInstanceContainer nodeInstanceContainer,
- RuleFlowProcessInstance processInstance) throws IOException {
- ObjectInputStream stream = context.stream;
- NodeInstanceImpl nodeInstance = null;
- long id = stream.readLong();
- long nodeId = stream.readLong();
- int nodeType = stream.readShort();
- switch ( nodeType ) {
- case PersisterEnums.RULE_SET_NODE_INSTANCE :
- nodeInstance = new RuleSetNodeInstance();
- break;
- case PersisterEnums.HUMAN_TASK_NODE_INSTANCE :
- nodeInstance = new HumanTaskNodeInstance();
- ((HumanTaskNodeInstance) nodeInstance).internalSetWorkItemId( stream.readLong() );
- break;
- case PersisterEnums.WORK_ITEM_NODE_INSTANCE :
- nodeInstance = new WorkItemNodeInstance();
- ((WorkItemNodeInstance) nodeInstance).internalSetWorkItemId( stream.readLong() );
- break;
- case PersisterEnums.SUB_PROCESS_NODE_INSTANCE :
- nodeInstance = new SubProcessNodeInstance();
- ((SubProcessNodeInstance) nodeInstance).internalSetProcessInstanceId( stream.readLong() );
- break;
- case PersisterEnums.MILESTONE_NODE_INSTANCE :
- nodeInstance = new MilestoneNodeInstance();
- int nbTimerInstances = stream.readInt();
- if (nbTimerInstances > 0) {
- List<Long> timerInstances = new ArrayList<Long>();
- for (int i = 0; i < nbTimerInstances; i++) {
- timerInstances.add(stream.readLong());
- }
- ((MilestoneNodeInstance) nodeInstance).internalSetTimerInstances(timerInstances);
- }
- break;
- case PersisterEnums.TIMER_NODE_INSTANCE :
- nodeInstance = new TimerNodeInstance();
- ((TimerNodeInstance) nodeInstance).internalSetTimerId( stream.readLong() );
- break;
- case PersisterEnums.JOIN_NODE_INSTANCE :
- nodeInstance = new JoinInstance();
- int number = stream.readInt();
- if ( number > 0 ) {
- Map<Long, Integer> triggers = new HashMap<Long, Integer>();
- for ( int i = 0; i < number; i++ ) {
- long l = stream.readLong();
- int count = stream.readInt();
- triggers.put( l,
- count );
- }
- ((JoinInstance) nodeInstance).internalSetTriggers( triggers );
- }
- break;
- case PersisterEnums.COMPOSITE_NODE_INSTANCE :
- nodeInstance = new CompositeContextNodeInstance();
- break;
- case PersisterEnums.FOR_EACH_NODE_INSTANCE :
- nodeInstance = new ForEachNodeInstance();
- break;
- default :
- throw new IllegalArgumentException( "Unknown node type: " + nodeType );
- }
- nodeInstance.setNodeId( nodeId );
- nodeInstance.setNodeInstanceContainer( nodeInstanceContainer );
- nodeInstance.setProcessInstance( processInstance );
- nodeInstance.setId( id );
- switch ( nodeType ) {
- case PersisterEnums.COMPOSITE_NODE_INSTANCE :
- int nbVariables = stream.readInt();
- if ( nbVariables > 0 ) {
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((CompositeContextNodeInstance) nodeInstance).getContextInstance( VariableScope.VARIABLE_SCOPE );
- for ( int i = 0; i < nbVariables; i++ ) {
- String name = stream.readUTF();
- try {
- Object value = stream.readObject();
- variableScopeInstance.setVariable( name,
- value );
- } catch ( ClassNotFoundException e ) {
- throw new IllegalArgumentException( "Could not reload variable " + name );
- }
- }
- }
- while ( stream.readShort() == PersisterEnums.NODE_INSTANCE ) {
- readNodeInstance( context,
- (CompositeContextNodeInstance) nodeInstance,
- processInstance );
- }
- break;
- case PersisterEnums.FOR_EACH_NODE_INSTANCE :
- while ( stream.readShort() == PersisterEnums.NODE_INSTANCE ) {
- readNodeInstance( context,
- (ForEachNodeInstance) nodeInstance,
- processInstance );
- }
- break;
- default :
- // do nothing
- }
- return nodeInstance;
- }
-
public static void readWorkItems(MarshallerReaderContext context) throws IOException {
InternalWorkingMemory wm = context.wm;
ObjectInputStream stream = context.stream;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/OutputMarshaller.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/OutputMarshaller.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/OutputMarshaller.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -28,11 +28,8 @@
import org.drools.common.RuleFlowGroupImpl;
import org.drools.common.WorkingMemoryAction;
import org.drools.marshalling.ObjectMarshallingStrategy;
-import org.drools.process.core.context.swimlane.SwimlaneContext;
-import org.drools.process.core.context.variable.VariableScope;
import org.drools.process.instance.WorkItemManager;
-import org.drools.process.instance.context.swimlane.SwimlaneContextInstance;
-import org.drools.process.instance.context.variable.VariableScopeInstance;
+import org.drools.process.instance.impl.ProcessInstanceImpl;
import org.drools.process.instance.timer.TimerInstance;
import org.drools.process.instance.timer.TimerManager;
import org.drools.reteoo.BetaNode;
@@ -49,8 +46,6 @@
import org.drools.reteoo.CollectNode.CollectMemory;
import org.drools.rule.EntryPoint;
import org.drools.rule.Rule;
-import org.drools.ruleflow.instance.RuleFlowProcessInstance;
-import org.drools.runtime.process.NodeInstance;
import org.drools.runtime.process.WorkItem;
import org.drools.spi.ActivationGroup;
import org.drools.spi.AgendaGroup;
@@ -58,15 +53,6 @@
import org.drools.spi.RuleFlowGroup;
import org.drools.util.ObjectHashMap;
import org.drools.util.ObjectHashSet;
-import org.drools.workflow.instance.node.CompositeContextNodeInstance;
-import org.drools.workflow.instance.node.ForEachNodeInstance;
-import org.drools.workflow.instance.node.HumanTaskNodeInstance;
-import org.drools.workflow.instance.node.JoinInstance;
-import org.drools.workflow.instance.node.MilestoneNodeInstance;
-import org.drools.workflow.instance.node.RuleSetNodeInstance;
-import org.drools.workflow.instance.node.SubProcessNodeInstance;
-import org.drools.workflow.instance.node.TimerNodeInstance;
-import org.drools.workflow.instance.node.WorkItemNodeInstance;
public class OutputMarshaller {
public static void writeSession(MarshallerWriteContext context) throws IOException {
@@ -744,166 +730,15 @@
}
} );
for ( org.drools.runtime.process.ProcessInstance processInstance : processInstances ) {
- stream.writeShort( PersisterEnums.PROCESS_INSTANCE );
- writeProcessInstance( context,
- (RuleFlowProcessInstance) processInstance );
+ stream.writeShort(PersisterEnums.PROCESS_INSTANCE);
+ String processType = ((ProcessInstanceImpl) processInstance).getProcess().getType();
+ stream.writeUTF(processType);
+ ProcessMarshallerRegistry.INSTANCE.getMarshaller(processType)
+ .writeProcessInstance(context, processInstance);
}
stream.writeShort( PersisterEnums.END );
}
- public static void writeProcessInstance(MarshallerWriteContext context,
- RuleFlowProcessInstance processInstance) throws IOException {
- ObjectOutputStream stream = context.stream;
- stream.writeLong( processInstance.getId() );
- stream.writeUTF( processInstance.getProcessId() );
- stream.writeInt( processInstance.getState() );
- stream.writeLong( processInstance.getNodeInstanceCounter() );
-
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance( VariableScope.VARIABLE_SCOPE );
- Map<String, Object> variables = variableScopeInstance.getVariables();
- List<String> keys = new ArrayList<String>( variables.keySet() );
- Collections.sort( keys,
- new Comparator<String>() {
- public int compare(String o1,
- String o2) {
- return o1.compareTo( o2 );
- }
- } );
- stream.writeInt( keys.size() );
- for ( String key : keys ) {
- stream.writeUTF( key );
- stream.writeObject( variables.get( key ) );
- }
-
- SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance.getContextInstance( SwimlaneContext.SWIMLANE_SCOPE );
- Map<String, String> swimlaneActors = swimlaneContextInstance.getSwimlaneActors();
- stream.writeInt( swimlaneActors.size() );
- for ( Map.Entry<String, String> entry : swimlaneActors.entrySet() ) {
- stream.writeUTF( entry.getKey() );
- stream.writeUTF( entry.getValue() );
- }
-
- List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>( processInstance.getNodeInstances() );
- Collections.sort( nodeInstances,
- new Comparator<NodeInstance>() {
- public int compare(NodeInstance o1,
- NodeInstance o2) {
- return (int) (o1.getId() - o2.getId());
- }
- } );
- for ( NodeInstance nodeInstance : nodeInstances ) {
- stream.writeShort( PersisterEnums.NODE_INSTANCE );
- writeNodeInstance( context,
- nodeInstance );
- }
- stream.writeShort( PersisterEnums.END );
- }
-
- public static void writeNodeInstance(MarshallerWriteContext context,
- NodeInstance nodeInstance) throws IOException {
- ObjectOutputStream stream = context.stream;
- stream.writeLong( nodeInstance.getId() );
- stream.writeLong( nodeInstance.getNodeId() );
- if ( nodeInstance instanceof RuleSetNodeInstance ) {
- stream.writeShort( PersisterEnums.RULE_SET_NODE_INSTANCE );
- } else if ( nodeInstance instanceof HumanTaskNodeInstance ) {
- stream.writeShort( PersisterEnums.HUMAN_TASK_NODE_INSTANCE );
- stream.writeLong( ((HumanTaskNodeInstance) nodeInstance).getWorkItem().getId() );
- } else if ( nodeInstance instanceof WorkItemNodeInstance ) {
- stream.writeShort( PersisterEnums.WORK_ITEM_NODE_INSTANCE );
- stream.writeLong( ((WorkItemNodeInstance) nodeInstance).getWorkItem().getId() );
- } else if ( nodeInstance instanceof SubProcessNodeInstance ) {
- stream.writeShort( PersisterEnums.SUB_PROCESS_NODE_INSTANCE );
- stream.writeLong( ((SubProcessNodeInstance) nodeInstance).getProcessInstanceId() );
- } else if ( nodeInstance instanceof MilestoneNodeInstance ) {
- stream.writeShort( PersisterEnums.MILESTONE_NODE_INSTANCE );
- List<Long> timerInstances =
- ((MilestoneNodeInstance) nodeInstance).getTimerInstances();
- if (timerInstances != null) {
- stream.writeInt(timerInstances.size());
- for (Long id: timerInstances) {
- stream.writeLong(id);
- }
- } else {
- stream.writeInt(0);
- }
- } else if ( nodeInstance instanceof TimerNodeInstance ) {
- stream.writeShort( PersisterEnums.TIMER_NODE_INSTANCE );
- stream.writeLong( ((TimerNodeInstance) nodeInstance).getTimerId() );
- } else if ( nodeInstance instanceof JoinInstance ) {
- stream.writeShort( PersisterEnums.JOIN_NODE_INSTANCE );
- Map<Long, Integer> triggers = ((JoinInstance) nodeInstance).getTriggers();
- stream.writeInt( triggers.size() );
- List<Long> keys = new ArrayList<Long>( triggers.keySet() );
- Collections.sort( keys,
- new Comparator<Long>() {
- public int compare(Long o1,
- Long o2) {
- return o1.compareTo( o2 );
- }
- } );
- for ( Long key : keys ) {
- stream.writeLong( key );
- stream.writeInt( triggers.get( key ) );
- }
- } else if ( nodeInstance instanceof CompositeContextNodeInstance ) {
- stream.writeShort( PersisterEnums.COMPOSITE_NODE_INSTANCE );
- CompositeContextNodeInstance compositeNodeInstance = (CompositeContextNodeInstance) nodeInstance;
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) compositeNodeInstance.getContextInstance( VariableScope.VARIABLE_SCOPE );
- Map<String, Object> variables = variableScopeInstance.getVariables();
- List<String> keys = new ArrayList<String>( variables.keySet() );
- Collections.sort( keys,
- new Comparator<String>() {
- public int compare(String o1,
- String o2) {
- return o1.compareTo( o2 );
- }
- } );
- stream.writeInt( keys.size() );
- for ( String key : keys ) {
- stream.writeUTF( key );
- stream.writeObject( variables.get( key ) );
- }
- List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>( compositeNodeInstance.getNodeInstances() );
- Collections.sort( nodeInstances,
- new Comparator<NodeInstance>() {
- public int compare(NodeInstance o1,
- NodeInstance o2) {
- return (int) (o1.getId() - o2.getId());
- }
- } );
- for ( NodeInstance subNodeInstance : nodeInstances ) {
- stream.writeShort( PersisterEnums.NODE_INSTANCE );
- writeNodeInstance( context,
- subNodeInstance );
- }
- stream.writeShort( PersisterEnums.END );
- } else if ( nodeInstance instanceof ForEachNodeInstance ) {
- stream.writeShort( PersisterEnums.FOR_EACH_NODE_INSTANCE );
- ForEachNodeInstance forEachNodeInstance = (ForEachNodeInstance) nodeInstance;
- List<NodeInstance> nodeInstances = new ArrayList<NodeInstance>( forEachNodeInstance.getNodeInstances() );
- Collections.sort( nodeInstances,
- new Comparator<NodeInstance>() {
- public int compare(NodeInstance o1,
- NodeInstance o2) {
- return (int) (o1.getId() - o2.getId());
- }
- } );
- for ( NodeInstance subNodeInstance : nodeInstances ) {
- if ( subNodeInstance instanceof CompositeContextNodeInstance ) {
- stream.writeShort( PersisterEnums.NODE_INSTANCE );
- writeNodeInstance( context,
- subNodeInstance );
- }
- }
- stream.writeShort( PersisterEnums.END );
- } else {
- // TODO ForEachNodeInstance
- // TODO timer manager
- throw new IllegalArgumentException( "Unknown node instance type: " + nodeInstance );
- }
- }
-
public static void writeWorkItems(MarshallerWriteContext context) throws IOException {
ObjectOutputStream stream = context.stream;
Modified: labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/RuleFlowProcessInstanceMarshaller.java
===================================================================
--- labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/RuleFlowProcessInstanceMarshaller.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-core/src/main/java/org/drools/marshalling/impl/RuleFlowProcessInstanceMarshaller.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -1,22 +1,12 @@
package org.drools.marshalling.impl;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-
-import org.drools.common.InternalRuleBase;
-import org.drools.common.InternalWorkingMemory;
-import org.drools.process.core.context.swimlane.SwimlaneContext;
-import org.drools.process.core.context.variable.VariableScope;
-import org.drools.process.instance.context.swimlane.SwimlaneContextInstance;
-import org.drools.process.instance.context.variable.VariableScopeInstance;
import org.drools.ruleflow.instance.RuleFlowProcessInstance;
-import org.drools.runtime.process.NodeInstance;
-import org.drools.runtime.process.ProcessInstance;
-import org.drools.workflow.instance.impl.NodeInstanceImpl;
+import org.drools.workflow.instance.impl.WorkflowProcessInstanceImpl;
/**
* Marshaller class for RuleFlowProcessInstances
*
+ * @author <a href="mailto:kris_verlaenen at hotmail.com">Kris Verlaenen</a>
* @author mfossati
*/
@@ -28,58 +18,8 @@
private RuleFlowProcessInstanceMarshaller() {
}
- public ProcessInstance readProcessInstance(MarshallerReaderContext context)
- throws IOException {
- ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
-
- RuleFlowProcessInstance processInstance = new RuleFlowProcessInstance();
- processInstance.setId(stream.readLong());
- String processId = stream.readUTF();
- processInstance.setProcessId(processId);
- if (ruleBase != null) {
- processInstance.setProcess(ruleBase.getProcess(processId));
- }
- processInstance.setState(stream.readInt());
- long nodeInstanceCounter = stream.readLong();
- processInstance.setWorkingMemory(wm);
-
- int nbVariables = stream.readInt();
- if (nbVariables > 0) {
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance
- .getContextInstance(VariableScope.VARIABLE_SCOPE);
- for (int i = 0; i < nbVariables; i++) {
- String name = stream.readUTF();
- try {
- Object value = stream.readObject();
- variableScopeInstance.setVariable(name, value);
- } catch (ClassNotFoundException e) {
- throw new IllegalArgumentException(
- "Could not reload variable " + name);
- }
- }
- }
-
- int nbSwimlanes = stream.readInt();
- if (nbSwimlanes > 0) {
- SwimlaneContextInstance swimlaneContextInstance = (SwimlaneContextInstance) processInstance
- .getContextInstance(SwimlaneContext.SWIMLANE_SCOPE);
- for (int i = 0; i < nbSwimlanes; i++) {
- String name = stream.readUTF();
- String value = stream.readUTF();
- swimlaneContextInstance.setActorId(name, value);
- }
- }
-
- while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
- readNodeInstance(context, processInstance, processInstance);
- }
-
- processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
- if (wm != null) {
- processInstance.reconnect();
- }
- return processInstance;
+ protected WorkflowProcessInstanceImpl createProcessInstance() {
+ return new RuleFlowProcessInstance();
}
+
}
Modified: labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java
===================================================================
--- labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/JPAProcessInstanceManager.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -43,7 +43,8 @@
return null;
}
processInstanceInfo.updateLastReadDate();
- ProcessInstance processInstance = (ProcessInstance) processInstanceInfo.getProcessInstance();
+ ProcessInstance processInstance = (ProcessInstance)
+ processInstanceInfo.getProcessInstance(workingMemory);
Process process = ((InternalRuleBase) workingMemory.getRuleBase()).getProcess( processInstance.getProcessId() );
if ( process == null ) {
throw new IllegalArgumentException( "Could not find process " + processInstance.getProcessId() );
Modified: labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
===================================================================
--- labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -20,15 +20,15 @@
import javax.persistence.Transient;
import javax.persistence.Version;
+import org.drools.WorkingMemory;
+import org.drools.common.InternalRuleBase;
+import org.drools.common.InternalWorkingMemory;
import org.drools.marshalling.impl.MarshallerReaderContext;
import org.drools.marshalling.impl.MarshallerWriteContext;
import org.drools.marshalling.impl.ProcessInstanceMarshaller;
import org.drools.marshalling.impl.ProcessMarshallerRegistry;
-import org.drools.marshalling.impl.RuleFlowProcessInstanceMarshaller;
import org.drools.process.instance.impl.ProcessInstanceImpl;
-import org.drools.ruleflow.instance.RuleFlowProcessInstance;
import org.drools.runtime.process.ProcessInstance;
-import org.drools.workflow.instance.impl.WorkflowProcessInstanceImpl;
import org.hibernate.annotations.CollectionOfElements;
@Entity
@@ -94,13 +94,14 @@
return state;
}
- public ProcessInstance getProcessInstance() {
+ public ProcessInstance getProcessInstance(WorkingMemory workingMemory) {
if (processInstance == null) {
try {
ByteArrayInputStream bais = new ByteArrayInputStream(
processInstanceByteArray);
MarshallerReaderContext context = new MarshallerReaderContext(
- bais, null, null, null);
+ bais, (InternalRuleBase) workingMemory.getRuleBase(), null, null);
+ context.wm = (InternalWorkingMemory) workingMemory;
ProcessInstanceMarshaller marshaller = getMarshallerFromContext(context);
processInstance = marshaller.readProcessInstance(context);
context.close();
Modified: labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SessionInfo.java
===================================================================
--- labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SessionInfo.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SessionInfo.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -86,7 +86,6 @@
@PreUpdate
public void update() {
// we always increase the last modification date for each action, so we know there will be an update
- System.out.println( "pre persist/update event" );
byte[] newByteArray = this.helper.getSnapshot();
if ( !Arrays.equals( newByteArray,
this.rulesByteArray ) ) {
Modified: labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SingleSessionCommandService.java
===================================================================
--- labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SingleSessionCommandService.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-persistence-jpa/src/main/java/org/drools/persistence/session/SingleSessionCommandService.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -152,7 +152,7 @@
if ( ut != null ) {
ut.rollback();
}
- throw new RuntimeException( "Could insert session data",
+ throw new RuntimeException( "Could not find session data for id " + sessionId,
t1 );
} catch ( Throwable t2 ) {
throw new RuntimeException( "Could not rollback transaction",
Modified: labs/jbossrules/trunk/drools-persistence-jpa/src/test/java/org/drools/persistence/session/PersistentStatefulSessionTest.java
===================================================================
--- labs/jbossrules/trunk/drools-persistence-jpa/src/test/java/org/drools/persistence/session/PersistentStatefulSessionTest.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-persistence-jpa/src/test/java/org/drools/persistence/session/PersistentStatefulSessionTest.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -1,14 +1,13 @@
package org.drools.persistence.session;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
-import java.util.Properties;
+import java.util.Map;
import javax.naming.InitialContext;
-import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
-import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import junit.framework.TestCase;
@@ -17,16 +16,14 @@
import org.drools.KnowledgeBaseFactory;
import org.drools.base.MapGlobalResolver;
import org.drools.builder.KnowledgeBuilder;
+import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
-import org.drools.impl.EnvironmentFactory;
-import org.drools.impl.EnvironmentImpl;
import org.drools.io.ResourceFactory;
import org.drools.io.impl.ClassPathResource;
import org.drools.persistence.jpa.JPAKnowledgeService;
import org.drools.runtime.Environment;
import org.drools.runtime.EnvironmentName;
-import org.drools.runtime.KnowledgeSessionConfiguration;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.drools.runtime.process.WorkItem;
@@ -322,5 +319,58 @@
processInstance = ksession.getProcessInstance( processInstance.getId() );
assertNull( processInstance );
}
+
+ public void testPersistenceVariables() {
+ KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
+ kbuilder.add( new ClassPathResource( "VariablesProcess.rf" ), ResourceType.DRF );
+ for (KnowledgeBuilderError error: kbuilder.getErrors()) {
+ System.out.println(error);
+ }
+ KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
+ kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
+ EntityManagerFactory emf = Persistence.createEntityManagerFactory( "org.drools.persistence.jpa" );
+ Environment env = KnowledgeBaseFactory.newEnvironment();
+ env.set( EnvironmentName.ENTITY_MANAGER_FACTORY, emf );
+
+ env.set( EnvironmentName.GLOBALS, new MapGlobalResolver() );
+
+ StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession( kbase, null, env );
+ int id = ksession.getId();
+
+ Map<String, Object> parameters = new HashMap<String, Object>();
+ parameters.put("name", "John Doe");
+ ProcessInstance processInstance = ksession.startProcess( "org.drools.test.TestProcess", parameters );
+
+ TestWorkItemHandler handler = TestWorkItemHandler.getInstance();
+ WorkItem workItem = handler.getWorkItem();
+ assertNotNull( workItem );
+ assertEquals( "John Doe", workItem.getParameter("name"));
+
+ ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( id, kbase, null, env );
+ processInstance = ksession.getProcessInstance( processInstance.getId() );
+ assertNotNull( processInstance );
+
+ ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( id, kbase, null, env );
+ ksession.getWorkItemManager().completeWorkItem( workItem.getId(), null );
+
+ workItem = handler.getWorkItem();
+ assertNotNull( workItem );
+ assertEquals( "John Doe", workItem.getParameter("text"));
+
+ ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( id, kbase, null, env );
+ processInstance = ksession.getProcessInstance( processInstance.getId() );
+ assertNotNull( processInstance );
+
+ ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( id, kbase, null, env );
+ ksession.getWorkItemManager().completeWorkItem( workItem.getId(), null );
+
+ workItem = handler.getWorkItem();
+ assertNull( workItem );
+
+ ksession = JPAKnowledgeService.loadStatefulKnowledgeSession( id, kbase, null, env );
+ processInstance = ksession.getProcessInstance( processInstance.getId() );
+ assertNull( processInstance );
+ }
+
}
Added: labs/jbossrules/trunk/drools-persistence-jpa/src/test/resources/VariablesProcess.rf
===================================================================
--- labs/jbossrules/trunk/drools-persistence-jpa/src/test/resources/VariablesProcess.rf (rev 0)
+++ labs/jbossrules/trunk/drools-persistence-jpa/src/test/resources/VariablesProcess.rf 2009-03-28 13:43:30 UTC (rev 25868)
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<process xmlns="http://drools.org/drools-5.0/process"
+ xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
+ xs:schemaLocation="http://drools.org/drools-5.0/process drools-processes-5.0.xsd"
+ type="RuleFlow" name="TestProcess" id="org.drools.test.TestProcess" package-name="org.drools.test" >
+
+ <header>
+ <variables>
+ <variable name="name" >
+ <type name="org.drools.process.core.datatype.impl.type.StringDataType" />
+ <value>xxx</value>
+ </variable>
+ </variables>
+ </header>
+
+ <nodes>
+ <start id="1" name="Start" />
+ <actionNode id="2" name="Action" >
+ <action type="expression" dialect="java" >System.out.println("Executing for " + name);</action>
+ </actionNode>
+ <workItem id="3" name="WorkItem1" >
+ <work name="MyWork" >
+ </work>
+ <mapping type="in" from="name" to="name" />
+ </workItem>
+ <actionNode id="4" name="Action" >
+ <action type="expression" dialect="java" >System.out.println("Executing for " + name);</action>
+ </actionNode>
+ <composite id="5" name="CompositeNode" >
+ <variables>
+ <variable name="text" >
+ <type name="org.drools.process.core.datatype.impl.type.StringDataType" />
+ </variable>
+ </variables>
+ <nodes>
+ <actionNode id="1" name="Action" >
+ <action type="expression" dialect="mvel" >kcontext.setVariable("text", name);</action>
+ </actionNode>
+ <workItem id="2" name="Log" x="130" y="46" width="80" height="40" >
+ <work name="MyWork" >
+ <parameter name="Message" >
+ <type name="org.drools.process.core.datatype.impl.type.StringDataType" />
+ </parameter>
+ </work>
+ <mapping type="in" from="text" to="text" />
+ </workItem>
+ <actionNode id="3" name="Action" x="129" y="109" >
+ <action type="expression" dialect="mvel" >System.out.println("Subprocess " + text);</action>
+ </actionNode>
+ </nodes>
+ <connections>
+ <connection from="1" to="2" />
+ <connection from="2" to="3" />
+ </connections>
+ <in-ports>
+ <in-port type="DROOLS_DEFAULT" nodeId="1" nodeInType="DROOLS_DEFAULT" />
+ </in-ports>
+ <out-ports>
+ <out-port type="DROOLS_DEFAULT" nodeId="3" nodeOutType="DROOLS_DEFAULT" />
+ </out-ports>
+ </composite>
+ <end id="6" name="End" />
+ </nodes>
+
+ <connections>
+ <connection from="1" to="2" />
+ <connection from="2" to="3" />
+ <connection from="3" to="4" />
+ <connection from="4" to="5" />
+ <connection from="5" to="6" />
+ </connections>
+
+</process>
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-process/drools-osworkflow/src/main/java/org/drools/osworkflow/persistence/marshaller/OSWorkflowProcessInstanceMarshaller.java
===================================================================
--- labs/jbossrules/trunk/drools-process/drools-osworkflow/src/main/java/org/drools/osworkflow/persistence/marshaller/OSWorkflowProcessInstanceMarshaller.java 2009-03-28 13:37:19 UTC (rev 25867)
+++ labs/jbossrules/trunk/drools-process/drools-osworkflow/src/main/java/org/drools/osworkflow/persistence/marshaller/OSWorkflowProcessInstanceMarshaller.java 2009-03-28 13:43:30 UTC (rev 25868)
@@ -4,20 +4,15 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
-import org.drools.common.InternalRuleBase;
-import org.drools.common.InternalWorkingMemory;
import org.drools.marshalling.impl.AbstractProcessInstanceMarshaller;
import org.drools.marshalling.impl.MarshallerReaderContext;
import org.drools.marshalling.impl.MarshallerWriteContext;
-import org.drools.marshalling.impl.PersisterEnums;
import org.drools.osworkflow.instance.OSWorkflowProcessInstance;
import org.drools.osworkflow.instance.node.StepNodeInstance;
-import org.drools.process.core.context.variable.VariableScope;
-import org.drools.process.instance.context.variable.VariableScopeInstance;
import org.drools.runtime.process.NodeInstance;
-import org.drools.runtime.process.ProcessInstance;
import org.drools.workflow.instance.WorkflowProcessInstance;
import org.drools.workflow.instance.impl.NodeInstanceImpl;
+import org.drools.workflow.instance.impl.WorkflowProcessInstanceImpl;
public class OSWorkflowProcessInstanceMarshaller extends
AbstractProcessInstanceMarshaller {
@@ -28,49 +23,8 @@
}
- @Override
- public ProcessInstance readProcessInstance(MarshallerReaderContext context)
- throws IOException {
- ObjectInputStream stream = context.stream;
- InternalRuleBase ruleBase = context.ruleBase;
- InternalWorkingMemory wm = context.wm;
-
- OSWorkflowProcessInstance processInstance = new OSWorkflowProcessInstance();
- processInstance.setId(stream.readLong());
- String processId = stream.readUTF();
- processInstance.setProcessId(processId);
- if (ruleBase != null) {
- processInstance.setProcess(ruleBase.getProcess(processId));
- }
- processInstance.setState(stream.readInt());
- long nodeInstanceCounter = stream.readLong();
- processInstance.setWorkingMemory(wm);
-
- int nbVariables = stream.readInt();
- if (nbVariables > 0) {
- VariableScopeInstance variableScopeInstance = (VariableScopeInstance) processInstance
- .getContextInstance(VariableScope.VARIABLE_SCOPE);
- for (int i = 0; i < nbVariables; i++) {
- String name = stream.readUTF();
- try {
- Object value = stream.readObject();
- variableScopeInstance.setVariable(name, value);
- } catch (ClassNotFoundException e) {
- throw new IllegalArgumentException(
- "Could not reload variable " + name);
- }
- }
- }
-
- while (stream.readShort() == PersisterEnums.NODE_INSTANCE) {
- readNodeInstance(context, processInstance, processInstance);
- }
-
- processInstance.internalSetNodeInstanceCounter(nodeInstanceCounter);
- if (wm != null) {
- processInstance.reconnect();
- }
- return processInstance;
+ public WorkflowProcessInstanceImpl createProcessInstance() {
+ return new OSWorkflowProcessInstance();
}
protected NodeInstanceImpl readNodeInstanceContent(int nodeType,
More information about the jboss-svn-commits
mailing list