Skip to content

Commit

Permalink
[LaraApi] Graph dot formatter now supports layout options
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagolascasas committed Apr 2, 2024
1 parent 70f0631 commit 2f4a5e2
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 176 deletions.
133 changes: 76 additions & 57 deletions LaraApi/src-lara/lara/graphs/DotFormatter.js
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));
}
}
Loading

0 comments on commit 2f4a5e2

Please sign in to comment.