From cdfefc699c0171e49590e19a688e7e5522a8ca7f Mon Sep 17 00:00:00 2001 From: Xie Yuheng Date: Sat, 29 Jul 2023 16:28:51 +0800 Subject: [PATCH] [graph] extract `interact` --- TODO.md | 6 +-- src/lang/graph/ActiveEdge.ts | 92 +----------------------------------- src/lang/graph/interact.ts | 91 +++++++++++++++++++++++++++++++++++ src/lang/graph/netRun.ts | 3 +- 4 files changed, 96 insertions(+), 96 deletions(-) create mode 100644 src/lang/graph/interact.ts diff --git a/TODO.md b/TODO.md index 08859658..3bdef072 100644 --- a/TODO.md +++ b/TODO.md @@ -1,8 +1,6 @@ -[graph] extract `interact` +[graph] `ActiveEdge` should be type instead of class -quit using `ActiveEdge` - -`ActiveEdge` should be type instead of class +[maybe] quit using `ActiveEdge` use new syntax diff --git a/src/lang/graph/ActiveEdge.ts b/src/lang/graph/ActiveEdge.ts index 4bcba3e9..b2298a06 100644 --- a/src/lang/graph/ActiveEdge.ts +++ b/src/lang/graph/ActiveEdge.ts @@ -1,10 +1,5 @@ -import { InternalError } from "../errors" -import { Net, Node, Port } from "../graph" -import { Mod } from "../mod" +import { Port } from "../graph" import { Rule } from "../rule" -import { netConnect } from "./netConnect" -import { netRemoveEdge } from "./netRemoveEdge" -import { netRemoveNode } from "./netRemoveNode" export class ActiveEdge { constructor( @@ -12,89 +7,4 @@ export class ActiveEdge { public end: Port, public rule: Rule, ) {} - - interact(mod: Mod, net: Net): void { - // NOTE The state of action. - const input: Array = [] - const output: Array = [] - - disconnectNode(net, this.end.node, input, output) - disconnectNode(net, this.start.node, input, output) - - net.portStack.push(...input) - - // NOTE Reconnect by rule. - for (const word of this.rule.words) { - word.apply(mod, net) - } - - reconnectOutput(net, output) - } -} - -function disconnectNode( - net: Net, - node: Node, - input: Array, - output: Array, -): void { - disconnectInput(net, node.input, input) - disconnectOutput(net, node.output, output) - netRemoveNode(net, node) -} - -function disconnectInput( - net: Net, - ports: Array, - input: Array, -): void { - for (const port of ports) { - if (!port.isPrincipal) { - if (port.connection === undefined) { - throw new InternalError( - "I meet a port without connection during disconnecting input.", - ) - } - - input.push(port.connection.port) - netRemoveEdge(net, port.connection.edge) - } - } -} - -function disconnectOutput( - net: Net, - ports: Array, - output: Array, -): void { - for (const port of ports) { - if (!port.isPrincipal) { - if (port.connection === undefined) { - throw new InternalError( - "I meet a port without connection during disconnecting output.", - ) - } - - output.unshift(port.connection.port) - netRemoveEdge(net, port.connection.edge) - } - } -} - -function reconnectOutput(net: Net, output: Array): void { - if (net.portStack.length !== output.length) { - throw new InternalError( - [ - `Resulting ports doesn't match prepared output ports`, - ` resulting ports length: ${net.portStack.length}`, - ` prepared output ports length: ${output.length}`, - ].join("\n"), - ) - } - - while (net.portStack.length > 0) { - const start = net.portStack.pop() as Port - const end = output.pop() as Port - netConnect(net, start, end) - } } diff --git a/src/lang/graph/interact.ts b/src/lang/graph/interact.ts new file mode 100644 index 00000000..fb685eab --- /dev/null +++ b/src/lang/graph/interact.ts @@ -0,0 +1,91 @@ +import { InternalError } from "../errors" +import { ActiveEdge, Net, Node, Port } from "../graph" +import { Mod } from "../mod" +import { netConnect } from "./netConnect" +import { netRemoveEdge } from "./netRemoveEdge" +import { netRemoveNode } from "./netRemoveNode" + +export function interact(mod: Mod, net: Net, activeEdge: ActiveEdge): void { + // NOTE The state of action. + const input: Array = [] + const output: Array = [] + + disconnectNode(net, activeEdge.end.node, input, output) + disconnectNode(net, activeEdge.start.node, input, output) + + net.portStack.push(...input) + + // NOTE Reconnect by rule. + for (const word of activeEdge.rule.words) { + word.apply(mod, net) + } + + reconnectOutput(net, output) +} + +function disconnectNode( + net: Net, + node: Node, + input: Array, + output: Array, +): void { + disconnectInput(net, node.input, input) + disconnectOutput(net, node.output, output) + netRemoveNode(net, node) +} + +function disconnectInput( + net: Net, + ports: Array, + input: Array, +): void { + for (const port of ports) { + if (!port.isPrincipal) { + if (port.connection === undefined) { + throw new InternalError( + "I meet a port without connection during disconnecting input.", + ) + } + + input.push(port.connection.port) + netRemoveEdge(net, port.connection.edge) + } + } +} + +function disconnectOutput( + net: Net, + ports: Array, + output: Array, +): void { + for (const port of ports) { + if (!port.isPrincipal) { + if (port.connection === undefined) { + throw new InternalError( + "I meet a port without connection during disconnecting output.", + ) + } + + output.unshift(port.connection.port) + netRemoveEdge(net, port.connection.edge) + } + } +} + +function reconnectOutput(net: Net, output: Array): void { + if (net.portStack.length !== output.length) { + throw new InternalError( + [ + `Resulting ports doesn't match prepared output ports`, + ` resulting ports length: ${net.portStack.length}`, + ` prepared output ports length: ${output.length}`, + ].join("\n"), + ) + } + + while (net.portStack.length > 0) { + const start = net.portStack.pop() as Port + const end = output.pop() as Port + netConnect(net, start, end) + } +} diff --git a/src/lang/graph/netRun.ts b/src/lang/graph/netRun.ts index 953c7a6b..57e99eac 100644 --- a/src/lang/graph/netRun.ts +++ b/src/lang/graph/netRun.ts @@ -1,5 +1,6 @@ import { InternalError } from "../errors" import { Net } from "./Net" +import { interact } from "./interact" import { netCleanUpWires } from "./netCleanUpWires" import { netCloseFreePorts } from "./netCloseFreePorts" import { netReleaseFreePorts } from "./netReleaseFreePorts" @@ -24,6 +25,6 @@ function netStep(net: Net): void { if (activeEdge === undefined) { return } else { - activeEdge.interact(net.mod, net) + interact(net.mod, net, activeEdge) } }