Oolite
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 NSString *stateKey = nil;
54
55 NSMutableSet *specialNodes = [NSMutableSet set];
56
57 foreachkey (stateKey, stateMachine)
58 {
59 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
60
61 [graphViz appendFormat:@"\t\n\tsubgraph cluster_%@\n\t{\n\t\tlabel=\"%@\"\n", stateKey, EscapedGraphVizString(stateKey)];
62
63 NSDictionary *state = [stateMachine oo_dictionaryForKey:stateKey];
64 NSString *handlerKey = nil;
65 foreachkey (handlerKey, state)
66 {
67 [graphViz appendFormat:@"\t\t%@ [label=\"%@\"]\n", HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet), EscapedGraphVizString(handlerKey)];
68 }
69
70 // Ensure there is an ENTER handler for arrows to point at.
71 if ([state objectForKey:@"ENTER"] == nil)
72 {
73 [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)];
74 }
75
76 [graphViz appendString:@"\t}\n"];
77
78 // Go through each handler looking for interesting methods.
79 foreachkey (handlerKey, state)
80 {
81 NSArray *handlerCommands = [state oo_arrayForKey:handlerKey];
82 NSUInteger commandIter, commandCount = [handlerCommands count];
83 BOOL haveSetOrSwichAI = NO;
84
85 for (commandIter = 0; commandIter < commandCount; commandIter++)
86 {
87 HandleOneCommand(graphViz, stateKey, handlerKey, handlerKeys, handlerCommands, commandIter, commandCount, specialNodes, uniqueSet, &haveSetOrSwichAI);
88 }
89 }
90
91 [pool release];
92 }
93
94 if ([specialNodes count] != 0)
95 {
96 [graphViz appendString:@"\t\n"];
97
98 NSString *special = nil;
99 foreach (special, specialNodes)
100 {
101 [graphViz appendString:special];
102 }
103 }
104
105 [graphViz appendString:@"}\n"];
106 [ResourceManager writeDiagnosticString:graphViz toFileNamed:[NSString stringWithFormat:@"AI Dumps/%@.dot", smName]];
107}
108
109
110static NSString *HandlerToken(NSString *state, NSString *handler, NSMutableDictionary *handlerKeys, NSMutableSet *uniqueSet)
111{
112 NSString *result = [[handlerKeys oo_dictionaryForKey:state] oo_stringForKey:handler];
113
114 if (result == nil)
115 {
116 result = [NSString stringWithFormat:@"%@_h_%@", state, handler];
117 result = GraphVizTokenString(result, uniqueSet);
118
119 NSMutableDictionary *stateDict = [handlerKeys objectForKey:state];
120 if (stateDict == nil)
121 {
122 stateDict = [NSMutableDictionary dictionary];
123 [handlerKeys setObject:stateDict forKey:state];
124 }
125
126 [stateDict setObject:result forKey:handler];
127 }
128
129 return result;
130}
131
132
133static void HandleOneCommand(NSMutableString *graphViz, NSString *stateKey, NSString *handlerKey, NSMutableDictionary *handlerKeys, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes, NSMutableSet *uniqueSet, BOOL *haveSetOrSwichAI)
134{
135 NSString *command = [handlerCommands oo_stringAtIndex:commandIter];
136 if (EXPECT_NOT(command == nil)) return;
137
138 NSArray *components = ScanTokensFromString(command);
139 NSString *method = [components objectAtIndex:0];
140 NSString *handlerToken = HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet);
141
142 if (!*haveSetOrSwichAI && [method isEqualToString:@"setStateTo:"])
143 {
144 if ([components count] > 1)
145 {
146 NSString *targetState = [components objectAtIndex:1];
147 NSString *targetLabel = HandlerToken(targetState, @"ENTER", handlerKeys, uniqueSet);
148 BOOL constraint = YES;
149 if ([targetState isEqualToString:stateKey]) constraint = NO;
150 else if ([targetState isEqualToString:@"GLOBAL"]) constraint = NO;
151
152 [graphViz appendFormat:@"\t%@ -> %@ [lhead=cluster_%@%@]\n", handlerToken, targetLabel, targetState, constraint ? @"" : @" constraint=false"];
153 }
154 else
155 {
156 [specialNodes addObject:@"\tspecial_brokenSetStateTo [label=\"Broken setStateTo: command!\\n(No target state specified.)\" color=\"#C00000\" shape=diamond]\n"];
157 [graphViz appendFormat:@"\t%@ -> special_brokenSetStateTo [color=\"#C00000\"]\n", handlerToken];
158 }
159 }
160 else if ([method isEqualToString:@"becomeExplosion"])
161 {
162 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeExplosion", @"diamond", @"804000", specialNodes);
163 }
164 else if ([method isEqualToString:@"becomeEnergyBlast"])
165 {
166 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeEnergyBlast", @"diamond", @"804000", specialNodes);
167 }
168 else if ([method isEqualToString:@"landOnPlanet"])
169 {
170 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"landOnPlanet", @"diamond", @"008040", specialNodes);
171 }
172 else if ([method isEqualToString:@"performHyperSpaceExit"])
173 {
174 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"performHyperSpaceExit", @"box", @"008080", specialNodes);
175 }
176 else if ([method isEqualToString:@"performHyperSpaceExitWithoutReplacing"])
177 {
178 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"performHyperSpaceExitWithoutReplacing", @"box", @"008080", specialNodes);
179 }
180 else if ([method isEqualToString:@"enterTargetWormhole"])
181 {
182 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"enterTargetWormhole", @"box", @"008080", specialNodes);
183 }
184 else if ([method isEqualToString:@"becomeUncontrolledThargon"])
185 {
186 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeUncontrolledThargon", @"ellipse", @"804000", specialNodes);
187 }
188 else if ([method isEqualToString:@"exitAIWithMessage:"])
189 {
190 NSString *message = ([components count] > 1) ? [components objectAtIndex:1] : nil;
191 AddExitAINode(graphViz, handlerToken, message, specialNodes);
192 }
193 else if ([method isEqualToString:@"setAITo:"] || [method isEqualToString:@"switchAITo:"])
194 {
195 *haveSetOrSwichAI = YES;
196 AddChangeAINode(graphViz, handlerToken, method, components, handlerCommands, commandIter, commandCount, specialNodes);
197 }
198}
199
200
201static void AddSimpleSpecialNodeLink(NSMutableString *graphViz, NSString *handlerToken, NSString *name, NSString *shape, NSString *color, NSMutableSet *specialNodes)
202{
203 NSString *identifier = GraphVizTokenString([@"special_" stringByAppendingString:name], nil);
204 NSString *declaration = [NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#%@\" shape=%@]\n", identifier, EscapedGraphVizString(name), color, shape];
205 [specialNodes addObject:declaration];
206
207 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#%@\"]\n", handlerToken, identifier, color];
208}
209
210
211static void AddExitAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *message, NSMutableSet *specialNodes)
212{
213 NSString *token = nil;
214 NSString *label = nil;
215 if ([message isEqualToString:@"RESTARTED"] || [message length] == 0)
216 {
217 token = @"exitAI";
218 label = @"exitAI";
219 }
220 else
221 {
222 token = GraphVizTokenString([@"exitAI_" stringByAppendingString:message], nil);
223 label = EscapedGraphVizString([@"exitAIWithMessage:\n" stringByAppendingString:message]);
224 }
225
226 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#0000A0\" shape=ellipse]\n", token, label]];
227 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#0000C0\"]\n", handlerToken, token];
228}
229
230
231static void AddChangeAINode(NSMutableString *graphViz, NSString *handlerToken, NSString *method, NSArray *components, NSArray *handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet *specialNodes)
232{
233 NSString *methodTag = [method substringToIndex:[method length] - 3]; // delete "To:".
234
235 if ([components count] > 1)
236 {
237 NSString *targetAI = [components objectAtIndex:1];
238 NSString *token = [NSString stringWithFormat:@"%@_%@", methodTag, targetAI];
239 NSString *label = [NSString stringWithFormat:@"%@\n%@", method, targetAI];
240
241 // Look through remaining commands for a setStateTo:, which applies to the new AI.
242 NSString *targetState = nil;
243 NSUInteger j = commandIter;
244 for (; j < commandCount; j++)
245 {
246 NSString *command = [handlerCommands oo_stringAtIndex:j];
247 if ([command hasPrefix:@"setStateTo:"])
248 {
249 NSArray *components = ScanTokensFromString(command);
250 if ([components count] > 1) targetState = [components objectAtIndex:1];
251 }
252 }
253 if (targetState != nil)
254 {
255 token = [NSString stringWithFormat:@"%@_%@", token, targetState];
256 label = [NSString stringWithFormat:@"%@ (%@)", label, targetState];
257 }
258
259 token = GraphVizTokenString(token, nil);
260 label = EscapedGraphVizString(label);
261
262 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#408000\" shape=ellipse]\n", token, label]];
263 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#408000\"]\n", handlerToken, token];
264 }
265 else
266 {
267 [specialNodes addObject:[NSString stringWithFormat:@"\tspecial_broken_%@ [label=\"Broken %@ command!\\n(No target AI specified.)\" color=\"#C00000\" shape=diamond]\n", methodTag, method]];
268 [graphViz appendFormat:@"\t%@ -> tspecial_broken_%@ [color=\"#C00000\"]\n", handlerToken, methodTag];
269 }
270}
271
272#endif
#define foreachkey(VAR, DICT)
Definition OOCocoa.h:353
#define EXPECT_NOT(x)
unsigned count
return nil
NSMutableArray * ScanTokensFromString(NSString *values)
BOOL writeDiagnosticString:toFileNamed:(NSString *string,[toFileNamed] NSString *name)