Oolite 1.91.0.7645-241119-222d325
Loading...
Searching...
No Matches
AIGraphViz.m
Go to the documentation of this file.
1/*
2
3AIGraphViz.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#if DEBUG_GRAPHVIZ
26
27#import "OOStringParsing.h"
28#import "ResourceManager.h"
30
31
32// Generate and track unique identifiers for state-handler pairs.
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);
38
39
40void GenerateGraphVizForAIStateMachine(NSDictionary *stateMachine, NSString *smName)
41{
42 NSMutableSet *uniqueSet = [NSMutableSet set];
43 NSMutableDictionary *handlerKeys = [NSMutableDictionary dictionary];
44
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)];
52
53 NSEnumerator *stateKeyEnum = [stateMachine keyEnumerator];
54 NSString *stateKey = nil;
55
56 NSMutableSet *specialNodes = [NSMutableSet set];
57
58 while ((stateKey = [stateKeyEnum nextObject]))
59 {
60 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
61
62 [graphViz appendFormat:@"\t\n\tsubgraph cluster_%@\n\t{\n\t\tlabel=\"%@\"\n", stateKey, EscapedGraphVizString(stateKey)];
63
64 NSDictionary *state = [stateMachine oo_dictionaryForKey:stateKey];
65 NSEnumerator *handlerKeyEnum = [state keyEnumerator];
66 NSString *handlerKey = nil;
67 while ((handlerKey = [handlerKeyEnum nextObject]))
68 {
69 [graphViz appendFormat:@"\t\t%@ [label=\"%@\"]\n", HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet), EscapedGraphVizString(handlerKey)];
70 }
71
72 // Ensure there is an ENTER handler for arrows to point at.
73 if ([state objectForKey:@"ENTER"] == nil)
74 {
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)];
76 }
77
78 [graphViz appendString:@"\t}\n"];
79
80 // Go through each handler looking for interesting methods.
81 handlerKeyEnum = [state keyEnumerator];
82 while ((handlerKey = [handlerKeyEnum nextObject]))
83 {
84 NSArray *handlerCommands = [state oo_arrayForKey:handlerKey];
85 NSUInteger commandIter, commandCount = [handlerCommands count];
86 BOOL haveSetOrSwichAI = NO;
87
88 for (commandIter = 0; commandIter < commandCount; commandIter++)
89 {
90 HandleOneCommand(graphViz, stateKey, handlerKey, handlerKeys, handlerCommands, commandIter, commandCount, specialNodes, uniqueSet, &haveSetOrSwichAI);
91 }
92 }
93
94 [pool release];
95 }
96
97 if ([specialNodes count] != 0)
98 {
99 [graphViz appendString:@"\t\n"];
100
101 NSEnumerator *specialEnum = [specialNodes objectEnumerator];
102 NSString *special = nil;
103 while ((special = [specialEnum nextObject]))
104 {
105 [graphViz appendString:special];
106 }
107 }
108
109 [graphViz appendString:@"}\n"];
110 [ResourceManager writeDiagnosticString:graphViz toFileNamed:[NSString stringWithFormat:@"AI Dumps/%@.dot", smName]];
111}
112
113
114static NSString *HandlerToken(NSString *state, NSString *handler, NSMutableDictionary *handlerKeys, NSMutableSet *uniqueSet)
115{
116 NSString *result = [[handlerKeys oo_dictionaryForKey:state] oo_stringForKey:handler];
117
118 if (result == nil)
119 {
120 result = [NSString stringWithFormat:@"%@_h_%@", state, handler];
121 result = GraphVizTokenString(result, uniqueSet);
122
123 NSMutableDictionary *stateDict = [handlerKeys objectForKey:state];
124 if (stateDict == nil)
125 {
126 stateDict = [NSMutableDictionary dictionary];
127 [handlerKeys setObject:stateDict forKey:state];
128 }
129
130 [stateDict setObject:result forKey:handler];
131 }
132
133 return result;
134}
135
136
137static void HandleOneCommand(NSMutableString *graphViz, NSString *stateKey, NSString *handlerKey, NSMutableDictionary *handlerKeys, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes, NSMutableSet *uniqueSet, BOOL *haveSetOrSwichAI)
138{
139 NSString *command = [handlerCommands oo_stringAtIndex:commandIter];
140 if (EXPECT_NOT(command == nil)) return;
141
142 NSArray *components = ScanTokensFromString(command);
143 NSString *method = [components objectAtIndex:0];
144 NSString *handlerToken = HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet);
145
146 if (!*haveSetOrSwichAI && [method isEqualToString:@"setStateTo:"])
147 {
148 if ([components count] > 1)
149 {
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;
155
156 [graphViz appendFormat:@"\t%@ -> %@ [lhead=cluster_%@%@]\n", handlerToken, targetLabel, targetState, constraint ? @"" : @" constraint=false"];
157 }
158 else
159 {
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];
162 }
163 }
164 else if ([method isEqualToString:@"becomeExplosion"])
165 {
166 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeExplosion", @"diamond", @"804000", specialNodes);
167 }
168 else if ([method isEqualToString:@"becomeEnergyBlast"])
169 {
170 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeEnergyBlast", @"diamond", @"804000", specialNodes);
171 }
172 else if ([method isEqualToString:@"landOnPlanet"])
173 {
174 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"landOnPlanet", @"diamond", @"008040", specialNodes);
175 }
176 else if ([method isEqualToString:@"performHyperSpaceExit"])
177 {
178 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"performHyperSpaceExit", @"box", @"008080", specialNodes);
179 }
180 else if ([method isEqualToString:@"performHyperSpaceExitWithoutReplacing"])
181 {
182 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"performHyperSpaceExitWithoutReplacing", @"box", @"008080", specialNodes);
183 }
184 else if ([method isEqualToString:@"enterTargetWormhole"])
185 {
186 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"enterTargetWormhole", @"box", @"008080", specialNodes);
187 }
188 else if ([method isEqualToString:@"becomeUncontrolledThargon"])
189 {
190 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeUncontrolledThargon", @"ellipse", @"804000", specialNodes);
191 }
192 else if ([method isEqualToString:@"exitAIWithMessage:"])
193 {
194 NSString *message = ([components count] > 1) ? [components objectAtIndex:1] : nil;
195 AddExitAINode(graphViz, handlerToken, message, specialNodes);
196 }
197 else if ([method isEqualToString:@"setAITo:"] || [method isEqualToString:@"switchAITo:"])
198 {
199 *haveSetOrSwichAI = YES;
200 AddChangeAINode(graphViz, handlerToken, method, components, handlerCommands, commandIter, commandCount, specialNodes);
201 }
202}
203
204
205static void AddSimpleSpecialNodeLink(NSMutableString *graphViz, NSString *handlerToken, NSString *name, NSString *shape, NSString *color, NSMutableSet *specialNodes)
206{
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];
210
211 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#%@\"]\n", handlerToken, identifier, color];
212}
213
214
215static void AddExitAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *message, NSMutableSet *specialNodes)
216{
217 NSString *token = nil;
218 NSString *label = nil;
219 if ([message isEqualToString:@"RESTARTED"] || [message length] == 0)
220 {
221 token = @"exitAI";
222 label = @"exitAI";
223 }
224 else
225 {
226 token = GraphVizTokenString([@"exitAI_" stringByAppendingString:message], nil);
227 label = EscapedGraphVizString([@"exitAIWithMessage:\n" stringByAppendingString:message]);
228 }
229
230 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#0000A0\" shape=ellipse]\n", token, label]];
231 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#0000C0\"]\n", handlerToken, token];
232}
233
234
235static void AddChangeAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *method, NSArray *components, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes)
236{
237 NSString *methodTag = [method substringToIndex:[method length] - 3]; // delete "To:".
238
239 if ([components count] > 1)
240 {
241 NSString *targetAI = [components objectAtIndex:1];
242 NSString *token = [NSString stringWithFormat:@"%@_%@", methodTag, targetAI];
243 NSString *label = [NSString stringWithFormat:@"%@\n%@", method, targetAI];
244
245 // Look through remaining commands for a setStateTo:, which applies to the new AI.
246 NSString *targetState = nil;
247 NSUInteger j = commandIter;
248 for (; j < commandCount; j++)
249 {
250 NSString *command = [handlerCommands oo_stringAtIndex:j];
251 if ([command hasPrefix:@"setStateTo:"])
252 {
253 NSArray *components = ScanTokensFromString(command);
254 if ([components count] > 1) targetState = [components objectAtIndex:1];
255 }
256 }
257 if (targetState != nil)
258 {
259 token = [NSString stringWithFormat:@"%@_%@", token, targetState];
260 label = [NSString stringWithFormat:@"%@ (%@)", label, targetState];
261 }
262
263 token = GraphVizTokenString(token, nil);
264 label = EscapedGraphVizString(label);
265
266 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#408000\" shape=ellipse]\n", token, label]];
267 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#408000\"]\n", handlerToken, token];
268 }
269 else
270 {
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];
273 }
274}
275
276#endif
#define EXPECT_NOT(x)
unsigned count
return nil
NSMutableArray * ScanTokensFromString(NSString *values)
BOOL writeDiagnosticString:toFileNamed:(NSString *string,[toFileNamed] NSString *name)