Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a very quick GraphViz file builder that outputs a DAG of the ma…

…chine in GV format and, if "dot" is available converts it into a PNG file.

Added the -g command line option to generate graphs.
  • Loading branch information...
commit 735b89aaf2d37b1f11cd0de7eddaa0ece41ed2ea 1 parent ac0259c
@mmower authored
View
17 Statec/Builder/StatecGVBuilder.h
@@ -0,0 +1,17 @@
+//
+// StatecGVBuilder.h
+// Statec
+//
+// Created by Matt Mower on 02/02/2012.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@class StatecMachine;
+
+@interface StatecGVBuilder : NSObject
+
+- (NSString *)gvSourceFromMachine:(StatecMachine *)machine;
+
+@end
View
56 Statec/Builder/StatecGVBuilder.m
@@ -0,0 +1,56 @@
+//
+// StatecGVBuilder.m
+// Statec
+//
+// Created by Matt Mower on 02/02/2012.
+// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
+//
+
+#import "StatecGVBuilder.h"
+
+#import "Statec.h"
+
+@implementation StatecGVBuilder
+
+- (NSString *)gvTransitions:(StatecMachine *)machine {
+ NSMutableString *gv = [NSMutableString string];
+
+ for( StatecState *state in [[machine states] allValues] ) {
+ for( StatecEvent *event in [state events] ) {
+ [gv appendFormat:
+ @"%@ -> %@ [label = \"%@\"];\n",
+ [state name],
+ [event targetState],
+ [event name]
+ ];
+ }
+ }
+
+ return gv;
+}
+
+
+- (NSString *)gvSourceFromMachine:(StatecMachine *)machine {
+ NSMutableString *gv = [NSMutableString string];
+
+ [gv appendFormat:
+ @"digraph %@ {\n"
+ @" rankdir=LR;\n"
+// @" size=\"16,8\";\n"
+ @" node [shape = circle];\n"
+ @" node [fontsize = 15];\n"
+ @" node [fontfamily = inconsolata];\n"
+ @" edge [fontsize = 15];\n"
+ @" edge [fontfamily = inconsolata];\n"
+ @"%@"
+ @"}",
+ [machine name],
+ [self gvTransitions:machine]
+ ];
+
+ return gv;
+}
+
+
+
+@end
View
31 Statec/main.m
@@ -12,6 +12,8 @@
#import "Statec.h"
+#import "StatecGVBuilder.h"
+
int main (int argc, const char * argv[])
{
@autoreleasepool {
@@ -20,10 +22,10 @@ int main (int argc, const char * argv[])
NSString *outputFolder;
NSString *inputFile;
- BOOL overwriteUserFiles = NO;
+ BOOL overwriteUserFiles = NO, generateGraph = NO;
int ch;
- while( ( ch = getopt( argc, argv, "d:i:o" ) ) != -1 ) {
+ while( ( ch = getopt( argc, argv, "d:i:og" ) ) != -1 ) {
switch( ch ) {
case 'd':
outputFolder = [[NSString alloc] initWithUTF8String:optarg];
@@ -34,6 +36,9 @@ int main (int argc, const char * argv[])
case 'o':
overwriteUserFiles = YES;
break;
+ case 'g':
+ generateGraph = YES;
+ break;
}
}
@@ -93,6 +98,28 @@ int main (int argc, const char * argv[])
} else {
NSLog( @"User machine already exists." );
}
+
+ if( generateGraph ) {
+ NSString *graphFileName = [outputFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.gv",[[compiler machine] name]]];
+ StatecGVBuilder *gvBuilder = [[StatecGVBuilder alloc] init];
+ NSString *gvSource = [gvBuilder gvSourceFromMachine:[compiler machine]];
+ [gvSource writeToFile:graphFileName
+ atomically:NO
+ encoding:NSUTF8StringEncoding error:nil];
+
+
+ NSString *pathEnv = [[[NSProcessInfo processInfo] environment] objectForKey:@"PATH"];
+ NSArray *paths = [pathEnv componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@":"]];
+ for( NSString *path in paths ) {
+ if( [fileManager fileExistsAtPath:[path stringByAppendingPathComponent:@"dot"]] ) {
+ NSLog( @"Generating image file" );
+ NSTask *dotTask = [NSTask launchedTaskWithLaunchPath:[path stringByAppendingPathComponent:@"dot"]
+ arguments:[NSArray arrayWithObjects:@"-O",@"-Tpng",graphFileName,nil]];
+ [dotTask waitUntilExit];
+ }
+ }
+ }
+
}
return 0;
Please sign in to comment.
Something went wrong with that request. Please try again.