feature[ats_ATS42145]: Change sm for single thread apps
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Blue.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Blue.java index b8a7784..b7419a3 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Blue.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Blue.java
@@ -10,11 +10,19 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import org.eclipse.ote.statemachine.BaseInput; -import org.eclipse.ote.statemachine.BaseState; public class Blue extends BaseState { + private String name; + + public Blue(){ + + } + + public Blue(String name){ + this.name = name; + } + @Override public void run(BaseInput intput) { @@ -29,5 +37,13 @@ public void exit() { } + + public String toString(){ + if(name != null){ + return name; + } else { + return super.toString(); + } + } }
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Change.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Change.java index 1ddd658..40e0406 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Change.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Change.java
@@ -10,18 +10,23 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import org.eclipse.ote.statemachine.BaseInput; -import org.eclipse.ote.statemachine.StateMachine; public class Change extends BaseInput{ private Object obj; - + private String name; + public Change(StateMachine sm, Object obj){ super(sm); this.obj = obj; } + public Change(StateMachine sm, Object obj, String name){ + super(sm); + this.obj = obj; + this.name = name; + } + @Override public Object getType() { @@ -29,7 +34,11 @@ } public String toString(){ - return this.obj.toString(); + if(name != null){ + return this.name; + } else { + return obj.toString(); + } } }
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Green.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Green.java index 057c321..57143a8 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Green.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Green.java
@@ -10,11 +10,19 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import org.eclipse.ote.statemachine.BaseInput; -import org.eclipse.ote.statemachine.BaseState; public class Green extends BaseState { + private String name; + + public Green(){ + + } + + public Green(String name){ + this.name = name; + } + @Override public void run(BaseInput intput) { @@ -30,4 +38,11 @@ } + public String toString(){ + if(name != null){ + return name; + } else { + return super.toString(); + } + } }
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Hierarchy.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Hierarchy.java index 03d213a..8904057 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Hierarchy.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Hierarchy.java
@@ -10,15 +10,19 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import org.eclipse.ote.statemachine.BaseInput; -import org.eclipse.ote.statemachine.ChildStateMachineState; -import org.eclipse.ote.statemachine.StateMachine; public class Hierarchy extends ChildStateMachineState { + private String name; + public Hierarchy(StateMachine sm) { super(sm); } + + public Hierarchy(StateMachine sm, String name) { + super(sm, name); + this.name = name; + } @Override public void exit() { @@ -36,5 +40,13 @@ // TODO Auto-generated method stub } + + public String toString(){ + if(name != null){ + return name; + } else { + return super.toString(); + } + } }
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Purple.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Purple.java index 960d5e3..353a6e4 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Purple.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Purple.java
@@ -10,16 +10,21 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import org.eclipse.ote.statemachine.BaseInput; -import org.eclipse.ote.statemachine.BaseState; public class Purple extends BaseState { private Change internalExit; + private String name; + public Purple(Change internalExit) { this.internalExit = internalExit; } + + public Purple(Change internalExit, String name) { + this.internalExit = internalExit; + this.name = name; + } @Override public void run(BaseInput intput) { @@ -36,4 +41,11 @@ internalExit.addToStateMachineQueue(); } + public String toString(){ + if(name != null){ + return name; + } else { + return super.toString(); + } + } }
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Red.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Red.java index 6d68b18..232570f 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Red.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Red.java
@@ -10,11 +10,19 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import org.eclipse.ote.statemachine.BaseInput; -import org.eclipse.ote.statemachine.BaseState; public class Red extends BaseState { + private String name; + + public Red(){ + + } + + public Red(String name){ + this.name = name; + } + @Override public void run(BaseInput intput) { @@ -29,5 +37,13 @@ public void exit() { } + + public String toString(){ + if(name != null){ + return name; + } else { + return super.toString(); + } + } }
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/StateMachineTest.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/StateMachineTest.java index 4b16992..231e4a1 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/StateMachineTest.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/StateMachineTest.java
@@ -10,8 +10,8 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; public class StateMachineTest { @@ -231,6 +231,91 @@ parent.stop(); } + @Test + public void hierarchyTestInThread() throws Exception{ + StateMachine parent = new StateMachine("Stoplight", true); + + Green green = new Green("green"); + Red red = new Red("red"); + Yellow yellow = new Yellow("yellow"); + Change change = new Change(parent, Object.class, "change"); + Change internalExit = new Change(parent, Integer.class, "internalExit1"); + Change internalExit2 = new Change(parent, Integer.class, "internalExit2"); + + ChildStateMachineState child = new Hierarchy(parent, "child"); + + //begin hier 2 + ChildStateMachineState grandchild = new Hierarchy(parent, "grandchild"); + Blue blud2 = new Blue("blud2"); + Purple purple2 = new Purple(internalExit2, "purple2"); + + grandchild.setDefaultInitialState(blud2); + grandchild.newTransition(blud2, change, purple2); + grandchild.newTransition(purple2, change, blud2); + //end hier 2 + + // Begin hier 1 + Blue blud = new Blue("blud"); + Purple purple = new Purple(internalExit, "purple"); + + child.setDefaultInitialState(blud); + child.newTransition(grandchild, internalExit2, purple); + child.newTransition(blud, change, grandchild); + child.newTransition(purple, change, blud); + // end hier 1 + + // begin master machine + parent.setDefaultInitialState(green); + parent.newTransition(green, change, yellow); + parent.newTransition(yellow, change, red); + parent.newTransition(red, change, child); + parent.newTransition(child, internalExit, green); + parent.initialize(); + // end master machine + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(yellow, parent.getCurrentState()); + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(red, parent.getCurrentState()); + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(child, parent.getCurrentState()); + Assert.assertEquals(blud, ((Hierarchy)child).getCurrentState()); + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(child, parent.getCurrentState()); + Assert.assertEquals(grandchild, ((Hierarchy)child).getCurrentState()); + Assert.assertEquals(blud2, ((Hierarchy)grandchild).getCurrentState()); + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(child, parent.getCurrentState()); + Assert.assertEquals(grandchild, ((Hierarchy)child).getCurrentState()); + Assert.assertEquals(purple2, ((Hierarchy)grandchild).getCurrentState()); + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(child, parent.getCurrentState()); + Assert.assertEquals(purple, ((Hierarchy)child).getCurrentState()); + Assert.assertEquals(blud2, ((Hierarchy)grandchild).getCurrentState()); + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(green, parent.getCurrentState()); + Assert.assertEquals(blud2, ((Hierarchy)grandchild).getCurrentState()); + Assert.assertEquals(blud, ((Hierarchy)child).getCurrentState()); + + parent.addToQueue(change); + parent.processUntilEmpty(); + Assert.assertEquals(yellow, parent.getCurrentState()); + parent.stop(); + } + private static class ChildStateMachine { private Blue blue;
diff --git a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Yellow.java b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Yellow.java index 0334da0..c041c3a 100644 --- a/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Yellow.java +++ b/org.eclipse.ote.statemachine.test/src/org/eclipse/ote/statemachine/Yellow.java
@@ -10,11 +10,19 @@ *******************************************************************************/ package org.eclipse.ote.statemachine; -import org.eclipse.ote.statemachine.BaseInput; -import org.eclipse.ote.statemachine.BaseState; public class Yellow extends BaseState { + private String name; + + public Yellow(){ + + } + + public Yellow(String name){ + this.name = name; + } + @Override public void run(BaseInput intput) { @@ -30,4 +38,11 @@ } + public String toString(){ + if(name != null){ + return name; + } else { + return super.toString(); + } + } }
diff --git a/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/ChildStateMachineState.java b/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/ChildStateMachineState.java index 1f571cc..db813e1 100644 --- a/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/ChildStateMachineState.java +++ b/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/ChildStateMachineState.java
@@ -17,9 +17,13 @@ private StateMachine stateMachine; public ChildStateMachineState(StateMachine sm){ - stateMachine = new StateMachine(sm.getIdFactory(), getClass().getSimpleName()); + stateMachine = new StateMachine(sm.getIdFactory(), getClass().getSimpleName(), sm.getRunInInputThread()); } - + + public ChildStateMachineState(StateMachine sm, String name){ + stateMachine = new StateMachine(sm.getIdFactory(), name, sm.getRunInInputThread()); + } + public abstract void preRunStateMachine(BaseInput input); public abstract void postRunStateMachine(BaseInput input); @@ -44,7 +48,7 @@ void addToQueue(BaseInput input) throws Exception { stateMachine.addToQueue(input); - stateMachine.processInput(); + stateMachine.processInput(); } public void setDefaultInitialState(BaseState initialState) {
diff --git a/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateMachine.java b/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateMachine.java index 5161a92..3832ea3 100644 --- a/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateMachine.java +++ b/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateMachine.java
@@ -39,30 +39,39 @@ private BaseState defaultInitialState; private int nextStateId = 0; private boolean debug = false; + private final boolean runInInputThread; public StateMachine(String name){ - this(new StateMachineIdFactory(), name); + this(new StateMachineIdFactory(), name, false); } - StateMachine(StateMachineIdFactory factory, String name){ + public StateMachine(String name, boolean runInInputThread){ + this(new StateMachineIdFactory(), name, runInInputThread); + } + + StateMachine(StateMachineIdFactory factory, String name, boolean runInInputThread){ this.name = name; this.factory = factory; stateTransitionTable = new StateTransitionTable(50,50); queue = new LinkedBlockingQueue<BaseInput>(); killInput = new KillInput(this); + this.runInInputThread = runInInputThread; } - BaseInput processInput() throws Exception{ + BaseInput processInput() throws RuntimeException{ BaseInput input = getInput(); - if(input != killInput){ + if(input != null && input != killInput){ if(currentState == null){ - throw new Exception("CurrentState is null, statemachine was not properly initialized. Please ensure that if any child state machines were created and entry() was implemented that you call super.entry()."); + throw new RuntimeException("CurrentState is null, statemachine was not properly initialized. Please ensure that if any child state machines were created and entry() was implemented that you call super.entry()."); } BaseState nextState = stateTransitionTable.getTransition(input, currentState); if(debug ) { System.out.println("======================"+name+"========================"); System.out.println("INPUT: " + input.toString()); System.out.println("CURRENTSTATE: " + currentState.toString()); + if(nextState == null){ + System.out.println("strange"); + } System.out.println("NEXTSTATE: " + (nextState == null ? "" : nextState.toString())); } if(nextState != null){ @@ -73,27 +82,46 @@ currentState = nextState; } } + if(debug ) { + System.out.println("DONE ======================"+name+"========================"); + System.out.println("INPUT: " + input.toString()); + System.out.println("CURRENTSTATE: " + currentState.toString()); + + } } return input; } public void start(){ - th = new Thread(new Runnable(){ - @Override - public void run() { - BaseInput input = null; - currentState.entry(); - while(input != killInput){ - try{ - input = processInput(); - } catch (Throwable th){ - th.printStackTrace(); + currentState.entry(); + if(!runInInputThread){ + th = new Thread(new Runnable(){ + @Override + public void run() { + BaseInput input = null; + while(input != killInput){ + try{ + input = processInput(); + } catch (Throwable th){ + th.printStackTrace(); + } } } - } - }); - th.setName("StateMachine " + name); - th.start(); + }); + th.setName("StateMachine " + name); + th.start(); + } + } + + public void processUntilEmpty(){ + BaseInput input = null; + while((input = processInput()) != null){ +// try{ +// input = processInput(); +// } catch (Throwable th){ +// th.printStackTrace(); +// } + } } public void setDefaultInitialState(BaseState initialState) { @@ -120,8 +148,16 @@ return currentState; } - private BaseInput getInput() throws InterruptedException { - return queue.take(); + private BaseInput getInput() { + if(runInInputThread){ + return queue.poll(); + } else { + try { + return queue.take(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } } public void newTransition(BaseState state, BaseInput input, BaseState nextState) throws Exception{ @@ -192,5 +228,9 @@ StateMachineIdFactory getIdFactory() { return this.factory; } + + boolean getRunInInputThread() { + return runInInputThread; + } }
diff --git a/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateTransitionTable.java b/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateTransitionTable.java index 2698527..1277721 100644 --- a/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateTransitionTable.java +++ b/org.eclipse.ote.statemachine/src/org/eclipse/ote/statemachine/StateTransitionTable.java
@@ -36,7 +36,12 @@ if(currentstate.getId() < 0){ throw new IllegalStateException("negative state id's are not allowed"); } - return lookup.get(currentstate.getId()); + BaseState state = lookup.get(currentstate.getId()); + if(state == null){ + return currentstate; + } else { + return state; + } } } return null;