-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYEDGraph.j
More file actions
270 lines (225 loc) · 8.57 KB
/
Copy pathYEDGraph.j
File metadata and controls
270 lines (225 loc) · 8.57 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
@import <Foundation/CPObject.j>
@import <Foundation/CPSet.j>
@import <Foundation/CPException.j>
@import <Foundation/CPNotificationCenter.j>
@import "YEDNode.j"
// Exceptions
YEDGraphCannotCreateDirectedEdgeException = @"YEDGraphCannotCreateDirectedEdgeException";
YEDGraphNodeTypeNotAllowedException = @"YEDGraphNodeTypeNotAllowedException";
// Notifications
YEDGraphNodeAddedNotification = @"YEDGraphNodeAddedNotification";
YEDGraphNodeRemovedNotification = @"YEDGraphNodeRemovedNotification";
YEDGraphEdgeNotAllowedNotification = @"YEDGraphEdgeNotAllowedNotification";
YEDGraphEdgeWouldCauseCycleNotification = @"YEDGraphEdgeWouldCauseCycleNotification";
YEDGraphEdgeAddedNotification = @"YEDGraphEdgeAddedNotification";
YEDGraphEdgeRemovedNotification = @"YEDGraphEdgeRemovedNotification"
/*! @class YEDGraph
** Delegate Protocol **
- (void)willAddNode:(YEDNode)aNode toGraph:(YEDGraph)aGraph
- (void)didAddNode(YEDNode)aNode toGraph:(YEDGraph)aGraph
- (void)willRemoveNode:(YEDNode)aNode fromGraph:(YEDGraph)aGraph
- (void)didRemoveNode:(YEDNode)aNode fromGraph:(YEDGraph)aGraph
- (void)willAddEdgeFromNode:(YEDNode)startNode toNode:(YEDNode)endNode inGraph:(YEDGraph)aGraph
- (void)didAddEdge:(BOOL)edgeAdded fromNode:(YEDNode)startNode toNode:(YEDNode)endNode inGraph:(YEDGraph)aGraph
- (void)willRemoveEdgeFromNode:(YEDNode)startNode toNode:(YEDNode)endNode inGraph:(YEDGraph)aGraph
- (void)didRemoveEdge:(BOOL)edgeRemoved fromNode:(YEDNode)startNode toNode:(YEDNode)endNode inGraph:(YEDGraph)aGraph
*/
@implementation YEDGraph : CPObject
{
CPSet nodes @accessors;
CPSet allowedNodeTypes;
id delegate @accessors;
}
- (id)init
{
self = [super init];
if(self)
{
nodes = [CPSet set];
allowedNodeTypes = [CPSet setWithObject:YEDNode];
}
return self;
}
- (id)initWithNodes:(CPArray)otherNodes
{
var self = [self init];
if(self)
{
var nodeIter = [otherNodes objectEnumerator];
var otherNode = nil;
while(otherNode = [nodeIter nextObject])
{
[self addNode:otherNode];
}
}
return self;
}
+ (id)graph
{
return [[self alloc] init];
}
+ (id)graphWithNodes:(CPArray)nodes
{
return [[self alloc] initWithNodes:nodes];
}
- (void)addNode:(YEDNode)aNode
{
if([self allowsNode:aNode])
{
if([delegate respondsToSelector:@selector(willAddNode:toGraph:)])
[delegate willAddNode:aNode toGraph:self];
[nodes addObject:aNode];
[[CPNotificationCenter defaultCenter]
postNotificationName:YEDGraphNodeAddedNotification
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
node:aNode
}]];
if([delegate respondsToSelector:@selector(didAddNode:toGraph:)])
[delegate didAddNode:aNode toGraph:self];
}
else
{
[CPException raise:YEDGraphNodeTypeNotAllowedException message:"The type of the added node is not allowed in this graph."];
}
}
- (void)removeNode:(YEDNode)aNode
{
if([nodes containsObject:aNode])
{
if([delegate respondsToSelector:@selector(willRemoveNode:fromGraph:)])
[delegate willRemoveNode:aNode fromGraph:self];
var outEdges = [aNode outEdges];
var inEdges = [aNode inEdges];
var edgeIter = [outEdges objectEnumerator];
var edgeNode = nil;
while(edgeNode = [edgeIter nextObject])
{
[self removeDirectedEdgeFrom:aNode to:edgeNode];
}
edgeIter = [inEdges objectEnumerator];
while(edgeNode = [edgeIter nextObject])
{
[self removeDirectedEdgeFrom:edgeNode to:aNode];
}
[aNode disconnectFromAllNodes];
[nodes removeObject:aNode];
[[CPNotificationCenter defaultCenter]
postNotificationName:YEDGraphNodeRemovedNotification
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
node:aNode
}]];
if([delegate respondsToSelector:@selector(didRemoveNode:fromGraph:)])
[delegate didRemoveNode:aNode fromGraph:self];
}
}
- (void)createDirectedEdgeFrom:(YEDNode)fromNode to:(YEDNode)toNode
{
// if(![nodes containsObject:fromNode])
// [self addNode:fromNode];
//
// if(![nodes containsObject:toNode])
// [self addNode:toNode];
// If the nodes aren't in the nodes set, then they aren't allowed
if(![nodes containsObject:fromNode] || ![nodes containsObject:toNode])
{
[CPException raise:YEDGraphCannotCreateDirectedEdgeException reason:"One of the nodes is not in the graph."];
return;
}
if([delegate respondsToSelector:@selector(willAddEdgeFromNode:toNode:inGraph:)])
[delegate willAddEdgeFromNode:fromNode toNode:toNode inGraph:self];
try
{
[fromNode directedEdgeTo:toNode];
}
catch(err)
{
if([err name] === YEDNodeNotAllowedException)
{
if([delegate respondsToSelector:@selector(directedEdgeFromNode:toNode:isNotAllowedInGraph:)])
[delegate directedEdgeFromNode:fromNode toNode:toNode isNotAllowedInGraph:self];
[[CPNotificationCenter defaultCenter]
postNotificationName:YEDGraphEdgeNotAllowedNotification
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
fromNode:fromNode,
toNode:toNode
}]];
if([delegate respondsToSelector:@selector(didAddEdge:fromNode:toNode:inGraph:)])
[delegate didAddEdge:NO fromNode:fromNode toNode:toNode inGraph:self];
throw(err);
}
else if([err name] === YEDNodeCycleException)
{
if([delegate respondsToSelector:@selector(directedEdgeFromNode:toNode:wouldIntroduceCycleInGraph:)])
[delegate directedEdgeFromNode:fromNode toNode:toNode wouldIntroduceCycleInGraph:self];
[[CPNotificationCenter defaultCenter]
postNotificationName:YEDGraphEdgeWouldCauseCycleNotification
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
fromNode:fromNode,
toNode:toNode
}]];
if([delegate respondsToSelector:@selector(didAddEdge:fromNode:toNode:inGraph:)])
[delegate didAddEdge:NO fromNode:fromNode toNode:toNode inGraph:self];
throw(err);
}
else
{
// Otherwise rethrow the error
throw err;
}
}
if([delegate respondsToSelector:@selector(didAddEdge:fromNode:toNode:inGraph:)])
[delegate didAddEdge:YES fromNode:fromNode toNode:toNode inGraph:self];
[[CPNotificationCenter defaultCenter]
postNotificationName:YEDGraphEdgeAddedNotification
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
fromNode:fromNode,
toNode:toNode
}]];
}
- (void)removeDirectedEdgeFrom:(YEDNode)fromNode to:(YEDNode)toNode
{
// Only remove the edge if both nodes are in the graph
if(![nodes containsObject:fromNode] || ![nodes containsObject:toNode])
return;
if([delegate respondsToSelector:@selector(willRemoveEdgeFromNode:toNode:inGraph:)])
[delegate willRemoveEdgeFromNode:fromNode toNode:toNode inGraph:self];
[fromNode removeDirectedEdgeTo:toNode];
if([delegate respondsToSelector:@selector(didRemoveEdge:fromNode:toNode:inGraph:)])
[delegate didRemoveEdge:YES fromNode:fromNode toNode:toNode inGraph:self];
[[CPNotificationCenter defaultCenter]
postNotificationName:YEDGraphEdgeRemovedNotification
object:self
userInfo:[CPDictionary dictionaryWithJSObject:{
fromNode:fromNode,
toNode:toNode
}]];
}
- (BOOL)containsNode:(YEDNode)aNode
{
return [nodes containsObject:aNode];
}
- (BOOL)containsEdgeFromNode:(YEDNode)startNode toNode:endNode
{
return [startNode hasOutgoingEdgeTo:endNode];
}
- (void)allowsNode:(YEDNode)aNode
{
var isAllowed = NO,
allowedNodeTypeIter = [allowedNodeTypes objectEnumerator],
allowedNodeType = nil;
while(allowedNodeType = [allowedNodeTypeIter nextObject])
{
if([aNode isKindOfClass:allowedNodeType])
{
isAllowed = YES;
break;
}
}
return isAllowed;
}
@end