def _build_convergence_loop_table(): """ Create the ``TransitionTable`` needed by the convergence loop FSM. :return TransitionTable: The transition table for the state machine for converging on the cluster configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition( S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ( [O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) return table
def build_convergence_loop_fsm(reactor, deployer): """ Create a convergence loop FSM. :param IReactorTime reactor: Used to schedule delays in the loop. :param IDeployer deployer: Used to discover local state and calcualte necessary changes to match desired configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition( S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.ITERATION_DONE: ([O.CONVERGE], S.CONVERGING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.ITERATION_DONE: ([], S.STOPPED), }) loop = ConvergenceLoop(reactor, deployer) fsm = constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.STOPPED, table=table, richInputs=[_ClientStatusUpdate], inputContext={}, world=MethodSuffixOutputer(loop)) loop.fsm = fsm return fsm
def build_cluster_status_fsm(convergence_loop_fsm): """ Create a new cluster status FSM. The automatic reconnection logic is handled by the ``AgentLoopService``; the world object here just gets notified of disconnects, it need schedule the reconnect itself. :param convergence_loop_fsm: A convergence loop FSM as output by ``build_convergence_loop_fsm``. """ S = ClusterStatusStates I = ClusterStatusInputs O = ClusterStatusOutputs table = TransitionTable() # We may be shut down in any state, in which case we disconnect if # necessary. table = table.addTransitions( S.DISCONNECTED, { # Store the client, then wait for cluster status to be sent # over AMP: I.CONNECTED_TO_CONTROL_SERVICE: ([O.STORE_CLIENT], S.IGNORANT), I.SHUTDOWN: ([], S.SHUTDOWN), }, ) table = table.addTransitions( S.IGNORANT, { # We never told agent to start, so no need to tell it to stop: I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.DISCONNECTED), # Tell agent latest cluster status, implicitly starting it: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.SHUTDOWN: ([O.DISCONNECT], S.SHUTDOWN), }, ) table = table.addTransitions( S.KNOWLEDGEABLE, { # Tell agent latest cluster status: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.DISCONNECTED_FROM_CONTROL_SERVICE: ([O.STOP], S.DISCONNECTED), I.SHUTDOWN: ([O.STOP, O.DISCONNECT], S.SHUTDOWN), }, ) table = table.addTransitions( S.SHUTDOWN, {I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.SHUTDOWN), I.STATUS_UPDATE: ([], S.SHUTDOWN)} ) return constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.DISCONNECTED, table=table, richInputs=[_ConnectedToControlService, _StatusUpdate], inputContext={}, world=MethodSuffixOutputer(ClusterStatus(convergence_loop_fsm)), )
def test_addTerminalState(self): """ L{TransitionTable.addTerminalState} returns a L{TransitionTable} that includes the given state in its table with no transitions defined. """ table = TransitionTable() more = table.addTerminalState("foo") self.assertEqual({"foo": {}}, more.table)
def test_addTransitionsDoesNotMutate(self): """ L{TransitionTable.addTransitions} does not change the L{TransitionTable} it is called on. """ table = TransitionTable({"foo": {"bar": Transition("baz", "quux")}}) table.addTransitions("apple", {"banana": ("clementine", "date")}) self.assertEqual({"foo": {"bar": Transition("baz", "quux")}}, table.table)
def test_addTransition(self): """ L{TransitionTable.addTransition} accepts a state, an input, an output, and a next state and adds the transition defined by those four values to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransition("foo", "bar", "baz", "quux") self.assertEqual({"foo": {"bar": Transition("baz", "quux")}}, more.table)
def test_addTransitionsDoesNotMutate(self): """ L{TransitionTable.addTransitions} does not change the L{TransitionTable} it is called on. """ table = TransitionTable({"foo": {"bar": Transition("baz", "quux")}}) table.addTransitions("apple", {"banana": ("clementine", "date")}) self.assertEqual({"foo": { "bar": Transition("baz", "quux") }}, table.table)
def test_addTransition(self): """ L{TransitionTable.addTransition} accepts a state, an input, an output, and a next state and adds the transition defined by those four values to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransition("foo", "bar", "baz", "quux") self.assertEqual({"foo": { "bar": Transition("baz", "quux") }}, more.table)
def test_extraInputContext(self): """ L{ExtraInputContext} is raised if there are keys in C{inputContext} which are not symbols in the output alphabet. """ extra = object() transitions = TransitionTable() transitions = transitions.addTransition(State.amber, Input.apple, [Output.aardvark], State.amber) exc = self.assertRaises(ExtraInputContext, constructFiniteStateMachine, Input, Output, State, transitions, State.amber, [], {extra: None}, NULL_WORLD) self.assertEqual(({extra}, ), exc.args)
def test_invalidInitialState(self): """ L{InvalidInitialState} is raised if the value given for C{initial} is not defined by C{state}. """ extra = object() transitions = TransitionTable() transitions = transitions.addTransition(State.amber, Input.apple, [Output.aardvark], State.amber) exc = self.assertRaises(InvalidInitialState, constructFiniteStateMachine, Input, Output, State, transitions, extra, [], {}, NULL_WORLD) self.assertEqual((extra, ), exc.args)
def test_richInputInterface(self): """ L{DoesNotImplement} is raised if a rich input type is given which does not implement the interface required by one of the outputs which can be produced when that input is received. """ apple = trivialInput(Input.apple) transitions = TransitionTable() transitions = transitions.addTransition(State.amber, Input.apple, [Output.aardvark], State.amber) self.assertRaises(DoesNotImplement, constructFiniteStateMachine, Input, Output, State, transitions, State.amber, [apple], {Output.aardvark: IRequiredByAardvark}, NULL_WORLD)
def test_nextStateNotMissingIfInitial(self): """ L{MissingTransitionNextState} is not raised if a value defined by C{state} appears nowhere in C{transitions} as a next state but is given as C{initial}. """ transitions = TransitionTable() transitions = transitions.addTransition(MoreState.amber, Input.apple, [Output.aardvark], MoreState.amber) transitions = transitions.addTerminalState(MoreState.blue) constructFiniteStateMachine(Input, Output, MoreState, transitions, MoreState.blue, [], {}, NULL_WORLD)
def test_nextStateNotMissingIfInitial(self): """ L{MissingTransitionNextState} is not raised if a value defined by C{state} appears nowhere in C{transitions} as a next state but is given as C{initial}. """ transitions = TransitionTable() transitions = transitions.addTransition( MoreState.amber, Input.apple, [Output.aardvark], MoreState.amber) transitions = transitions.addTerminalState(MoreState.blue) constructFiniteStateMachine( Input, Output, MoreState, transitions, MoreState.blue, [], {}, NULL_WORLD)
def test_extraInputContext(self): """ L{ExtraInputContext} is raised if there are keys in C{inputContext} which are not symbols in the output alphabet. """ extra = object() transitions = TransitionTable() transitions = transitions.addTransition( State.amber, Input.apple, [Output.aardvark], State.amber) exc = self.assertRaises( ExtraInputContext, constructFiniteStateMachine, Input, Output, State, transitions, State.amber, [], {extra: None}, NULL_WORLD) self.assertEqual(({extra},), exc.args)
def test_invalidInitialState(self): """ L{InvalidInitialState} is raised if the value given for C{initial} is not defined by C{state}. """ extra = object() transitions = TransitionTable() transitions = transitions.addTransition( State.amber, Input.apple, [Output.aardvark], State.amber) exc = self.assertRaises( InvalidInitialState, constructFiniteStateMachine, Input, Output, State, transitions, extra, [], {}, NULL_WORLD) self.assertEqual((extra,), exc.args)
def test_empty(self): """ When constructed with no arguments, L{TransitionTable} contains a table with no states or transitions. """ table = TransitionTable() self.assertEqual({}, table.table)
def test_missingTransitionNextState(self): """ L{MissingTransitionNextState} is raised if any of the values defined by C{state} appears nowhere in C{transitions} as a next state. """ transitions = TransitionTable() transitions = transitions.addTransition( MoreState.amber, Input.apple, [Output.aardvark], MoreState.amber) transitions = transitions.addTerminalState(MoreState.blue) exc = self.assertRaises( MissingTransitionNextState, constructFiniteStateMachine, Input, Output, MoreState, transitions, MoreState.amber, [], {}, NULL_WORLD) self.assertEqual(({MoreState.blue},), exc.args)
def test_addTransitions(self): """ L{TransitionTable.addTransitions} accepts a state and a mapping from inputs to output, next state pairs and adds all of those transitions to the given state to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransitions( "apple", { "banana": ("clementine", "date"), "eggplant": ("fig", "grape")}) self.assertEqual( {"apple": { "banana": Transition("clementine", "date"), "eggplant": Transition("fig", "grape")}}, more.table)
def test_missingTransitionNextState(self): """ L{MissingTransitionNextState} is raised if any of the values defined by C{state} appears nowhere in C{transitions} as a next state. """ transitions = TransitionTable() transitions = transitions.addTransition(MoreState.amber, Input.apple, [Output.aardvark], MoreState.amber) transitions = transitions.addTerminalState(MoreState.blue) exc = self.assertRaises(MissingTransitionNextState, constructFiniteStateMachine, Input, Output, MoreState, transitions, MoreState.amber, [], {}, NULL_WORLD) self.assertEqual(({MoreState.blue}, ), exc.args)
def test_initial(self): """ When constructed with a transition table as an argument, L{TransitionTable} contains exactly that table. """ expected = {"foo": {"bar": Transition("baz", "quux")}} table = TransitionTable(expected) self.assertIs(expected, table.table)
def test_richInputInterface(self): """ L{DoesNotImplement} is raised if a rich input type is given which does not implement the interface required by one of the outputs which can be produced when that input is received. """ apple = trivialInput(Input.apple) transitions = TransitionTable() transitions = transitions.addTransition( State.amber, Input.apple, [Output.aardvark], State.amber) self.assertRaises( DoesNotImplement, constructFiniteStateMachine, Input, Output, State, transitions, State.amber, [apple], {Output.aardvark: IRequiredByAardvark}, NULL_WORLD)
def test_addTransitions(self): """ L{TransitionTable.addTransitions} accepts a state and a mapping from inputs to output, next state pairs and adds all of those transitions to the given state to a new L{TransitionTable} which it returns. """ table = TransitionTable() more = table.addTransitions("apple", { "banana": ("clementine", "date"), "eggplant": ("fig", "grape") }) self.assertEqual( { "apple": { "banana": Transition("clementine", "date"), "eggplant": Transition("fig", "grape") } }, more.table)
def test_missingTransitionState(self): """ L{MissingTransitionState} is raised if there are any keys in C{transitions} that are not defined by C{state}. """ exc = self.assertRaises(MissingTransitionState, constructFiniteStateMachine, Input, Output, State, TransitionTable({}), State.amber, [], {}, NULL_WORLD) self.assertEqual(({State.amber}, ), exc.args)
def test_missingTransitionInput(self): """ L{MissingTransitionInput} is raised if any of the values defined by C{input} appears in none of the values of C{transitions}. """ exc = self.assertRaises(MissingTransitionInput, constructFiniteStateMachine, Input, Output, State, TransitionTable({State.amber: {}}), State.amber, [], {}, NULL_WORLD) self.assertEqual(({Input.apple}, ), exc.args)
def _build_cluster_status_fsm_table(): """ Create the ``TransitionTable`` needed by the cluster status FSM. :return TransitionTable: The transition table for the state machine for keeping track of cluster state and configuration. """ S = ClusterStatusStates I = ClusterStatusInputs O = ClusterStatusOutputs table = TransitionTable() # We may be shut down in any state, in which case we disconnect if # necessary. table = table.addTransitions( S.DISCONNECTED, { # Store the client, then wait for cluster status to be sent # over AMP: I.CONNECTED_TO_CONTROL_SERVICE: ([O.STORE_CLIENT], S.IGNORANT), I.SHUTDOWN: ([], S.SHUTDOWN), }) table = table.addTransitions( S.IGNORANT, { # We never told agent to start, so no need to tell it to stop: I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.DISCONNECTED), # Tell agent latest cluster status, implicitly starting it: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.SHUTDOWN: ([O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.KNOWLEDGEABLE, { # Tell agent latest cluster status: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.DISCONNECTED_FROM_CONTROL_SERVICE: ([O.STOP], S.DISCONNECTED), I.SHUTDOWN: ([O.STOP, O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.SHUTDOWN, { I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.SHUTDOWN), I.STATUS_UPDATE: ([], S.SHUTDOWN), }) return table
def test_missingTransitionOutput(self): """ L{MissingTransitionOutput} is raised if any of the values defined by C{output} does not appear as an output value defined by C{transitions}. """ exc = self.assertRaises( MissingTransitionOutput, constructFiniteStateMachine, Input, Output, State, TransitionTable({State.amber: { Input.apple: Transition([], None) }}), State.amber, [], {}, NULL_WORLD) self.assertEqual(({Output.aardvark}, ), exc.args)
def _build_convergence_loop_table(): """ Create the ``TransitionTable`` needed by the convergence loop FSM. :return TransitionTable: The transition table for the state machine for converging on the cluster configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition(S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ([O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) return table
def test_extraTransitionState(self): """ L{ExtraTransitionState} is raised if there are any keys in C{transitions} that are not defined by C{state}. """ extra = object() exc = self.assertRaises(ExtraTransitionState, constructFiniteStateMachine, Input, Output, State, TransitionTable({ State.amber: {}, extra: {} }), State.amber, [], {}, NULL_WORLD) self.assertEqual(({extra}, ), exc.args)
def test_extraTransitionOutput(self): """ L{ExtraTransitionInput} is raised if there are any output values defined by C{transitions} that are not defined by C{output}. """ extra = object() exc = self.assertRaises( ExtraTransitionOutput, constructFiniteStateMachine, Input, Output, State, TransitionTable( {State.amber: { Input.apple: Transition([extra], None) }}), State.amber, [], {}, NULL_WORLD) self.assertEqual(({extra}, ), exc.args)
def build_cluster_status_fsm(convergence_loop_fsm): """ Create a new cluster status FSM. The automatic reconnection logic is handled by the ``AgentLoopService``; the world object here just gets notified of disconnects, it need schedule the reconnect itself. :param convergence_loop_fsm: A convergence loop FSM as output by ``build_convergence_loop_fsm``. """ S = ClusterStatusStates I = ClusterStatusInputs O = ClusterStatusOutputs table = TransitionTable() # We may be shut down in any state, in which case we disconnect if # necessary. table = table.addTransitions( S.DISCONNECTED, { # Store the client, then wait for cluster status to be sent # over AMP: I.CONNECTED_TO_CONTROL_SERVICE: ([O.STORE_CLIENT], S.IGNORANT), I.SHUTDOWN: ([], S.SHUTDOWN), }) table = table.addTransitions( S.IGNORANT, { # We never told agent to start, so no need to tell it to stop: I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.DISCONNECTED), # Tell agent latest cluster status, implicitly starting it: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.SHUTDOWN: ([O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.KNOWLEDGEABLE, { # Tell agent latest cluster status: I.STATUS_UPDATE: ([O.UPDATE_STATUS], S.KNOWLEDGEABLE), I.DISCONNECTED_FROM_CONTROL_SERVICE: ([O.STOP], S.DISCONNECTED), I.SHUTDOWN: ([O.STOP, O.DISCONNECT], S.SHUTDOWN), }) table = table.addTransitions( S.SHUTDOWN, { I.DISCONNECTED_FROM_CONTROL_SERVICE: ([], S.SHUTDOWN), I.STATUS_UPDATE: ([], S.SHUTDOWN), }) return constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.DISCONNECTED, table=table, richInputs=[_ConnectedToControlService, _StatusUpdate], inputContext={}, world=MethodSuffixOutputer(ClusterStatus(convergence_loop_fsm)))
def test_extraTransitionNextState(self): """ L{ExtraTransitionNextState} is raised if any of the next state definitions in C{transitions} is not defined by C{state}. """ extra = object() exc = self.assertRaises( ExtraTransitionNextState, constructFiniteStateMachine, MoreInput, Output, State, TransitionTable().addTransitions( State.amber, { MoreInput.apple: ([Output.aardvark], State.amber), MoreInput.banana: ([Output.aardvark], extra) }), State.amber, [], {}, NULL_WORLD) self.assertEqual(({extra}, ), exc.args)
from __future__ import print_function # python3 print function from builtins import * from twisted.python.constants import Names, NamedConstant from machinist import TransitionTable, MethodSuffixOutputer, constructFiniteStateMachine table = TransitionTable() class States(Names): S_CONFIG_W = NamedConstant() S_POR_W = NamedConstant() S_PWR_UP_W = NamedConstant() S_RX_ACTIVE = NamedConstant() S_RX_ON = NamedConstant() S_SDN = NamedConstant() S_STANDBY = NamedConstant() S_TX_ACTIVE = NamedConstant() S_DEFAULT = NamedConstant() class Events(Names): E_0NOP = NamedConstant() E_CONFIG_DONE = NamedConstant() E_CRC_ERROR = NamedConstant() E_INVALID_SYNC = NamedConstant() E_PACKET_RX = NamedConstant() E_PACKET_SENT = NamedConstant() E_PREAMBLE_DETECT = NamedConstant() E_RX_THRESH = NamedConstant() E_STANDBY = NamedConstant() E_SYNC_DETECT = NamedConstant()
table = TransitionTable({ State.IDLE: { Input.REQUEST_START: Transition([Output.START], State.STARTING), Input.REQUEST_STOP: Transition([], State.IDLE), }, State.STARTING: { Input.REQUEST_START: Transition([], State.STARTING), Input.REQUEST_STOP: Transition([], State.START_CANCELLED), Input.INSTANCE_STARTED: Transition([], State.ACTIVE), Input.START_FAILED: Transition([Output.START], State.STARTING), }, State.START_CANCELLED: { Input.REQUEST_START: Transition([], State.STARTING), Input.REQUEST_STOP: Transition([], State.START_CANCELLED), Input.INSTANCE_STARTED: Transition([Output.STOP], State.STOPPING), Input.START_FAILED: Transition([], State.IDLE), }, State.ACTIVE: { Input.REQUEST_START: Transition([], State.ACTIVE), Input.REQUEST_STOP: Transition([Output.STOP], State.STOPPING), }, State.STOPPING: { Input.REQUEST_START: Transition([], State.STOP_CANCELLED), Input.REQUEST_STOP: Transition([], State.STOPPING), Input.INSTANCE_STOPPED: Transition([], State.IDLE), Input.STOP_FAILED: Transition([Output.STOP], State.STOPPING), }, State.STOP_CANCELLED: { Input.REQUEST_START: Transition([], State.STOP_CANCELLED), Input.REQUEST_STOP: Transition([], State.STOPPING), Input.INSTANCE_STOPPED: Transition([Output.START], State.STARTING), Input.STOP_FAILED: Transition([], State.ACTIVE), }, })
ENGAGE_LOCK = NamedConstant() DISENGAGE_LOCK = NamedConstant() class State(Names): LOCKED = NamedConstant() UNLOCKED = NamedConstant() ACTIVE = NamedConstant() # end setup # begin table def from machinist import TransitionTable table = TransitionTable() # end table def # begin first transition table = table.addTransition(State.LOCKED, Input.FARE_PAID, [Output.DISENGAGE_LOCK], State.ACTIVE) # end first transition # begin second transition table = table.addTransition(State.UNLOCKED, Input.ARM_TURNED, [Output.ENGAGE_LOCK], State.ACTIVE) # end second transition # begin last transitions table = table.addTransitions( State.ACTIVE, {
class TurnstileInput(Names): FARE_PAID = NamedConstant() ARM_UNLOCKED = NamedConstant() ARM_TURNED = NamedConstant() ARM_LOCKED = NamedConstant() class TurnstileOutput(Names): ENGAGE_LOCK = NamedConstant() DISENGAGE_LOCK = NamedConstant() class TurnstileState(Names): LOCKED = NamedConstant() UNLOCKED = NamedConstant() ACTIVE = NamedConstant() table = TransitionTable() table = table.addTransitions( TurnstileState.UNLOCKED, { TurnstileInput.ARM_TURNED: ([TurnstileOutput.ENGAGE_LOCK], TurnstileState.ACTIVE), }) table = table.addTransitions( TurnstileState.ACTIVE, { TurnstileInput.ARM_LOCKED: ([], TurnstileState.LOCKED), TurnstileInput.ARM_UNLOCKED: ([], TurnstileState.UNLOCKED), }) table = table.addTransitions( TurnstileState.LOCKED, { TurnstileInput.FARE_PAID: ([TurnstileOutput.DISENGAGE_LOCK], TurnstileState.ACTIVE), })
world = object() self.assertEqual("<Output / %s>" % (world, ), repr(MethodSuffixOutputer(world))) class IFood(Interface): radius = Attribute("The radius of the food (all food is spherical)") @implementer(IFood) class Gravenstein(trivialInput(Input.apple)): # Conveniently, apples are approximately spherical. radius = 3 TRANSITIONS = TransitionTable() TRANSITIONS = TRANSITIONS.addTransition(MoreState.amber, Input.apple, [Output.aardvark], MoreState.blue) TRANSITIONS = TRANSITIONS.addTerminalState(MoreState.blue) class FiniteStateMachineTests(TestCase): """ Tests for the L{IFiniteStateMachine} provider returned by L{constructFiniteStateMachine}. """ def setUp(self): self.animals = [] self.initial = MoreState.amber self.world = AnimalWorld(self.animals)
def build_convergence_loop_fsm(reactor, deployer): """ Create a convergence loop FSM. Once cluster config+cluster state updates from control service are received the basic loop is: 1. Discover local state. 2. Calculate ``IStateChanges`` based on local state and cluster configuration and cluster state we received from control service. 3. Execute the change. 4. Sleep. However, if an update is received during sleep then we calculate based on that updated config+state whether a ``IStateChange`` needs to happen. If it does that means this change will have impact on what we do, so we interrupt the sleep. If calculation suggests a no-op then we keep sleeping. Notably we do **not** do a discovery of local state when an update is received while sleeping, since that is an expensive operation that can involve talking to external resources. Moreover an external update only implies external state/config changed, so we're not interested in the latest local state in trying to decide if this update requires us to do something; a recently cached version should suffice. :param IReactorTime reactor: Used to schedule delays in the loop. :param IDeployer deployer: Used to discover local state and calcualte necessary changes to match desired configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition(S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ([O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) loop = ConvergenceLoop(reactor, deployer) fsm = constructFiniteStateMachine(inputs=I, outputs=O, states=S, initial=S.STOPPED, table=table, richInputs=[_ClientStatusUpdate, _Sleep], inputContext={}, world=MethodSuffixOutputer(loop)) loop.fsm = fsm return fsm
def build_convergence_loop_fsm(reactor, deployer): """ Create a convergence loop FSM. Once cluster config+cluster state updates from control service are received the basic loop is: 1. Discover local state. 2. Calculate ``IStateChanges`` based on local state and cluster configuration and cluster state we received from control service. 3. Execute the change. 4. Sleep. However, if an update is received during sleep then we calculate based on that updated config+state whether a ``IStateChange`` needs to happen. If it does that means this change will have impact on what we do, so we interrupt the sleep. If calculation suggests a no-op then we keep sleeping. Notably we do **not** do a discovery of local state when an update is received while sleeping, since that is an expensive operation that can involve talking to external resources. Moreover an external update only implies external state/config changed, so we're not interested in the latest local state in trying to decide if this update requires us to do something; a recently cached version should suffice. :param IReactorTime reactor: Used to schedule delays in the loop. :param IDeployer deployer: Used to discover local state and calcualte necessary changes to match desired configuration. """ I = ConvergenceLoopInputs O = ConvergenceLoopOutputs S = ConvergenceLoopStates table = TransitionTable() table = table.addTransition( S.STOPPED, I.STATUS_UPDATE, [O.STORE_INFO, O.CONVERGE], S.CONVERGING) table = table.addTransitions( S.CONVERGING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.STOP: ([], S.CONVERGING_STOPPING), I.SLEEP: ([O.SCHEDULE_WAKEUP], S.SLEEPING), }) table = table.addTransitions( S.CONVERGING_STOPPING, { I.STATUS_UPDATE: ([O.STORE_INFO], S.CONVERGING), I.SLEEP: ([], S.STOPPED), }) table = table.addTransitions( S.SLEEPING, { I.WAKEUP: ([O.CLEAR_WAKEUP, O.CONVERGE], S.CONVERGING), I.STOP: ([O.CLEAR_WAKEUP], S.STOPPED), I.STATUS_UPDATE: ( [O.STORE_INFO, O.UPDATE_MAYBE_WAKEUP], S.SLEEPING), }) loop = ConvergenceLoop(reactor, deployer) fsm = constructFiniteStateMachine( inputs=I, outputs=O, states=S, initial=S.STOPPED, table=table, richInputs=[_ClientStatusUpdate, _Sleep], inputContext={}, world=MethodSuffixOutputer(loop)) loop.fsm = fsm return fsm
class IFood(Interface): radius = Attribute("The radius of the food (all food is spherical)") @implementer(IFood) class Gravenstein(trivialInput(Input.apple)): # Conveniently, apples are approximately spherical. radius = 3 TRANSITIONS = TransitionTable() TRANSITIONS = TRANSITIONS.addTransition( MoreState.amber, Input.apple, [Output.aardvark], MoreState.blue) TRANSITIONS = TRANSITIONS.addTerminalState(MoreState.blue) class FiniteStateMachineTests(TestCase): """ Tests for the L{IFiniteStateMachine} provider returned by L{constructFiniteStateMachine}. """ def setUp(self): self.animals = [] self.initial = MoreState.amber self.world = AnimalWorld(self.animals)
ARM_LOCKED = NamedConstant() class Output(Names): ENGAGE_LOCK = NamedConstant() DISENGAGE_LOCK = NamedConstant() class State(Names): LOCKED = NamedConstant() UNLOCKED = NamedConstant() ACTIVE = NamedConstant() # end setup # begin table def from machinist import TransitionTable table = TransitionTable() # end table def # begin first transition table = table.addTransition( State.LOCKED, Input.FARE_PAID, [Output.DISENGAGE_LOCK], State.ACTIVE) # end first transition # begin second transition table = table.addTransition( State.UNLOCKED, Input.ARM_TURNED, [Output.ENGAGE_LOCK], State.ACTIVE) # end second transition # begin last transitions table = table.addTransitions( State.ACTIVE, {