Skip to content

Commit

Permalink
Add rudimentary graph validation
Browse files Browse the repository at this point in the history
  • Loading branch information
bergie committed Nov 16, 2017
1 parent c3b892b commit 63ec365
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions grammar/fbp.peg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@

var serialize, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };

parser.validateContents = function(graph, options) {
// Ensure all nodes have a component
if (graph.processes) {
Object.keys(graph.processes).forEach(function (node) {
if (!graph.processes[node].component) {
throw new Error('Node "' + node + '" does not have a component defined');
}
});
}
// Ensure all edges have nodes defined
if (graph.connections) {
graph.connections.forEach(function (edge) {
if (edge.tgt && !graph.processes[edge.tgt.process]) {
if (edge.data) {
throw new Error('IIP containing "' + edge.data + '" is connected to an undefined target node');
}
throw new Error('Edge from "' + edge.src.process + '" port "' + edge.src.port + '" is connected to an undefined target node');
}
if (edge.src && !graph.processes[edge.src.process]) {
throw new Error('Edge to "' + edge.tgt.process + '" port "' + edge.tgt.port + '" is connected to an undefined source node');
}
});
}
};

parser.serialize = function(graph) {
var conn, getInOutName, getName, i, inPort, input, len, name, namedComponents, outPort, output, process, ref, ref1, ref2, src, srcName, srcPort, srcProcess, tgt, tgtName, tgtPort, tgtProcess;
if (options == null) {
Expand Down Expand Up @@ -163,6 +188,11 @@
}
}
result.caseSensitive = options.caseSensitive;

if (typeof options.validateContents === 'undefined' || options.validateContents) {
parser.validateContents(result);
}

return result;
}

Expand Down
30 changes: 30 additions & 0 deletions lib/fbp.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63ec365

Please sign in to comment.