complete basic structures
This commit is contained in:
		| @@ -2,6 +2,11 @@ export class Edge { | ||||
|   public origin: string; | ||||
|   public target: string; | ||||
|  | ||||
|   public constructor (origin: string, target: string) { | ||||
|     this.origin = origin; | ||||
|     this.target = target; | ||||
|   } | ||||
|  | ||||
|   // eslint-disable-next-line @typescript-eslint/naming-convention | ||||
|   public toString (): string { | ||||
|     return `${this.origin} -> ${this.target}`; | ||||
|   | ||||
							
								
								
									
										17
									
								
								lib/Element.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								lib/Element.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| export class Element { | ||||
|   public name: string; | ||||
|   public parent: string; | ||||
|   public get full_name (): string { | ||||
|     return `${this.parent}_${this.name}`; | ||||
|   } | ||||
|  | ||||
|   public constructor (name: string, parent = '') { | ||||
|     this.name = name; | ||||
|     this.parent = parent; | ||||
|   } | ||||
|  | ||||
|   // eslint-disable-next-line @typescript-eslint/naming-convention | ||||
|   public toString (): string { | ||||
|     return this.full_name; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										55
									
								
								lib/Graph.ts
									
									
									
									
									
								
							
							
						
						
									
										55
									
								
								lib/Graph.ts
									
									
									
									
									
								
							| @@ -1,23 +1,54 @@ | ||||
| import { Node } from './Node'; | ||||
| import { Element } from './Element'; | ||||
| import { Edge } from './Edge'; | ||||
| import { Node } from './Node'; | ||||
|  | ||||
| export class Graph extends Node { | ||||
| export class Graph extends Element { | ||||
|   public children: Array<Graph> = []; | ||||
|   public nodes: Array<GraphNode> = []; | ||||
|   public nodes: Array<Node> = []; | ||||
|   public is_root = false; | ||||
|   public edges: Array<Edge>; | ||||
|   public edges: Array<Edge> = []; | ||||
|  | ||||
|   // eslint-disable-next-line @typescript-eslint/naming-convention | ||||
|   public toString (): string { | ||||
|     return `subgraph cluster_${this.full_name} {  | ||||
|   ${this.children.map ((c) => c.toString ()) | ||||
|     .join ('\n')} | ||||
|    | ||||
|   public toString (level = 0): string { | ||||
|     return `subgraph cluster_${this.full_name} { | ||||
|   ${this.children.map ((c) => c.toString (level + 1)) | ||||
|     .join ('\n  ')} | ||||
|  | ||||
|   ${this.nodes.map ((c) => c.toString ()) | ||||
|     .join ('\n')} | ||||
|     .join ('\n  ')} | ||||
|  | ||||
|   ${this.edges.map ((c) => c.toString ()) | ||||
|     .join ('\n')} | ||||
| }`; | ||||
|     .join ('\n  ')} | ||||
| }`.replace (/\n/gu, `\n${'  '.repeat (level)}`) | ||||
|       .replace (/^\s+$/gmu, ''); | ||||
|   } | ||||
|  | ||||
|   public add_node (constructor: () => Node): void { | ||||
|     const node = constructor (); | ||||
|     node.parent = this.full_name; | ||||
|     this.nodes.push (node); | ||||
|   } | ||||
|  | ||||
|   public add_graph (constructor: () => Graph): void { | ||||
|     const graph = constructor (); | ||||
|     graph.parent = this.full_name; | ||||
|     graph.update_parent (); | ||||
|     this.children.push (graph); | ||||
|   } | ||||
|  | ||||
|   public update_parent (): void { | ||||
|     for (const node of this.nodes) | ||||
|       node.parent = this.full_name; | ||||
|  | ||||
|     for (const graph of this.children) { | ||||
|       graph.parent = this.full_name; | ||||
|       graph.update_parent (); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   public add_edge (origin: string, target: string): void { | ||||
|     this.edges.push ( | ||||
|       new Edge (`${this.full_name}_${origin}`, `${this.full_name}_${target}`) | ||||
|     ); | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -1,20 +0,0 @@ | ||||
| import { Node } from './Node'; | ||||
|  | ||||
| export class GraphNode extends Node { | ||||
|   public label: string; | ||||
|   public is_table: boolean; | ||||
|   public table_contents: Array<Array<string>>; | ||||
|  | ||||
|   private get serialized_table (): string { | ||||
|     const mapped_columns = this.table_contents | ||||
|       .map ((val) => `<td>${val.join ('</td><td>')}</td>`); | ||||
|     return `<table><tr>${mapped_columns.join ('</tr><tr>')}</tr></table>`; | ||||
|   } | ||||
|  | ||||
|   // eslint-disable-next-line @typescript-eslint/naming-convention | ||||
|   public toString (): string { | ||||
|     return `${this.full_name}[label=<${this.is_table | ||||
|       ? this._serialized_table | ||||
|       : this.label}>]`; | ||||
|   } | ||||
| } | ||||
							
								
								
									
										33
									
								
								lib/Node.ts
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								lib/Node.ts
									
									
									
									
									
								
							| @@ -1,12 +1,33 @@ | ||||
| export class Node { | ||||
|   public name; | ||||
|   public parent; | ||||
|   public get full_name (): string { | ||||
|     return `${this.parent}_${this.name}`; | ||||
| import { Element } from './Element'; | ||||
|  | ||||
| export class Node extends Element { | ||||
|   public label?: string; | ||||
|   public is_table = false; | ||||
|   public table_contents?: Array<Array<string>>; | ||||
|  | ||||
|   public constructor (name: string, parent?: string, label?: string) { | ||||
|     super (name, parent); | ||||
|     this.label = label; | ||||
|   } | ||||
|  | ||||
|   private get serialized_table (): string { | ||||
|     if (typeof this.table_contents === 'undefined') | ||||
|       throw new Error ('table contents are undefined'); | ||||
|  | ||||
|     const mapped_columns = this.table_contents | ||||
|       .map ((val) => `<td>${val.join ('</td><td>')}</td>`); | ||||
|     return `<table>\n  <tr>${ | ||||
|       mapped_columns.join ('</tr>\n  <tr>') | ||||
|     }</tr>\n</table>`; | ||||
|   } | ||||
|  | ||||
|   // eslint-disable-next-line @typescript-eslint/naming-convention | ||||
|   public toString (): string { | ||||
|     return this.full_name; | ||||
|     if (this.is_table || typeof this.label !== 'undefined') { | ||||
|       return `${this.full_name} [label=<${this.is_table | ||||
|         ? this.serialized_table | ||||
|         : this.label}>]`; | ||||
|     } | ||||
|     return `${this.full_name}`; | ||||
|   } | ||||
| } | ||||
|   | ||||
| @@ -1,4 +1,4 @@ | ||||
| export { Graph } from './Graph'; | ||||
| export { GraphNode } from './GraphNode'; | ||||
| export { Node } from './Node'; | ||||
| export { Element } from './Element'; | ||||
| export { Edge } from './Edge'; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user