diff options
| author | Andrew Branson <andrew@andrewbranson.net> | 2011-06-21 15:46:02 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew@andrewbranson.net> | 2011-06-21 15:46:02 +0200 |
| commit | 254ee6f47eebfc00462c10756a92066e82cc1a96 (patch) | |
| tree | 8273ff95c704e6faa3f92b4711253427b9ba0481 /source/com/c2kernel/lifecycle/instance/stateMachine | |
Initial commit2.2
Diffstat (limited to 'source/com/c2kernel/lifecycle/instance/stateMachine')
3 files changed, 223 insertions, 0 deletions
diff --git a/source/com/c2kernel/lifecycle/instance/stateMachine/StateMachine.java b/source/com/c2kernel/lifecycle/instance/stateMachine/StateMachine.java new file mode 100755 index 0000000..da7419c --- /dev/null +++ b/source/com/c2kernel/lifecycle/instance/stateMachine/StateMachine.java @@ -0,0 +1,142 @@ +
+package com.c2kernel.lifecycle.instance.stateMachine;
+
+import java.io.Serializable;
+
+import com.c2kernel.lifecycle.instance.Activity;
+import com.c2kernel.utils.Logger;
+
+/**
+ * @version $Revision: 1.30 $ $Date: 2004/06/04 09:39:19 $
+ * @author $Author: sgaspard $
+ */
+/** this class represents the link between 2 successive activities */
+public class StateMachine implements Serializable
+{
+ public int state = 0;
+ private Activity activity;
+
+ public static final String SKIPPABLE = "Skippable";
+ public static final String REPEATABLE = "Repeatable";
+ public static final String IGNORABLE = "Ignorable";
+ public static final String AUTOSTART = "Autostart";
+
+
+ /**
+ * Method StateMachine.
+ * @param act
+ */
+ public StateMachine(Activity act)
+ {
+ activity = act;
+ }
+
+ /** row : States from (WAITING,RESERVED,STARTED,SUSPENDED,FINISHED,RWAITING,RRESERVED,RSTARTED,RSUSPENDED)
+ * collumn : transition (RESERVE,START,SKIP,DONE,COMPLETE,SUSPEND,REASIGN,RESUME,REPEAT,IGNORE,PROCEED)
+ * cell : State that is reached (-1 if transition not allowed)
+ */
+ private int[][] getCurrentMachine()
+ {
+ int [][] returnArray =
+ { /*RESERVE, START, SKIP, DONE,COMPLETE,SUSPEND,REASIGN,RESUME, REPEAT, IGNORE, PROCEED*/
+ /*0 WAITING*/ { 1,getActive()?2:-1,getSkippable()?4:-1,getActive()?4:-1, -1, -1, -1, -1, -1, -1, -1},/*0 WAITING*/
+ /*1 RESERVED*/ { -1,getActive()?2:-1,getSkippable()?4:-1,getActive()?4:-1, -1, -1, -1, -1, -1, 0, -1},/*1 RESERVED*/
+ /*2 STARTED*/ { -1, -1, -1, -1, 4, 3, -1, -1, -1,getIgnorable()?0:-1, -1},/*2 STARTED*/
+ /*3 SUSPENDED*/ { -1, -1, -1, -1, -1, -1, 2, 2, -1,getIgnorable()?0:-1, -1},/*3 SUSPENDED*/
+ /*4 FINISHED*/ { -1, -1, -1, -1, -1, -1, -1, -1,getRepeatable()?!getAutoStart()?5:7:-1, -1,getActive()?4:-1},/*4 FINISHED*/
+ /*5 RWAITING*/ { 6,getActive()?7:-1,getSkippable()?4:-1,getActive()?4:-1, -1, -1, -1, -1, -1, -1, -1},/*5 RWAITING*/
+ /*6 RRESERVED*/ { -1,getActive()?7:-1,getSkippable()?4:-1,getActive()?4:-1, -1, -1, -1, -1, -1, -1, -1},/*6 RRESERVED*/
+ /*7 RSTARTED*/ { -1, -1, -1, -1, 4, 8, -1, -1, -1,getIgnorable()?5:-1, -1},/*7 RSTARTED*/
+ /*8 RSUSPENDED*/ { -1, -1, -1, -1, -1, -1, 8, 7, -1, -1, -1} /*8 RSUSPENDED*/
+ };
+ return returnArray;
+ }
+
+ /**
+ * @see java.lang.Object#Object()
+ */
+ public StateMachine()
+ {
+ }
+
+ /**
+ * Method getCurrentState.
+ * @return String
+ */
+ public int getCurrentState()
+ {
+ return state;
+ }
+
+ /**
+ * Method possibleTransition.
+ * @return String[]
+ */
+ public int[] possibleTransition()
+ {
+ int[] trans = new int[9];
+ int cmpt = 0;
+ for (int i=0; i< getCurrentMachine()[state].length;i++)
+ if (getCurrentMachine()[state][i]!=-1) trans[cmpt++]=i;
+
+ int [] result = new int[cmpt];
+ for (int i=0;i<cmpt;i++) result[i] = trans[i];
+ return result;
+ }
+
+ /**
+ * Method traverse.
+ * @param transition
+ * @return boolean
+ */
+ public boolean traverse(int transition)
+ {
+ int newState = getCurrentMachine()[state][transition];
+ if (newState > -1) {
+ state=newState;
+ return true;
+ }
+ Logger.msg("StateMachine.traverse() - Illegal transition "+Transitions.getTransitionName(transition)+" from "+States.getStateName(state));
+ return false;
+ }
+ public int simulate(int transition)
+ {
+ return getCurrentMachine()[state][transition];
+ }
+ /**
+ * Returns the ignorable.
+ * @return boolean
+ */
+ public boolean getIgnorable()
+ {
+ return ((Boolean)activity.getProperties().get(IGNORABLE)).booleanValue();
+ }
+
+ /**
+ * Returns the repeatable.
+ * @return boolean
+ */
+ public boolean getRepeatable()
+ {
+ return ((Boolean)activity.getProperties().get(REPEATABLE)).booleanValue();
+ }
+
+ /**
+ * Returns the skippable.
+ * @return boolean
+ */
+ public boolean getSkippable()
+ {
+ return ((Boolean)activity.getProperties().get(SKIPPABLE)).booleanValue();
+ }
+
+ public boolean getAutoStart()
+ {
+ return ((Boolean)activity.getProperties().get(AUTOSTART)).booleanValue();
+ }
+ public boolean getActive()
+ {
+ return activity.getActive();
+ }
+
+}
diff --git a/source/com/c2kernel/lifecycle/instance/stateMachine/States.java b/source/com/c2kernel/lifecycle/instance/stateMachine/States.java new file mode 100755 index 0000000..638a7b0 --- /dev/null +++ b/source/com/c2kernel/lifecycle/instance/stateMachine/States.java @@ -0,0 +1,40 @@ +package com.c2kernel.lifecycle.instance.stateMachine;
+
+
+/**
+ * @author XSeb74
+ *
+ * To change this generated comment edit the template variable "typecomment":
+ * Window>Preferences>Java>Templates.
+ * To enable and disable the creation of type comments go to
+ * Window>Preferences>Java>Code Generation.
+ */
+public class States
+{
+ public final static int WAITING = 0;
+ public final static int RESERVED = 1;
+ public final static int STARTED = 2;
+ public final static int SUSPENDED = 3;
+ public final static int FINISHED = 4;
+ public final static int RWAITING = 5;
+ public final static int RRESERVED = 6;
+ public final static int RSTARTED = 7;
+ public final static int RSUSPENDED = 8;
+
+ //everything less that this constant is NOT a repeating state
+ public final static int REPEATSTATESTART = 5;
+
+ public static final String[] states = { "Waiting", "Reserved", "Started", "Suspended", "Finished", "Waiting(R)", "Reserved(R)", "Started(R)", "Suspended(R)" };
+
+ public static String getStateName(int state)
+ {
+ try
+ {
+ return states[state];
+ }
+ catch (ArrayIndexOutOfBoundsException ex)
+ {
+ return "Invalid State: " + state;
+ }
+ }
+}
diff --git a/source/com/c2kernel/lifecycle/instance/stateMachine/Transitions.java b/source/com/c2kernel/lifecycle/instance/stateMachine/Transitions.java new file mode 100755 index 0000000..6597686 --- /dev/null +++ b/source/com/c2kernel/lifecycle/instance/stateMachine/Transitions.java @@ -0,0 +1,41 @@ +package com.c2kernel.lifecycle.instance.stateMachine;
+
+import com.c2kernel.utils.Language;
+
+/**
+ * @author XSeb74
+ *
+ * To change this generated comment edit the template variable "typecomment":
+ * Window>Preferences>Java>Templates.
+ * To enable and disable the creation of type comments go to
+ * Window>Preferences>Java>Code Generation.
+ */
+public class Transitions
+{
+ public final static int RESERVE = 0;
+ public final static int START = 1;
+ public final static int SKIP = 2;
+ public final static int DONE = 3;
+ public final static int COMPLETE = 4;
+ public final static int SUSPEND = 5;
+ public final static int REASSIGN = 6;
+ public final static int RESUME = 7;
+ public final static int REPEAT = 8;
+ public final static int IGNORE = 9;
+ public final static int PROCEED = 10;
+ public final static int ACTIVATION = 11;
+
+ private static String[] transitions = { "reserve", "start", "skip", "done", "complete", "suspend", "reassign", "resume", "repeat","ignore","proceed","activation" };
+
+ public static String getTransitionName(int trans)
+ {
+ try
+ {
+ return Language.translate(transitions[trans]);
+ }
+ catch (ArrayIndexOutOfBoundsException ex)
+ {
+ return "Invalid Transition: " + trans;
+ }
+ }
+}
|
