DavidDurman / statechart

Statechart implementation in JavaScript
MIT License
100 stars 16 forks source link

How to Create Workflow? #2

Closed subodhcjoshi82 closed 10 years ago

subodhcjoshi82 commented 10 years ago

I will want to create a work flow something like this

                             Root
                                |
                             Start
                           /        \
                     Node0      Node1
                      \                     /
                     Node2       Node3
                        \             /
                           Node4

Is this possible with this API. If yes how can thanks

gregwebs commented 10 years ago

The graph is a rendering a little weird, but is the main question whether Node4 can be a part of 2 different branches?

subodhcjoshi82 commented 10 years ago

Yes Node4 is a child of Node3 and Node2 i want this type of graph or workflow

DavidDurman commented 10 years ago

This is indeed possible. Looks like a flattened finite state machine:

var myflow = _.extend({

    initialState: "Root",
    states: {
        'Root': {
            'evt1':  { target: 'Start'  }
        },
        'Start': {
            'evt2':  { target: 'Node0'  },
            'evt3': { target: 'Node1' }
        },
        'Node0': {
            'evt4':  { target: 'Node2'   }
        },
        'Node1': {
            'evt5':  { target: 'Node3'   }
        },
        'Node2': {
            'evt6':  { target: 'Node4'   }
        },
        'Node3': {
            'evt7':  { target: 'Node4'   }
        },
        'Node4': {  },
    }
}, Statechart);

// Initialize the state machine and make the initial transition (to the `Root` state).
myflow.run();

// Dispatch the `evt1` event to the machine which causes it to transit to the `Start` state. Similarly, one can transit through the whole flow by dispatching appropriate events.
myflow.dispatch('evt1');
subodhcjoshi82 commented 10 years ago

Can we edit name of any Node at run time and in my case all information come from DB i mean Node Names? ANd i am using Java in my webapplication.

DavidDurman commented 10 years ago

Not sure if I understand your question properly. Dynamically changing the structure of the state machine is not possible at this time. But if it's only the naming, then one can have a mapping of the actual state names to whatever he wants and dynamically change the mapping only.

subodhcjoshi82 commented 10 years ago

I updated my last question

DavidDurman commented 10 years ago

Same thing. No need to change the actual state names, just keep a mapping and change the mapping if needed e.g. for display.

subodhcjoshi82 commented 10 years ago

I will try to use your API tomorrow from office and let you know if i got any issue

gregwebs commented 10 years ago

@subodhcjoshi82 the graph violates the philosophy of https://github.com/gregwebs/StateTree

But from the perspective of StateTree you can also flatten things by instead doing is creating a new branch in the state hierarchy just for Node4 and then restricting access so that it can only be entered via Node2 or Node3.

DavidDurman commented 10 years ago

@gregwebs Why does the graph violate StateTree philosophy? This is just a classical finite state machine not even using nested states or any other advanced StateChart specific features.

gregwebs commented 10 years ago

Actually, you are right, I added a signals feature that can be used to create state machines like this.

It is impossible to say whether it is better to use that approach or leverage the tree in StateTree. In a graph like this you can't leverage the tree. One advantage of being a tree is that you can ask for the active ancestors (the branch that must be walked from the root to get to the current state). Another advantage of the Tree is that events become unnecessary for many use cases,.

DavidDurman commented 10 years ago

@gregwebs Right, I see. I think I have to study StateTree a little more to get a better understanding of it.

subodhcjoshi82 commented 10 years ago

I was trying to run the test example given in your API

Statechart Tests

but i saw this test example not worked you mentioned node_modules but i did not get any such folder in your downloaded code Can you please guide me what i am doing wrong and how to run test example so that i can integrate it with my Java web application. Thanks

subodhcjoshi82 commented 10 years ago

Any update on my last question ?

DavidDurman commented 10 years ago

You need to install NodeJS (http://nodejs.org) first and then run npm install in the root directory of this repo. The package.json file lists all the dependencies.

subodhcjoshi82 commented 10 years ago

Sorry David can you please add this in document please .Second thing i have to ask if i have to give me product to another company i have to tell them to install this nodejs in their respective machine? Otherwise this workflow will not work?

I Just install Nodejs.org but these file not there expect.js,mocha.js,underscorejs

DavidDurman commented 10 years ago

The NodeJS is only needed for development of the statechart library, more specifically, for running the tests. The statechart library itself runs in browsers and NodeJS. There is no need to install anything if you just want to use the library.

subodhcjoshi82 commented 10 years ago

Sorry but i run your test example in Firefox it show me blank page What i am doing wrong?

DavidDurman commented 10 years ago

What is the error?

subodhcjoshi82 commented 10 years ago

Failed to load resource file:///C:/Users/sjoshi/Desktop/statechart-master/node_modules/mocha/mocha.css Failed to load resource file:///C:/Users/sjoshi/Desktop/statechart-master/node_modules/expect.js/expect.js Failed to load resource file:///C:/Users/sjoshi/Desktop/statechart-master/node_modules/mocha/mocha.js Uncaught TypeError: Object # has no method 'setup' index.html:12 Failed to load resource file:///C:/Users/sjoshi/Desktop/statechart-master/nodemodules/underscore/underscore.js Uncaught ReferenceError: is not defined samek.js:10 Uncaught ReferenceError: _ is not defined switch.js:10 Uncaught TypeError: Object # has no method 'run' index.html:17

This i found in the Chrome browser

DavidDurman commented 10 years ago

That's what I mentioned. If you want to run the tests, you need to have NodeJS installed and then run npm install. This will create the node_modules directory with the test dependencies (mocha, expect and underscore).

subodhcjoshi82 commented 10 years ago

Ok it worked but it is not showing any Flowchart it I thought it should display some work flow kind of thing but its showing Samek test machine and other thing

DavidDurman commented 10 years ago

That's correct. This library is a statechart engine, not visualization. If you'd like to visualize your workflow, you might want to take a look at JointJS diagramming library: http://jointjs.com.

subodhcjoshi82 commented 10 years ago

So you mean with the help of your API i can not create the Workflow i showed to you?

DavidDurman commented 10 years ago

"Create a workflow" has a broad meaning. The way I perceive it is having an engine that interprets the defined workflow and, potentially, a visualizer that displays the workflow on the screen. This library is an engine. JointJS is a visualizer. Both can be combined together in order to create an application for designing and interpreting workflows.

subodhcjoshi82 commented 10 years ago

Ok thanks i will try i am new so its hard to imagine