-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Update Json Graph Traversal Algorithm #431
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
Open
acpaquette
wants to merge
17
commits into
AykutSarac:main
Choose a base branch
from
acpaquette:traversal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
21cb36a
Initial messing with json traversal
acpaquette 4d44168
Further messing
acpaquette f1655c2
Finished node traversal algorithm
acpaquette 78d75e0
Final pass on the traversal algorithm
acpaquette 15b5ab3
Added check for empty array nodes
acpaquette 3553317
Fixed added comments and fixed linting issues
acpaquette 8e15150
Filtered out object nodes with no text
acpaquette 93fa96e
Update comment to traversal algorithm
acpaquette 53b8efb
Update comment to traversal algorithm
acpaquette 1bdb0a4
Update comment to traversal algorithm
acpaquette 1bd16f7
Update comment to traversal algorithm
acpaquette 866e9b4
Update comment to traversal algorithm
acpaquette b746ef7
Update comment to traversal algorithm
acpaquette 46d694a
Further updated comments similar to previous suggestions
acpaquette ddcb27d
Fix array traversal
acpaquette dfe346c
Update src/containers/Editor/components/views/GraphView/lib/utils/tra…
acpaquette 5eca661
Update src/containers/Editor/components/views/GraphView/lib/utils/tra…
acpaquette File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
324 changes: 96 additions & 228 deletions
324
src/containers/Editor/components/views/GraphView/lib/utils/traverse.ts
This file contains hidden or 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,258 +1,126 @@ | ||
import type { Node, NodeType } from "jsonc-parser"; | ||
import type { | ||
Graph, | ||
States, | ||
} from "src/containers/Editor/components/views/GraphView/lib/jsonParser"; | ||
import { calculateNodeSize } from "src/containers/Editor/components/views/GraphView/lib/utils/calculateNodeSize"; | ||
// import type { Node, NodeType } from "jsonc-parser"; | ||
import type { Node } from "jsonc-parser"; | ||
import type { States } from "src/containers/Editor/components/views/GraphView/lib/jsonParser"; | ||
import { addEdgeToGraph } from "./addEdgeToGraph"; | ||
import { addNodeToGraph } from "./addNodeToGraph"; | ||
|
||
type PrimitiveOrNullType = "boolean" | "string" | "number" | "null"; | ||
|
||
type Traverse = { | ||
states: States; | ||
objectToTraverse: Node; | ||
parentType?: string; | ||
myParentId?: string; | ||
myParentId: string; | ||
nextType?: string; | ||
}; | ||
|
||
const isPrimitiveOrNullType = (type: unknown): type is PrimitiveOrNullType => { | ||
return ["boolean", "string", "number", "null"].includes(type as string); | ||
}; | ||
|
||
const alignChildren = (nodeA: Node, nodeB: Node): number => { | ||
const aChildType = nodeA?.children?.[1]?.type; | ||
const bChildType = nodeB?.children?.[1]?.type; | ||
|
||
if (isPrimitiveOrNullType(aChildType) && !isPrimitiveOrNullType(bChildType)) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
}; | ||
|
||
function handleNoChildren( | ||
value: string | undefined, | ||
states: States, | ||
graph: Graph, | ||
myParentId?: string, | ||
parentType?: string, | ||
nextType?: string | ||
) { | ||
if (value === undefined) return; | ||
const traverseArray = (states: States, array: Node, parentId: string) => { | ||
// Unpack input args | ||
const graph = states.graph; | ||
|
||
if (parentType === "property" && nextType !== "object" && nextType !== "array") { | ||
states.brothersParentId = myParentId; | ||
if (nextType === undefined && Array.isArray(states.brothersNode)) { | ||
states.brothersNode.push([states.brotherKey, value]); | ||
} else { | ||
states.brotherKey = value; | ||
// Check that the array has children. | ||
if (array.children) { | ||
// Records the number of child elements the array will have. | ||
const parentNode = graph.nodes.at(Number(parentId) - 1); | ||
if (parentNode) { | ||
parentNode.data.childrenCount = array.children.length; | ||
} | ||
} else if (parentType === "array") { | ||
const nodeFromArrayId = addNodeToGraph({ graph, text: String(value) }); | ||
|
||
if (myParentId) { | ||
addEdgeToGraph(graph, myParentId, nodeFromArrayId); | ||
} | ||
} | ||
// Begin looping over each array element processing accordingly. | ||
for (let i = 0; i < array.children.length; i++) { | ||
const child = array.children[i]; | ||
|
||
// Set up the node text. | ||
// For an array of complex type (object/array), we need | ||
// to construct dummy nodes that handle either nested arrays | ||
// or multiple nodes within an object. | ||
// For simple types, we can just read in the child object's value. | ||
let nodeText = ""; | ||
if (child.value !== undefined) { | ||
nodeText = String(child.value); | ||
} | ||
|
||
if (nextType && parentType !== "array" && (nextType === "object" || nextType === "array")) { | ||
states.parentName = value; | ||
const nodeId = addNodeToGraph({ | ||
graph, | ||
text: String(nodeText), | ||
type: child.type, | ||
}); | ||
addEdgeToGraph(graph, parentId, nodeId); | ||
|
||
// Call the appropriate traversal function | ||
// or end if there are no more nested elements. | ||
if (child.type === "array") { | ||
traverseArray(states, array.children[i], nodeId); | ||
} else if (child.type === "object") { | ||
traverse({ objectToTraverse: child, states, myParentId: nodeId }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function handleHasChildren( | ||
type: NodeType, | ||
states: States, | ||
graph: Graph, | ||
children: Node[], | ||
myParentId?: string, | ||
parentType?: string | ||
) { | ||
let parentId: string | undefined; | ||
|
||
if (type !== "property" && states.parentName !== "") { | ||
// add last brothers node and add parent node | ||
|
||
if (states.brothersNode.length > 0) { | ||
const findBrothersNode = states.brothersNodeProps.find( | ||
e => | ||
e.parentId === states.brothersParentId && | ||
e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1] | ||
); | ||
|
||
if (findBrothersNode) { | ||
const findNodeIndex = graph.nodes.findIndex(e => e.id === findBrothersNode?.id); | ||
|
||
if (findNodeIndex !== -1) { | ||
const modifyNodes = [...graph.nodes]; | ||
const foundNode = modifyNodes[findNodeIndex]; | ||
|
||
foundNode.text = foundNode.text.concat(states.brothersNode as any); | ||
const { width, height } = calculateNodeSize(foundNode.text, false); | ||
|
||
foundNode.width = width; | ||
foundNode.height = height; | ||
|
||
graph.nodes = modifyNodes; | ||
states.brothersNode = []; | ||
} | ||
} else { | ||
const brothersNodeId = addNodeToGraph({ graph, text: states.brothersNode }); | ||
|
||
states.brothersNode = []; | ||
}; | ||
|
||
if (states.brothersParentId) { | ||
addEdgeToGraph(graph, states.brothersParentId, brothersNodeId); | ||
} else { | ||
states.notHaveParent.push(brothersNodeId); | ||
export const traverse = ({ objectToTraverse, states, myParentId }: Traverse) => { | ||
// Unpack input arguments | ||
const graph = states.graph; | ||
const { children, type } = objectToTraverse; | ||
|
||
// Set up initial step conditions. | ||
let nodeId = ""; | ||
const nodeText: [string, string][] = []; | ||
const childQueue: Node[] = []; | ||
if (children) { | ||
if (type === "object") { | ||
// Loop over the children of the JSON node | ||
for (let i = 0; i < children.length; i++) { | ||
const child = children[i]; | ||
if (child.children) { | ||
// If the child of our child is not an array or object, it is a property; | ||
// record it into the nodeText. | ||
acpaquette marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Otherwise, push it onto the nodes to be traversed. | ||
if (child.children[1].type !== "object" && child.children[1].type !== "array") { | ||
nodeText.push([child.children[0].value, child.children[1].value]); | ||
} else { | ||
childQueue.push(child); | ||
} | ||
} | ||
|
||
states.brothersNodeProps.push({ | ||
id: brothersNodeId, | ||
parentId: states.brothersParentId, | ||
objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1], | ||
}); | ||
} | ||
} | ||
} | ||
|
||
// Add parent node | ||
parentId = addNodeToGraph({ graph, type, text: states.parentName }); | ||
states.bracketOpen.push({ id: parentId, type }); | ||
states.parentName = ""; | ||
|
||
// Add edges from parent node | ||
const brothersProps = states.brothersNodeProps.filter( | ||
e => | ||
e.parentId === myParentId && | ||
e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1] | ||
); | ||
|
||
if ( | ||
(brothersProps.length > 0 && | ||
states.bracketOpen[states.bracketOpen.length - 2]?.type !== "object") || | ||
(brothersProps.length > 0 && states.bracketOpen.length === 1) | ||
) { | ||
addEdgeToGraph(graph, brothersProps[brothersProps.length - 1].id, parentId); | ||
} else if (myParentId) { | ||
addEdgeToGraph(graph, myParentId, parentId); | ||
} else { | ||
states.notHaveParent.push(parentId); | ||
else if (type === "array") { | ||
traverseArray(states, objectToTraverse, myParentId); | ||
} | ||
} else if (parentType === "array") { | ||
states.objectsFromArray = [...states.objectsFromArray, states.objectsFromArrayId++]; | ||
} | ||
const traverseObject = (objectToTraverse: Node, nextType: string) => { | ||
traverse({ | ||
states, | ||
objectToTraverse, | ||
parentType: type, | ||
myParentId: states.bracketOpen[states.bracketOpen.length - 1]?.id, | ||
nextType, | ||
}); | ||
}; | ||
|
||
const traverseArray = () => { | ||
children.forEach((objectToTraverse, index, array) => { | ||
const nextType = array[index + 1]?.type; | ||
|
||
traverseObject(objectToTraverse, nextType); | ||
}); | ||
}; | ||
|
||
if (type === "object") { | ||
children.sort(alignChildren); | ||
traverseArray(); | ||
} else { | ||
traverseArray(); | ||
// If we have parent and we have recorded some number of properties, | ||
// add the recorded properties as a node in the graph. | ||
if (myParentId && nodeText.length !== 0) { | ||
nodeId = addNodeToGraph({ graph, text: nodeText }); | ||
addEdgeToGraph(graph, myParentId, nodeId); | ||
} | ||
|
||
if (type !== "property") { | ||
// Add or concatenate brothers node when it is the last parent node | ||
if (states.brothersNode.length > 0) { | ||
const findBrothersNode = states.brothersNodeProps.find( | ||
e => | ||
e.parentId === states.brothersParentId && | ||
e.objectsFromArrayId === states.objectsFromArray[states.objectsFromArray.length - 1] | ||
); | ||
|
||
if (findBrothersNode) { | ||
const modifyNodes = [...graph.nodes]; | ||
const findNodeIndex = modifyNodes.findIndex(e => e.id === findBrothersNode?.id); | ||
|
||
if (modifyNodes[findNodeIndex] && typeof states.brothersNode === "string") { | ||
modifyNodes[findNodeIndex].text += states.brothersNode; | ||
|
||
const { width, height } = calculateNodeSize(modifyNodes[findNodeIndex].text, false); | ||
|
||
modifyNodes[findNodeIndex].width = width; | ||
modifyNodes[findNodeIndex].height = height; | ||
|
||
graph.nodes = modifyNodes; | ||
states.brothersNode = []; | ||
} | ||
} else { | ||
const brothersNodeId = addNodeToGraph({ graph, text: states.brothersNode }); | ||
|
||
states.brothersNode = []; | ||
|
||
if (states.brothersParentId) { | ||
addEdgeToGraph(graph, states.brothersParentId, brothersNodeId); | ||
} else { | ||
states.notHaveParent = [...states.notHaveParent, brothersNodeId]; | ||
} | ||
|
||
const brothersNodeProps = { | ||
id: brothersNodeId, | ||
parentId: states.brothersParentId, | ||
objectsFromArrayId: states.objectsFromArray[states.objectsFromArray.length - 1], | ||
}; | ||
|
||
states.brothersNodeProps = [...states.brothersNodeProps, brothersNodeProps]; | ||
// Record the number of child objects the node will have. | ||
if (myParentId) { | ||
const node = graph.nodes.at(Number(myParentId) - 1); | ||
if (node) { | ||
node.data.childrenCount = childQueue.length; | ||
if (nodeText.length !== 0) { | ||
node.data.childrenCount += 1; | ||
} | ||
} | ||
} | ||
|
||
// Close brackets | ||
if (parentType === "array") { | ||
if (states.objectsFromArray.length > 0) { | ||
states.objectsFromArray.pop(); | ||
// Iterate over the child queue, processing each child accordingly. | ||
childQueue.forEach(child => { | ||
if (child.children) { | ||
const brotherNodeId = addNodeToGraph({ | ||
graph, | ||
text: child.children[0].value, | ||
type: child.children[1].type, | ||
}); | ||
if (myParentId) { | ||
addEdgeToGraph(graph, myParentId, brotherNodeId); | ||
} | ||
} else { | ||
if (states.bracketOpen.length > 0) { | ||
states.bracketOpen.pop(); | ||
if (child.children[1].type === "object") { | ||
traverse({ objectToTraverse: child.children[1], states, myParentId: brotherNodeId }); | ||
} else if (child.children[1].type === "array") { | ||
traverseArray(states, child.children[1], brotherNodeId); | ||
} | ||
} | ||
|
||
if (parentId) { | ||
const myChildren = graph.edges.filter(edge => edge.from === parentId); | ||
const parentIndex = graph.nodes.findIndex(node => node.id === parentId); | ||
|
||
graph.nodes = graph.nodes.map((node, index) => { | ||
if (index === parentIndex) { | ||
const childrenCount = myChildren.length; | ||
|
||
return { ...node, data: { ...node.data, childrenCount } }; | ||
} | ||
return node; | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export const traverse = ({ | ||
objectToTraverse, | ||
states, | ||
myParentId, | ||
nextType, | ||
parentType, | ||
}: Traverse) => { | ||
const graph = states.graph; | ||
const { type, children, value } = objectToTraverse; | ||
|
||
if (!children) { | ||
handleNoChildren(value, states, graph, myParentId, parentType, nextType); | ||
} else if (children) { | ||
handleHasChildren(type, states, graph, children, myParentId, parentType); | ||
} | ||
}); | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.