55 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-04-24 12:02:32 +02:00
import { Element } from './Element';
2020-04-17 15:43:34 +02:00
import { Edge } from './Edge';
2020-04-24 12:02:32 +02:00
import { Node } from './Node';
2020-04-17 15:43:34 +02:00
2020-04-24 12:02:32 +02:00
export class Graph extends Element {
2020-04-17 15:43:34 +02:00
public children: Array<Graph> = [];
2020-04-24 12:02:32 +02:00
public nodes: Array<Node> = [];
2020-04-17 15:43:34 +02:00
public is_root = false;
2020-04-24 12:02:32 +02:00
public edges: Array<Edge> = [];
2020-04-17 15:43:34 +02:00
// eslint-disable-next-line @typescript-eslint/naming-convention
2020-04-24 12:02:32 +02:00
public toString (level = 0): string {
return `subgraph cluster_${this.full_name} {
${this.children.map ((c) => c.toString (level + 1))
.join ('\n ')}
2020-04-17 15:43:34 +02:00
${this.nodes.map ((c) => c.toString ())
2020-04-24 12:02:32 +02:00
.join ('\n ')}
2020-04-17 15:43:34 +02:00
${this.edges.map ((c) => c.toString ())
2020-04-24 12:02:32 +02:00
.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}`)
);
2020-04-17 15:43:34 +02:00
}
}