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
28#import "OOStringParsing.h"
29#import "ResourceManager.h"
30
31// Generate and track unique identifiers for state-handler pairs.
32static NSString* HandlerToken(NSString* state, NSString* handler, NSMutableDictionary* handlerKeys, NSMutableSet* uniqueSet);
33static void HandleOneCommand(NSMutableString* graphViz, NSString* stateKey, NSString* handlerKey, NSMutableDictionary* handlerKeys, NSArray* handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet* specialNodes, NSMutableSet* uniqueSet, BOOL* haveSetOrSwichAI);
34static void AddSimpleSpecialNodeLink(NSMutableString* graphViz, NSString* handlerToken, NSString* name, NSString* shape, NSString* color, NSMutableSet* specialNodes);
35static void AddExitAINode(NSMutableString* graphViz, NSString* handlerToken, NSString* message, NSMutableSet* specialNodes);
36static void AddChangeAINode(NSMutableString* graphViz, NSString* handlerToken, NSString* method, NSArray* components, NSArray* handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet* specialNodes);
37
38void GenerateGraphVizForAIStateMachine(NSDictionary* stateMachine, NSString* smName)
39{
40 NSMutableSet* uniqueSet = [NSMutableSet set];
41 NSMutableDictionary* handlerKeys = [NSMutableDictionary dictionary];
42
43 NSMutableString* graphViz =
44 [NSMutableString stringWithFormat:
45 @"digraph ai_flow\n{\n"
46 "\tgraph [charset=\"UTF-8\", label=\"%@ transition diagram\", labelloc=t, labeljust=l rankdir=LR compound=true nodesep=0.1 ranksep=2.5 fontname=Helvetica]\n"
47 "\tedge [arrowhead=normal]\n"
48 "\tnode [shape=box height=0.2 width=3.5 fontname=Helvetica color=\"#808080\"]\n\t\n"
49 "\tspecial_start [shape=ellipse color=\"#0000C0\" label=\"Start\"]\n\tspecial_start -> %@ [lhead=\"cluster_GLOBAL\" color=\"#0000A0\"]\n",
50 EscapedGraphVizString(smName), HandlerToken(@"GLOBAL", @"ENTER", handlerKeys, uniqueSet)];
51
52 NSString* stateKey = nil;
53
54 NSMutableSet* specialNodes = [NSMutableSet set];
55
56 foreachkey(stateKey, stateMachine)
57 {
58 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
59
60 [graphViz appendFormat:@"\t\n\tsubgraph cluster_%@\n\t{\n\t\tlabel=\"%@\"\n", stateKey, EscapedGraphVizString(stateKey)];
61
62 NSDictionary* state = [stateMachine oo_dictionaryForKey:stateKey];
63 NSString* handlerKey = nil;
64 foreachkey(handlerKey, state)
65 {
66 [graphViz appendFormat:@"\t\t%@ [label=\"%@\"]\n", HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet), EscapedGraphVizString(handlerKey)];
67 }
68
69 // Ensure there is an ENTER handler for arrows to point at.
70 if ([state objectForKey:@"ENTER"] == nil) {
71 [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)];
72 }
73
74 [graphViz appendString:@"\t}\n"];
75
76 // Go through each handler looking for interesting methods.
77 foreachkey(handlerKey, state)
78 {
79 NSArray* handlerCommands = [state oo_arrayForKey:handlerKey];
80 NSUInteger commandIter, commandCount = [handlerCommands count];
81 BOOL haveSetOrSwichAI = NO;
82
83 for (commandIter = 0; commandIter < commandCount; commandIter++) {
84 HandleOneCommand(graphViz, stateKey, handlerKey, handlerKeys, handlerCommands, commandIter, commandCount, specialNodes, uniqueSet, &haveSetOrSwichAI);
85 }
86 }
87
88 [pool release];
89 }
90
91 if ([specialNodes count] != 0) {
92 [graphViz appendString:@"\t\n"];
93
94 NSString* special = nil;
95 foreach (special, specialNodes) {
96 [graphViz appendString:special];
97 }
98 }
99
100 [graphViz appendString:@"}\n"];
101 [ResourceManager writeDiagnosticString:graphViz toFileNamed:[NSString stringWithFormat:@"AI Dumps/%@.dot", smName]];
102}
103
104static NSString* HandlerToken(NSString* state, NSString* handler, NSMutableDictionary* handlerKeys, NSMutableSet* uniqueSet)
105{
106 NSString* result = [[handlerKeys oo_dictionaryForKey:state] oo_stringForKey:handler];
107
108 if (result == nil) {
109 result = [NSString stringWithFormat:@"%@_h_%@", state, handler];
110 result = GraphVizTokenString(result, uniqueSet);
111
112 NSMutableDictionary* stateDict = [handlerKeys objectForKey:state];
113 if (stateDict == nil) {
114 stateDict = [NSMutableDictionary dictionary];
115 [handlerKeys setObject:stateDict forKey:state];
116 }
117
118 [stateDict setObject:result forKey:handler];
119 }
120
121 return result;
122}
123
124static void HandleOneCommand(NSMutableString* graphViz, NSString* stateKey, NSString* handlerKey, NSMutableDictionary* handlerKeys, NSArray* handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet* specialNodes, NSMutableSet* uniqueSet, BOOL* haveSetOrSwichAI)
125{
126 NSString* command = [handlerCommands oo_stringAtIndex:commandIter];
127 if (EXPECT_NOT(command == nil))
128 return;
129
130 NSArray* components = ScanTokensFromString(command);
131 NSString* method = [components objectAtIndex:0];
132 NSString* handlerToken = HandlerToken(stateKey, handlerKey, handlerKeys, uniqueSet);
133
134 if (!*haveSetOrSwichAI && [method isEqualToString:@"setStateTo:"]) {
135 if ([components count] > 1) {
136 NSString* targetState = [components objectAtIndex:1];
137 NSString* targetLabel = HandlerToken(targetState, @"ENTER", handlerKeys, uniqueSet);
138 BOOL constraint = YES;
139 if ([targetState isEqualToString:stateKey])
140 constraint = NO;
141 else if ([targetState isEqualToString:@"GLOBAL"])
142 constraint = NO;
143
144 [graphViz appendFormat:@"\t%@ -> %@ [lhead=cluster_%@%@]\n", handlerToken, targetLabel, targetState, constraint ? @"" : @" constraint=false"];
145 } else {
146 [specialNodes addObject:@"\tspecial_brokenSetStateTo [label=\"Broken setStateTo: command!\\n(No target state specified.)\" color=\"#C00000\" shape=diamond]\n"];
147 [graphViz appendFormat:@"\t%@ -> special_brokenSetStateTo [color=\"#C00000\"]\n", handlerToken];
148 }
149 } else if ([method isEqualToString:@"becomeExplosion"]) {
150 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeExplosion", @"diamond", @"804000", specialNodes);
151 } else if ([method isEqualToString:@"becomeEnergyBlast"]) {
152 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeEnergyBlast", @"diamond", @"804000", specialNodes);
153 } else if ([method isEqualToString:@"landOnPlanet"]) {
154 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"landOnPlanet", @"diamond", @"008040", specialNodes);
155 } else if ([method isEqualToString:@"performHyperSpaceExit"]) {
156 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"performHyperSpaceExit", @"box", @"008080", specialNodes);
157 } else if ([method isEqualToString:@"performHyperSpaceExitWithoutReplacing"]) {
158 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"performHyperSpaceExitWithoutReplacing", @"box", @"008080", specialNodes);
159 } else if ([method isEqualToString:@"enterTargetWormhole"]) {
160 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"enterTargetWormhole", @"box", @"008080", specialNodes);
161 } else if ([method isEqualToString:@"becomeUncontrolledThargon"]) {
162 AddSimpleSpecialNodeLink(graphViz, handlerToken, @"becomeUncontrolledThargon", @"ellipse", @"804000", specialNodes);
163 } else if ([method isEqualToString:@"exitAIWithMessage:"]) {
164 NSString* message = ([components count] > 1) ? [components objectAtIndex:1] : nil;
165 AddExitAINode(graphViz, handlerToken, message, specialNodes);
166 } else if ([method isEqualToString:@"setAITo:"] || [method isEqualToString:@"switchAITo:"]) {
167 *haveSetOrSwichAI = YES;
168 AddChangeAINode(graphViz, handlerToken, method, components, handlerCommands, commandIter, commandCount, specialNodes);
169 }
170}
171
172static void AddSimpleSpecialNodeLink(NSMutableString* graphViz, NSString* handlerToken, NSString* name, NSString* shape, NSString* color, NSMutableSet* specialNodes)
173{
174 NSString* identifier = GraphVizTokenString([@"special_" stringByAppendingString:name], nil);
175 NSString* declaration = [NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#%@\" shape=%@]\n", identifier, EscapedGraphVizString(name), color, shape];
176 [specialNodes addObject:declaration];
177
178 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#%@\"]\n", handlerToken, identifier, color];
179}
180
181static void AddExitAINode(NSMutableString* graphViz, NSString* handlerToken, NSString* message, NSMutableSet* specialNodes)
182{
183 NSString* token = nil;
184 NSString* label = nil;
185 if ([message isEqualToString:@"RESTARTED"] || [message length] == 0) {
186 token = @"exitAI";
187 label = @"exitAI";
188 } else {
189 token = GraphVizTokenString([@"exitAI_" stringByAppendingString:message], nil);
190 label = EscapedGraphVizString([@"exitAIWithMessage:\n" stringByAppendingString:message]);
191 }
192
193 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#0000A0\" shape=ellipse]\n", token, label]];
194 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#0000C0\"]\n", handlerToken, token];
195}
196
197static void AddChangeAINode(NSMutableString* graphViz, NSString* handlerToken, NSString* method, NSArray* components, NSArray* handlerCommands, NSUInteger commandIter, NSUInteger commandCount, NSMutableSet* specialNodes)
198{
199 NSString* methodTag = [method substringToIndex:[method length] - 3]; // delete "To:".
200
201 if ([components count] > 1) {
202 NSString* targetAI = [components objectAtIndex:1];
203 NSString* token = [NSString stringWithFormat:@"%@_%@", methodTag, targetAI];
204 NSString* label = [NSString stringWithFormat:@"%@\n%@", method, targetAI];
205
206 // Look through remaining commands for a setStateTo:, which applies to the new AI.
207 NSString* targetState = nil;
208 NSUInteger j = commandIter;
209 for (; j < commandCount; j++) {
210 NSString* command = [handlerCommands oo_stringAtIndex:j];
211 if ([command hasPrefix:@"setStateTo:"]) {
212 NSArray* components = ScanTokensFromString(command);
213 if ([components count] > 1)
214 targetState = [components objectAtIndex:1];
215 }
216 }
217 if (targetState != nil) {
218 token = [NSString stringWithFormat:@"%@_%@", token, targetState];
219 label = [NSString stringWithFormat:@"%@ (%@)", label, targetState];
220 }
221
222 token = GraphVizTokenString(token, nil);
223 label = EscapedGraphVizString(label);
224
225 [specialNodes addObject:[NSString stringWithFormat:@"\t%@ [label=\"%@\" color=\"#408000\" shape=ellipse]\n", token, label]];
226 [graphViz appendFormat:@"\t%@ -> %@ [color=\"#408000\"]\n", handlerToken, token];
227 } else {
228 [specialNodes addObject:[NSString stringWithFormat:@"\tspecial_broken_%@ [label=\"Broken %@ command!\\n(No target AI specified.)\" color=\"#C00000\" shape=diamond]\n", methodTag, method]];
229 [graphViz appendFormat:@"\t%@ -> tspecial_broken_%@ [color=\"#C00000\"]\n", handlerToken, methodTag];
230 }
231}
232
233#endif
#define foreachkey(VAR, DICT)
#define EXPECT_NOT(x)
unsigned count
return nil
NSMutableArray * ScanTokensFromString(NSString *values)
BOOL writeDiagnosticString:toFileNamed:(NSString *string,[toFileNamed] NSString *name)