Coder Social home page Coder Social logo

jkrumow / tbstatemachine Goto Github PK

View Code? Open in Web Editor NEW
49.0 49.0 6.0 1.2 MB

A lightweight hierarchical state machine framework in Objective-C.

License: MIT License

Objective-C 98.56% Ruby 0.97% Shell 0.03% C 0.44%
nested-states objective-c state-machine

tbstatemachine's People

Contributors

jkrumow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

tbstatemachine's Issues

sub state selected automatically

Hello,

I am playing with the state machine and I have the following example

  TBSMStateMachine *sm = [TBSMStateMachine stateMachineWithName:@"SM"];
  TBSMStateMachine *smA = [TBSMStateMachine stateMachineWithName:@"SMA"];
  TBSMSubState *a = [TBSMSubState subStateWithName:@"A"];
  TBSMState *a1 = [TBSMState stateWithName:@"A1"];
  TBSMState *a2 = [TBSMState stateWithName:@"A2"];
  smA.states = @[a1,a2];
  a.stateMachine = smA;
  sm.states = @[a];
  sm.initialState = a;
  [sm setUp:nil];

I've noticed that A1 is the default state of SMA by default and it's enterBlock is called at setup.

It is not possible to be in state A and not be in the same time in state A1?

Please help me understand what I am missing,

Thank you

Rework state switching

  • update API to switch by paths instead of single states:
    • traverse state machine hierarchy and create path
    • find LCA from source state's path and destination state's path
    • change -switchState:data:action: to -switchPath:data:action:
    • change -enter:(id<TBStateMachineNode>)state data:(NSDictionary *)data to -enter:(NSArray *)state data:(NSDictionary *)data
    • change -exit:(id<TBStateMachineNode>)state data:(NSDictionary *)data to -exit:(NSArray *)state data:(NSDictionary *)data

Improve thread safety

When scheduleEvent:data: is called from different concurrent threads:

  • correct execution sequence of events
  • correct execution sequence of state switching

Question - parent state

Hi,
How can I create a parent state which listen to event, transition its substates and still listen to the same event.

Is it a good practice to change state in guard block? Is there any alternative?

Generally speaking:
I have 3 states: stateA, stateB, stateC
and one event eventA with attached data dataA

Statemachine is in stateA and receives eventA.
if dataA is valid statemachine should switch to stateB if not it should switch to stateC.
What is best practice for doing it with your library.

Here is my current implementation, but I am not sure that it is a good practice.

[waitingForFirstVideoFrame addHandlerForEvent:ZZMRecordingSessionVideoFrameEvent
                                               target:recording
                                                 kind:TBSMTransitionExternal
                                               action:nil
        guard:^BOOL(TBSMState * _Nullable sourceState, TBSMState * _Nullable targetState, ZZMFrame *videoFrame) {
            BOOL setup = [self startRecordingWithFrame:videoFrame];
            if (!setup) {
                [_stateMachine switchState:sourceState targetState:die action:nil data:nil];
            }
            return setup;
        }];

Thank you in advance.

Refactor event handling - API

  • separation into target - action - guard
  • remove return values from action block - use target
  • execute action between exit and enter in switch state

Add debug method to visualize state machine.

Minimal implementation can be as following.
[stateMachine umpleRepresentation];

#=>
class CourseSection
{
  status
  {
    Planned {
      openRegistration -> NotEnoughStudents;
    }
    Open {
      cancel -> Cancelled;
      NotEnoughStudents {
        closeRegistration -> Cancelled;
        register [getClassSize() > getMinimumClassSize()] -> EnoughStudents;
      }
      EnoughStudents {
        closeRegistration -> Closed;
        register [getClassSize() > getMaximumClassSize()] -> Closed;
      }
    }
    Cancelled {}
    Closed {}
  }
}

Graphical representation:
http://cruise.eecs.uottawa.ca/umpleonline/umple.php?model=160218638587&diagramtype=state

Sub state machines should behave like a state

  • it should be possible to register event handlers
  • it should be possible to define enter and exit blocks
    • enter block will be executed before the enter block of the respective substate
    • exit block will be executed after the exit block of the respective substate

Add event deferral.

A state should be able to define a given event as deferred.
The state machine should be able to queue these events until a state has been entered which can handle the deferred event.
Substates should take precedence over super states. In orthogonal regions the consuming state takes precedence over the deferring state.

Add support for internal transitions.

The state machine should only execute guard and action blocks when parameter target in method -registerEvent:target:action:guard: in class TBSMState is set to nil. The exit block should not be executed.

Rework state switching II

  • each node knows its path inside the state machine hierarchy when it is added to the hierarchy
  • find LCA from source state's path and destination state's
  • switches to next sub state machine

Fix LCA implementation

  • execution sequence of exit - action - enter is messed up
  • state is entered by the wrong state machine in the hierarchy

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.