Skip to content

Commit

Permalink
feat(dot): support includes, update subgraph handling
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 3, 2020
1 parent fb2c632 commit ed53c90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/dot/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export interface Graph {
edges: Edge[];
id?: string;
nodes: IObjectOf<Partial<Node>>;
sub?: IObjectOf<Graph>;
sub?: Graph[];
include?: string;
}

export interface NodeAttribs {
Expand Down
30 changes: 16 additions & 14 deletions packages/dot/src/serialize.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { isArray } from "@thi.ng/checks";
import type { IObjectOf } from "@thi.ng/api";
import type {
Edge,
Graph,
GraphAttribs,
Node
} from "./api";
import { isArray } from "@thi.ng/checks";
import type { Edge, Graph, GraphAttribs, Node } from "./api";

let nextID = 0;

const nextSubgraphID = () => "cluster" + nextID++;

const wrapQ = (x: any) => `"${x}"`;

const escape = (x: any) =>
String(x)
.replace(/\"/g, `\\"`)
.replace(/\n/g, "\\n");
String(x).replace(/\"/g, `\\"`).replace(/\n/g, "\\n");

const formatGraphAttribs = (attribs: Partial<GraphAttribs>, acc: string[]) => {
for (let a in attribs) {
Expand Down Expand Up @@ -103,9 +100,14 @@ export const serializeEdge = (e: Edge, directed = true) => {
return acc.join("");
};

export const serializeGraph = (graph: Graph, acc?: string[]) => {
export const serializeGraph = (graph: Graph, isSub = false) => {
const directed = graph.directed !== false;
acc || (acc = [`${directed ? "di" : ""}graph ${graph.id || "g"} {`]);
const acc = isSub
? [`subgraph ${graph.id || nextSubgraphID()} {`]
: [`${directed ? "di" : ""}graph ${graph.id || "g"} {`];
if (graph.include) {
acc.push(graph.include);
}
if (graph.attribs) {
formatGraphAttribs(graph.attribs, acc);
}
Expand All @@ -116,8 +118,8 @@ export const serializeGraph = (graph: Graph, acc?: string[]) => {
acc.push(serializeEdge(e, directed));
}
if (graph.sub) {
for (let id in graph.sub) {
acc.push(serializeGraph(graph.sub[id], [`subgraph ${id} {`]));
for (let sub of graph.sub) {
acc.push(serializeGraph(sub, true));
}
}
acc.push("}");
Expand Down
22 changes: 14 additions & 8 deletions packages/dot/test/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ const operator: Partial<dot.Node> = {
fillcolor: "yellow",
shape: "Mrecord",
ins: { 0: "a", 1: "b" },
outs: { "out": "out" }
outs: { out: "out" },
};

fs.writeFileSync(
"../../assets/dot-example.dot",
"export/dot-example.dot",
dot.serializeGraph({
directed: true, // default
// graph attributes
Expand All @@ -33,14 +33,14 @@ fs.writeFileSync(
node: {
style: "filled",
fontname: "Inconsolata",
fontsize: 11
fontsize: 11,
},
// edge defaults
edge: {
arrowsize: 0.75,
fontname: "Inconsolata",
fontsize: 9
}
fontsize: 9,
},
},
// graph nodes (the keys are used as node IDs)
// use spread operator to inject style presets
Expand All @@ -55,9 +55,15 @@ fs.writeFileSync(
edges: [
{ src: "x", dest: "op1", destPort: 1 },
{ src: "y", dest: "op1", destPort: 0 },
{ src: "y", dest: "op2", destPort: 0, label: "xform", color: "blue" },
{
src: "y",
dest: "op2",
destPort: 0,
label: "xform",
color: "blue",
},
{ src: "op1", srcPort: "out", dest: "op2", destPort: 1 },
{ src: "op2", srcPort: "out", dest: "res" },
]
],
})
);
);

0 comments on commit ed53c90

Please sign in to comment.