Skip to content

Commit

Permalink
Trivial implementation on just .action; gated off for efficiency fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed May 3, 2022
1 parent c95411f commit f1534ee
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 44 deletions.
14 changes: 7 additions & 7 deletions benchmark/results/general.chart.html
Expand Up @@ -28,7 +28,7 @@
</head>
<body>
<div class="container">
<canvas id="chart1651474127574" width="16" height="9"></canvas>
<canvas id="chart1651604448035" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1651474127574 = document
.getElementById('chart1651474127574')
const ctx1651604448035 = document
.getElementById('chart1651604448035')
.getContext('2d')
const chart1651474127574 = new Chart(ctx1651474127574, {
const chart1651604448035 = new Chart(ctx1651604448035, {
type: 'bar',
data: {
labels: ["Blind cycle a traffic light 500 times by transition","Blind cycle a traffic light 500 times by action"],
datasets: [
{
data: [12088,9261],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(91.932, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(91.932, 85%, 55%)"],
data: [12187,9279],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(91.368, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(91.368, 85%, 55%)"],
borderWidth: 2,
},
],
Expand Down
12 changes: 6 additions & 6 deletions benchmark/results/general.json
@@ -1,19 +1,19 @@
{
"name": "General performance suite",
"date": "2022-05-02T06:48:47.574Z",
"date": "2022-05-03T19:00:48.035Z",
"version": "1.0.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 12088,
"margin": 2.32,
"ops": 12187,
"margin": 1.89,
"percentSlower": 0
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 9261,
"margin": 2.07,
"percentSlower": 23.39
"ops": 9279,
"margin": 0.95,
"percentSlower": 23.86
}
],
"fastest": {
Expand Down
1 change: 1 addition & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -37,6 +37,7 @@ declare class Machine<mDT> {
_arrange_end_declaration: Array<Array<StateType>>;
_theme: FslTheme;
_flow: FslDirection;
_has_hooks: boolean;
_hooks: Map<string, Function>;
_named_hooks: Map<string, Function>;
constructor({ start_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout }: JssmGenericConfig<mDT>);
Expand Down
38 changes: 33 additions & 5 deletions dist/es6/jssm.js
Expand Up @@ -170,6 +170,12 @@ function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
}
return edge;
}
function hook_name(from, to) {
return JSON.stringify([from, to]);
}
function named_hook_name(from, to, action) {
return JSON.stringify([from, to, action]);
}
function wrap_parse(input, options) {
return parse(input, options || {});
}
Expand Down Expand Up @@ -345,6 +351,7 @@ class Machine {
this._theme = theme;
this._flow = flow;
this._graph_layout = graph_layout;
this._has_hooks = false;
this._hooks = new Map();
this._named_hooks = new Map();
if (state_declaration) {
Expand Down Expand Up @@ -701,10 +708,10 @@ class Machine {
set_hook(HookDesc) {
switch (HookDesc.kind) {
case 'hook':
this._hooks.set(JSON.stringify([HookDesc.from, HookDesc.to]), HookDesc.handler);
this._hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
break;
case 'named':
this._named_hooks.set(JSON.stringify([HookDesc.from, HookDesc.to, HookDesc.action]), HookDesc.handler);
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
break;
case 'entry':
console.log('TODO: Should add entry hook here');
Expand All @@ -726,8 +733,27 @@ class Machine {
// todo major incomplete whargarbl comeback
if (this.valid_action(name, newData)) {
const edge = this.current_action_edge_for(name);
this._state = edge.to;
return true;
if (this._has_hooks) {
let hook_permits = undefined;
const nhn = named_hook_name(this._state, edge.to, name), maybe_hook = this._named_hooks.get(nhn);
if (maybe_hook === undefined) {
hook_permits = true;
}
else {
hook_permits = maybe_hook('TODO FIXME');
}
if (hook_permits) {
this._state = edge.to;
return true;
}
else {
return false;
}
}
else {
this._state = edge.to;
return true;
}
}
else {
return false;
Expand Down Expand Up @@ -760,7 +786,9 @@ class Machine {
}
current_action_for(action) {
const action_base = this._actions.get(action);
return action_base ? action_base.get(this.state()) : undefined;
return action_base
? action_base.get(this.state())
: undefined;
}
current_action_edge_for(action) {
const idx = this.current_action_for(action);
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.cjs.js

Large diffs are not rendered by default.

38 changes: 33 additions & 5 deletions dist/jssm.es5.cjs.nonmin.js
Expand Up @@ -16055,6 +16055,12 @@ function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
}
return edge;
}
function hook_name(from, to) {
return JSON.stringify([from, to]);
}
function named_hook_name(from, to, action) {
return JSON.stringify([from, to, action]);
}
function wrap_parse(input, options) {
return peg$parse(input, options || {});
}
Expand Down Expand Up @@ -16230,6 +16236,7 @@ class Machine {
this._theme = theme;
this._flow = flow;
this._graph_layout = graph_layout;
this._has_hooks = false;
this._hooks = new Map();
this._named_hooks = new Map();
if (state_declaration) {
Expand Down Expand Up @@ -16586,10 +16593,10 @@ class Machine {
set_hook(HookDesc) {
switch (HookDesc.kind) {
case 'hook':
this._hooks.set(JSON.stringify([HookDesc.from, HookDesc.to]), HookDesc.handler);
this._hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
break;
case 'named':
this._named_hooks.set(JSON.stringify([HookDesc.from, HookDesc.to, HookDesc.action]), HookDesc.handler);
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
break;
case 'entry':
console.log('TODO: Should add entry hook here');
Expand All @@ -16611,8 +16618,27 @@ class Machine {
// todo major incomplete whargarbl comeback
if (this.valid_action(name, newData)) {
const edge = this.current_action_edge_for(name);
this._state = edge.to;
return true;
if (this._has_hooks) {
let hook_permits = undefined;
const nhn = named_hook_name(this._state, edge.to, name), maybe_hook = this._named_hooks.get(nhn);
if (maybe_hook === undefined) {
hook_permits = true;
}
else {
hook_permits = maybe_hook('TODO FIXME');
}
if (hook_permits) {
this._state = edge.to;
return true;
}
else {
return false;
}
}
else {
this._state = edge.to;
return true;
}
}
else {
return false;
Expand Down Expand Up @@ -16645,7 +16671,9 @@ class Machine {
}
current_action_for(action) {
const action_base = this._actions.get(action);
return action_base ? action_base.get(this.state()) : undefined;
return action_base
? action_base.get(this.state())
: undefined;
}
current_action_edge_for(action) {
const idx = this.current_action_for(action);
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.iife.js

Large diffs are not rendered by default.

38 changes: 33 additions & 5 deletions dist/jssm.es5.iife.nonmin.js
Expand Up @@ -16054,6 +16054,12 @@ var jssm = (function (exports) {
}
return edge;
}
function hook_name(from, to) {
return JSON.stringify([from, to]);
}
function named_hook_name(from, to, action) {
return JSON.stringify([from, to, action]);
}
function wrap_parse(input, options) {
return peg$parse(input, options || {});
}
Expand Down Expand Up @@ -16229,6 +16235,7 @@ var jssm = (function (exports) {
this._theme = theme;
this._flow = flow;
this._graph_layout = graph_layout;
this._has_hooks = false;
this._hooks = new Map();
this._named_hooks = new Map();
if (state_declaration) {
Expand Down Expand Up @@ -16585,10 +16592,10 @@ var jssm = (function (exports) {
set_hook(HookDesc) {
switch (HookDesc.kind) {
case 'hook':
this._hooks.set(JSON.stringify([HookDesc.from, HookDesc.to]), HookDesc.handler);
this._hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
break;
case 'named':
this._named_hooks.set(JSON.stringify([HookDesc.from, HookDesc.to, HookDesc.action]), HookDesc.handler);
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
break;
case 'entry':
console.log('TODO: Should add entry hook here');
Expand All @@ -16610,8 +16617,27 @@ var jssm = (function (exports) {
// todo major incomplete whargarbl comeback
if (this.valid_action(name, newData)) {
const edge = this.current_action_edge_for(name);
this._state = edge.to;
return true;
if (this._has_hooks) {
let hook_permits = undefined;
const nhn = named_hook_name(this._state, edge.to, name), maybe_hook = this._named_hooks.get(nhn);
if (maybe_hook === undefined) {
hook_permits = true;
}
else {
hook_permits = maybe_hook('TODO FIXME');
}
if (hook_permits) {
this._state = edge.to;
return true;
}
else {
return false;
}
}
else {
this._state = edge.to;
return true;
}
}
else {
return false;
Expand Down Expand Up @@ -16644,7 +16670,9 @@ var jssm = (function (exports) {
}
current_action_for(action) {
const action_base = this._actions.get(action);
return action_base ? action_base.get(this.state()) : undefined;
return action_base
? action_base.get(this.state())
: undefined;
}
current_action_edge_for(action) {
const idx = this.current_action_for(action);
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/assets/search.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/docs/classes/Machine.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/docs/modules.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions jssm.d.ts
Expand Up @@ -37,6 +37,7 @@ declare class Machine<mDT> {
_arrange_end_declaration: Array<Array<StateType>>;
_theme: FslTheme;
_flow: FslDirection;
_has_hooks: boolean;
_hooks: Map<string, Function>;
_named_hooks: Map<string, Function>;
constructor({ start_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout }: JssmGenericConfig<mDT>);
Expand Down
34 changes: 26 additions & 8 deletions src/ts/jssm.ts
Expand Up @@ -488,6 +488,7 @@ class Machine<mDT> {
_theme : FslTheme;
_flow : FslDirection;

_has_hooks : boolean;
_hooks : Map<string, Function>;
_named_hooks : Map<string, Function>;

Expand Down Expand Up @@ -546,6 +547,7 @@ class Machine<mDT> {
this._flow = flow;
this._graph_layout = graph_layout;

this._has_hooks = false;
this._hooks = new Map();
this._named_hooks = new Map();

Expand Down Expand Up @@ -1001,11 +1003,11 @@ class Machine<mDT> {
switch (HookDesc.kind) {

case 'hook':
this._hooks.set(JSON.stringify([HookDesc.from, HookDesc.to]), HookDesc.handler);
this._hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
break;

case 'named':
this._named_hooks.set(JSON.stringify([HookDesc.from, HookDesc.to, HookDesc.action]), HookDesc.handler);
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
break;

case 'entry':
Expand Down Expand Up @@ -1034,15 +1036,31 @@ class Machine<mDT> {
// todo whargarbl implement data stuff
// todo major incomplete whargarbl comeback
if (this.valid_action(name, newData)) {
const edge : JssmTransition<mDT> = this.current_action_edge_for(name),
nhk : string = JSON.stringify([this._state, edge.to, name]); // named hook key

let hook_permits : boolean | undefined = undefined;
const edge : JssmTransition<mDT> = this.current_action_edge_for(name);

if (this._named_hooks.has
if (this._has_hooks) {

let hook_permits : boolean | undefined = undefined;

const nhn : string = named_hook_name(this._state, edge.to, name),
maybe_hook : Function | undefined = this._named_hooks.get(nhn);

if (maybe_hook === undefined) { hook_permits = true; }
else { hook_permits = maybe_hook('TODO FIXME'); }

if (hook_permits) {
this._state = edge.to;
return true;
} else {
return false;
}

} else {
this._state = edge.to;
return true;
}

this._state = edge.to;
return true;
} else {
return false;
}
Expand Down

0 comments on commit f1534ee

Please sign in to comment.