Skip to content

Commit

Permalink
Rudimentary demo page in place. Browserify now exporting. Basic API.
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed May 7, 2017
1 parent c1c78f6 commit 791328b
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "jssm",
"version": "0.5.1",
"version": "0.6.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 All @@ -9,7 +9,7 @@
"babel": "babel src/js -d build/",
"setver": "node ./set_version.js",
"rename": "node -e \"require('fs').renameSync('./build/jssm.js', './build/jssm.es5.js');\"",
"pack": "browserify build/jssm.es5.js > build/jssm.es5.amd.js",
"pack": "browserify -r ./build/jssm.es5.js:jssm -o build/jssm.es5.amd.js",
"make": "npm run clean && npm run babel && npm run rename && npm run setver && npm run pack",
"flow": "flow",
"eslint": "eslint src/js/jssm.js",
Expand Down
18 changes: 18 additions & 0 deletions src/demo/index.html
@@ -0,0 +1,18 @@
<!doctype html>
<html>

<head>

<link rel="stylesheet" type="text/css" href="./style.css" />

<script defer type="text/javascript" src="../../build/jssm.es5.amd.js"></script>

<script defer type="text/javascript">

</script>

</head>

<body></body>

</html>
2 changes: 2 additions & 0 deletions src/demo/style.css
@@ -0,0 +1,2 @@
html, body { padding: 0; margin: 0; border: 0; font-family: helvetica neue, helvetica, arial, sans-serif; }
body { padding: 2em; }
13 changes: 4 additions & 9 deletions src/js/jssm-tests.js
Expand Up @@ -9,16 +9,8 @@ const jssm = require('../../build/jssm.es5.js');

test('build-set version number is present', t => t.is(typeof jssm.version, 'string'));

// build 200 tests that delay up to 3 sec each. completes in 3 sec because they're all
// being run in parallel

const seq = upTo => new Array(upTo).fill(false).map( (_,i) => i );

/*
seq(200).map(i => test(`Delay test ${i}`, t =>
new Promise( (res, rej) => setTimeout(() => { t.true(); res('res'); }, Math.random() * 3000))) );
*/




Expand All @@ -27,10 +19,13 @@ function promise_delay(how_long, f) {
return new Promise( (resolve, reject) => setTimeout( () => { resolve(f()); }, how_long ) );
}

// todo whargarbl get rid of this nonsense before 1.0
seq(3000).map(i =>
test(`Delay test ${i}`, t => promise_delay(Math.random() * 5000, () => { t.is(1,1); return 'res'; }))
test(`Delay test ${i}`, t => promise_delay(Math.random() * 1500, () => { t.is(1,1); return 'res'; }))
);

// p_test('text', 'expected', test());




Expand Down
115 changes: 84 additions & 31 deletions src/js/jssm-types.js
Expand Up @@ -5,42 +5,85 @@



type JssmEdgePermitter<NT, DT> = (OldState: NT, NewState: NT, OldData: DT, NewData: DT) => boolean;
type JssmEdgePermitterDef<NT, DT> = JssmEdgePermitter<NT, DT> | Array< JssmEdgePermitter<NT, DT> >;

type JssmEdge<NT, DT> = {
from : NT,
to : NT,
name? : string,
valid? : JssmEdgePermitterDef<NT, DT>, // validate this edge's transition; usually about data
likelihood? : number, // for stoch modelling
usual? : NT // most common exit, for graphing; likelihood overrides
};
type JssmSuccess = { success: true };
type JssmFailure = { success: false, error: mixed };
type JssmIncomplete = { success: 'incomplete' };
type JssmResult = JssmSuccess | JssmFailure | JssmIncomplete;




type ARD = 'allow' | 'require' | 'disallow';




type JssmEdges<NT, DT> = Array< JssmEdge<NT, DT> >;

type JssmTransitionPermitter<NT, DT> = (OldState: NT, NewState: NT, OldData: DT, NewData: DT) => boolean;
type JssmTransitionPermitterMaybeArray<NT, DT> = JssmTransitionPermitter<NT, DT> | Array< JssmTransitionPermitter<NT, DT> >;




type JssmStatePermitter<NT, DT> = (OldState: NT, NewState: NT, OldData: DT, NewData: DT) => boolean;
type JssmStatePermitterDef<NT, DT> = JssmStatePermitter<NT, DT> | Array< JssmStatePermitter<NT, DT> >;

type JssmStatePermitter<NT, DT> = (OldState: NT, NewState: NT, OldData: DT, NewData: DT) => boolean;
type JssmStatePermitterMaybeArray<NT, DT> = JssmStatePermitter<NT, DT> | Array< JssmStatePermitter<NT, DT> >;

