-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYEDNode.j
More file actions
317 lines (268 loc) · 8.61 KB
/
YEDNode.j
File metadata and controls
317 lines (268 loc) · 8.61 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
@import <Foundation/CPObject.j>
@import <Foundation/CPSet.j>
@import <Foundation/CPException.j>
// Constants
YEDNodeCycleException = "YEDNodeCycleException";
YEDNodeNotAllowedException = "YEDNodeNotAllowedException";
/*
* Performs a depth-first-search of an edge set,
* tracking each node encountered. If a node is
* encountered more than once, then a cycle has
* been found.
*/
YEDNodeGraphHasCycles = function(aNode, traverseParents)
{
CPLog.trace("YEDNodeGraphHasCycles: Testing for cycles starting at %s", [aNode name]);
traverseParents = traverseParents || NO;
var stack = []
function isAcyclic(node)
{
if([stack containsObject:node])
{
return false;
}
stack.push(node);
CPLog.trace("YEDNodeGraphHasCycles: at node %s", [node name]);
var targetNode = nil;
var nodeIter = traverseParents ? [[node inEdges] objectEnumerator] :
[[node outEdges] objectEnumerator];
traverseParents ? CPLog.trace("YEDNodeGraphHasCycles: Traversing inEdges") :
CPLog.trace("YEDNodeGraphHasCycles: Travering outEdges");
while(targetNode = [nodeIter nextObject])
{
if(!isAcyclic(targetNode, traverseParents))
{
return false;
}
}
stack.pop();
return true;
}
var result = isAcyclic(aNode);
CPLog.trace("YEDNodeGraphHasCycles: Graph staring at %s is acyclic: %s", [aNode name], result);
return !result;
};
@implementation YEDNode : CPObject
{
CPString name @accessors;
CPSet outEdges @accessors(readonly);
CPSet inEdges @accessors(readonly);
CPSet allowsConnectionsTo @accessors(readonly);
CPSet allowsConnectionsFrom @accessors(readonly);
BOOL isAcyclic @accessors;
}
- (id)init
{
self = [super init];
if(self)
{
outEdges = [CPSet set];
inEdges = [CPSet set];
allowsConnectionsTo = [CPSet setWithObject:[self className]];
allowsConnectionsFrom = [CPSet setWithObject:[self className]];
isAcyclic = NO;
}
return self;
}
- (id)initAcyclic
{
self = [self init]
if(self)
{
isAcyclic = YES;
}
return self;
}
+ (id)node
{
return [[self alloc] init];
}
+ (id)nodeWithName:(CPString)aName
{
var node = [self node];
[node setName:aName];
return node;
}
+ (id)acyclicNode
{
return [[self alloc] initAcyclic];
}
+ (id)acyclicNodeWithName:(CPString)aName
{
var node = [self acyclicNode];
[node setName:aName]
return node;
}
- (BOOL)isNode
{
return YES;
}
// - (BOOL)isEqual:(id)other
// {
// if(other === self)
// return YES;
// if(!other || ![other isKindOfClass:[self class]])
// return NO
// return [self isEqualToNode:other];
// }
//
// - (BOOL)isEqualToNode:(YEDNode)otherNode
// {
// if(otherNode === self)
// return YES;
// if(![[self name] isEqual:[otherNode name]])
// return NO;
// if(![[self isAcyclic] isEqual:[otherNode isAcyclic]])
// return NO;
// if(![[self allowsConnectionsTo] isEqualToSet:[otherNode allowsConnectionsTo]])
// return NO;
// if(![[self allowsConnectionsFrom] isEqualToSet:[otherNode allowsConnectionsFrom]])
// return NO;
// return YES;
// }
- (BOOL)hasOutgoingEdgeTo:(YEDNode)otherNode
{
return [outEdges containsObject:otherNode];
}
- (BOOL)hasIncomingEdgeFrom:(YEDNode)otherNode
{
return [otherNode hasOutgoingEdgeTo:self];
}
- (void)directedEdgeTo:(YEDNode)otherNode
{
if([self canConnectTo:otherNode])
{
CPLog.trace("directedEdgeTo: %s allowed to connect to %s", [self name], [otherNode name]);
// If the otherNode has no incoming or outgoing edges, then it cannot introduce
// any cycles into the graph. We test because adding a single, indepedent node
// is a common case.
var otherNodeCouldIntroduceCycles = [[otherNode inEdges] anyObject] || [[otherNode outEdges] anyObject];
CPLog.trace("directedEdgeTo: %s could introduce a cycle?: %s", [otherNode name], otherNodeCouldIntroduceCycles);
//Add the node to the graph first
[[self outEdges] addObject:otherNode];
[[otherNode inEdges] addObject:self];
if(isAcyclic && otherNodeCouldIntroduceCycles)
{
if([self cycleInDescendents] || [self cycleInParents])
{
CPLog.warn("directedEdgeTo: detected a cycle! removing edge from %s to %s", [self name], [otherNode name]);
//If adding the edge/connection introduces cycles, we should remove it.
[[self outEdges] removeObject:otherNode];
[[otherNode inEdges] removeObject:self];
[CPException raise:YEDNodeCycleException reason:"Connecting the node would introduce a cycle."];
}
}
}
else
{
CPLog.warn("directedEdgeTo: %s not allowed to connect to %s", [self name], [otherNode name]);
[CPException raise:YEDNodeNotAllowedException reason:"Connecting to this node type is not allowed."];
}
}
- (void)directedEdgeFrom:(YEDNode)otherNode
{
[otherNode directedEdgeTo:self];
}
- (void)removeDirectedEdgeTo:(YEDNode)otherNode
{
[[self outEdges] removeObject:otherNode];
[[otherNode inEdges] removeObject:self];
}
- (void)disconnectFromAllNodes
{
var node = nil,
outIter = [outEdges objectEnumerator],
inIter = [inEdges objectEnumerator];
while(node = [outIter nextObject])
{
[self removeDirectedEdgeTo:node];
}
while(node = [inIter nextObject])
{
[node removeDirectedEdgeTo:self];
}
}
/*
* Is the node allowed to connect to another node.
* By default all YEDNodes can connect to any other node.
*/
- (BOOL)canConnectTo:(YEDNode)otherNode
{
var allowTo = false;
var allowToIter = [allowsConnectionsTo objectEnumerator];
var allowedNodeClass = nil;
while(allowedNodeClass = objj_lookUpClass([allowToIter nextObject]))
{
if([otherNode isKindOfClass:allowedNodeClass])
allowTo = true;
}
var allowFrom = false;
var allowFromIter = [[otherNode allowsConnectionsFrom] objectEnumerator];
var allowedNodeClass = nil;
while(allowedNodeClass = objj_lookUpClass([allowFromIter nextObject]))
{
if([self isKindOfClass:allowedNodeClass])
allowFrom = true;
}
return allowTo && allowFrom;
}
- (BOOL)canRecieveConnectionFrom:(YEDNode)otherNode
{
return [otherNode canConnectTo:self];
}
- (CPSet)allDescendents
{
var descendents = [CPSet set];
function traverse(node) {
[descendents unionSet:[node outEdges]];
var iter = [[node outEdges] objectEnumerator];
while(node = [iter nextObject])
{
traverse(node);
}
}
traverse(self);
return descendents;
}
- (BOOL)cycleInDescendents
{
return YEDNodeGraphHasCycles(self,NO);
}
- (BOOL)cycleInParents
{
return YEDNodeGraphHasCycles(self,YES);
}
@end
// Coding Keys
var YEDNodeNameKey = @"YEDNodeNameKey",
YEDNodeIsAcyclicKey = @"YEDNodeIsAcyclicKey",
YEDNodeOutEdgesKey = @"YEDNodeOutEdgesKey",
YEDNodeInEdgesKey = @"YEDNodeInEdgesKey",
YEDNodeAllowsConnectionsToKey = @"YEDNodeAllowsConnectionsToKey",
YEDNodeAllowsConnectionsFromKey = @"YEDNodeAllowsConnectionsFromKey";
@implementation YEDNode (CPCoding)
- (id)initWithCoder:(CPCoder)coder
{
self = [super init];
if(self)
{
name = [coder decodeObjectForKey:YEDNodeNameKey];
isAcyclic = [coder decodeObjectForKey:YEDNodeIsAcyclicKey];
outEdges = [coder decodeObjectForKey:YEDNodeOutEdgesKey];
inEdges = [coder decodeObjectForKey:YEDNodeInEdgesKey];
allowsConnectionsTo = [coder decodeObjectForKey:YEDNodeAllowsConnectionsToKey];
allowsConnectionsFrom = [coder decodeObjectForKey:YEDNodeAllowsConnectionsFromKey];
}
return self;
}
- (void)encodeWithCoder:(CPCoder)coder
{
// [super encodeWithCoder:coder];
[coder encodeObject:name forKey:YEDNodeNameKey];
[coder encodeObject:isAcyclic forKey:YEDNodeIsAcyclicKey];
[coder encodeObject:outEdges forKey:YEDNodeOutEdgesKey];
[coder encodeObject:inEdges forKey:YEDNodeInEdgesKey];
[coder encodeObject:allowsConnectionsTo forKey:YEDNodeAllowsConnectionsToKey];
[coder encodeObject:allowsConnectionsFrom forKey:YEDNodeAllowsConnectionsFromKey];
}
@end