[jboss-svn-commits] JBL Code SVN: r29153 - in labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src: test/resources and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Sep 2 15:42:37 EDT 2009


Author: salaboy21
Date: 2009-09-02 15:42:36 -0400 (Wed, 02 Sep 2009)
New Revision: 29153

Modified:
   labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
   labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf
Log:
support for different variables scopes - first approach

Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java	2009-09-02 17:11:24 UTC (rev 29152)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/main/java/org/drools/persistence/processinstance/ProcessInstanceInfo.java	2009-09-02 19:42:36 UTC (rev 29153)
@@ -1,5 +1,7 @@
 package org.drools.persistence.processinstance;
 
+import java.util.Collection;
+import org.drools.definition.process.Node;
 import org.drools.persistence.processinstance.variabletypes.VariableInstanceInfo;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -40,17 +42,31 @@
 import org.drools.WorkingMemory;
 import org.drools.common.InternalRuleBase;
 import org.drools.common.InternalWorkingMemory;
+
+
+import org.drools.definition.process.NodeContainer;
 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.process.core.ContextContainer;
 import org.drools.process.core.context.variable.VariableScope;
+import org.drools.process.core.impl.ContextContainerImpl;
+import org.drools.process.instance.ContextInstance;
+import org.drools.process.instance.ContextInstanceContainer;
 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.Environment;
 import org.drools.runtime.EnvironmentName;
+
 import org.drools.runtime.process.ProcessInstance;
+
+import org.drools.runtime.process.NodeInstance;
+
+import org.drools.runtime.process.NodeInstanceContainer;
+import org.drools.workflow.instance.impl.NodeInstanceImpl;
+import org.drools.workflow.instance.impl.WorkflowProcessInstanceImpl;
 import org.hibernate.annotations.CollectionOfElements;
 
 
@@ -82,6 +98,8 @@
     ProcessInstance processInstance;
     @Transient
     private Environment env;
+    @Transient
+    private final String variableSeparator = "+";
     private boolean serializeVariables = false;
 
    // @OneToMany(mappedBy="processInstanceInfo", targetEntity=VariableInstanceInfo.class )
@@ -161,21 +179,97 @@
         return processInstance;
     }
 
