Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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