Slide 1

Slide 1 text

Ghost in the finite State Machine

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

What happens after you click “Buy Now”?

Slide 4

Slide 4 text

We now have a NEW order

Slide 5

Slide 5 text

WAITING FOR PAYMENT

Slide 6

Slide 6 text

What if Payment doesn’t arrive?

Slide 7

Slide 7 text

Payment has arrived

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

“Hey, your Package has shipped”

Slide 10

Slide 10 text

Cancel your order?

Slide 11

Slide 11 text

“What we've got here is failure to communicate implement a FSM”

Slide 12

Slide 12 text

Signs you have a State machine, but maybe don’t know it yet

Slide 13

Slide 13 text

1) Lots of booleanS (timestamps count too)

Slide 14

Slide 14 text

2) You have a field called `state` or `status`

Slide 15

Slide 15 text

How to implement a state machine?

Slide 16

Slide 16 text

We need states

Slide 17

Slide 17 text

We need transitions between states

Slide 18

Slide 18 text

We can implement it ourselves

Slide 19

Slide 19 text

We have the technology

Slide 20

Slide 20 text

the 6_000_000 states machine man

Slide 21

Slide 21 text

state_machine

Slide 22

Slide 22 text

require 'state_machine' class Order state_machine :state, :initial => :new do around_transition do |order, transition, block| puts "Starting transition from #{transition.from}" block.call puts "Finished transition to #{transition.to}" end event :submit do transition :new => :payment_waiting end event :payment_received do transition :payment_waiting => :waiting_for_processing end event :payment_failed do transition :payment_waiting => :payment_failed

Slide 23

Slide 23 text

event :payment_received do transition :payment_waiting => :waiting_for_processing end event :payment_failed do transition :payment_waiting => :payment_failed end event :retry_payment do transition :payment_failed => :payment_waiting end event :process do transition :waiting_for_processing => :waiting_for_shipping end event :ship do transition :waiting_for_shipping => :shipped end after_transition :on => :ship, :do => :notify_customer

Slide 24

Slide 24 text

end after_transition :on => :ship, :do => :notify_customer # Multiple transitions, first matching is taken event :cancel do transition :waiting_for_processing => :canceled transition :waiting_for_shipping => :canceled transition :shipped => :waiting_for_return end event :return_shipment_received do transition :waiting_for_return => :canceled end end def notify_customer puts "Your package has been shipped! :shipit:" end end

Slide 25

Slide 25 text

end end if __FILE__ == $0 order_fsm = Order.new order_fsm.submit! order_fsm.payment_received! order_fsm.process! order_fsm.ship! puts "\n\n" print "Received payment twice (soft): “ order_fsm.payment_received puts order_fsm.state puts "Received payment twice: “ order_fsm.payment_received! end

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

rake state_machine:draw \ CLASS=Order \ FILE=./order_fsm.rb \ FORMAT=pdf \ ORIENTATION=landscape \ HUMAN_NAMES=true

Slide 28

Slide 28 text

new payment waiting submit waiting for processing payment received payment failed payment failed waiting fo process retry payment

Slide 29

Slide 29 text

waiting for processing nt received payment failed ent failed waiting for shipping process cancel payment shipped ship cancel waiting for return cancel retu

Slide 30

Slide 30 text

for shipping canceled cancel shipped ship cancel waiting for return cancel return shipment received

Slide 31

Slide 31 text

Acts as state machine (aasm)

Slide 32

Slide 32 text

Everything has state

Slide 33

Slide 33 text

Use an FSM before it is too late

Slide 34

Slide 34 text

“Why developers should be force-fed state machines”

Slide 35

Slide 35 text

fin

Slide 36

Slide 36 text

BONUS!

Slide 37

Slide 37 text

A state machine in swift via http://www.figure.ink/blog/2015/2/9/swift-state-machines-part-4-redirect

Slide 38

Slide 38 text

import Foundation class StateMachine { private unowned let delegate:P private let validTransitions: [P.StateType: [P.StateType]] private var _state:P.StateType{ didSet { delegate.didTransitionFrom(oldValue, to:_state) } } var state:P.StateType { get{ return _state } set{ // Can't be an observer because we need the option to CONDITIONALLY set state attemptTransitionTo(newValue) }

Slide 39

Slide 39 text

delegate.didTransitionFrom(oldValue, to:_state) } } var state:P.StateType { get{ return _state } set{ // Can't be an observer because we need the option to CONDITIONALLY set state attemptTransitionTo(newValue) } } init(initialState:P.StateType, delegate:P, validTransitions: [P.StateTy [P.StateType]]) { _state = initialState //set the primitive to avoid calling the delegate. self.validTransitions = validTransitions

Slide 40

Slide 40 text

init(initialState:P.StateType, delegate:P, validTransitions: [P.StateTy [P.StateType]]) { _state = initialState //set the primitive to avoid calling the delegate. self.validTransitions = validTransitions self.delegate = delegate } private func attemptTransitionTo(to:P.StateType) { if let validNexts = validTransitions[_state] { if contains(validNexts, to) { _state = to } else { // error, etc } } } }

Slide 41

Slide 41 text

import UIKit class Example : UIView { private var machine:StateMachine! enum TrafficLight : Int { case Stop, Go, Caution } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) let tx = [ TrafficLight.Stop: [TrafficLight.Go], TrafficLight.Caution: [TrafficLight.Stop], TrafficLight.Go: [TrafficLight.Caution] ]

Slide 42

Slide 42 text

required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) let tx = [ TrafficLight.Stop: [TrafficLight.Go], TrafficLight.Caution: [TrafficLight.Stop], TrafficLight.Go: [TrafficLight.Caution] ] machine = StateMachine(initialState: .Stop, delegate: self, validTransitions: tx) } @IBAction func tappedGo(sender:AnyObject) { machine.state = .Go } @IBAction func tappedCaution(sender:AnyObject) {

Slide 43

Slide 43 text

} } extension Example : StateMachineDelegateProtocol { typealias StateType = TrafficLight func didTransitionFrom(from: StateType, to: StateType) { switch to{ case .Stop: backgroundColor = UIColor.redColor() case .Go: backgroundColor = UIColor.greenColor() case .Caution: backgroundColor = UIColor.yellowColor() } } }

Slide 44

Slide 44 text

via https://daniel-levin.github.io/2015/01/19/primitive-state-machine-in-haskell.html

Slide 45

Slide 45 text

data State = S0 | S1 | S2 accepts :: State -> String -> Bool accepts S0 ('a':xs) = accepts S1 xs accepts S0 ('b':xs) = accepts S2 xs accepts S1 ('a':xs) = accepts S2 xs accepts S1 ('b':xs) = accepts S0 xs accepts S2 ('a':xs) = accepts S0 xs accepts S2 ('b':xs) = accepts S2 xs accepts S2 _ = True accepts _ _ = False decide :: String -> Bool decide = accepts S0

Slide 46

Slide 46 text

No content