-
-
Notifications
You must be signed in to change notification settings - Fork 68
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
Model refactor api expr to circ #1238
Merged
LeonMontealegre
merged 30 commits into
model_refactor_api
from
model_refactor_api_expr_to_circ
Feb 6, 2024
Merged
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
a4fb430
add main files back in
TGCrystal 2cc1949
Merge branch 'model_refactor_api' into model_refactor_api_expr_to_circ
TGCrystal daf7d00
expr to circuit refactor progress
TGCrystal e698b47
vscode issues
TGCrystal 4085471
Merge branch 'model_refactor_api_views1' into model_refactor_api_expr…
TGCrystal b290cf7
Reimplement basic circuit tree to component algo
TGCrystal f237b81
Merge branch 'model_refactor_api' into model_refactor_api_expr_to_circ
TGCrystal a319216
Progress on Result Type
TGCrystal 2e74a00
treeToCircuitCore working better
TGCrystal 19f1f60
Incorporate Result into the rest of Expr to Circ algo
TGCrystal 89267df
Attempt at fixing expression parser imports
TGCrystal 320bbf8
Add toBeOk matcher
TGCrystal 818cfb6
Get basic test working
TGCrystal 2c8b5c4
Fix error case tests
TGCrystal 82d736c
Merge branch 'model_refactor_api_headless_view' into model_refactor_a…
TGCrystal c38943c
Uncomment last error test
TGCrystal 7168f31
Merge branch 'model_refactor_api' into model_refactor_api_expr_to_circ
TGCrystal 1227b7a
Uncommented some more
TGCrystal 4d12152
Merge branch 'model_refactor_api' into model_refactor_api_expr_to_circ
TGCrystal c49e0bd
Comment popup test
TGCrystal 1fdb90d
Finsh cleaning up tests
TGCrystal 258df28
Final changes before review
TGCrystal 0f0859b
Apply Leon's suggestions
TGCrystal ba6df30
Add `.toIncludeError` matcher
TGCrystal 274499c
Merge branch 'model_refactor_api' into model_refactor_api_expr_to_circ
TGCrystal 095f0da
Fix lingering merge issue
TGCrystal bb321ff
Remove overzealous result usage
TGCrystal 3ef208b
Final cleaning
TGCrystal 0078640
Merge branch 'model_refactor_api' into model_refactor_api_expr_to_circ
TGCrystal 32ffb96
Fix suggestions, update branch
TGCrystal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,35 @@ | ||
import {Graph} from "math/Graph"; | ||
import {Component} from "./Component"; | ||
import {Obj} from "./Obj"; | ||
import {Port} from "./Port"; | ||
import {Wire} from "./Wire"; | ||
import {Circuit} from "./Circuit"; | ||
|
||
|
||
export const isObjComponent = (obj: Obj): obj is Component => (obj.baseKind === "Component"); | ||
export const isObjWire = (obj: Obj): obj is Wire => (obj.baseKind === "Wire"); | ||
export const isObjPort = (obj: Obj): obj is Port => (obj.baseKind === "Port"); | ||
export const isObjPort = (obj: Obj): obj is Port => (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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Checks if the substring of a given input starting at a given index is equal to a given sequence. | ||
* | ||
* @param input The input string that a substring of will be examined. | ||
* @param index The starting index of input to compare at. | ||
* @param sequence The sequence to check equality with. | ||
* @returns True if input has a substring starting at index that matches sequence, false otherwise. | ||
*/ | ||
export function SubStrEquals(input: string, index: number, sequence: string): boolean { | ||
return input.slice(index, index + sequence.length) === sequence; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import crypto from "node:crypto"; | ||
import {Result} from "core/utils/Result"; | ||
import crypto from "node:crypto"; | ||
|
||
|
||
// Define crypto for Jest for uuid generation | ||
|
@@ -11,6 +12,8 @@ declare global { | |
interface Matchers<R> { | ||
toApproximatelyEqual(expected: unknown, epsilon?: number): CustomMatcherResult; | ||
toBeCloseToAngle(otherAngle: number, epsilon?: number): CustomMatcherResult; | ||
toBeOk(): CustomMatcherResult; | ||
toIncludeError(message: string): CustomMatcherResult; | ||
// toBeConnectedTo(a: DigitalComponent, options?: {depth?: number}): CustomMatcherResult; | ||
} | ||
} | ||
|
@@ -81,6 +84,56 @@ expect.extend({ | |
}; | ||
}, | ||
|
||
toBeOk(received: unknown) { | ||
if (!received || typeof received !== "object" || !("ok" in received)) { | ||
return { | ||
message: () => "supplied value is not a Result", | ||
pass: false, | ||
} | ||
} | ||
const result = received as Result; | ||
if (result.ok) { | ||
return { | ||
message: () => "expected Result to be Ok", | ||
pass: true, | ||
} | ||
} | ||
return { | ||
message: () => `expected Result to not have errors:\n - ${ | ||
result.error.errors | ||
.map((err) => err.message) | ||
.join("\n - ") | ||
}`, | ||
pass: false, | ||
} | ||
}, | ||
|
||
toIncludeError(received: unknown, message: string) { | ||
if (!received || typeof received !== "object" || !("ok" in received)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
return { | ||
message: () => "supplied value is not a Result", | ||
pass: false, | ||
} | ||
} | ||
const result = received as Result; | ||
if (result.ok) { | ||
return { | ||
message: () => "expected Result to be not be Ok", | ||
pass: false, | ||
} | ||
} | ||
return { | ||
message: () => `expected Result to contain an error including the text "${message}", ` + | ||
`instead includes:\n - ${ | ||
result.error.errors | ||
.map((err) => err.message) | ||
.join("\n - ") | ||
}`, | ||
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!"); | ||
|
2 changes: 1 addition & 1 deletion
2
src/site/pages/digital/src/containers/ExprToCircuitPopup/CustomOps.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/site/pages/digital/src/containers/ExprToCircuitPopup/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TGCrystal marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// TODO[model_refactor_api](trevor): Get this working for expression to circuit frontend | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not get this working now ? |
||
// import {Vector} from "Vector"; | ||
|
||
// import {CreateGraph, IOObjectSet} from "./ComponentUtils"; | ||
|
||
|
||
// const ORGANIZE_SEP_X = 4; | ||
// const ORGANIZE_SEP_Y = 3; | ||
|
||
// function OrganizeCore(groups: IOObjectSet, start: Vector, depths: number[][]): void { | ||
// // Depths is a 2d array where the index of the inner array indicates the depth of all of the nodes inside that array | ||
// const components = groups.getComponents(); | ||
|
||
// depths.forEach((nodes, depth) => | ||
// nodes.forEach((node, index) => | ||
// // extra space for labels | ||
// components[node].setPos(start.add(ORGANIZE_SEP_X*(depth - (depths.length - 1)/2) + ORGANIZE_SEP_X/2, | ||
// -ORGANIZE_SEP_Y*(index - (nodes.length - 1)/2))) | ||
// ) | ||
// ); | ||
// } | ||
|
||
// /** | ||
// * Organizes the components so that components at greater depth are further to the right, | ||
// * using the getMaxNodeDepths function of Graph to accomplish this. | ||
// * | ||
// * @param groups The components to organize. | ||
// * @param start The top left coordinate where the organization should start. | ||
// */ | ||
// export function OrganizeMaxDepth(groups: IOObjectSet, start: Vector): void { | ||
// OrganizeCore(groups, start, CreateGraph(groups).getMaxNodeDepths()); | ||
// } | ||
|
||
// /** | ||
// * Organizes the components so that components at greater depth are further to the right, | ||
// * using the getMinNodeDepths function of Graph to accomplish this. | ||
// * | ||
// * @param groups The components to organize. | ||
// * @param start The top left coordinate where the organization should start. | ||
// */ | ||
// export function OrganizeMinDepth(groups: IOObjectSet, start: Vector): void { | ||
// OrganizeCore(groups, start, CreateGraph(groups).getMinNodeDepths()); | ||
// } |
66 changes: 66 additions & 0 deletions
66
src/site/pages/digital/src/utils/ExpressionParser/Constants/DataStructures.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** Represents the operand of a binary operation. */ | ||
export type InputTreeBinOpType = "|" | "^" | "&"; | ||
/** Represents the operand of a unary operation. */ | ||
export type InputTreeUnOpType = "!"; | ||
/** Represents operands of both unary and binary operations. */ | ||
export type InputTreeOpType = InputTreeBinOpType | InputTreeUnOpType; | ||
export type ParenType = "(" | ")"; | ||
export type TokenType = | ||
| InputTreeOpType | ||
| ParenType; | ||
|
||
/** Represents that a "binary" operation can have 2-8 children. */ | ||
export type BinOpChildren = [InputTree, InputTree, InputTree?, InputTree?, | ||
InputTree?, InputTree?, InputTree?, InputTree?]; | ||
|
||
/** Represents a node on the parsed tree that itself represents an input component. */ | ||
export interface InputTreeIdent { | ||
kind: "leaf"; | ||
ident: string; | ||
} | ||
/** Represents a node on the parsed tree that itself represents a unary operation. */ | ||
export interface InputTreeUnOpNode { | ||
kind: "unop"; | ||
type: InputTreeUnOpType; | ||
child: InputTree; | ||
} | ||
/** Represents a node on the parsed tree that itself represents a binary operation. */ | ||
export interface InputTreeBinOpNode { | ||
kind: "binop"; | ||
type: InputTreeBinOpType; | ||
isNot: boolean; // true for NAND, false for AND | ||
// Can have 2-8 children | ||
children: BinOpChildren; | ||
} | ||
/** Represents all possible types of nodes for the input tree. */ | ||
export type InputTree = | ||
| InputTreeIdent | ||
| InputTreeUnOpNode | ||
| InputTreeBinOpNode | ||
|
||
/** | ||
* Used to represent a unary operation, binary operation, or parenthesis when parsing the intial expression | ||
* to a token list. | ||
*/ | ||
export interface OpToken { | ||
type: TokenType; | ||
} | ||
/** Used to represent the token of an input component when parsing the initial expression to a token list. */ | ||
export interface InputToken { | ||
type: "input"; | ||
name: string; | ||
} | ||
export type Token = | ||
| OpToken | ||
| InputToken; | ||
|
||
/** When adding a new format, this type must also be expanded to include its new unique icon. */ | ||
export type OperatorFormatLabel = "|" | "||" | "+" | "+_" | "OR" | "or" | "custom"; | ||
|
||
/** Represents a format to represent the operators in the expression. */ | ||
export type OperatorFormat = { | ||
label: string; // Text displayed to explain the OperatorFormat | ||
separator: string; | ||
icon: OperatorFormatLabel; // All icons should be unique (and "custom" should be reserved for frontend) | ||
ops: Record<TokenType, string>; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just say
received: Result
and then you don't have to type-check it, I should probably do that for the others but I was probably referencing some js