33static NSString *HandlerToken(NSString *state, NSString *handler, NSMutableDictionary *handlerKeys, NSMutableSet *uniqueSet);
34static void HandleOneCommand(NSMutableString *graphViz, NSString *stateKey, NSString *handlerKey, NSMutableDictionary *handlerKeys, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes, NSMutableSet *uniqueSet, BOOL *haveSetOrSwichAI);
35static void AddSimpleSpecialNodeLink(NSMutableString *graphViz, NSString *handlerToken, NSString *name, NSString *shape, NSString *color, NSMutableSet *specialNodes);
36static void AddExitAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *message, NSMutableSet *specialNodes);
37static void AddChangeAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *method, NSArray *components, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes);
40void GenerateGraphVizForAIStateMachine(NSDictionary *stateMachine, NSString *smName)
42 NSMutableSet *uniqueSet = [NSMutableSet set];
43 NSMutableDictionary *handlerKeys = [NSMutableDictionary dictionary];
45 NSMutableString *graphViz =
46 [NSMutableString stringWithFormat:
47 @"digraph ai_flow\n{\n"
48 "\tgraph [charset=\"UTF-8\", label=\"%@ transition diagram\", labelloc=t, labeljust=l rankdir=LR compound=true nodesep=0.1 ranksep=2.5 fontname=Helvetica]\n"
49 "\tedge [arrowhead=normal]\n"
50 "\tnode [shape=box height=0.2 width=3.5 fontname=Helvetica color=\"#808080\"]\n\t\n"
51 "\tspecial_start [shape=ellipse color=\"#0000C0\" label=\"Start\"]\n\tspecial_start -> %@ [lhead=\"cluster_GLOBAL\" color=\"#0000A0\"]\n", EscapedGraphVizString(smName), HandlerToken(@"GLOBAL", @"ENTER", handlerKeys, uniqueSet)];
53 NSEnumerator *stateKeyEnum = [stateMachine keyEnumerator];
54 NSString *stateKey =
nil;
56 NSMutableSet *specialNodes = [NSMutableSet set];
58 while ((stateKey = [stateKeyEnum nextObject]))
60 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
62 [graphViz appendFormat:@"\t\n\tsubgraph cluster_%@\n\t{\n\t\tlabel=\"%@\"\n", stateKey, EscapedGraphVizString(stateKey)];
64 NSDictionary *state = [stateMachine oo_dictionaryForKey:stateKey];
65 NSEnumerator *handlerKeyEnum = [state keyEnumerator];
66 NSString *handlerKey =
nil;
67 while ((handlerKey = [handlerKeyEnum nextObject]))
69 [graphViz appendFormat:@"\t\t%@ [label=\"%@\"]\n", HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet), EscapedGraphVizString(handlerKey)];
73 if ([state objectForKey:
@"ENTER"] ==
nil)
75 [graphViz appendFormat:@"\t\t%@ [label=\"ENTER (implicit)\"] // No ENTER handler in file, but it's still the target of any incoming transitions.\n", HandlerToken(stateKey, @"ENTER", handlerKeys, uniqueSet)];
78 [graphViz appendString:@"\t}\n"];
81 handlerKeyEnum = [state keyEnumerator];
82 while ((handlerKey = [handlerKeyEnum nextObject]))
84 NSArray *handlerCommands = [state oo_arrayForKey:handlerKey];
85 NSUInteger commandIter, commandCount = [handlerCommands count];
86 BOOL haveSetOrSwichAI = NO;
88 for (commandIter = 0; commandIter < commandCount; commandIter++)
90 HandleOneCommand(graphViz, stateKey, handlerKey, handlerKeys, handlerCommands, commandIter, commandCount, specialNodes, uniqueSet, &haveSetOrSwichAI);
97 if ([specialNodes
count] != 0)
99 [graphViz appendString:@"\t\n"];
101 NSEnumerator *specialEnum = [specialNodes objectEnumerator];
102 NSString *special =
nil;
103 while ((special = [specialEnum nextObject]))
105 [graphViz appendString:special];
109 [graphViz appendString:@"}\n"];
114static NSString *HandlerToken(NSString *state, NSString *handler, NSMutableDictionary *handlerKeys, NSMutableSet *uniqueSet)
116 NSString *result = [[handlerKeys oo_dictionaryForKey:state] oo_stringForKey:handler];
120 result = [NSString stringWithFormat:@"%@_h_%@", state, handler];
121 result = GraphVizTokenString(result, uniqueSet);
123 NSMutableDictionary *stateDict = [handlerKeys objectForKey:state];
124 if (stateDict ==
nil)
126 stateDict = [NSMutableDictionary dictionary];
127 [handlerKeys setObject:stateDict forKey:state];
130 [stateDict setObject:result forKey:handler];
137static void HandleOneCommand(NSMutableString *graphViz, NSString *stateKey, NSString *handlerKey, NSMutableDictionary *handlerKeys, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes, NSMutableSet *uniqueSet, BOOL *haveSetOrSwichAI)
139 NSString *command = [handlerCommands oo_stringAtIndex:commandIter];
143 NSString *method = [components objectAtIndex:0];
144 NSString *handlerToken = HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet);
146 if (!*haveSetOrSwichAI && [method isEqualToString:
@"setStateTo:"])
148 if ([components
count] > 1)
150 NSString *targetState = [components objectAtIndex:1];
151 NSString *targetLabel = HandlerToken(targetState,
@"ENTER", handlerKeys, uniqueSet);
152 BOOL constraint = YES;
153 if ([targetState isEqualToString:stateKey]) constraint = NO;
154 else if ([targetState isEqualToString:
@"GLOBAL"]) constraint = NO;
156 [graphViz appendFormat:@"\t%@ -> %@ [lhead=cluster_%@%@]\n", handlerToken, targetLabel, targetState, constraint ? @"" : @" constraint=false"];
160 [specialNodes addObject:@"\tspecial_brokenSetStateTo [label=\"Broken setStateTo: command!\\n(No target state specified.)\" color=\"#C00000\" shape=diamond]\n"];
161 [graphViz appendFormat:@"\t%@ -> special_brokenSetStateTo [color=\"#C00000\"]\n", handlerToken];
164 else if ([method isEqualToString:
@"becomeExplosion"])
166 AddSimpleSpecialNodeLink(graphViz, handlerToken,
@"becomeExplosion",
@"diamond",
@"804000", specialNodes);
168 else if ([method isEqualToString:
@"becomeEnergyBlast"])
170 AddSimpleSpecialNodeLink(graphViz, handlerToken,
@"becomeEnergyBlast",
@"diamond",
@"804000", specialNodes);
172 else if ([method isEqualToString:
@"landOnPlanet"])
174 AddSimpleSpecialNodeLink(graphViz, handlerToken,
@"landOnPlanet",
@"diamond",
@"008040", specialNodes);
176 else if ([method isEqualToString:
@"performHyperSpaceExit"])
178 AddSimpleSpecialNodeLink(graphViz, handlerToken,
@"performHyperSpaceExit",
@"box",
@"008080", specialNodes);
180 else if ([method isEqualToString:
@"performHyperSpaceExitWithoutReplacing"])
182 AddSimpleSpecialNodeLink(graphViz, handlerToken,
@"performHyperSpaceExitWithoutReplacing",
@"box",
@"008080", specialNodes);
184 else if ([method isEqualToString:
@"enterTargetWormhole"])
186 AddSimpleSpecialNodeLink(graphViz, handlerToken,
@"enterTargetWormhole",
@"box",
@"008080", specialNodes);
188 else if ([method isEqualToString:
@"becomeUncontrolledThargon"])
190 AddSimpleSpecialNodeLink(graphViz, handlerToken,
@"becomeUncontrolledThargon",
@"ellipse",
@"804000", specialNodes);
192 else if ([method isEqualToString:
@"exitAIWithMessage:"])
194 NSString *message = ([components count] > 1) ? [components objectAtIndex:1] :
nil;
195 AddExitAINode(graphViz, handlerToken, message, specialNodes);
197 else if ([method isEqualToString:
@"setAITo:"] || [method isEqualToString:
@"switchAITo:"])
199 *haveSetOrSwichAI = YES;
200 AddChangeAINode(graphViz, handlerToken, method, components, handlerCommands, commandIter, commandCount, specialNodes);
205static void AddSimpleSpecialNodeLink(NSMutableString *graphViz, NSString *handlerToken, NSString *name, NSString *shape, NSString *color, NSMutableSet *specialNodes)
207 NSString *identifier = GraphVizTokenString([
@"special_" stringByAppendingString:name],
nil);
208 NSString *declaration = [NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#%@\" shape=%@]\n", identifier, EscapedGraphVizString(name), color, shape];
209 [specialNodes addObject:declaration];
211 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#%@\"]\n", handlerToken, identifier, color];
215static void AddExitAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *message, NSMutableSet *specialNodes)
217 NSString *token =
nil;
218 NSString *label =
nil;
219 if ([message isEqualToString:
@"RESTARTED"] || [message length] == 0)
226 token = GraphVizTokenString([
@"exitAI_" stringByAppendingString:message],
nil);
227 label = EscapedGraphVizString([
@"exitAIWithMessage:\n" stringByAppendingString:message]);
230 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#0000A0\" shape=ellipse]\n", token, label]];
231 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#0000C0\"]\n", handlerToken, token];
235static void AddChangeAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *method, NSArray *components, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes)
237 NSString *methodTag = [method substringToIndex:[method length] - 3];
239 if ([components
count] > 1)
241 NSString *targetAI = [components objectAtIndex:1];
242 NSString *token = [NSString stringWithFormat:@"%@_%@", methodTag, targetAI];
243 NSString *label = [NSString stringWithFormat:@"%@\n%@", method, targetAI];
246 NSString *targetState =
nil;
247 NSUInteger j = commandIter;
248 for (; j < commandCount; j++)
250 NSString *command = [handlerCommands oo_stringAtIndex:j];
251 if ([command hasPrefix:
@"setStateTo:"])
254 if ([components
count] > 1) targetState = [components objectAtIndex:1];
257 if (targetState !=
nil)
259 token = [NSString stringWithFormat:@"%@_%@", token, targetState];
260 label = [NSString stringWithFormat:@"%@ (%@)", label, targetState];
263 token = GraphVizTokenString(token,
nil);
264 label = EscapedGraphVizString(label);
266 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#408000\" shape=ellipse]\n", token, label]];
267 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#408000\"]\n", handlerToken, token];
271 [specialNodes addObject:[NSString stringWithFormat:@"\tspecial_broken_%@ [label=\"Broken %@ command!\\n(No target AI specified.)\" color=\"#C00000\" shape=diamond]\n", methodTag, method]];
272 [graphViz appendFormat:@"\t%@ -> tspecial_broken_%@ [color=\"#C00000\"]\n", handlerToken, methodTag];
NSMutableArray * ScanTokensFromString(NSString *values)
BOOL writeDiagnosticString:toFileNamed:(NSString *string,[toFileNamed] NSString *name)