Skip to content

Commit

Permalink
Fix blueprint step bugs (#20)
Browse files Browse the repository at this point in the history
* fix blueprint step bugs

* version
  • Loading branch information
erhant authored Dec 14, 2023
1 parent d32af6a commit 3780042
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "firstbatch",
"version": "0.1.4",
"version": "0.1.5",
"author": "FirstBatch Team <[email protected]>",
"license": "MIT",
"contributors": [
Expand Down
8 changes: 6 additions & 2 deletions src/algorithm/blueprint/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ export class UserAction {
return new UserAction('BATCH');
}

/** Equality check with another `UserAction`. */
/** Equality check with another `UserAction`.
*
* - either they are both `BATCH` actions
* - or their underlying action types are equal
*/
eq(other: UserAction) {
return this.isBatch === other.isBatch || this.actionType.eq(other.actionType);
return (this.isBatch && other.isBatch) || this.actionType.eq(other.actionType);
}
}
11 changes: 6 additions & 5 deletions src/algorithm/blueprint/blueprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class Blueprint {
let vertex: Vertex;
if (!(state in this.map)) {
if (state === '0') {
// FIXME: edge case until API is fixes
// FIXME: edge case until API is fixed
vertex = this.vertices[0];
} else {
throw new Error('No vertex found for ' + state);
Expand All @@ -62,10 +62,11 @@ export class Blueprint {
vertex = this.map[state];
}

// find an edge from that vertex with the given action, or one that has DEFAULT signal
const edge = this.edges.find(
e => e.start.eq(vertex) && (e.edgeType.eq(action) || Signals.DEFAULT.eq(e.edgeType.actionType))
);
// find an edge from that vertex with the given action, or a DEFAULT edge
const edge =
this.edges.find(e => e.start.eq(vertex) && e.edgeType.eq(action)) ||
this.edges.find(e => Signals.DEFAULT.eq(e.edgeType.actionType));

if (!edge) {
// this should never happen if `DFAParser.validateEdges` works correctly
throw new Error('Expected to find an edge');
Expand Down
3 changes: 2 additions & 1 deletion tests/data/blueprints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const example1: string = `{
{"name": "edge4", "edge_type": "BATCH", "start": "2", "end": "3"},
{"name": "edge5", "edge_type": "DEFAULT", "start": "2", "end": "2"},
{"name": "edge6", "edge_type": "BATCH", "start": "3", "end": "2"},
{"name": "edge7", "edge_type": "DEFAULT", "start": "3", "end": "2"}
{"name": "edge7", "edge_type": "DEFAULT", "start": "3", "end": "2"},
{"name": "edge8", "edge_type": "LIKE", "start": "3", "end": "1"}
]
}`;

Expand Down
5 changes: 4 additions & 1 deletion tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('parser & blueprint', () => {
const blueprint = parser.parse();

expect(blueprint.vertices.length).toBe(3);
expect(blueprint.edges.length).toBe(8);
expect(blueprint.edges.length).toBe(9);

// signal should be added by reading the parsed blueprint
const newSignal = Signals.NEW_SIGNAL;
Expand All @@ -43,5 +43,8 @@ describe('parser & blueprint', () => {
// new signal should be able to be used in step
const [nextState] = blueprint.step('1', new UserAction(Signals.NEW_SIGNAL));
expect(nextState.name).toBe('3');

const [finalState] = blueprint.step(nextState.name, new UserAction('LIKE'));
expect(finalState.name).toBe('1');
});
});

0 comments on commit 3780042

Please sign in to comment.