Added SSR
This commit is contained in:
@@ -4,30 +4,28 @@ import deepcopy from "deepcopy";
|
||||
import {isNonNullable, shuffleInplace, sortByProperty, uniquify} from "../utils";
|
||||
import seedrandom from "seedrandom";
|
||||
|
||||
|
||||
|
||||
function generateIds<T extends Dict<unknown>>(node: GraphNode<T>): GraphNodeWithIds<T> {
|
||||
function generateIds<T extends Dict<unknown>>(node: GraphNode<T>, nodeUid: () => string): GraphNodeWithIds<T> {
|
||||
return {
|
||||
...node,
|
||||
___uid: `${node.name.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${crypto.randomUUID().substring(0, 3)}`,
|
||||
___uid: `${node.name.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${nodeUid()}`,
|
||||
___uidInputs: [],
|
||||
___uidOutputs: []
|
||||
}
|
||||
}
|
||||
|
||||
function generateAdditional<T extends Dict<unknown>>(uid: string, type: 'i'|'h'|'o'): GraphNodeWithIds<AdditionalNode> {
|
||||
function generateAdditional<T extends Dict<unknown>>(uid: string, type: 'i'|'h'|'o', nodeUid: () => string): GraphNodeWithIds<AdditionalNode> {
|
||||
return {
|
||||
inputs: type !== 'i' ? [uid] : [],
|
||||
outputs: type !== 'o' ? [uid] : [],
|
||||
name: uid,
|
||||
___type: type,
|
||||
___uid: `${uid.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${crypto.randomUUID().substring(0, 3)}_${type}`,
|
||||
___uid: `${uid.replace(/[^a-z]/gi, '').toLowerCase().substring(0, 3)}_${nodeUid()}_${type}`,
|
||||
___uidInputs: [],
|
||||
___uidOutputs: []
|
||||
}
|
||||
}
|
||||
|
||||
function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNode<T>[]): [string[][], Dict<GraphNodeWithIds<T>>] {
|
||||
function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNode<T>[], nodeUid: () => string): [string[][], Dict<GraphNodeWithIds<T>>] {
|
||||
const nodesWithId: Dict<GraphNodeWithIds<T>> = {}
|
||||
const available = new Set(inputs)
|
||||
let queue = [...nodes]
|
||||
@@ -40,7 +38,7 @@ function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNo
|
||||
queue = queue.filter((node) => {
|
||||
const isPlaceable = node.inputs.every(input => available.has(input))
|
||||
if (isPlaceable) {
|
||||
const nodeWithId: GraphNodeWithIds<T> = generateIds(node)
|
||||
const nodeWithId: GraphNodeWithIds<T> = generateIds(node, nodeUid)
|
||||
rows[rows.length - 1].push(nodeWithId.___uid)
|
||||
nodesWithId[nodeWithId.___uid] = nodeWithId
|
||||
availableOfRow.push(...node.outputs)
|
||||
@@ -58,7 +56,7 @@ function splitIntoRows<T extends Dict<unknown>>(inputs: string[], nodes: GraphNo
|
||||
return [rows, nodesWithId]
|
||||
}
|
||||
|
||||
function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesRaw: Dict<GraphNodeWithIds<T>>, inputs: string[], outputs: string[]): [string[][], Dict<GraphNodeWithIds<T|AdditionalNode>>] {
|
||||
function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesRaw: Dict<GraphNodeWithIds<T>>, inputs: string[], outputs: string[], nodeUid: () => string): [string[][], Dict<GraphNodeWithIds<T|AdditionalNode>>] {
|
||||
const addedInputs: Dict<number> = {}
|
||||
const res: string[][] = deepcopy([[], ...rowsRaw, []])
|
||||
const resNodes: Dict<GraphNodeWithIds<T|AdditionalNode>> = deepcopy(nodesRaw)
|
||||
@@ -67,13 +65,13 @@ function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesR
|
||||
for (let rowInput of rowInputs) {
|
||||
if (rowInput in addedInputs) {
|
||||
for (let j = addedInputs[rowInput]+1; j < i+1; j++) {
|
||||
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'h')
|
||||
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'h', nodeUid)
|
||||
res[j].push(nodeWithId.___uid)
|
||||
resNodes[nodeWithId.___uid] = nodeWithId
|
||||
}
|
||||
addedInputs[rowInput] = i
|
||||
} else if (inputs.includes(rowInput)) {
|
||||
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'i')
|
||||
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowInput, 'i', nodeUid)
|
||||
res[i].push(nodeWithId.___uid)
|
||||
resNodes[nodeWithId.___uid] = nodeWithId
|
||||
addedInputs[rowInput] = i
|
||||
@@ -82,7 +80,7 @@ function addAdditionalNodes<T extends Dict<unknown>>(rowsRaw: string[][], nodesR
|
||||
const rowOutputs = uniquify(rowsRaw[i].flatMap(row => resNodes[row].outputs))
|
||||
for (let rowOutput of rowOutputs) {
|
||||
if (outputs?.includes(rowOutput)) {
|
||||
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowOutput, 'o')
|
||||
const nodeWithId: GraphNodeWithIds<AdditionalNode> = generateAdditional(rowOutput, 'o', nodeUid)
|
||||
res[i+2].push(nodeWithId.___uid)
|
||||
resNodes[nodeWithId.___uid] = nodeWithId
|
||||
}
|
||||
@@ -232,12 +230,14 @@ export function findBest<T extends Dict<unknown>>(rowsWithInOut: string[][], nod
|
||||
}
|
||||
|
||||
export function graphUntangled<T extends Dict<unknown>>(nodes: GraphNode<T>[], inputs: string[], outputs: string[], timeLimit = 0): [string[][], Dict<GraphNodeWithIds<T|AdditionalNode>>] {
|
||||
const nodeSeed = seedrandom.alea("node")
|
||||
const nodeUid = () => Math.abs(nodeSeed.int32()).toString(16).substring(0, 3)
|
||||
//console.log('---------------')
|
||||
const [rowsRaw, nodesRaw] = splitIntoRows(inputs, nodes)
|
||||
const [rowsRaw, nodesRaw] = splitIntoRows(inputs, nodes, nodeUid)
|
||||
//console.log("Step 1")
|
||||
//console.table(rowsRaw)
|
||||
//console.table(nodesRaw)
|
||||
const [rowsWithInOut, nodesWithInOut] = addAdditionalNodes(rowsRaw, nodesRaw, inputs, outputs)
|
||||
const [rowsWithInOut, nodesWithInOut] = addAdditionalNodes(rowsRaw, nodesRaw, inputs, outputs, nodeUid)
|
||||
//console.log("Step 2")
|
||||
//console.table(rowsWithInOut)
|
||||
//console.table(nodesWithInOut)
|
||||
|
||||
Reference in New Issue
Block a user