Git Product home page Git Product logo

Comments (9)

inamiy avatar inamiy commented on July 1, 2024

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.

inamiy avatar inamiy commented on July 1, 2024

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.

Bouke avatar Bouke commented on July 1, 2024

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:

HFSM
source

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:

SFSM
source

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.

UML
source

from swiftstate.

inamiy avatar inamiy commented on July 1, 2024

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.

Bouke avatar Bouke commented on July 1, 2024

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.

inamiy avatar inamiy commented on July 1, 2024

I added a HierarchicalStateMachine feature in #10.
Please take a look and tell me your thoughts :)

from swiftstate.

inamiy avatar inamiy commented on July 1, 2024

I will merge #10 anyway and close this issue.
If you've got any opinions, feel free to reopen.

from swiftstate.

Bouke avatar Bouke commented on July 1, 2024

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.

inamiy avatar inamiy commented on July 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.