http://jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&feed=atom&action=historyJava/Collections Data Structure/State Machine - История изменений2024-03-28T17:15:40ZИстория изменений этой страницы в викиMediaWiki 1.30.0http://jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&diff=9061&oldid=prevAdmin: 1 версия2010-06-01T07:23:50Z<p>1 версия</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr style="vertical-align: top;" lang="ru">
<td colspan="1" style="background-color: white; color:black; text-align: center;">← Предыдущая</td>
<td colspan="1" style="background-color: white; color:black; text-align: center;">Версия 07:23, 1 июня 2010</td>
</tr><tr><td colspan="2" style="text-align: center;" lang="ru"><div class="mw-diff-empty">(нет различий)</div>
</td></tr></table>Adminhttp://jexp.ru/index.php?title=Java/Collections_Data_Structure/State_Machine&diff=9060&oldid=prev в 18:01, 31 мая 20102010-05-31T18:01:48Z<p></p>
<p><b>Новая страница</b></p><div>== The representation of a finite state machine ==<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<!-- start source code --><br />
<br />
<source lang="java"><br />
<br />
/*<br />
* JBoss, Home of Professional Open Source<br />
* Copyright 2005, JBoss Inc., and individual contributors as indicated<br />
* by the @authors tag. See the copyright.txt in the distribution for a<br />
* full listing of individual contributors.<br />
*<br />
* This is free software; you can redistribute it and/or modify it<br />
* under the terms of the GNU Lesser General Public License as<br />
* published by the Free Software Foundation; either version 2.1 of<br />
* the License, or (at your option) any later version.<br />
*<br />
* This software is distributed in the hope that it will be useful,<br />
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br />
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br />
* Lesser General Public License for more details.<br />
*<br />
* You should have received a copy of the GNU Lesser General Public<br />
* License along with this software; if not, write to the Free<br />
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA<br />
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.<br />
*/<br />
import java.util.HashMap;<br />
import java.util.HashSet;<br />
import java.util.Iterator;<br />
import java.util.Map;<br />
import java.util.Set;<br />
/**<br />
* The representation of a finite state machine.<br />
* <br />
* @author Scott.Stark@jboss.org<br />
* @version $Revision: 2787 $<br />
*/<br />
@SuppressWarnings("unchecked")<br />
public class StateMachine implements Cloneable {<br />
/** A description of the state machine */<br />
private String description;<br />
/** The set of states making up the state machine */<br />
private HashSet states;<br />
/** The starting state */<br />
private State startState;<br />
/** The current state of the state machine */<br />
private State currentState;<br />
/**<br />
* Create a state machine given its states and start state.<br />
* <br />
* @param states -<br />
* Set<State> for the state machine<br />
* @param startState -<br />
* the starting state<br />
*/<br />
public StateMachine(Set states, State startState) {<br />
this(states, startState, null);<br />
}<br />
/**<br />
* Create a state machine given its states and start state.<br />
* <br />
* @param states -<br />
* Set<State> for the state machine<br />
* @param startState -<br />
* the starting state<br />
* @param description -<br />
* an optional description of the state machine<br />
*/<br />
public StateMachine(Set states, State startState, String description) {<br />
this.states = new HashSet(states);<br />
this.startState = startState;<br />
this.currentState = startState;<br />
this.description = description;<br />
}<br />
/**<br />
* Make a copy of the StateMachine maintaining the current state.<br />
* <br />
* @return a copy of the StateMachine.<br />
*/<br />
public Object clone() {<br />
StateMachine clone = new StateMachine(states, startState, description);<br />
clone.currentState = currentState;<br />
return clone;<br />
}<br />
/**<br />
* Get the state machine description.<br />
* <br />
* @return an possibly null description.<br />
*/<br />
public String getDescription() {<br />
return description;<br />
}<br />
/**<br />
* Get the current state of the state machine.<br />
* <br />
* @return the current state.<br />
*/<br />
public State getCurrentState() {<br />
return currentState;<br />
}<br />
/**<br />
* Get the start state of the state machine.<br />
* <br />
* @return the start state.<br />
*/<br />
public State getStartState() {<br />
return startState;<br />
}<br />
/**<br />
* Get the states of the state machine.<br />
* <br />
* @return the machine states.<br />
*/<br />
public Set getStates() {<br />
return states;<br />
}<br />
/**<br />
* Transition to the next state given the name of a valid transition.<br />
* <br />
* @param actionName -<br />
* the name of transition that is valid for the current state.<br />
* @return the next state<br />
* @throws IllegalTransitionException<br />
*/<br />
public State nextState(String actionName) throws IllegalTransitionException {<br />
Transition t = currentState.getTransition(actionName);<br />
if (t == null) {<br />
String msg = "No transition for action: "" + actionName + "" from state: ""<br />
+ currentState.getName() + """;<br />
throw new IllegalTransitionException(msg);<br />
}<br />
State nextState = t.getTarget();<br />
System.out.println("nextState(" + actionName + ") = " + nextState);<br />
currentState = nextState;<br />
return currentState;<br />
}<br />
/**<br />
* Reset the state machine back to the start state<br />
* <br />
* @return the start state<br />
*/<br />
public State reset() {<br />
this.currentState = startState;<br />
return currentState;<br />
}<br />
public String toString() {<br />
StringBuffer tmp = new StringBuffer("StateMachine[:\n");<br />
tmp.append("\tCurrentState: " + currentState.getName());<br />
Iterator i = states.iterator();<br />
while (i.hasNext()) {<br />
tmp.append("\n").append(i.next());<br />
}<br />
tmp.append("]");<br />
return tmp.toString();<br />
}<br />
}<br />
/*<br />
* JBoss, Home of Professional Open Source Copyright 2005, JBoss Inc., and<br />
* individual contributors as indicated by the @authors tag. See the<br />
* copyright.txt in the distribution for a full listing of individual<br />
* contributors.<br />
* <br />
* This is free software; you can redistribute it and/or modify it under the<br />
* terms of the GNU Lesser General Public License as published by the Free<br />
* Software Foundation; either version 2.1 of the License, or (at your option)<br />
* any later version.<br />
* <br />
* This software is distributed in the hope that it will be useful, but WITHOUT<br />
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS<br />
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more<br />
* details.<br />
* <br />
* You should have received a copy of the GNU Lesser General Public License<br />
* along with this software; if not, write to the Free Software Foundation,<br />
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF<br />
* site: http://www.fsf.org.<br />
*/<br />
/**<br />
* The respresentation of a state in a state machine.<br />
* <br />
* @author Scott.Stark@jboss.org<br />
* @version $Revision: 2787 $<br />
*/<br />
@SuppressWarnings("unchecked")<br />
class State {<br />
/** The name of the state */<br />
private String name;<br />
/** HashMap<String, Transition> */<br />
private HashMap allowedTransitions = new HashMap();<br />
/** Arbitrary state data */<br />
private Object data;<br />
public State(String name) {<br />
this(name, null);<br />
}<br />
public State(String name, Map transitions) {<br />
this.name = name;<br />
if (transitions != null) {<br />
allowedTransitions.putAll(transitions);<br />
}<br />
}<br />
/**<br />
* Get the state name.<br />
* <br />
* @return the name of the state.<br />
*/<br />
public String getName() {<br />
return name;<br />
}<br />
public Object getData() {<br />
return data;<br />
}<br />
public void setData(Object data) {<br />
this.data = data;<br />
}<br />
/**<br />
* An accept state is indicated by no transitions<br />
* <br />
* @return true if this is an accept state, false otherwise.<br />
*/<br />
public boolean isAcceptState() {<br />
return allowedTransitions.size() == 0;<br />
}<br />
/**<br />
* Add a transition to the allowed transition map.<br />
* <br />
* @param transition<br />
* @return this to allow chained addTransition calls<br />
*/<br />
public State addTransition(Transition transition) {<br />
allowedTransitions.put(transition.getName(), transition);<br />
return this;<br />
}<br />
/**<br />
* Lookup an allowed transition given its name.<br />
* <br />
* @param name -<br />
* the name of a valid transition from this state.<br />
* @return the valid transition if it exists, null otherwise.<br />
*/<br />
public Transition getTransition(String name) {<br />
Transition t = (Transition) allowedTransitions.get(name);<br />
return t;<br />
}<br />
/**<br />
* Get the Map<String, Transition> of allowed transitions for this state.<br />
* <br />
* @return the allowed transitions map.<br />
*/<br />
public Map getTransitions() {<br />
return allowedTransitions;<br />
}<br />
public String toString() {<br />
StringBuffer tmp = new StringBuffer("State(name=");<br />
tmp.append(name);<br />
Iterator i = allowedTransitions.entrySet().iterator();<br />
while (i.hasNext()) {<br />
Map.Entry e = (Map.Entry) i.next();<br />
tmp.append("\n\t on: ");<br />
tmp.append(e.getKey());<br />
Transition t = (Transition) e.getValue();<br />
tmp.append(" go to: ");<br />
tmp.append(t.getTarget().getName());<br />
}<br />
tmp.append(")");<br />
return tmp.toString();<br />
}<br />
}<br />
/*<br />
* JBoss, Home of Professional Open Source Copyright 2005, JBoss Inc., and<br />
* individual contributors as indicated by the @authors tag. See the<br />
* copyright.txt in the distribution for a full listing of individual<br />
* contributors.<br />
* <br />
* This is free software; you can redistribute it and/or modify it under the<br />
* terms of the GNU Lesser General Public License as published by the Free<br />
* Software Foundation; either version 2.1 of the License, or (at your option)<br />
* any later version.<br />
* <br />
* This software is distributed in the hope that it will be useful, but WITHOUT<br />
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS<br />
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more<br />
* details.<br />
* <br />
* You should have received a copy of the GNU Lesser General Public License<br />
* along with this software; if not, write to the Free Software Foundation,<br />
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF<br />
* site: http://www.fsf.org.<br />
*/<br />
/**<br />
* A representation of a transition from a state to another state.<br />
* <br />
* @author Scott.Stark@jboss.org<br />
* @version $Revision: 1958 $<br />
*/<br />
class Transition {<br />
private String name;<br />
private State target;<br />
public Transition(String name, State target) {<br />
this.name = name;<br />
this.target = target;<br />
}<br />
public String getName() {<br />
return name;<br />
}<br />
public State getTarget() {<br />
return target;<br />
}<br />
}<br />
/*<br />
* JBoss, Home of Professional Open Source Copyright 2005, JBoss Inc., and<br />
* individual contributors as indicated by the @authors tag. See the<br />
* copyright.txt in the distribution for a full listing of individual<br />
* contributors.<br />
* <br />
* This is free software; you can redistribute it and/or modify it under the<br />
* terms of the GNU Lesser General Public License as published by the Free<br />
* Software Foundation; either version 2.1 of the License, or (at your option)<br />
* any later version.<br />
* <br />
* This software is distributed in the hope that it will be useful, but WITHOUT<br />
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS<br />
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more<br />
* details.<br />
* <br />
* You should have received a copy of the GNU Lesser General Public License<br />
* along with this software; if not, write to the Free Software Foundation,<br />
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF<br />
* site: http://www.fsf.org.<br />
*/<br />
/**<br />
* An exception thrown when an invalid transition is attempted from a state.<br />
* <br />
* @author Scott.Stark@jboss.org<br />
* @version $Revision: 2800 $<br />
*/<br />
class IllegalTransitionException extends Exception {<br />
/** The serialVersionUID */<br />
private static final long serialVersionUID = -3392564168782896452L;<br />
public IllegalTransitionException(String msg) {<br />
super(msg);<br />
}<br />
}<br />
<br />
</source><br />
<br />
<br />
<!-- end source code --></div>