DiaDes  0.1
DIAgnosis of Discrete-Event System
sync-info.cc
Go to the documentation of this file.
1 
11 #include<iostream>
12 #include<fstream>
13 #include<list>
14 #include<string>
15 #include<set>
16 
17 
18 #include<utils/Exceptions.hh>
19 #include<utils/CmdInterface.hh>
20 #include<automata/SyncRulesLightLexer.hpp>
21 #include<automata/SyncRulesLightParser.hpp>
22 
23 using namespace std;
24 using namespace Diades::Utils;
25 
26 /************************************************************************************/
27 typedef enum { COMPONENTS=0, EVENTS=1, NUMBER=2, CONNECTIONWITHEVENT=3, HELP=4 } Option;
28 typedef enum { RULES=0 } FileExtension;
29 unsigned numberOfOptions = 5;
31 
32 
34 vector<string> options(numberOfOptions);
35 vector<bool> isSet(numberOfOptions,false);
36 
37 string description="Usage: dd-sync-info sync.rules [--help | --components | --events | --number | --connection-with-event event]\n\t Print information about the synchronization law in 'sync.rules\n\t\t'--help' print this usage message\n\t\t'--components' the list of component names involved in this law\n\t\t- '--events' the list of synchronized events\n\t\t'--number' the number of connections\n\t\t'--connection-with-event event' the list of events involved in a connection with 'event'";
38 
40 {
41  options[COMPONENTS] = "--components";
42  options[EVENTS] = "--events";
43  options[NUMBER] = "--number";
44  options[CONNECTIONWITHEVENT] = "--connection-with-event";
45  options[HELP] = "--help";
46  fileExtensions[RULES] = "rules";
47 }
48 
49 
50 /************************************************************************************/
51 
52 void readParameters(int argc, char * argv[], string & rulesFile, string & eventLabel);
53 
54 /************************************************************************************/
55 
56 int main(int argc, char * argv[])
57 {
58  string rulesFile;
59  string eventLabel;
61  readParameters(argc,argv,rulesFile,eventLabel);
62  if(isSet[HELP])
63  {
65  }
66  if(rulesFile.empty())
67  {
68  printError("No synchronisation file found");
69  }
70  else
71  {
72  set<string> components;
73  list< set<string> > connections;
74  try
75  {
76  // Specific parsing as diades::automata one
77  // requires the use of Component, Event, etc.
78  ifstream file(rulesFile);
79  Diades::Automata::SyncRulesLightLexer lexer(file);
80  Diades::Automata::SyncRulesLightParser parser(lexer);
81  parser.syncrules(components,connections);
82  file.close();
83  }
84  catch(exception & e)
85  {
86  string msg = "Something unexpected happened: ";
87  msg += e.what();
88  printError(msg);
89  }
90 
91 
92  if(isSet[COMPONENTS])
93  {
94  for(const string & comp : components)
95  {
96  cout << comp << " ";
97  }
98  }
99 
100 
101  if(isSet[EVENTS])
102  {
103  set<string> events;
104 
105  for(const set<string> & setEvts : connections)
106  {
107  events.insert(setEvts.begin(), setEvts.end());
108  }
109 
110 
111  for(const string & evt : events)
112  {
113  cout << evt << " ";
114  }
115  }
116 
117  if(isSet[NUMBER])
118  {
119  cout << connections.size();
120  }
121 
123  {
124  if(eventLabel.empty())
125  {
126  printError("No event label found after option --connection-with-event");
127  }
128  set<string> events;
129 
130  for(const set<string> & setEvts : connections)
131  {
132  if(setEvts.find(eventLabel) != setEvts.end())
133  {
134  events.insert(setEvts.begin(), setEvts.end());
135  }
136  }
137  events.erase(eventLabel);
138  for(const string & evt : events)
139  {
140  cout << evt << " ";
141  }
142  }
143  }
144  return 0;
145 }
146 
147 /*************************************************************************************/
148 
149 void readParameters(int argc, char * argv[], string & rulesFile, string & eventLabel)
150 {
151  if(argc < 1)
152  {
154  exit(1);
155  }
156 
157 
158  int index = 1;
159  while( index < argc )
160  {
161  Option currentOption;
162  if(getOption<Option>(argv[index],options,currentOption))
163  {
164  if(isSet[currentOption])
165  {
166  printError("The option '" + options[currentOption] + "' occurs at least twice.");
167  }
168  else
169  {
170  isSet[currentOption] = true;
171  }
172  switch(currentOption)
173  {
174  case COMPONENTS:
175  {
176  ++index;
177  break;
178  }
179  case EVENTS:
180  {
181  ++index;
182  break;
183  }
184  case NUMBER:
185  {
186  ++index;
187  break;
188  }
189  case HELP:
190  {
191  ++index;
192  break;
193  }
194  case CONNECTIONWITHEVENT:
195  {
196  ++index;
197  getParameter<string>(argc,argv,index,eventLabel);
198  break;
199  }
200  }
201  }
202  else
203  {
204  vector<string>::const_iterator it =
205  getFileExtension(string(argv[index]),fileExtensions.begin(),fileExtensions.end());
206  if(it == fileExtensions.end())
207  {
208  printError("Unrecognized file extension in file name: " + string(argv[index]));
209  }
210  FileExtension extension = RULES;
211  switch(extension)
212  {
213  case RULES:
214  {
215  rulesFile = string(argv[index]);
216  break;
217  }
218  }
219  ++index;
220  }
221  }
222 }
223 
vector< bool > isSet(numberOfOptions, false)
int main(int argc, char *argv[])
Definition: sync-info.cc:56
InputIterator getFileExtension(const std::basic_string< CharType, CharTraits > &fileName, InputIterator begin, InputIterator end)
Definition: StringTools.hh:57
vector< string > fileExtensions(numberOfFileExtensions)
STL namespace.
FileExtension
Definition: abstract.cc:33
string description
Definition: sync-info.cc:37
void initialiseOptions()
Definition: sync-info.cc:39
FileExtension
Definition: sync-info.cc:28
vector< string > options(numberOfOptions)
Option
Definition: abstract.cc:32
unsigned numberOfFileExtensions
Definition: sync-info.cc:30
unsigned numberOfOptions
Definition: sync-info.cc:29
void readParameters(int argc, char *argv[], string &rulesFile, string &eventLabel)
Definition: sync-info.cc:149
void printError(string parameter, string message)
Option
Definition: sync-info.cc:27
void printUsage(const po::options_description &desc)