@openhps/core
Version:
Open Hybrid Positioning System - Core component
34 lines • 1.4 kB
JavaScript
export class GraphValidator {
static validate(graph) {
this.validateNodes(graph);
this.validateEdges(graph);
}
static _validateInternalNode(graph, node) {
if (node.outlets.length === 0 && node.inlets.length === 0) {
graph.deleteNode(node);
} else if (!graph.findNodeByUID(node.uid)) {
throw new Error(`Internal node ${node.uid} (${node.name}) is not connected to the graph!`);
}
}
static validateNodes(graph) {
GraphValidator._validateInternalNode(graph, graph.internalSource);
GraphValidator._validateInternalNode(graph, graph.internalSink);
graph.nodes.forEach(node => {
if (node.graph === undefined) {
throw new Error(`Node ${node.uid} (${node.name}) does not have a graph set!`);
}
if (node.inlets.length === 0 && node.outlets.length === 0) {
throw new Error(`Node ${node.uid} (${node.name}) is not connected to the graph!`);
}
});
}
static validateEdges(graph) {
graph.edges.forEach(edge => {
if (!graph.findNodeByUID(edge.inputNode.uid)) {
throw new Error(`Node ${edge.inputNode.uid} (${edge.inputNode.name}) is used in an edge but not added to the graph!`);
} else if (!graph.findNodeByUID(edge.outputNode.uid)) {
throw new Error(`Node ${edge.outputNode.uid} (${edge.outputNode.name}) is used in an edge but not added to the graph!`);
}
});
}
}