Revision: 17957a0093cd
Author: Thomas Quintana <[email protected]>
Date: Fri Jun 15 11:14:48 2012
Log: Finished the finite state machine package.
http://code.google.com/p/commscale/source/detail?r=17957a0093cd&repo=commons
Added:
/src/test/java/org/mobicents/commons/fsm/LightBulbException.java
/src/test/java/org/mobicents/commons/fsm/Off.java
/src/test/java/org/mobicents/commons/fsm/On.java
/src/test/java/org/mobicents/commons/fsm/Power.java
/src/test/java/org/mobicents/commons/fsm/PrintToScreen.java
Modified:
/src/main/java/org/mobicents/commons/fsm/FiniteStateMachine.java
/src/test/java/org/mobicents/commons/event/AbstractHandler.java
/src/test/java/org/mobicents/commons/fsm/FiniteStateMachineTest.java
/src/test/java/org/mobicents/commons/fsm/LightBulb.java
=======================================
--- /dev/null
+++ /src/test/java/org/mobicents/commons/fsm/LightBulbException.java Fri
Jun 15 11:14:48 2012
@@ -0,0 +1,30 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.commons.fsm;
+
+import org.mobicents.commons.annotations.NotThreadSafe;
+
+/**
+ * @author [email protected] (Thomas Quintana)
+ */
+@NotThreadSafe public final class LightBulbException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ public LightBulbException(final Throwable cause) {
+ super(cause);
+ }
+}
=======================================
--- /dev/null
+++ /src/test/java/org/mobicents/commons/fsm/Off.java Fri Jun 15 11:14:48
2012
@@ -0,0 +1,35 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.commons.fsm;
+
+import org.mobicents.commons.annotations.ThreadSafe;
+
+/**
+ * @author [email protected] (Thomas Quintana)
+ */
+@ThreadSafe public class Off extends Power {
+ public Off(LightBulb bulb) {
+ super("Off", new PrintToScreen(), null, bulb);
+ }
+
+ @Override protected void change() {
+ try { getLightBulb().turnOn(); }
+ catch(final LightBulbException exception) {
+ exception.printStackTrace();
+ }
+ }
+}
=======================================
--- /dev/null
+++ /src/test/java/org/mobicents/commons/fsm/On.java Fri Jun 15 11:14:48
2012
@@ -0,0 +1,35 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.commons.fsm;
+
+import org.mobicents.commons.annotations.ThreadSafe;
+
+/**
+ * @author [email protected] (Thomas Quintana)
+ */
+@ThreadSafe public final class On extends Power {
+ public On(LightBulb bulb) {
+ super("On", new PrintToScreen(), null, bulb);
+ }
+
+ @Override protected void change() {
+ try { getLightBulb().turnOff(); }
+ catch(final LightBulbException exception) {
+ exception.printStackTrace();
+ }
+ }
+}
=======================================
--- /dev/null
+++ /src/test/java/org/mobicents/commons/fsm/Power.java Fri Jun 15 11:14:48
2012
@@ -0,0 +1,38 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.commons.fsm;
+
+import org.mobicents.commons.annotations.NotThreadSafe;
+
+/**
+ * @author [email protected] (Thomas Quintana)
+ */
+@NotThreadSafe public abstract class Power extends State {
+ private final LightBulb bulb;
+
+ public Power(final String id, final Action actionOnEnter, final Action
actionOnExit,
+ final LightBulb bulb) {
+ super(id, actionOnEnter, actionOnExit);
+ this.bulb = bulb;
+ }
+
+ protected abstract void change();
+
+ protected LightBulb getLightBulb() {
+ return bulb;
+ }
+}
=======================================
--- /dev/null
+++ /src/test/java/org/mobicents/commons/fsm/PrintToScreen.java Fri Jun 15
11:14:48 2012
@@ -0,0 +1,33 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.mobicents.commons.fsm;
+
+import org.mobicents.commons.annotations.ThreadSafe;
+import org.mobicents.commons.event.Event;
+
+/**
+ * @author [email protected] (Thomas Quintana)
+ */
+@ThreadSafe public final class PrintToScreen implements Action {
+ public PrintToScreen() {
+ super();
+ }
+
+ @Override public <T> void execute(final Event<T> event, final State
state) {
+ System.out.println(state.getId());
+ }
+}
=======================================
--- /src/main/java/org/mobicents/commons/fsm/FiniteStateMachine.java Fri
Jun 15 09:12:45 2012
+++ /src/main/java/org/mobicents/commons/fsm/FiniteStateMachine.java Fri
Jun 15 11:14:48 2012
@@ -39,8 +39,8 @@
this.lock = new ReentrantLock();
}
- private boolean canTransitionTo(final State newState) {
- return transitions.get(state).containsKey(newState);
+ private boolean canTransitionTo(final State state) {
+ return transitions.get(this.state).containsKey(state);
}
private void checkIsInitialized() throws FiniteStateMachineException {
@@ -89,8 +89,17 @@
return buffer.toString();
}
- private Transition getTransitionTo(final State newState) {
- return transitions.get(state).get(newState);
+ protected State getState() {
+ lock();
+ try {
+ return state;
+ } finally {
+ unlock();
+ }
+ }
+
+ private Transition getTransitionTo(final State state) {
+ return transitions.get(this.state).get(state);
}
protected void initialize(final State state, final Set<Transition>
transitions) {
@@ -124,26 +133,28 @@
lock();
try {
if(!canTransitionTo(state)) {
- final String message = noTransitionFound(state, state);
+ final String message = noTransitionFound(this.state, state);
throw new TransitionNotFoundException(message, event, state);
}
final Transition transition = getTransitionTo(state);
final Condition condition = transition.getCondition();
+ boolean accept = true;
if(condition != null) {
- if(condition.accept(event, transition)) {
- final Action actionOnExit = state.getActionOnExit();
- if(actionOnExit != null) {
- actionOnExit.execute(event, state);
- }
- this.state = state;
- final Action actionOnEnter = state.getActionOnEnter();
- if(actionOnEnter != null) {
- actionOnEnter.execute(event, state);
- }
- } else {
- final String message = conditionFailed(event, transition);
- throw new TransitionFailedException(message, event, transition);
- }
+ accept = condition.accept(event, transition);
+ }
+ if(accept) {
+ final Action actionOnExit = this.state.getActionOnExit();
+ if(actionOnExit != null) {
+ actionOnExit.execute(event, this.state);
+ }
+ this.state = state;
+ final Action actionOnEnter = this.state.getActionOnEnter();
+ if(actionOnEnter != null) {
+ actionOnEnter.execute(event, this.state);
+ }
+ } else {
+ final String message = conditionFailed(event, transition);
+ throw new TransitionFailedException(message, event, transition);
}
} catch(final RuntimeException exception) {
final String message = finiteStateMachineFailed();
=======================================
--- /src/test/java/org/mobicents/commons/event/AbstractHandler.java Tue May
29 23:43:15 2012
+++ /src/test/java/org/mobicents/commons/event/AbstractHandler.java Fri Jun
15 11:14:48 2012
@@ -21,10 +21,12 @@
import java.util.GregorianCalendar;
import java.util.concurrent.atomic.AtomicInteger;
+import org.mobicents.commons.annotations.NotThreadSafe;
+
/**
* @author [email protected] (Thomas Quintana)
*/
-public abstract class AbstractHandler implements EventHandler {
+@NotThreadSafe public abstract class AbstractHandler implements
EventHandler {
private final AtomicInteger counter;
public AbstractHandler() {
=======================================
--- /src/test/java/org/mobicents/commons/fsm/FiniteStateMachineTest.java
Fri Jun 15 09:12:45 2012
+++ /src/test/java/org/mobicents/commons/fsm/FiniteStateMachineTest.java
Fri Jun 15 11:14:48 2012
@@ -16,6 +16,10 @@
*/
package org.mobicents.commons.fsm;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
import static org.junit.Assert.*;
import org.junit.Test;
@@ -29,7 +33,24 @@
super();
}
- @Test public void test() {
-
+ @Test public void test() throws InterruptedException {
+ final ExecutorService executor = Executors.newFixedThreadPool(4);
+ final LightBulb bulb = new LightBulb();
+ for(int counter = 0; counter < 4; counter++) {
+ flipSwitch(executor, bulb, 5);
+ }
+ executor.shutdown();
+ executor.awaitTermination(30, TimeUnit.SECONDS);
+ assertTrue(bulb.getCount() == 20);
+ }
+
+ private void flipSwitch(final ExecutorService executor, final LightBulb
bulb, final int times) {
+ executor.execute(new Runnable() {
+ @Override public void run() {
+ for(int counter = 0; counter < times; counter++) {
+ bulb.power();
+ }
+ }
+ });
}
}
=======================================
--- /src/test/java/org/mobicents/commons/fsm/LightBulb.java Fri Jun 15
09:12:45 2012
+++ /src/test/java/org/mobicents/commons/fsm/LightBulb.java Fri Jun 15
11:14:48 2012
@@ -1,11 +1,66 @@
+/*
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
package org.mobicents.commons.fsm;
-public final class LightBulb extends FiniteStateMachine {
+import java.util.HashSet;
+import java.util.Set;
+
+import org.mobicents.commons.annotations.ThreadSafe;
+
+/**
+ * @author [email protected] (Thomas Quintana)
+ */
+@ThreadSafe public final class LightBulb extends FiniteStateMachine {
+ public final Power on;
+ public final Power off;
+ public int counter;
+
public LightBulb() {
super();
+ on = new On(this);
+ off = new Off(this);
+ final Set<Transition> transitions = new HashSet<Transition>();
+ transitions.add(new Transition(on, off, null));
+ transitions.add(new Transition(off, on, null));
+ initialize(off, transitions);
+ counter = 0;
+ }
+
+ public int getCount() {
+ return counter;
}
public void power() {
-
+ lock();
+ try {
+ final Power power = (Power)getState();
+ power.change();
+ } finally {
+ unlock();
+ }
+ }
+
+ public void turnOff() throws LightBulbException {
+ try { transition(null, off); counter++; }
+ catch(final Exception exception) { throw new
LightBulbException(exception); }
+ }
+
+ public void turnOn() throws LightBulbException {
+ try { transition(null, on); counter++; }
+ catch(final Exception exception) { throw new
LightBulbException(exception); }
}
}