+    
+   
     private void restoreVariables() {
+        
+        
+        for (String variable: variables.keySet()){
 
-        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((RuleFlowProcessInstance) this.processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
-        for (String variable: variables.keySet()){
-        	System.out.println("Restoring " + variable + " " + variables.get(variable));
-            VariablePersistenceStrategy persistenceStrategy =
+            System.out.println("Restoring " + variable + " " + variables.get(variable));
+            restoreVariable(variable, variable, null);
+            
+            
+        }
+
+
+    }
+
+    private void restoreVariable(String variable, String processedVariable, NodeInstanceContainer nodeInstanceContainer) throws NumberFormatException {
+        VariablePersistenceStrategy persistenceStrategy =
                     VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory();
+        //If the original variable doesn't contain the variable separator it need to be restored at the processInstance scope
+        if(!variable.contains(variableSeparator)){
+            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((WorkflowProcessInstanceImpl) this.processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
             VariableInstanceInfo variableInfo = variables.get(variable);
-
             variableScopeInstance.setVariable(variableInfo.getName(), persistenceStrategy.getVariable(variableInfo, this.env));
+            return;
         }
 
+        String[] variableHierarchy = processedVariable.split(variableSeparator);
 
+
+        if (variableHierarchy.length == 2) {
+
+                Long nodeId = Long.valueOf(variableHierarchy[0]);
+                //find the node and get the variableScopeInstance and insert the variable..
+                Collection<NodeInstance> nodeInstances = null;
+                if(nodeInstanceContainer == null){
+                    nodeInstances = ((WorkflowProcessInstanceImpl) this.processInstance).getNodeInstances();
+                }
+                else{
+                    nodeInstances = nodeInstanceContainer.getNodeInstances();
+                }
+                for (NodeInstance nodeInstance : nodeInstances) {
+                    if (nodeInstance.getId() == nodeId) {
+                        if (nodeInstance instanceof NodeInstanceContainer || nodeInstance instanceof ContextInstanceContainer) {
+
+                            List<ContextInstance> nodeContextInstances = ((ContextInstanceContainer) nodeInstance).getContextInstances(VariableScope.VARIABLE_SCOPE);
+                            for (ContextInstance contextInstance : nodeContextInstances) {
+                                if (contextInstance.getContextType().equals(VariableScope.VARIABLE_SCOPE)) {
+                                    VariableScopeInstance variableScopeInstanceNode = (VariableScopeInstance) contextInstance;
+                                     VariableInstanceInfo variableInfo = variables.get(variableHierarchy[1]);
+                                    variableScopeInstanceNode.setVariable(variableInfo.getName(), persistenceStrategy.getVariable(variableInfo, this.env));
+                                }
+                            }
+                        }
+                    }
+
+                }
+        }
+        if (variableHierarchy.length > 2) {
+            Long nodeId = Long.valueOf(variableHierarchy[0]);
+            Collection<NodeInstance> nodeInstances = null;
+                if(nodeInstanceContainer == null){
+                    nodeInstances = ((WorkflowProcessInstanceImpl) this.processInstance).getNodeInstances();
+                }
+                else{
+                    nodeInstances = nodeInstanceContainer.getNodeInstances();
+                }
+                for (NodeInstance nodeInstance : nodeInstances) {
+                    if (nodeInstance.getId() == nodeId) {
+                        if (nodeInstance instanceof NodeInstanceContainer){
+                            String newProcessedVariable = "";
+                            String add = "";
+                            for(int i = 1; i > variableHierarchy.length; i++){
+                                if(i == (variableHierarchy.length -1)){
+                                    add = "";
+                                }else{
+                                    add = variableSeparator;
+                                }
+
+                                newProcessedVariable += variableHierarchy[i]+add;
+                            }
+                            restoreVariable(variable, newProcessedVariable, nodeInstance.getNodeInstanceContainer());
+                        }
+                    }
+                }
+
+        }
+
     }
 
+
     private ProcessInstanceMarshaller getMarshallerFromContext(
             MarshallerReaderContext context) throws IOException {
         ObjectInputStream stream = context.stream;
@@ -226,8 +320,8 @@
     
     private boolean persistVariables() {
     	boolean changed = false;
-       
-        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((RuleFlowProcessInstance) this.processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
+        //Get Process Variables
+        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) ((WorkflowProcessInstanceImpl) this.processInstance).getContextInstance(VariableScope.VARIABLE_SCOPE);
         Map<String, Object> processVariables = variableScopeInstance.getVariables();
         List<String> keys = new ArrayList<String>(processVariables.keySet());
         Collections.sort(keys, new Comparator<String>() {
@@ -236,19 +330,15 @@
                 return o1.compareTo(o2);
             }
         });
-        VariablePersistenceStrategy persistenceStrategy = VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory("PersistenceStrategies.conf");
-        
-        for (String key : keys) {
+        //Persist Process Variables
+        persist(keys, processVariables,"");
 
-                 VariableInstanceInfo variable =        
-                         persistenceStrategy.persistVariable(key, processVariables.get(key), this.env);
-                if (variable != null) {
-                        getVariables().put(variable.getName(), variable);
-                      //  variable.setProcessInstanceInfo(this);
-                      //  variable.setProcessId(this.processInstanceId);
-                }
 
+        Collection<NodeInstance> nodeInstances = ((WorkflowProcessInstanceImpl) this.processInstance).getNodeInstances();
+        if(nodeInstances.size() > 0){
+            persistNodeVariables(nodeInstances,"");
         }
+
         //If we wont find any VariableInstanceInfo that mactches with the persistence strategy,
         //we just serialize the variables inside the blob
 //        if (this.getVariables().size() == 0) {
@@ -257,6 +347,48 @@
         return changed;
     }
 
+     private void persist(List<String> keys, Map<String, Object> processVariables, String prefix) {
+        VariablePersistenceStrategy persistenceStrategy = VariablePersistenceStrategyFactory.getVariablePersistenceStrategyFactory("PersistenceStrategies.conf");
+        for (String key : keys) {
+            VariableInstanceInfo variable = persistenceStrategy.persistVariable(key, processVariables.get(key), this.env);
+            if (variable != null) {
+                String add = "";
+                if(!prefix.equals("")){
+                    add += prefix + variableSeparator;
+                }
+                getVariables().put( add  + variable.getName(), variable);
+            }
+        }
+    }
+
+    private void persistNodeVariables(Collection<NodeInstance> nodeInstances,String parentPrefix) {
+
+        for (NodeInstance nodeInstance : nodeInstances) {
+            if(nodeInstance instanceof NodeContainer || nodeInstance instanceof ContextInstanceContainer){
+                List<ContextInstance> nodeContextInstances =  ((ContextInstanceContainer)nodeInstance).getContextInstances(VariableScope.VARIABLE_SCOPE);
+                for(ContextInstance contextInstance : nodeContextInstances){
+                    VariableScopeInstance variableScopeInstanceNode = (VariableScopeInstance) contextInstance;
+                    Map<String, Object> nodeVariables = variableScopeInstanceNode.getVariables();
+                    List<String> nodeKeys = new ArrayList<String>(nodeVariables.keySet());
+                    Collections.sort(nodeKeys, new Comparator<String>() {
+
+                        public int compare(String o1, String o2) {
+                            return o1.compareTo(o2);
+                        }
+                    });
+
+                    persist(nodeKeys, nodeVariables, parentPrefix + variableSeparator + nodeInstance.getId());
+
+                    if (nodeInstance instanceof NodeInstanceContainer) {
+                        Collection<NodeInstance> nodeInstancesInsideTheContainer = ((NodeInstanceContainer) nodeInstance).getNodeInstances();
+                        persistNodeVariables(nodeInstancesInsideTheContainer, ""+nodeInstance.getId());
+                    }
+                }
+            }
+        }
+    }
+
+
     /**
      * @return the variables
      */
@@ -382,4 +514,5 @@
         return hash;
     }
 
+
 }

Modified: labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf
===================================================================
--- labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf	2009-09-02 17:11:24 UTC (rev 29152)
+++ labs/jbossrules/branches/salaboy_RelationalVariablePersistence/drools-persistence-jpa/src/test/resources/VariablePersistenceStrategyProcessTypeChange.rf	2009-09-02 19:42:36 UTC (rev 29153)
@@ -15,20 +15,20 @@
         <value></value>
       </variable>
       <variable name="y" >
-        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" className="org.drools.persistence.session.MyEntity" />
+        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" className="java.lang.Object" />
       </variable>
       <variable name="z" >
-        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" className="org.drools.persistence.session.MyVariableSerializable" />
+        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" className="java.lang.Object" />
       </variable>
       <variable name="a" >
         <type name="org.drools.process.core.datatype.impl.type.StringDataType" />
         <value></value>
       </variable>
       <variable name="b" >
-        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" />
+        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" className="java.lang.Object" />
       </variable>
       <variable name="c" >
-        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" />
+        <type name="org.drools.process.core.datatype.impl.type.ObjectDataType" className="java.lang.Object" />
       </variable>
     </variables>
   </header>



More information about the jboss-svn-commits mailing list