blueshell
Version:
A Behavior Tree implementation in modern Javascript
102 lines • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeDotTree = serializeDotTree;
const models_1 = require("../models");
const nodes_1 = require("../nodes");
const DefaultStyle = 'style="filled,bold"';
const DecoratorShape = 'shape=ellipse';
const CompositeShape = 'shape=house height=1';
const IfElseShape = 'shape=diamond height=1';
const DefaultShape = 'shape=box';
const SuccessColor = 'fillcolor="#4daf4a"';
const FailureColor = 'fillcolor="#984ea3"';
const RunningColor = 'fillcolor="#377eb8"';
const ErrorColor = 'fillcolor="#e41a1c"';
const EnteredColor = 'fillcolor="#ff7f00"';
const DefaultColor = 'fillcolor="#e5e5e5"';
const DefaultEdgeColor = 'color="#000000"';
function getShape(node) {
if (node instanceof nodes_1.Decorator) {
return DecoratorShape;
}
else if (node instanceof nodes_1.IfElse) {
return IfElseShape;
}
else if ((0, models_1.isParentNode)(node)) {
return CompositeShape;
}
else {
return '';
}
}
function getColor(node, state) {
if (state) {
const eventCounter = node.getTreeEventCounter(state);
const lastEventSeen = node.getLastEventSeen(state);
const lastResult = node.getLastResult(state);
if (lastEventSeen === eventCounter) {
if (lastResult) {
switch (lastResult) {
case models_1.rc.ERROR:
return ErrorColor;
case models_1.rc.SUCCESS:
return SuccessColor;
case models_1.rc.RUNNING:
return RunningColor;
case models_1.rc.FAILURE:
return FailureColor;
}
}
else {
return EnteredColor;
}
}
}
return '';
}
function getPath(node) {
return `path="${node.path}"`;
}
function getLabel(node) {
if (node.symbol) {
return `label="${node.name}\\n${node.symbol}"`;
}
else {
return `label="${node.name}"`;
}
}
function getTooltip(node) {
return `tooltip="${node.constructor.name}"`;
}
function serializeDotTree(root, state) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!root) {
return '';
}
const nodesToVisit = [];
let resultingString = `digraph G {
graph [ordering=out]
node [${DefaultShape} ${DefaultColor} ${DefaultStyle}]
edge [${DefaultEdgeColor}]
`;
nodesToVisit.push(root);
// NodeIdMap.getInstance().clearMap();
while (nodesToVisit.length) {
const currentNode = nodesToVisit.pop();
const nodeId = currentNode.id;
resultingString += `\t${nodeId} `;
resultingString += `[${getLabel(currentNode)} ${getShape(currentNode)} ${getTooltip(currentNode)}`;
resultingString += ` ${getPath(currentNode)} ${getColor(currentNode, state)}];\n`;
if (!!currentNode && (0, models_1.isParentNode)(currentNode)) {
resultingString = currentNode
.getChildren()
.reduce((acc, child) => `${acc}\t${nodeId}->${child.id};\n`, resultingString);
for (const child of [...currentNode.getChildren()].reverse()) {
nodesToVisit.push(child);
}
}
}
resultingString += '}';
return resultingString;
}
//# sourceMappingURL=dotTree.js.map