summaryrefslogtreecommitdiffstats
path: root/apps/uscxml-dot.cpp
blob: 0fcd54815132a3c2dbc2b049228bc169aeb5f766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "uscxml/config.h"
#include "uscxml/Interpreter.h"
#include "uscxml/dom/DOMUtils.h"
#include "uscxml/debug/SCXMLDotWriter.h"
#include <glog/logging.h>
#include "getopt.h"

#include "uscxml/Factory.h"
#include <boost/algorithm/string.hpp>


using namespace uscxml;

void printUsageAndExit(const char* progName) {
	// remove path from program name
	std::string progStr(progName);
	if (progStr.find_last_of(PATH_SEPERATOR) != std::string::npos) {
		progStr = progStr.substr(progStr.find_last_of(PATH_SEPERATOR) + 1, progStr.length() - (progStr.find_last_of(PATH_SEPERATOR) + 1));
	}

	printf("%s version " USCXML_VERSION " (" CMAKE_BUILD_TYPE " build - " CMAKE_COMPILER_STRING ")\n", progStr.c_str());
	printf("Usage\n");
	printf("\t%s", progStr.c_str());
	printf(" [-eTYPE] [-dN] [-tN] URL");
	printf(" [[-dN] [-tN] [-eTYPE] state_id1] .. [[-dN] [-tN] [-eTYPE] state_idM]");
	printf("\n");
	printf("Options\n");
	printf("\tURL       : URL of SCXML document\n");
	printf("\t-e TYPE   : type of edges to use:\n");
	printf("\t             'target' - aggregate per target node (default)\n");
	printf("\t             'event' - aggregate per event name\n");
	printf("\t             'transition' no aggregation, display each transition\n");
	printf("\t-d        : depth below anchor node (INF per default)\n");
	printf("\t-t        : transition depth below anchor (INF per default)\n");
	printf("\tstate_id  : anchor node state id (topmost scxml element per default)\n");
	printf("\n");
	exit(1);
}

int currOpt = 1;

int main(int argc, char** argv) {

	// setup logging
	google::LogToStderr();
	google::InitGoogleLogging(argv[0]);


	if (argc < 2)
		printUsageAndExit(argv[0]);

	std::list<SCXMLDotWriter::StateAnchor> stateAnchors;
	SCXMLDotWriter::StateAnchor rootAnchor;
	SCXMLDotWriter::StateAnchor currAnchor;

	int option;
	while ((option = getopt(argc, argv, "d:t:e:")) != -1) {
		switch(option) {
		case 'd':
			rootAnchor.childDepth = strTo<int32_t>(optarg);
			break;
		case 't':
			rootAnchor.transDepth = strTo<int32_t>(optarg);
			break;
		case 'e': {
			std::string edgeType(optarg);
			if (edgeType == "target") {
				rootAnchor.type = SCXMLDotWriter::PORT_TARGET;
			} else if (edgeType == "event") {
				rootAnchor.type = SCXMLDotWriter::PORT_EVENT;
			} else if (edgeType == "transition") {
				rootAnchor.type = SCXMLDotWriter::PORT_TRANSITION;
			} else {
				printUsageAndExit(argv[0]);
			}
			break;
		}
		default:
			break;
		}
	}

	if (rootAnchor)
		stateAnchors.push_back(rootAnchor);

	try {
		// current option has to be the interpreter's name
		URL inputFile(argv[optind]);
		Interpreter interpreter = Interpreter::fromURL(inputFile.asString());
		optind++;

		while(optind < argc) {
			// are
			while ((option = getopt(argc, argv, "d:t:")) != -1) {
				switch(option) {
				case 'd':
					currAnchor.childDepth = strTo<int32_t>(optarg);
					break;
				case 't':
					currAnchor.transDepth = strTo<int32_t>(optarg);
					break;
				default:
					break;
				}
			}
			if (argc > optind) {
				std::string expr(argv[optind++]);
				currAnchor.element = interpreter.getImpl()->getState(expr);
			} else {
				printUsageAndExit(argv[0]);
			}

			if (currAnchor) {
				currAnchor.type = rootAnchor.type;
				stateAnchors.push_back(currAnchor);
			}

			currAnchor = SCXMLDotWriter::StateAnchor();
		}

		std::string outName;
		if (boost::starts_with("file", inputFile.scheme())) {
			outName = inputFile.path() + ".dot";
		} else {
			outName = inputFile.file() + ".dot";
		}

		SCXMLDotWriter::toDot(outName, interpreter, stateAnchors);

	} catch(Event e) {
		std::cerr << e << std::cout;
	}

	return EXIT_SUCCESS;
}