-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LaraApi] Graph dot formatter now supports layout options
- Loading branch information
1 parent
70f0631
commit 2f4a5e2
Showing
2 changed files
with
198 additions
and
176 deletions.
There are no files selected for viewing
This file contains 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,71 +1,90 @@ | ||
class DotFormatter { | ||
// Array of objects that contains the properties 'attr' (string) and 'predicate' (function) | ||
#nodeAttrs; | ||
// Array of objects that contains the properties 'attr' (string) and 'predicate' (function) | ||
#nodeAttrs; | ||
|
||
// Array of objects that contains the properties 'attr' (string) and 'predicate' (function) | ||
#edgeAttrs; | ||
// Array of objects that contains the properties 'attr' (string) and 'predicate' (function) | ||
#edgeAttrs; | ||
|
||
// Function that receives a node and returns the corresponding label. By default, call .toString() over the data | ||
#nodeLabelFormatter; | ||
// Map with selected layout options | ||
#layoutOptions; | ||
|
||
// Function that receives an edge and returns the corresponding label. By default, call .toString() over the data | ||
#edgeLabelFormatter; | ||
// Function that receives a node and returns the corresponding label. By default, call .toString() over the data | ||
#nodeLabelFormatter; | ||
|
||
constructor() { | ||
this.#nodeAttrs = []; | ||
this.#edgeAttrs = []; | ||
// Function that receives an edge and returns the corresponding label. By default, call .toString() over the data | ||
#edgeLabelFormatter; | ||
|
||
this.#nodeLabelFormatter = (node) => node.data().toString(); | ||
this.#edgeLabelFormatter = (edge) => edge.data().toString(); | ||
} | ||
|
||
static #sanitizeDotLabel(label) { | ||
return label.replaceAll("\n", "\\l").replaceAll("\r", ""); | ||
} | ||
constructor() { | ||
this.#nodeAttrs = []; | ||
this.#edgeAttrs = []; | ||
this.#layoutOptions = new Map(); | ||
|
||
addNodeAttribute(attrString, predicate) { | ||
if (predicate === undefined) { | ||
predicate = (node) => true; | ||
this.#nodeLabelFormatter = (node) => node.data().toString(); | ||
this.#edgeLabelFormatter = (edge) => edge.data().toString(); | ||
} | ||
|
||
this.#nodeAttrs.push({ attr: attrString, predicate: predicate }); | ||
} | ||
static #sanitizeDotLabel(label) { | ||
return label.replaceAll("\n", "\\l").replaceAll("\r", ""); | ||
} | ||
|
||
addNodeAttribute(attrString, predicate) { | ||
if (predicate === undefined) { | ||
predicate = (node) => true; | ||
} | ||
|
||
this.#nodeAttrs.push({ attr: attrString, predicate: predicate }); | ||
} | ||
|
||
addEdgeAttribute(attrString, predicate) { | ||
if (predicate === undefined) { | ||
predicate = (edge) => true; | ||
} | ||
|
||
this.#edgeAttrs.push({ attr: attrString, predicate: predicate }); | ||
} | ||
|
||
setLayoutOption(option, value) { | ||
this.#layoutOptions.set(option, value); | ||
} | ||
|
||
setNodeLabelFormatter(nodeLabelFormatter) { | ||
this.#nodeLabelFormatter = nodeLabelFormatter; | ||
} | ||
|
||
addEdgeAttribute(attrString, predicate) { | ||
if (predicate === undefined) { | ||
predicate = (edge) => true; | ||
setEdgeLabelFormatter(edgeLabelFormatter) { | ||
this.#edgeLabelFormatter = edgeLabelFormatter; | ||
} | ||
|
||
this.#edgeAttrs.push({ attr: attrString, predicate: predicate }); | ||
} | ||
|
||
setNodeLabelFormatter(nodeLabelFormatter) { | ||
this.#nodeLabelFormatter = nodeLabelFormatter; | ||
} | ||
|
||
setEdgeLabelFormatter(edgeLabelFormatter) { | ||
this.#edgeLabelFormatter = edgeLabelFormatter; | ||
} | ||
|
||
getNodeAttributes(node) { | ||
return this.#nodeAttrs | ||
.filter((obj) => obj.predicate(node)) | ||
.map((obj) => obj.attr) | ||
.join(" "); | ||
} | ||
|
||
getEdgeAttributes(edge) { | ||
return this.#edgeAttrs | ||
.filter((obj) => obj.predicate(edge)) | ||
.map((obj) => obj.attr) | ||
.join(" "); | ||
} | ||
|
||
getNodeLabel(node) { | ||
return DotFormatter.#sanitizeDotLabel(this.#nodeLabelFormatter(node)); | ||
} | ||
|
||
getEdgeLabel(edge) { | ||
return DotFormatter.#sanitizeDotLabel(this.#edgeLabelFormatter(edge)); | ||
} | ||
getNodeAttributes(node) { | ||
return this.#nodeAttrs | ||
.filter((obj) => obj.predicate(node)) | ||
.map((obj) => obj.attr) | ||
.join(" "); | ||
} | ||
|
||
getEdgeAttributes(edge) { | ||
return this.#edgeAttrs | ||
.filter((obj) => obj.predicate(edge)) | ||
.map((obj) => obj.attr) | ||
.join(" "); | ||
} | ||
|
||
getLayoutOptions() { | ||
let options = []; | ||
|
||
this.#layoutOptions.forEach((option, val) => { | ||
options.push(`${val}="${option}";`); | ||
}); | ||
|
||
return options.join("\n"); | ||
} | ||
|
||
getNodeLabel(node) { | ||
return DotFormatter.#sanitizeDotLabel(this.#nodeLabelFormatter(node)); | ||
} | ||
|
||
getEdgeLabel(edge) { | ||
return DotFormatter.#sanitizeDotLabel(this.#edgeLabelFormatter(edge)); | ||
} | ||
} |
Oops, something went wrong.