Skip to content

Commit

Permalink
Final cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
TGCrystal committed May 31, 2023
1 parent bb321ff commit 3ef208b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 70 deletions.
1 change: 1 addition & 0 deletions src/app/core/public/api/Circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface Circuit {
getObj(id: GUID): Obj | undefined;
getObjs(): Obj[];
getComponents(): Component[];
getWires(): Wire[];
getComponentInfo(kind: string): ComponentInfo | undefined;

// Object manipulation
Expand Down
24 changes: 24 additions & 0 deletions src/app/core/public/api/CircuitUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {Graph} from "math/Graph";
import {Circuit} from "./Circuit";

/**
* This function converts the provided circuit to a graph where the nodes are components and edges are wires.
* Both are represented by guids rather than object references.
*
* @param circuit The circuit to convert to a graph.
* @returns The provided circuit as a graph.
*/
export function CreateGraph(circuit: Circuit): Graph<string, string> {
const graph = new Graph<string, string>();

const objs = circuit.getComponents();
for (const obj of objs) {
graph.createNode(obj.id);
}
const wires = circuit.getWires();
for (const wire of wires) {
graph.createEdge(wire.p1.parent.id, wire.p2.parent.id, wire.id);
}

return graph;
}
26 changes: 0 additions & 26 deletions src/app/core/public/api/Utilities.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {Graph} from "math/Graph";
import {Component} from "./Component";
import {Port} from "./Port";
import {Wire} from "./Wire";
import {Circuit} from "./Circuit";


export const isObjComponent = <
Expand All @@ -24,27 +22,3 @@ export const isObjPort = <
WireT extends Wire = Wire,
PortT extends Port = Port,
>(obj: ComponentT | WireT | PortT): obj is PortT => (obj.baseKind === "Port");

// TODO[model_refactor_api_expr_to_circ](trevor) Is this the right place? Check for circular dependencies.
/**
* This function converts the provided circuit to a graph where the nodes are components and edges are wires.
* Both are represented by guids rather than object references.
*
* @param circuit The circuit to convert to a graph.
* @returns The provided circuit as a graph.
*/
export function CreateGraph(circuit: Circuit): Graph<string, string> {
const graph = new Graph<string, string>();

// TODO[model_refactor_api_expr_to_circ](trevor): Replace with getComponents and getWires functions on Circuit
const objs = circuit.getObjs().filter((obj) => (isObjComponent(obj)));
for (const obj of objs) {
graph.createNode(obj.id);
}
const wires = circuit.getObjs().filter((obj) => (isObjWire(obj))) as readonly Wire[];
for (const wire of wires) {
graph.createEdge(wire.p1.parent.id, wire.p2.parent.id, wire.id);
}

return graph;
}
11 changes: 7 additions & 4 deletions src/app/core/public/api/impl/Circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {DebugOptions, GUID} from "core/internal";
import {RenderHelper} from "core/internal/view/rendering/RenderHelper";
import {RenderOptions} from "core/internal/view/rendering/RenderOptions";

import {Camera} from "../Camera";
import {Circuit} from "../Circuit";
import {Selections} from "../Selections";
import {isObjComponent} from "../Utilities";
import {Camera} from "../Camera";
import {Circuit} from "../Circuit";
import {Selections} from "../Selections";
import {isObjComponent, isObjWire} from "../Utilities";

import {CameraImpl} from "./Camera";
import {CircuitState, CircuitTypes} from "./CircuitState";
Expand Down Expand Up @@ -139,6 +139,9 @@ export function CircuitImpl<CircuitT extends Circuit, T extends CircuitTypes>(st
getComponents(): T["Component[]"] {
return this.getObjs().filter(isObjComponent);
},
getWires(): T["Wire[]"] {
return this.getObjs().filter(isObjWire);
},
getComponentInfo(kind: string): T["ComponentInfo"] | undefined {
// TODO[.](kevin) - getComponentInfo should probably return a Result right?
// Or should we add a method to check if a component exists?
Expand Down
38 changes: 0 additions & 38 deletions src/app/tests/Extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,42 +132,4 @@ expect.extend({
pass: result.error.errors.some((error) => error.message.includes(message)),
}
},

// TODO[model_refactor_api_expr_to_circ](trevor): Check if this is/was needed for expression to circuit
// toBeConnectedTo(source: unknown, target: DigitalComponent, options = { depth: Infinity }) {
// if (!(source instanceof DigitalComponent))
// throw new Error("toBeConnectedTo can only be used with DigitalComponents!");

// const { depth } = options;

// const visited = new Set<DigitalComponent>();
// function bfs(layer: DigitalComponent[], depth: number): boolean {
// if (depth === 0 || layer.length === 0)
// return false;

// const queue = [] as DigitalComponent[];
// for (const cur of layer) {
// visited.add(cur);

// const connections = [
// ...cur.getOutputs().map((w) => w.getOutputComponent()),
// ...cur.getInputs().map((w) => w.getInputComponent()),
// ].filter((c) => !visited.has(c));

// if (connections.includes(target))
// return true;

// queue.push(...connections);
// }
// return bfs(queue, depth-1);
// }

// const pass = bfs([source], depth);

// return {
// message: () => `expected ${source.getName()} to ${pass ? "" : "not "}be connected to ${target.getName()}` +
// ` within ${options.depth} connections`,
// pass,
// };
// },
});
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ function handleUnary(currentOp: "!", tokens: readonly Token[], ops: Record<Token
*/
function handleBinary(currentOp: "|" | "^" | "&", nextOpNum: number, tokens: readonly Token[],
ops: Record<TokenType, string>, currentOpNum: number, index: number): Result<NewTreeRetValue> {
// This section gets the part of the tree from the left side of the operator.
// "!" and "(" only have operands on their right side, so this section is skipped for them
return generateInputTreeCore(tokens, ops, nextOpNum, index).andThen((leftRet): Result<NewTreeRetValue> => {
index = leftRet.index;
// If this isn't the right operation to apply, return
Expand Down

0 comments on commit 3ef208b

Please sign in to comment.