def test_addTransitionDoesNotMutate(self): """ L{TransitionTable.addTransition} does not change the L{TransitionTable} it is called on. """ table = TransitionTable({"foo": {"bar": Transition("baz", "quux")}}) table.addTransition("apple", "banana", "clementine", "date") self.assertEqual({"foo": {"bar": Transition("baz", "quux")}}, table.table)
def test_addTransitionDoesNotMutate(self): """ L{TransitionTable.addTransition} does not change the L{TransitionTable} it is called on. """ table = TransitionTable({"foo": {"bar": Transition("baz", "quux")}}) table.addTransition("apple", "banana", "clementine", "date") self.assertEqual({"foo": { "bar": Transition("baz", "quux") }}, table.table)
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_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_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 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_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_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_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_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_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_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_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_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_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)
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, { Input.ARM_UNLOCKED: ([], State.UNLOCKED), Input.ARM_LOCKED: ([], State.LOCKED), }) # end last transitions
A_PWR_UP = NamedConstant() A_READY = NamedConstant() A_RX_CMP = NamedConstant() A_RX_CNT_CRC = NamedConstant() A_RX_DRAIN_FF = NamedConstant() A_RX_START = NamedConstant() A_RX_TIMEOUT = NamedConstant() A_STANDBY = NamedConstant() A_TX_CMP = NamedConstant() A_TX_FILL_FF = NamedConstant() A_TX_START = NamedConstant() A_TX_TIMEOUT = NamedConstant() A_UNSHUT = NamedConstant() table = table.addTransition(States.S_TX_ACTIVE, Events.E_TX_THRESH, [Actions.A_TX_FILL_FF], States.S_TX_ACTIVE) table = table.addTransition(States.S_RX_ON, Events.E_INVALID_SYNC, [Actions.A_CLEAR_SYNC], States.S_RX_ON) table = table.addTransition(States.S_RX_ACTIVE, Events.E_INVALID_SYNC, [Actions.A_CLEAR_SYNC], States.S_RX_ACTIVE) table = table.addTransition(States.S_RX_ACTIVE, Events.E_PACKET_RX, [Actions.A_RX_CMP], States.S_RX_ON) table = table.addTransition(States.S_SDN, Events.E_TURNON, [Actions.A_UNSHUT], States.S_POR_W) table = table.addTransition(States.S_SDN, Events.E_0NOP, [Actions.A_NOP], States.S_SDN) table = table.addTransition(States.S_STANDBY, Events.E_TURNON, [Actions.A_READY], States.S_RX_ON) table = table.addTransition(States.S_TX_ACTIVE, Events.E_PACKET_SENT, [Actions.A_TX_CMP], States.S_RX_ON) table = table.addTransition(States.S_RX_ON, Events.E_TRANSMIT, [Actions.A_TX_START], States.S_TX_ACTIVE) table = table.addTransition(States.S_CONFIG_W, Events.E_CONFIG_DONE, [Actions.A_READY], States.S_RX_ON) table = table.addTransition(States.S_RX_ACTIVE, Events.E_RX_THRESH, [Actions.A_RX_DRAIN_FF], States.S_RX_ACTIVE) table = table.addTransition(States.S_SDN, Events.E_STANDBY, [Actions.A_CONFIG], States.S_STANDBY) table = table.addTransition(States.S_RX_ON, Events.E_STANDBY, [Actions.A_STANDBY], States.S_STANDBY) table = table.addTransition(States.S_RX_ACTIVE, Events.E_STANDBY, [Actions.A_STANDBY], States.S_STANDBY) table = table.addTransition(States.S_TX_ACTIVE, Events.E_STANDBY, [Actions.A_STANDBY], States.S_STANDBY) table = table.addTransition(States.S_POR_W, Events.E_WAIT_DONE, [Actions.A_PWR_UP], States.S_PWR_UP_W)
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, { Input.ARM_UNLOCKED: ([], State.UNLOCKED), Input.ARM_LOCKED: ([], State.LOCKED), }) # end last transitions
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) self.fsm = constructFiniteStateMachine( Input, Output, MoreState, TRANSITIONS, self.initial, [Gravenstein],
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) self.fsm = constructFiniteStateMachine( Input, Output, MoreState, TRANSITIONS, self.initial,
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