/
arc_core_digraph_algorithm_request.js
161 lines (139 loc) · 7.25 KB
/
arc_core_digraph_algorithm_request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
Copyright (C) 2014-2016 Christopher D. Russell
This library is published under the MIT License and is part of the
Encapsule Project System in Cloud (SiC) open service architecture.
Please follow https://twitter.com/Encapsule for news and updates
about jsgraph and other time saving libraries that do amazing things
with in-memory data on Node.js and HTML.
*/
var helperFunctions = require('./arc_core_graph_util');
var TRAVERSE_CONTEXT = require('./arc_core_digraph_algorithm_context');
/*
request = {
digraph: reference to jsgraph.DirectedGraph container object (required)
visitor: reference to jsgraph BFV visitor object (required)
options: {
startVector: reference to a vertex ID string, or an array of vertex ID strings (optional)
Note: if ommitted, BFT uses the digraph's root vertex set as the start vertex set
allowEmptyStartVector: Boolean flag (optional - default is false is omitted)
signalStart: Boolean flag (optional - default is true if ommitted)
Note: By default, BFT will call startVertex on each search root vertex.
In advanced scenarios you may wish to override this behavior.
traverseContext: reference to BFT search context object (optional)
Note: By default, BFT allocates the traversal context internally and returns it to
the caller. In advanced scenarios you may wish to provide a pre-initialized
(or potentially pre-colored) traversal context object.
}
}
}
response = {
error: null or string explaining why result is null
result: Traversal context object or null if error
}
*/
module.exports = function (request_) {
var response = { error: null, result: null };
var errors = [];
var nrequest = null;
var inBreakScope = false;
var createTraverseContext = function() {
var response = TRAVERSE_CONTEXT({ digraph: nrequest.digraph });
var result = null;
if (response.error) {
errors.unshift(response.error);
} else {
result = response.result;
}
return result;
};
var getRootVertices = function() {
return nrequest.digraph.getRootVertices();
};
while (!inBreakScope) {
inBreakScope = true;
// Verify the outer shape of the request object.
var innerResponse = helperFunctions.JSType(request_);
if (innerResponse !== '[object Object]') {
errors.unshift("Missing request object ~. Found type '" + innerResponse + "'.");
break;
}
nrequest = {};
innerResponse = helperFunctions.JSType(request_.digraph);
if (innerResponse !== '[object Object]') {
errors.unshift("Missing required DirectedGraph reference ~.digraph. Found type '" + innerResponse + "'.");
break;
}
nrequest.digraph = request_.digraph;
innerResponse = helperFunctions.JSType(request_.visitor);
if (innerResponse !== '[object Object]') {
errors.unshift("Missing required visitor object reference ~.visitor. Found type '" + innerResponse + "'.");
break;
}
nrequest.visitor = request_.visitor;
innerResponse = helperFunctions.JSType(request_.options);
if ((innerResponse !== '[object Undefined]') && (innerResponse !== '[object Object]')) {
errors.unshift("Options object ~.options is the wrong type. Found type '" + innerResponse + "'.");
break;
}
nrequest.options = {};
if (innerResponse === '[object Object]') {
innerResponse = helperFunctions.JSType(request_.options.startVector);
switch (innerResponse) {
case '[object Undefined]':
break;
case '[object String]':
nrequest.options.startVector = [ request_.options.startVector ];
break;
case '[object Array]':
nrequest.options.startVector = request_.options.startVector;
break;
default:
errors.unshift("Options object property ~.options.startVector is the wrong type. Expected either '[object String]', '[object Array]', or '[object Undefined]'. Found type '" + innerResponse + "'.");
break;
} // end switch
if (errors.length) {
break;
}
innerResponse = helperFunctions.JSType(request_.options.allowEmptyStartVector);
if ((innerResponse !== '[object Undefined]') && (innerResponse !== '[object Boolean]')) {
errors.unshift("Options object property ~.options.allowEmptyStartVector is the wrong type. Expected either '[object Boolean]' or '[object Undefined]. Found type '" + innerResponse + "'.");
break;
}
if (innerResponse == '[object Boolean]') {
nrequest.options.allowEmptyStartVector = request_.options.allowEmptyStartVector;
}
innerResponse = helperFunctions.JSType(request_.options.signalStart);
if ((innerResponse !== '[object Undefined]') && (innerResponse !== '[object Boolean]')) {
errors.unshift("Options object property ~.options.signalStart is the wrong type. Expected either '[object Boolean]' or '[object Undefined]'. Found type '" + innerResponse + "'.");
break;
}
if (innerResponse === '[object Boolean]') {
nrequest.options.signalStart = request_.options.signalStart;
}
innerResponse = helperFunctions.JSType(request_.options.traverseContext);
if ((innerResponse !== '[object Undefined]') && (innerResponse !== '[object Object]')) {
errors.unshift("Options object property ~.options.traverseContext is the wrong type. Expected either '[object Object]' or '[object Undefined']. Found type '" + innerResponse + "'.");
break;
}
if (innerResponse === '[object Object]') {
nrequest.options.traverseContext = request_.options.traverseContext;
}
} // end if options object specified
helperFunctions.setPropertyValueIfUndefined(nrequest.options, 'startVector', getRootVertices);
helperFunctions.setPropertyValueIfUndefined(nrequest.options, 'allowEmptyStartVector', false);
helperFunctions.setPropertyValueIfUndefined(nrequest.options, 'signalStart', true);
helperFunctions.setPropertyValueIfUndefined(nrequest.options, 'traverseContext', createTraverseContext);
// Ensure that the starting vertex set is not empty (unless allowed).
if (!nrequest.options.startVector.length && !nrequest.options.allowEmptyStartVector) {
errors.unshift("Traversal aborted because we don't know which vertex to start on. Specify a graph that has at least one root vertex, explicity specify the start vertex (or vertices) via `request.options.startVector` array, or suppress this error by setting `request.options.allowEmptyStartVector` to Boolean true.");
break;
}
response.result = nrequest;
}
if (errors.length) {
response.error = errors.join(' ');
} else {
response.result = nrequest;
}
return response;
};