type JssmGenericMachine<NT, DT> = {

name? : string,
state : NT,
data? : DT,
nodes? : Array<NT>,
edges : JssmEdges<NT, DT>,
valid? : JssmStatePermitterDef<NT, DT>,
name? : string,
state : NT,
data? : DT,
nodes? : Array<NT>,
transitions : JssmTransitions<NT, DT>,
valid? : JssmStatePermitterMaybeArray<NT, DT>,

min_transitions? : number,
max_transitions? : number,

allow_empty? : boolean,
allow_islands? : boolean,
allow_force? : boolean

};



min_edges? : number,
max_edges? : number,

allow_empty? : boolean,
allow_islands? : boolean,
allow_force? : boolean

type JssmTransition<NT, DT> = {
from : NT,
to : NT,
name? : string,
action? : string,
valid? : JssmTransitionPermitterMaybeArray<NT, DT>, // validate this edge's transition; usually about data
likelihood? : number, // for stoch modelling, would like to constrain to [0..1], dunno how
usual? : '' // most common exit, for graphing; likelihood overrides
};

type JssmTransitions<NT, DT> = Array< JssmTransition<NT, DT> >;





type JssmGenericConfig<NT, DT> = {

initial_state : NT,

transitions : JssmTransitions<NT, DT>,

name? : string,
data? : mixed,
nodes? : Array<NT>, // uncommon
valid? : JssmStatePermitterMaybeArray<NT, DT>,

//locked? : bool = true,
min_exits? : number,
max_exits? : number,
allow_islands? : false,
allow_force? : false,
actions? : ARD

};

Expand All @@ -49,24 +92,34 @@ type JssmGenericMachine<NT, DT> = {


type JssmMachine = JssmGenericMachine<string, mixed>;
type JssmConfig = JssmGenericConfig<string, mixed>;





export type {

JssmEdgePermitter,
JssmEdgePermitterDef,
JssmResult,
JssmSuccess,
JssmFailure,
JssmIncomplete,

ARD,

JssmEdge,
JssmEdges,
JssmTransitionPermitter,
JssmTransitionPermitterMaybeArray,

JssmTransition,
JssmTransitions,

JssmStatePermitter,
JssmStatePermitterDef,
JssmStatePermitterMaybeArray,

JssmGenericMachine,

JssmMachine
JssmMachine,

JssmConfig

};
119 changes: 117 additions & 2 deletions src/js/jssm.js
@@ -1,7 +1,7 @@

// @flow

import type { JssmMachine } from './jssm-types';
import type { JssmMachine, JssmConfig, JssmTransitions } from './jssm-types';

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

Expand All @@ -10,13 +10,128 @@ const version = null; // replaced from package.js in build


const new_machine = (props:mixed) : JssmMachine => {
return {state:'1', edges: []}; // whargarbl this should not work
return {state:'1', transitions: []}; // whargarbl this should not work
};





class jssm {


constructor({ transitions } : JssmConfig) {

transitions.map(tr => {
console.log(`hook up ${JSON.stringify(tr)}`);
});

}



state() : string {
return ''; // todo whargarbl
}

machine_state() : mixed {
return {}; // todo whargarbl
}


nodes() : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}

transitions() : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}

named_transitions() : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}

actions() : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}


transitions_for(whichNode : string) : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}

entrances_for(whichNode : string) : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}

exits_for(whichNode : string) : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}


actions_for(whichNode : string) : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}

action_entrances_for(whichNode : string) : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}

action_exits_for(whichNode : string) : Array<mixed> { // todo whargarbl
return []; // todo whargarbl
}


is_unenterable(whichNode : string) : boolean {
return this.entrances_for(whichNode).length === 0;
}

has_unenterables() : boolean {
return this.nodes.some(this.is_unenterable);
}


is_terminal(whichNode : string) : boolean {
return this.exits_for(whichNode).length === 0;
}

has_terminals() : boolean {
return this.nodes.some(this.is_terminal);
}


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

transition(newNode : string, new_data? : mixed) : boolean {
return false; // todo whargarbl
}

force_transition(newNode : string, new_data? : mixed) : boolean {
return false; // todo whargarbl
}


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

valid_transition(newNode : string, new_data : mixed) : boolean {
return false; // todo whargarbl
}

valid_force_transition(newNode : string, new_data : mixed) : boolean {
return false; // todo whargarbl
}


}





export {

new_machine,
Expand Down

0 comments on commit 791328b

Please sign in to comment.