Comments (9)
Hi @Bouke, thanks for your feedback.
Your situation can be solved using condition
in addRoute(state:condition:)
.
To do that, try
machine.addRoute(nil => .StateC, condition: { transition in
// from anywhere to .StateC at first, but restrict fromState to only A or B, using condition
return transition.fromState == .StateA || transition.fromState == .StateB
}
For more information, please see blacklisting technique in: StateMachineTests.swift#L116-L129
from swiftstate.
Oops, I forgot the handler part. Please use this instead: addRoute(state:condition:handler).
You can also do like this:
machine.addHandler(nil => .StateC) { context in
if context.fromState != .StateA || context.fromState != .StateB { return }
// do whatever handling you want for A => C or B => C only
...
}
from swiftstate.
Well that's also what I meant by using the nil
state. However, add another 20 states and it becomes very hard to maintain or even grasp the states of the machine by looking at the routes. For example, take the following picture of a HFSM:
This state machine allows to go from (Dive | Crouch) => Roll => Crouch
. This is a very clear state transition.
Another solution would be to use SFSM (nested FSM), this could be modelled as 4 different state machines:
Or the following picture, where an UML state machine is shown. In my example case both A and B would be within s1, and the transition from s1 to s2 would equal the nil => C
case in the example.
from swiftstate.
Thank you for detailed information!
Your post gave me a good insight 😉
To achieve your (Dive | Crouch) => Roll
transition (HFSM),
I added a simple helper in #9 (db73c10) as:
machine.addRoute([.Dive, .Crouch] => .Roll)
Now complex routing becomes more declarative and understandable :)
For nested FSM, please let me take some more time to dig into.
from swiftstate.
Thanks for the changes, that already cleaned up some code. 👍
Being able to also specify handlers that way, would also be great. However these changes somewhat eases the burden on complex state machines; it would still be able to address so-to-speak categories. Like in the example above only define s1 -> s2
transition, without worrying about the sub-states of s1
and s2
.
from swiftstate.
I added a HierarchicalStateMachine
feature in #10.
Please take a look and tell me your thoughts :)
from swiftstate.
I will merge #10 anyway and close this issue.
If you've got any opinions, feel free to reopen.
from swiftstate.
Sorry, i haven't been able to look at this thoroughly. Good to hear that you'll be merging your solution. However, would it be somehow possible to also create a HSM with static typed states, with enums?
from swiftstate.
In ver 4.0.0, I refactored many codes and now states with enums are supported for hierarchical state machine.
Please see Ver 4.0.0 Release Notes and new HierarchicalMachineTests.swift for more info.
from swiftstate.
Related Issues (20)
- Example in Readme lacks MyEvent. HOT 3
- Allow Swift 2.0 code to be used by CocoaPods HOT 1
- Whats meaning of nil => .State2 ? Got an error HOT 2
- Can't use event handler by itself HOT 9
- Can't build with Swift 2.0 on XCode7 beta5 HOT 5
- suggestion on transition and condition, which should include Event HOT 10
- Machine.addHandler(transition: order: handler:) -> Disposable not working ? HOT 6
- Request for improvement HOT 2
- SwiftLint config error on carthage build HOT 2
- iOS 7 support HOT 4
- Swift 3.0 compatible? HOT 7
- Arrow pointing the wrong direction for sending events? HOT 2
- onEntry and onExit handlers? HOT 3
- Swift 4.0 compatible? HOT 2
- Carthage Build failed with exit code 65 HOT 3
- CocoaPod doens't compile with Xcode 9.2 HOT 1
- Would be really helpful if you version it better with each commit to master or swift/4.0
- Version 4.1.0 - doesnt compile HOT 3
- How to use with a Vapor3 project HOT 1
- State & Event enums with associated values complete example
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from swiftstate.