Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor graph package (addresses #166) #170

Merged
merged 29 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
72f3a33
Rename some methods in graph.ts
axmmisaka Jun 22, 2023
c308ac0
Remove utility functions of addedge
axmmisaka Jun 22, 2023
8af3083
Update tests accoringly
axmmisaka Jun 22, 2023
33d99f4
Refactor hasCycle() and getOriginsOfEffect()
axmmisaka Jun 22, 2023
c061b06
Add a mermaid.js representation builder
axmmisaka Jun 22, 2023
870ff7b
Add vertices not connected to any edges
axmmisaka Jun 22, 2023
1bc3558
Remove indentation, and fix tests?
axmmisaka Jun 23, 2023
6738255
Directly check cycle without caring about collapsation
axmmisaka Jun 24, 2023
c2347bc
Revert "Directly check cycle without caring about collapsation"
axmmisaka Jun 24, 2023
fa8bfb9
Refactor weeding out port into graph.ts
axmmisaka Jun 24, 2023
98686e5
minor naming changes
axmmisaka Jun 24, 2023
a7171f1
Run prettier
axmmisaka Jun 24, 2023
aab2888
Rename graph APIs
axmmisaka Jun 26, 2023
b6a49de
Changes made together with @axmmmisaka
lhstrh Jun 27, 2023
2c4ba7c
Reverted some changed. Getting all the tests to pass.
lhstrh Jun 27, 2023
9e22241
Commit changes that were not saved
lhstrh Jun 28, 2023
4c080da
Starting to write comments. Some minor API changes.
lhstrh Jun 28, 2023
7b3e092
More comments and API change once again
lhstrh Jun 28, 2023
6f86f3f
Fix tests
axmmisaka Jun 28, 2023
2fea5e7
Fix tests
axmmisaka Jun 28, 2023
36353ee
Revert function signatures to be (origin, effect)
axmmisaka Jun 28, 2023
d0a83b6
Refactor SortablePrecedenceGraph
lhstrh Jun 28, 2023
0199dfe
Roll back some changes
lhstrh Jun 28, 2023
590f4c5
Remove one more unnecessary arg
lhstrh Jun 28, 2023
abea058
Fix comments
lhstrh Jun 28, 2023
6a32662
Add more tests, and fix mermaid edgesWithIssue bug
axmmisaka Jun 28, 2023
c523762
Run prettier on changed files
axmmisaka Jun 28, 2023
e9273c1
Add ReactionGraph class for convenience and readability
lhstrh Jun 28, 2023
554339c
Comments and formatting
lhstrh Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 73 additions & 44 deletions __tests__/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type {Reaction, Priority, Sortable} from "../src/core/internal";
import {Priority, Sortable, ReactionGraph} from "../src/core/internal";
import {
Reactor,
App,
Triggers,
Args,
InPort,
SortableDependencyGraph,
SortablePrecedenceGraph,
PrioritySet,
Log,
StringUtil
Expand Down Expand Up @@ -56,37 +56,37 @@ class CNode<T> implements Sortable<Priority> {
}

describe("Manually constructed simple precedence graphs", () => {
var graph = new SortableDependencyGraph<Sortable<Priority>>();
var graph = new ReactionGraph();
var reactor = new SR(new App());

var nodes = reactor.getNodes();

graph.addNode(nodes[0]);

it("graph equality", () => {
expect([...graph.nodes()]).toEqual(nodes);
expect([...graph.getNodes()]).toEqual(nodes);
});
});

describe("Test for corner cases", () => {
var graph = new SortableDependencyGraph<Sortable<Priority>>();
var graph = new SortablePrecedenceGraph<Sortable<Priority>>();
const node: Sortable<Priority> = new CNode<Priority>();
graph.addEdge(node, new CNode<Priority>());
graph.addEdge(new CNode<Priority>(), node);
});

describe("Manually constructed precedence graphs", () => {
var graph = new SortableDependencyGraph<Sortable<Priority>>();
var graph = new ReactionGraph();
var reactor = new R(new App());

var nodes = reactor.getNodes();

graph.addEdge(nodes[3], nodes[5]);
graph.addEdge(nodes[4], nodes[3]);
graph.addEdge(nodes[2], nodes[3]);
graph.addEdge(nodes[1], nodes[2]);
graph.addEdge(nodes[1], nodes[4]);
graph.addEdge(nodes[0], nodes[1]);
graph.addEdge(nodes[0], nodes[4]);
graph.addEdge(nodes[5], nodes[3]);
graph.addEdge(nodes[3], nodes[4]);
graph.addEdge(nodes[3], nodes[2]);
graph.addEdge(nodes[2], nodes[1]);
graph.addEdge(nodes[4], nodes[1]);
graph.addEdge(nodes[1], nodes[0]);
graph.addEdge(nodes[4], nodes[0]);

it("reaction equality", () => {
expect(Object.is(nodes[0], nodes[1])).toBeFalsy();
Expand All @@ -96,11 +96,20 @@ describe("Manually constructed precedence graphs", () => {
expect(graph.size()[0]).toEqual(6); // V
expect(graph.size()[1]).toEqual(7); // E
expect(graph.toString()).toBe(
StringUtil.dontIndent`digraph G {
"app.R[R0]"->"app.R[R1]"->"app.R[R4]"->"app.R[R3]"->"app.R[R5]";
"app.R[R0]"->"app.R[R4]";
"app.R[R1]"->"app.R[R2]"->"app.R[R3]";
}`
StringUtil.dontIndent`graph
0["app.R[R3]"]
1["app.R[R5]"]
2["app.R[R4]"]
3["app.R[R2]"]
4["app.R[R1]"]
5["app.R[R0]"]
1 --> 0
0 --> 2
0 --> 3
3 --> 4
2 --> 4
4 --> 5
2 --> 5`
);
});

Expand All @@ -115,15 +124,23 @@ describe("Manually constructed precedence graphs", () => {
});

it("remove dependency 4 -> 5", () => {
graph.removeEdge(nodes[4], nodes[3]);
graph.removeEdge(nodes[3], nodes[4]);
expect(graph.size()[0]).toEqual(6); // V
expect(graph.size()[1]).toEqual(6); // E
expect(graph.toString()).toBe(
`digraph G {
"app.R[R0]"->"app.R[R1]"->"app.R[R2]"->"app.R[R3]"->"app.R[R5]";
"app.R[R1]"->"app.R[R4]";
"app.R[R0]"->"app.R[R4]";
}`
StringUtil.dontIndent`graph
0["app.R[R3]"]
1["app.R[R5]"]
2["app.R[R4]"]
3["app.R[R2]"]
4["app.R[R1]"]
5["app.R[R0]"]
1 --> 0
0 --> 3
3 --> 4
2 --> 4
4 --> 5
2 --> 5`
);
});

Expand All @@ -133,25 +150,37 @@ describe("Manually constructed precedence graphs", () => {
expect(graph.size()[1]).toEqual(3); // E
Log.global.debug(graph.toString());
expect(graph.toString()).toBe(
StringUtil.dontIndent`digraph G {
"app.R[R2]"->"app.R[R3]"->"app.R[R5]";
"app.R[R0]"->"app.R[R4]";
}`
StringUtil.dontIndent`graph
0["app.R[R3]"]
1["app.R[R5]"]
2["app.R[R4]"]
3["app.R[R2]"]
4["app.R[R0]"]
1 --> 0
0 --> 3
2 --> 4`
);
});

it("add node 7, make 3 dependent on it", () => {
graph.addNode(nodes[6]);
graph.addEdges(nodes[2], new Set([nodes[6], nodes[3]]));
graph.addEdge(nodes[6], nodes[2]);
graph.addEdge(nodes[3], nodes[2]);
expect(graph.size()[0]).toEqual(6); // V
expect(graph.size()[1]).toEqual(4); // E
Log.global.debug(graph.toString());
expect(graph.toString()).toBe(
StringUtil.dontIndent`digraph G {
"app.R[R2]"->"app.R[R3]"->"app.R[R5]";
"app.R[R0]"->"app.R[R4]";
"app.R[R2]"->"app.R[R6]";
}`
StringUtil.dontIndent`graph
0["app.R[R3]"]
1["app.R[R5]"]
2["app.R[R4]"]
3["app.R[R2]"]
4["app.R[R0]"]
5["app.R[R6]"]
1 --> 0
0 --> 3
5 --> 3
2 --> 4`
);
});

Expand All @@ -167,25 +196,25 @@ describe("Manually constructed precedence graphs", () => {
});

it("introduce a cycle", () => {
graph.addEdge(nodes[5], nodes[2]);
graph.addEdge(nodes[2], nodes[5]);
expect(graph.updatePriorities(false)).toBeFalsy();
Log.global.debug(graph.toString());
});
});

describe("ReactionQ", () => {
var graph = new SortableDependencyGraph<Reaction<unknown>>();
var graph = new ReactionGraph();
var reactor = new R(new App());

var nodes = reactor.getNodes();

graph.addEdge(nodes[3], nodes[5]);
graph.addEdge(nodes[4], nodes[3]);
graph.addEdge(nodes[2], nodes[3]);
graph.addEdge(nodes[1], nodes[2]);
graph.addEdge(nodes[1], nodes[4]);
graph.addEdge(nodes[0], nodes[1]);
graph.addEdge(nodes[0], nodes[4]);
graph.addEdge(nodes[5], nodes[3]);
graph.addEdge(nodes[3], nodes[4]);
graph.addEdge(nodes[3], nodes[2]);
graph.addEdge(nodes[2], nodes[1]);
graph.addEdge(nodes[4], nodes[1]);
graph.addEdge(nodes[1], nodes[0]);
graph.addEdge(nodes[4], nodes[0]);
graph.updatePriorities(false);

var reactionQ = new PrioritySet<Priority>();
Expand Down
Loading