### 3D Force Graph Visualization Setup - TypeScript Source: https://context7.com/typefox/language-engineering-visualization/llms.txt This snippet showcases how to create an interactive 3D force-directed graph visualization of an AST using the `3d-force-graph` library. It involves fetching a serialized AST, deserializing it, converting it to a graph format, and configuring the 3D force graph for node selection, camera controls, and hierarchical layouts. ```typescript import { AstNode, convertASTtoGraph, deserializeAST } from 'langium-ast-helper'; import ForceGraph3D from '3d-force-graph'; async function visualizeAST(dataFile: string, elementId: string) { // Fetch serialized AST const response = await fetch(`/data/${dataFile}`); const serializedAst = await response.text(); // Deserialize and convert to graph const ast = deserializeAST(serializedAst); const graph = convertASTtoGraph(ast); // Transform to 3d-force-graph format const graphData = { nodes: graph.nodes.map(node => ({ id: (node as any).$__dotID, nodeType: node.$type, node: node })), links: graph.edges.map(edge => ({ source: (edge.from as any).$__dotID, target: (edge.to as any).$__dotID })) }; // Create 3D visualization const element = document.getElementById(elementId); ForceGraph3D()(element) .dagMode('td') // top-down layout .width(800) .height(600) .nodeColor(node => `#${Math.floor(Math.random() * 16777215).toString(16)}`) .nodeLabel(node => node.nodeType) .linkDirectionalArrowLength(3.5) .linkDirectionalArrowRelPos(1) .cameraPosition({ z: 1400 }) .onNodeClick(node => { console.log(`Clicked: ${node.nodeType}`); // Access full AST node console.log(node.node); }) .graphData(graphData); } // Initialize visualization visualizeAST('langium-grammar.ast.json', 'graph-container'); ``` -------------------------------- ### Deserialize and Convert Langium AST to Graph (TypeScript) Source: https://github.com/typefox/language-engineering-visualization/blob/main/packages/langium-ast-helper/README.md This snippet demonstrates how to use the langium-ast-helper package to deserialize a JSON AST, convert it into a graph representation, and then transform that graph into a DOT program. It assumes you have a serialized AST (jsonAst) as input and shows the sequence of operations: deserialization, AST to graph conversion, and graph to DOT conversion. ```typescript import { deserializeAST, convertASTtoGraph, convertGraphtoDOT } from 'langium-ast-helper'; // get a serialized AST from the cli, a build-phase listen from a worker, etc. const jsonAst = ...; // deserialize the AST, producing an object with cross references re-constructed const ast = deserializeAST(jsonAst); // convert the AST to nodes & edges graph representation const graph = convertASTtoGraph(ast); // convert this Graph representation into a DOT program const dot = convertGraphtoDOT(graph); // do something with your program! console.log(dot); ``` -------------------------------- ### Generate DOT Program from Graph (TypeScript) Source: https://context7.com/typefox/language-engineering-visualization/llms.txt Converts a graph representation into a DOT program string, suitable for rendering with Graphviz. This process involves deserializing the AST, converting it to a graph, and then converting the graph to a DOT program, which is then saved to a file. The program includes automatic node coloring. ```TypeScript import { deserializeAST, convertASTtoGraph, convertGraphtoDOT } from 'langium-ast-helper'; import { readFileSync, writeFileSync } from 'fs'; // Full pipeline: deserialize -> graph -> DOT const serializedAst = readFileSync('./data/langium-grammar.ast.json').toString(); const ast = deserializeAST(serializedAst); const graph = convertASTtoGraph(ast); const dot = convertGraphtoDOT(graph); // Write DOT program to file writeFileSync('grammar.dot', dot); // Example DOT output: // strict digraph { // 0 [label="Grammar" style=filled fillcolor="#123456" fontcolor=white fontsize=32] // 1 [label="Rule" style=filled fillcolor="#234567" fontcolor=white fontsize=32] // 0 -> 1 // } // Render with Graphviz CLI: // $ dot -Tpng -o grammar.png grammar.dot ``` -------------------------------- ### Convert AST to Graph Representation (TypeScript) Source: https://context7.com/typefox/language-engineering-visualization/llms.txt Transforms a deserialized Langium AST into a graph data structure (nodes and edges), suitable for graph visualization libraries. It uses `convertASTtoGraph` from the `langium-ast-helper` to convert the AST, and then iterates through the nodes and edges of the resulting graph. ```TypeScript import { deserializeAST, convertASTtoGraph } from 'langium-ast-helper'; import { readFileSync } from 'fs'; // Deserialize AST const serializedAst = readFileSync('./data/statemachine-grammar.ast.json').toString(); const ast = deserializeAST(serializedAst); // Convert to graph representation const graph = convertASTtoGraph(ast); // Access graph data console.log(`Graph contains ${graph.nodes.length} nodes`); console.log(`Graph contains ${graph.edges.length} edges`); // Iterate through nodes graph.nodes.forEach(node => { console.log(`Node type: ${node.$type}`); }); // Iterate through edges graph.edges.forEach(edge => { console.log(`Edge: ${edge.from.$type} -> ${edge.to.$type}`); }); ``` -------------------------------- ### Generate D3-Compatible TreeMap Data (TypeScript) Source: https://context7.com/typefox/language-engineering-visualization/llms.txt Transforms an AST into a hierarchical tree structure that is compatible with D3.js and other tree visualization libraries. The conversion includes generating a root node with color and size metadata, and children representing the AST's subtrees. The result is saved as a JSON file for use with D3. ```TypeScript import { deserializeAST, convertASTtoTreeMap } from 'langium-ast-helper'; import { readFileSync, writeFileSync } from 'fs'; // Deserialize AST const serializedAst = readFileSync('./data/domainmodel-grammar.ast.json').toString(); const ast = deserializeAST(serializedAst); // Convert to TreeMap structure const treemap = convertASTtoTreeMap(ast); // Access TreeMap data console.log(`Root: ${treemap.title}`); console.log(`Color: ${treemap.color}`); console.log(`Size: ${treemap.size}`); console.log(`Children: ${treemap.children.length}`); // TreeMap structure is D3-compatible const treemapData = { title: "Grammar", color: "#123456", size: 15, children: [ { title: "Rule", color: "#234567", size: 5, children: [] } ] }; // Use with D3.js treemap writeFileSync('treemap-data.json', JSON.stringify(treemap, null, 2)); ``` -------------------------------- ### Deserialize Langium AST from JSON (TypeScript) Source: https://context7.com/typefox/language-engineering-visualization/llms.txt Deserializes a serialized Langium AST string, reconstructing the full AST with cross-references linked. It uses the `deserializeAST` function from the `langium-ast-helper` library to restore the object graph. The output includes AST properties and allows navigation of the AST structure. ```TypeScript import { deserializeAST } from 'langium-ast-helper'; import { readFileSync } from 'fs'; // Load serialized AST from file const serializedAst = readFileSync('./data/minilogo-program.ast.json').toString(); // Deserialize and reconstruct cross-references const ast = deserializeAST(serializedAst); // Access AST properties console.log(ast.$type); // e.g., "Program" console.log(ast.$container); // undefined for root node console.log(ast.$containerProperty); // undefined for root node // Navigate AST structure if ('statements' in ast) { console.log(`Found ${ast.statements.length} statements`); } ``` -------------------------------- ### Extract All Cross-References - TypeScript Source: https://context7.com/typefox/language-engineering-visualization/llms.txt This snippet demonstrates how to extract all cross-references from an AST. It first deserializes a serialized AST and then uses the `getASTCrossRefs` function to retrieve the cross-references. The extracted references are then iterated to display their paths and resolved targets. ```typescript import { deserializeAST, getASTCrossRefs } from 'langium-ast-helper'; import { readFileSync } from 'fs'; // Deserialize AST with cross-references const serializedAst = readFileSync('./data/statemachine-grammar.ast.json').toString(); const ast = deserializeAST(serializedAst); // Extract all cross-references const crossRefs = getASTCrossRefs(ast); console.log(`Found ${crossRefs.length} cross-references`); // Iterate through cross-references crossRefs.forEach(ref => { console.log(`Reference path: ${ref.$ref}`); if (ref.ref) { console.log(`Resolved to: ${ref.ref.$type}`); // Access referenced node properties if ('name' in ref.ref) { console.log(`Referenced name: ${ref.ref.name}`); } } else { console.log('Unresolved reference'); } }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.