Skip to content

Commit

Permalink
tests, types, bump
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed May 9, 2017
1 parent 69ed674 commit 94e7405
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jssm",
"version": "0.10.1",
"version": "0.11.0",
"description": "A Javascript state machine with a simple API. Well tested, and typed with Flowtype. MIT License.",
"main": "dist/jssm.es5.browserified.js",
"scripts": {
Expand Down
42 changes: 41 additions & 1 deletion src/js/jssm-tests.js
Expand Up @@ -15,7 +15,7 @@ const seq = upTo => new Array(upTo).fill(false).map( (_,i) => i );



describe('Stop light', async it => {
describe('Simple stop light', async it => {

const light = new jssm.machine({
initial_state: 'red',
Expand Down Expand Up @@ -44,6 +44,46 @@ describe('Stop light', async it => {



describe('Complex stop light', async it => {

const light2 = new jssm.machine({

initial_state: 'off',

transitions:[

{ name:'turn_on', action:'power_on', from:'off', to:'red'},

{ action:'power_off', from:'red', to:'off', probability: 0.01 },
{ action:'power_off', from:'yellow', to:'off', probability: 0.01 },
{ action:'power_off', from:'green', to:'off', probability: 0.01 },

{ name:'switch_warn', from:'green', to:'yellow' },
{ name:'switch_halt', from:'yellow', to:'red' },
{ name:'switch_go', from:'red', to:'green' }

]

});

const r_states = light2.states();
it('has the right state count', t => t.is(r_states.length, 4));
['red', 'yellow', 'green', 'off'].map(c =>
it(`has state "${c}"`, t => t.is(r_states.includes(c), true))
);

const r_names = light2.named_transitions();
it('has the right named transition count', t => t.is(r_names.size, 4));
['turn_on', 'switch_warn', 'switch_halt', 'switch_go'].map(a =>
it(`has named transition "${a}"`, t => t.is(r_names.has(a), true))
);

});





describe('Illegal machines', async it => {

it('catch repeated names', t => t.throws(() => {
Expand Down
24 changes: 7 additions & 17 deletions src/js/jssm-types.js
Expand Up @@ -14,9 +14,9 @@ type JssmResult = JssmSuccess | JssmFailure | JssmIncomplete;


type JssmGenericState<NT> = {
from: Array<NT>,
name: NT,
to: Array<NT>
from : Array< NT > ,
name : NT ,
to : Array< NT >
};


Expand Down Expand Up @@ -98,17 +98,11 @@ type JssmGenericConfig<NT, DT> = {
max_exits? : number,
allow_islands? : false,
allow_force? : false,
actions? : ARD

};


actions? : ARD,

auto_api? : boolean | string; // boolean false means don't; boolean true means do; string means do-with-this-prefix


type JssmState = JssmGenericState<string>;
type JssmMachine = JssmGenericMachine<string, JssmState>;
type JssmConfig = JssmGenericConfig<string, JssmState>;
};



Expand Down Expand Up @@ -136,10 +130,6 @@ export type {

JssmGenericMachine,
JssmGenericConfig,
JssmGenericState,

JssmMachine,
JssmConfig,
JssmState
JssmGenericState

};
48 changes: 24 additions & 24 deletions src/js/jssm.js
@@ -1,7 +1,7 @@

// @flow

import type { JssmMachine, JssmState, JssmGenericState, JssmGenericConfig, JssmTransition, JssmTransitions, JssmTransitionList } from './jssm-types';
import type { JssmGenericState, JssmGenericConfig, JssmTransition, JssmTransitions, JssmTransitionList } from './jssm-types';

const version = null; // replaced from package.js in build

Expand All @@ -13,13 +13,13 @@ class machine<mNT, mDT> {


_state : mNT;
_states : Map<mNT, JssmGenericState<mNT>>; // todo whargarbl this really should't be string
_edges : Array<JssmTransition<string, mixed>>; // remove mixed todo whargarbl
_edge_map : Map<string, Map<string, number>>;
_named_transitions : Map<string, number>; // remove mixed todo whargarbl
_actions : Map<string, Map<string, number>>;
_reverse_actions : Map<string, Map<string, number>>;
_reverse_action_targets : Map<string, Map<string, mixed>>; // remove mixed todo whargarbl
_states : Map<mNT, JssmGenericState<mNT>>;
_edges : Array<JssmTransition<mNT, mDT>>;
_edge_map : Map<mNT, Map<mNT, number>>;
_named_transitions : Map<mNT, number>;
_actions : Map<mNT, Map<mNT, number>>;
_reverse_actions : Map<mNT, Map<mNT, number>>;
//_reverse_action_targets : Map<string, Map<string, mixed>>; // todo // remove mixed todo whargarbl


constructor({ initial_state, transitions } : JssmGenericConfig<mNT, mDT>) {
Expand All @@ -31,7 +31,7 @@ class machine<mNT, mDT> {
this._named_transitions = new Map();
this._actions = new Map();
this._reverse_actions = new Map();
this._reverse_action_targets = new Map();
// this._reverse_action_targets = new Map(); // todo

transitions.map( (tr:any) => { // whargarbl burn out any

Expand Down Expand Up @@ -160,24 +160,24 @@ todo comeback
return [... this._states.keys()];
}

transitions() : Array< JssmTransition<string, mixed> > { // todo burn out mixed
transitions() : Array< JssmTransition<mNT, mDT> > {
return this._edges;
}

named_transitions() : Map<string, number> {
named_transitions() : Map<mNT, number> {
return this._named_transitions;
}

actions() : Array<string> {
actions() : Array<mNT> {
return [... this._actions.keys()];
}


edge_id(from:string, to:string) {
edge_id(from: mNT, to: mNT) {
return this._edge_map.has(from)? (this._edge_map.get(from) : any).get(to) : undefined;
}

edge(from:string, to:string) {
edge(from: mNT, to: mNT) {
const id = this.edge_id(from, to);
return (id === undefined)? undefined : this._edges[id];
}
Expand All @@ -196,11 +196,11 @@ todo comeback
}


actions_for(whichState : string) : Array<string> {
actions_for(whichState : mNT) : Array<mNT> {
return [... ((this._reverse_actions.get(whichState) || new Map()).keys() || [])]; // wasteful
}

action_found_on_states(whichState : string) : Array<string> {
action_found_on_states(whichState : mNT) : Array<mNT> {
return [... ((this._actions.get(whichState) || new Map()).keys() || [])]; // wasteful
}
/*
Expand All @@ -213,10 +213,10 @@ todo comeback
}
*/

action_exits_at(whichState : string) : Array<string> {
action_exits_at(whichState : mNT) : Array<mNT> {
return [... (this._reverse_actions.get(whichState) || new Map()).values()] // wasteful
.map( (edgeId:number) => this._edges[edgeId] ) // whargarbl burn out any
.filter( (o:any) => o.from === whichState)
.filter( o => o.from === whichState)
.map( filtered => filtered.to );
}

Expand All @@ -239,28 +239,28 @@ todo comeback
}


action(name : string, new_data? : mixed) : boolean {
action(name : mNT, new_data? : mDT) : boolean {
return false; // major todo whargarbl
}

transition(newState : string, new_data? : mixed) : boolean {
transition(newState : mNT, new_data? : mDT) : boolean {
return false; // major todo whargarbl
}

force_transition(newState : string, new_data? : mixed) : boolean {
force_transition(newState : mNT, new_data? : mDT) : boolean {
return false; // major todo whargarbl
}


valid_action(action : string, new_data : mixed) : boolean {
valid_action(action : mNT, new_data : mDT) : boolean {
return false; // major todo whargarbl
}

valid_transition(newState : string, new_data : mixed) : boolean {
valid_transition(newState : mNT, new_data : mDT) : boolean {
return false; // major todo whargarbl
}

valid_force_transition(newState : string, new_data : mixed) : boolean {
valid_force_transition(newState : mNT, new_data : mDT) : boolean {
return false; // major todo whargarbl
}

Expand Down

0 comments on commit 94e7405

Please sign in to comment.