DiaDes  0.1
DIAgnosis of Discrete-Event System
SupremicaFormat.cc
Go to the documentation of this file.
1 #include <list>
2 #include <map>
3 #include <iostream>
4 #include <boost/property_tree/ptree.hpp>
5 #include <boost/property_tree/xml_parser.hpp>
6 #include <boost/foreach.hpp>
7 #include <utils/CmdInterface.hh>
8 
9 
10 using std::list;
11 using std::map;
12 using namespace Diades::Utils;
13 
14 
15 /************************************************************************************/
16 
17 typedef enum { HELP=0 } Option;
18 typedef enum { XML=0 } FileExtension;
19 unsigned numberOfOptions = 1;
21 
22 
24 vector<string> options(numberOfOptions);
25 vector<bool> isSet(numberOfOptions,false);
26 
27 string description="Usage: dd-convert-supremica [--help | automaton.xml] \n\t Print the component described in the xml file (export of supremica) in the 'des_comp' format.";
28 
30 {
31  options[HELP] = "--help";
32  fileExtensions[XML] = "xml";
33 }
34 
35 /************************************************************************************/
36 
37 
38 void readParameters(int argc, char * argv[],string & filename);
39 
40 
41 /************************************************************************************/
42 
43 int main(int argc, char * argv[])
44 {
45 
46  using std::string;
47  string filename;
49  readParameters(argc,argv,filename);
50  string initialState;
51  string otherStates;
52 
53  string observables;
54  unsigned numberOfObservables = 0;
55 
56  string nonObservables;
57  unsigned numberOfNonObservables = 0;
58 
59  unsigned stateNumber = 0;
60 
61 
62 
63  unsigned transNumber = 0;
64  string transitions;
65 
66  using boost::property_tree::ptree;
67  ptree pt;
68  read_xml(filename,pt);
69 
70  map<unsigned,string> mapStates;
71  map<unsigned,string> mapEvents;
72 
73 
74 
75  BOOST_FOREACH(ptree::value_type const& v, pt.get_child("Automata"))
76  {
77  if(v.first == "<xmlattr>")
78  {
79  //std::cout << v.second.get<std::string>("name") << std::endl;
80  //std::cout << v.second.get<unsigned>("major") << std::endl;
81  //std::cout << v.second.get<unsigned>("minor") << std::endl;
82  }
83  if(v.first == "Automaton")
84  {
85  BOOST_FOREACH(ptree::value_type const& v2, v.second.get_child(""))
86  {
87 
88  if(v2.first == "<xmlattr>")
89  {
90  std::cout << v2.second.get<std::string>("name") << std::endl;
91 
92  }
93 
94  if(v2.first == "Events")
95  {
96  BOOST_FOREACH(ptree::value_type const& evt, v2.second.get_child(""))
97  {
98  BOOST_FOREACH(ptree::value_type const& evtAttr, evt.second.get_child(""))
99  {
100 
101  //std::cout << "Event: id=" << evtAttr.second.get<unsigned>("id") << " label=" << evtAttr.second.get<std::string>("label") << std::endl;
102  if(evtAttr.second.count("observable") != 0)
103  {
104  if( evtAttr.second.get<bool>("observable") )
105  {
106  observables += "o" + evtAttr.second.get<std::string>("id") + " ";
107  mapEvents[evtAttr.second.get<unsigned>("id")] ="o" + evtAttr.second.get<std::string>("id");
108  ++numberOfObservables;
109  }
110  else
111  {
112  nonObservables += "u" + evtAttr.second.get<std::string>("id") + " ";
113  mapEvents[evtAttr.second.get<unsigned>("id")] = "u" + evtAttr.second.get<std::string>("id");
114  ++numberOfNonObservables;
115 
116  }
117  //std::cout << " observable? " << evtAttr.second.get<bool>("observable") << std::endl;
118  }
119  else
120  {
121  observables += "o" + evtAttr.second.get<std::string>("id") + " ";
122  mapEvents[evtAttr.second.get<unsigned>("id")] ="o" + evtAttr.second.get<std::string>("id");
123  ++numberOfObservables;
124  //std::cout << " observable? " << true << std::endl;
125  }
126  if(evtAttr.second.count("controllable") != 0)
127  {
128  //std::cout << " controllable? " << evtAttr.second.get<bool>("controllable") << std::endl;
129  }
130  else
131  {
132  //std::cout << " controllable? " << true << std::endl;
133  }
134  }
135  }
136 
137 
138 
139  }
140  if(v2.first == "States")
141  {
142  BOOST_FOREACH(ptree::value_type const& state, v2.second.get_child(""))
143  {
144 
145  BOOST_FOREACH(ptree::value_type const& stateAttr, state.second.get_child(""))
146  {
147  stateNumber++;
148  mapStates[stateAttr.second.get<unsigned>("id")] = "q" + stateAttr.second.get<string>("id");
149  //std::cout << "State: id=" << stateAttr.second.get<unsigned>("id") << " name=" << stateAttr.second.get<std::string>("name") << std::endl;
150  // if(stateAttr.second.count("accepting") != 0)
151  // {
152  //std::cout << " accepting? " << stateAttr.second.get<bool>("accepting") << std::endl;
153  // }
154  //else
155  // {
156  //std::cout << " accepting? " << false << std::endl;
157  // }
158 
159  if(stateAttr.second.count("initial") != 0)
160  {
161  if(stateAttr.second.get<bool>("initial"))
162  {
163  initialState = "q" + stateAttr.second.get<std::string>("id");
164  }
165  else
166  {
167  otherStates += "q" + stateAttr.second.get<std::string>("id") + " ";
168  }
169  //std::cout << " initial? " << stateAttr.second.get<bool>("initial") << std::endl;
170  }
171  else
172  {
173  //std::cout << " initial? " << false << std::endl;
174  otherStates += "q" + stateAttr.second.get<std::string>("id") + " ";
175  }
176  }
177  }
178  }
179  //stateAttr id="497" name="on1.open1.on2.closed2.faulty3_off.open3" accepting="true
180  if(v2.first == "Transitions")
181  {
182  BOOST_FOREACH(ptree::value_type const& transition, v2.second.get_child(""))
183  {
184 
185  BOOST_FOREACH(ptree::value_type const& transAttr, transition.second.get_child(""))
186  {
187  ++transNumber;
188  transitions += mapStates[transAttr.second.get<unsigned>("source")] + " -> " + mapStates[transAttr.second.get<unsigned>("dest")] + " " + mapEvents[transAttr.second.get<unsigned>("event")] + "\n";
189  //std::cout << "Transition: source=" << transAttr.second.get<unsigned>("source")
190  // << " dest=" << transAttr.second.get<unsigned>("dest") << " event="
191  // << transAttr.second.get<unsigned>("event") << std::endl;
192  }
193  }
194  }
195  //<Transition source="511" dest="509" event="8"/>
196 
197  }
198 
199  }
200  }
201 
202  std::cout << stateNumber << std::endl;
203  std::cout << initialState << " " << otherStates << std::endl;
204  std::cout << 0 << std::endl;
205  std::cout << numberOfNonObservables << std::endl;
206  std::cout << nonObservables << std::endl;
207  std::cout << 0 << std::endl;
208  std::cout << numberOfObservables << std::endl;
209  std::cout << observables << std::endl;
210  std::cout << transNumber << std::endl;
211  std::cout << transitions << std::endl;
212 }
213 
214 
215 void readParameters(int argc, char * argv[],string & filename)
216 {
217  list<string> modelFiles;
218  if(argc < 1)
219  {
221  exit(1);
222  }
223 
224  int index = 1;
225  while( index < argc )
226  {
227  Option currentOption;
228  if(getOption<Option>(argv[index],options,currentOption))
229  {
230  if(isSet[currentOption])
231  {
232  printError("The option '" + options[currentOption] + "' occurs at least twice.");
233  exit(1);;
234  }
235  else
236  {
237  isSet[currentOption] = true;
238  }
239  switch(currentOption)
240  {
241  case HELP:
242  {
244  exit(0);;
245  break;
246  }
247  }
248  }
249  else
250  {
251 
252  vector<string>::const_iterator it =
253  getFileExtension(string(argv[index]),fileExtensions.begin(),fileExtensions.end());
254  if(it == fileExtensions.end())
255  {
256  printError("Unrecognized file extension in file name: " + string(argv[index]));
257  exit(1);;
258  }
259  FileExtension extension = XML;
260  switch(extension)
261  {
262  case XML:
263  {
264  modelFiles.push_back(string(argv[index]));
265  break;
266  }
267  }
268  ++index;
269  }
270  }
271  if(modelFiles.size() >1)
272  {
273  printError("Too many xml files, one only is allowed");
274  exit(1);
275  }
276  if(modelFiles.size() == 0)
277  {
278  printError("No xml files, one only is mandatory");
279  exit(1);
280  }
281  filename = modelFiles.front();
282 }
283 
string description
InputIterator getFileExtension(const std::basic_string< CharType, CharTraits > &fileName, InputIterator begin, InputIterator end)
Definition: StringTools.hh:57
vector< bool > isSet(numberOfOptions, false)
vector< string > fileExtensions(numberOfFileExtensions)
vector< string > options(numberOfOptions)
unsigned numberOfFileExtensions
FileExtension
Definition: abstract.cc:33
Option
Definition: abstract.cc:32
int main(int argc, char *argv[])
void initialiseOptions()
void printError(string parameter, string message)
void readParameters(int argc, char *argv[], string &filename)
unsigned numberOfOptions
FileExtension
Option
void printUsage(const po::options_description &desc)