NBStateMachine 0.4

NBStateMachine 0.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2015
SPMSupports SPM

Maintained by Noam bar-on.



NBStateMachine

Simple State Machine written in Swift

Requirements

  • iOS 9.0+
  • xCode 7+

Example App

To run the example project, clone the repo, and run

Installation

NBStateMachine is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "NBStateMachine"

Set States

//states
let stateLyingDown = State(name: "Lying Down")
let stateStanding = State(name: "Standing")
let stateSitting = State(name: "Sitting Down")
let stateRunning = State(name: "Running")

Set Events

//events
let eventSitDown = Event(eventName: "Sit Down", sourceStates: [stateStanding, stateLyingDown], destinationState: stateSitting)
eventSitDown.willFireEvent = { (event:Event) -> Bool in
print("I'm about to sit down")
return true
}
eventSitDown.didFireEvent = { (event:Event) -> Void in
print("I'm am siting down")
}



let eventStandUp = Event(eventName: "Stand Up", sourceStates: [stateSitting, stateRunning], destinationState: stateStanding)
eventStandUp.willFireEvent = { (event:Event) -> Bool in
print("I'm about to Stand")
return true
}
eventStandUp.didFireEvent = { (event:Event) -> Void in
print("I'm am Standing")
}


let eventStartRunning = Event(eventName: "Start Running", sourceStates: [stateStanding, stateRunning], destinationState: stateRunning)
eventStartRunning.willFireEvent = { (event:Event) -> Bool in
print("I'm about to Start running")
return true
}
eventStartRunning.didFireEvent = { (event:Event) -> Void in
print("I'm am Running")
}

Initiate NBStateMachine

//initiate NBStateMachine
let machine = NBStateMachine(initialState: stateLyingDown)
machine.addStates([stateSitting, stateRunning, stateStanding])
machine.addEvents([eventSitDown, eventStandUp, eventStartRunning])

Fire an event

//fire an event
let transition = machine.fireEvent(eventSitDown) //try also eventStandUp, eventStartRunning

Introspec a transition

print("transition from State:\(transition.sourceState.name)  to State:\(transition.destinationState.name) was successful :\(transition.successful) with error:\(transition.error?.description)")

Collaboration

Feel free to collaborate with ideas, issues and/or pull requests.

Author

Noam Bar-on, https://www.linkedin.com/in/noambaron

License

The MIT License (MIT)

Copyright © 2015 Noam Bar-on.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.