DiaDes  0.1
DIAgnosis of Discrete-Event System
GeneratePta.cc
Go to the documentation of this file.
1 #include<string>
2 #include<cmath>
3 #include<algorithm>
4 #include<fstream>
5 #include<sstream>
8 
9 using std::string;
10 using std::ifstream;
18 
19 string replaceSpace(const string & label)
20 {
21  string result = label;
22  std::replace( result.begin(), result.end(), ' ', '_');
23  return result;
24 }
25 
26 void exportToDesComp(const Component & ptaMoore, const string & fileName)
27 {
28  ofstream file(fileName.c_str());
29  file << ptaMoore.name() << endl;
30  file << ptaMoore.numberOfStates() << endl;
31  file << replaceSpace(ptaMoore.getLabel(*ptaMoore.initialStateBegin())) << " ";
32  for(Component::StateIterator stateIt = ptaMoore.stateBegin();
33  stateIt != ptaMoore.stateEnd();
34  ++stateIt)
35  {
36  if(*stateIt != *(ptaMoore.initialStateBegin()))
37  {
38  file << replaceSpace(ptaMoore.getLabel(*stateIt)) << " ";
39  }
40  }
41  file << endl << 0 << endl;
42 
43 
44 
45  set<string> events;
46  vector<string> transitions;
47 
48  for(Component::TransitionIterator transIt= ptaMoore.transitionBegin();
49  transIt!= ptaMoore.transitionEnd();
50  ++transIt)
51  {
52  string transitionBase;
53  string transition;
54  transitionBase = replaceSpace(ptaMoore.getLabel(transIt->source())) + " -> " + replaceSpace(ptaMoore.getLabel(transIt->target()));
55  transition = transitionBase + " " + ptaMoore.getEvent(*transIt).nickname();
56  transitions.push_back(transition);
57  events.insert(ptaMoore.getEvent(*transIt).nickname());
58  }
59  // writing the events
60  file << events.size() << endl;
61  for(set<string>::const_iterator it = events.begin(); it != events.end(); ++it)
62  {
63  file << *it << " ";
64  }
65  file << endl << 0 << endl;
66 
67  file << 0 << endl;
68 
69  // writing the transitions
70  file << endl << transitions.size() << endl;
71  for(vector<string>::size_type i = 0; i < transitions.size(); ++i)
72  {
73  file << transitions[i] << endl;
74  }
75 
76  // writing the end of file
77 
78  file.close();
79 }
80 
81 string stringOf(unsigned & counter)
82 {
83  stringstream stream;
84  stream << counter;
85  ++counter;
86  return stream.str();
87 }
88 
89 
90 void MooreEncoding(const string & filename)
91 {
93  unsigned counter = 0;
94  Component ptaMoore;
95  ptaMoore.setName("ptaMoore");
96  State initialMoore = ptaMoore.newState(stringOf(counter));
97  ptaMoore.setInitial(initialMoore);
98  // first, get the normal logs
99  ifstream log(filename);
100  std::string line;
101  while (std::getline(log, line))
102  {
103  std::istringstream iss(line);
104  string type;
105  iss >> type;
106  if(type=="N")
107  {
108  string event;
109  State currentMoore = *ptaMoore.initialStateBegin();
110  while(iss >> event)
111  {
112  const Event & evt = EventFactory::factory()->getEvent(event);
113  ptaMoore.insertEvent(evt);
115  ptaMoore.outputEventTransitionBegin(currentMoore,evt);
116  if(itMoore != ptaMoore.outputEventTransitionEnd(currentMoore,evt))
117  {
118  currentMoore = itMoore->target();
119  }
120  else
121  {
122  State targetMoore = ptaMoore.newState(stringOf(counter));
123  ptaMoore.newTransition(currentMoore,targetMoore,evt);
124  currentMoore = targetMoore;
125  }
126  }
127  // this is the last event, need to change the class of the last state for ptaMoore
128  Transition previousMoore = *currentMoore.inEdgeBegin();
129  currentMoore = previousMoore.source();
130  State newTargetNormalState = ptaMoore.newState(ptaMoore.getLabel(previousMoore.target())+ " N");
131  ptaMoore.newTransition(currentMoore, newTargetNormalState, ptaMoore.getEvent(previousMoore));
132 
133  for(Component::OutputTransitionIterator oit = ptaMoore.outputTransitionBegin(previousMoore.target());
134  oit != ptaMoore.outputTransitionEnd(previousMoore.target());
135  ++oit)
136  {
137  ptaMoore.newTransition(newTargetNormalState, oit->target(), ptaMoore.getEvent(*oit));
138  }
139  ptaMoore.deleteState(previousMoore.target());
140  }
141  }
142  log.close();
143 
144  ifstream log2(filename);
145  while (std::getline(log2, line))
146  {
147  std::istringstream iss(line);
148  string type;
149  iss >> type;
150  if(type!="N")
151  {
152  string event;
153  State currentMoore = *ptaMoore.initialStateBegin();
154  bool stateCreationMoore = false;
155  while(iss >> event)
156  {
157 
158  const Event & evt = EventFactory::factory()->getEvent(event);
159  ptaMoore.insertEvent(evt);
161  ptaMoore.outputEventTransitionBegin(currentMoore,evt);
162  if(itMoore != ptaMoore.outputEventTransitionEnd(currentMoore,evt))
163  {
164  currentMoore = itMoore->target();
165  }
166  else
167  {
168  State targetMoore = ptaMoore.newState(stringOf(counter));
169  ptaMoore.newTransition(currentMoore,targetMoore,evt);
170  currentMoore=targetMoore;
171  stateCreationMoore = true;
172  }
173  }
174 
175  // now we update the ptaMoore with the fault class
176  Transition previousMoore = *currentMoore.inEdgeBegin();
177 
178 
179  if(stateCreationMoore)
180  {
181  // the class of this log is unique, the current
182  // state is at the end of one path
183  string counter = ptaMoore.getLabel(currentMoore);
184  string faultClass = counter + " " + type;
185  State targetFaultMoore = ptaMoore.newState(faultClass);
186  ptaMoore.newTransition(previousMoore.source(),targetFaultMoore,
187  ptaMoore.getEvent(previousMoore));
188  ptaMoore.deleteState(previousMoore.target());
189  }
190  else
191  {
192  // the log reaches a state that was already built
193  // need to insert the new class
194  string label = ptaMoore.getLabel(currentMoore);
195  std::set<string> labels;
196  istringstream iss2(label);
197  string faultClass;
198  while(iss2 >> faultClass)
199  {
200  labels.insert(faultClass);
201  }
202  labels.insert(type);
203  faultClass = "";
204  for(const string & lbl : labels)
205  {
206  faultClass += lbl + " ";
207  }
208  State targetFaultMoore = ptaMoore.newState(faultClass);
209  ptaMoore.newTransition(previousMoore.source(),targetFaultMoore,
210  ptaMoore.getEvent(previousMoore));
211 
212  for(Component::OutputTransitionIterator oit = ptaMoore.outputTransitionBegin(previousMoore.target());
213  oit != ptaMoore.outputTransitionEnd(previousMoore.target());
214  ++oit)
215  {
216  ptaMoore.newTransition(targetFaultMoore, oit->target(), ptaMoore.getEvent(*oit));
217  }
218  ptaMoore.deleteState(previousMoore.target());
219  }
220  }
221  }
222  log2.close();
223  ptaMoore.component2dot("ptaMoore.dot");
224  exportToDesComp(ptaMoore,"ptaMoore.des_comp");
225 }
226 
227 
228 void MealyEncodingV1(const string & filename)
229 {
231  Component pta;
232  pta.setName("pta");
233  State initial = pta.newState();
234  pta.setInitial(initial);
235 
236  // first, get the normal logs
237  ifstream log(filename);
238  std::string line;
239  while (std::getline(log, line))
240  {
241  std::istringstream iss(line);
242  string type;
243  iss >> type;
244  if(type=="N")
245  {
246  string event;
248  while(iss >> event)
249  {
250  const Event & evt = EventFactory::factory()->getEvent(event);
251  pta.insertEvent(evt);
253  pta.outputEventTransitionBegin(current,evt);
254  if(it != pta.outputEventTransitionEnd(current,evt))
255  {
256  current = it->target();
257  }
258  else
259  {
260  State target = pta.newState();
261  pta.newTransition(current,target,evt);
262  current = target;
263  }
264  }
265  }
266  }
267  log.close();
268 
269  ifstream log2(filename);
270  while (std::getline(log2, line))
271  {
272  std::istringstream iss(line);
273  string type;
274  iss >> type;
275  if(type!="N")
276  {
277  const Event & fault = EventFactory::factory()->getEvent(type);
278  pta.insertEvent(fault);
279  string event;
281  bool stateCreation = false;
282  while(iss >> event)
283  {
284 
285  const Event & evt = EventFactory::factory()->getEvent(event);
286  pta.insertEvent(evt);
287 
289  pta.outputEventTransitionBegin(current,evt);
290  if(it != pta.outputEventTransitionEnd(current,evt))
291  {
292  current = it->target();
293  }
294  else
295  {
296  State target = pta.newState();
297  pta.newTransition(current,target,evt);
298  current=target;
299  stateCreation = true;
300  }
301  }
302 
303  // now we add the fault transition in the mealy pta
304  Transition previous = *current.inEdgeBegin();
305  current = previous.source();
306  State targetFault = pta.newState();
307  pta.newTransition(current,targetFault,fault);
308  current = targetFault;
309  State target = pta.newState();
310  pta.newTransition(current, target, pta.getEvent(previous));
311 
312  if(stateCreation)
313  {
314  // the log was definitely not a normal one
315  // so the transition with the last observable event that
316  // was created within the loop must be destroyed
317  pta.deleteState(previous.target());
318  }
319  }
320  }
321  log2.close();
322  pta.component2dot("pta.dot");
323  pta.exportDesCompModel("pta.des_comp");
324 }
325 
326 
327 string encodeTargetState(unsigned id, const vector<string> & varNames)
328 {
329  string result;
330  string op= ":=";
331  string fals = "false";
332  string tru = "true";
333 
334  for(unsigned i = 0; i < varNames.size(); ++i)
335  {
336  string assign = varNames[i] + op + ((id % 2 == 0) ? fals : tru);
337  result += (i < varNames.size() -1) ? assign + "," : assign;
338  id = id/2;
339  }
340  return result;
341 }
342 
343 string encodeSourceState(unsigned id, const vector<string> & varNames)
344 {
345  string result= "(" ;
346  string notS = " not ";
347  string andS = " and ";
348  string equal= "=";
349  for(unsigned i = 0; i < varNames.size(); ++i)
350  {
351  string assign = (id % 2 == 0) ? notS + varNames[i] : varNames[i];
352  result += (i < varNames.size() -1) ? assign + andS : assign;
353  id = id/2;
354  }
355  result+= ")";
356  return result;
357 }
358 
359 
360 
361 
362 void component2Orange(const ObservableComponent & pta, ostream & os)
363 {
364  os << "componenttype PTA {" << std::endl;
365  int encodingSize = (int) ceil(log(std::max((unsigned int)2,pta.numberOfStates())) / log(2));
366  vector<string> varNames(encodingSize);
367  os << std::endl;
368  for(unsigned i = 0; i < varNames.size(); ++i)
369  {
370  stringstream stream;
371  stream << "v" << i;
372  varNames[i] = stream.str();
373  os << " var " << varNames[i] << ": bool;" << std::endl;
374  }
375  os << std::endl;
376  for(auto & event : pta.events())
377  {
378  if(pta.mask().isUnobservable(event))
379  {
380  os << " event " << event << ";" << std::endl;
381  }
382  }
383  os << std::endl;
384  for(auto it = pta.observableBegin();
385  it != pta.observableEnd();
386  ++it)
387  {
388  if(*it!=pta.mask().noEvent())
389  {
390  os << " observable event " << *it << ";" << std::endl;
391  }
392  }
393  os << std::endl;
394  for(auto it = pta.observableBegin();
395  it != pta.observableEnd();
396  ++it)
397  {
398  if(*it!=pta.mask().noEvent())
399  {
400  unsigned counter = 0;
401  stringstream stream;
402  stream << " transition T" << *it;
403  os << stream.str() << std::endl;
404  for(auto trIt = pta.eventTransitionBegin(*it);
405  trIt != pta.eventTransitionEnd(*it);
406  ++trIt)
407  {
408  stringstream stream2;
409  stream2 << stream.str() << "_" << ++counter;
410  os << stream2.str() << " "
411  << encodeSourceState(trIt->source().id(),varNames)
412  << " -> "
413  << encodeTargetState(trIt->target().id(),varNames)
414  << ";" << std::endl;
415  }
416  os << " triggeredby " << *it << ";" << std::endl << std::endl;
417  }
418  }
419 
420  for(auto & event : pta.events())
421  {
422  if(event!=pta.mask().noEvent())
423  {
424  unsigned counter = 0;
425  stringstream stream;
426  stream << " transition T" << event;
427  os << stream.str() << std::endl;
428  for(auto trIt = pta.eventTransitionBegin(event);
429  trIt != pta.eventTransitionEnd(event);
430  ++trIt)
431  {
432  stringstream stream2;
433  stream2 << stream.str() << "_" << ++counter;
434  os << stream2.str() << " "
435  << encodeSourceState(trIt->source().id(),varNames)
436  << " -> "
437  << encodeTargetState(trIt->target().id(),varNames)
438  << ";" << std::endl;
439  }
440  os << " triggeredby " << event << ";" << std::endl << std::endl;
441  }
442  }
443  os << "}" << std::endl << std::endl;
444  os << "component pta0 = PTA;" << std::endl;
445 }
446 
447 
448 
449 
450 
451 
452 void MealyEncoding(const string & filename,const string & targetName)
453 {
456  ObservableMask mask;
457  pta.setName("pta");
458  State initial = pta.newState();
459  pta.setInitial(initial);
460 
461  // first, get the normal logs
462  ifstream log(filename);
463  std::string line;
464  while (std::getline(log, line))
465  {
466  std::istringstream iss(line);
467  string type;
468  iss >> type;
469 
470  string event;
472  while(iss >> event)
473  {
474  const Event & evt = EventFactory::factory()->getEvent(event);
475  pta.insertEvent(evt);
476  mask.addMask(evt,evt);
478  pta.outputEventTransitionBegin(current,evt);
479  if(it != pta.outputEventTransitionEnd(current,evt))
480  {
481  current = it->target();
482  }
483  else
484  {
485  State target = pta.newState();
486  pta.newTransition(current,target,evt);
487  current = target;
488  }
489  }
490 
491  if(type!="N")
492  {
493  const Event & faultEvt = EventFactory::factory()->getEvent(type);
494  pta.insertEvent(faultEvt);
495  mask.makeUnobservable(faultEvt);
497  pta.outputEventTransitionBegin(current,faultEvt);
498  if(it == pta.outputEventTransitionEnd(current,faultEvt))
499  {
500  State target = pta.newState();
501  pta.newTransition(current,target,faultEvt);
502  current = target;
503  }
504  else
505  {
506  current = it->target();
507  }
508  }
509  const Event & endEvt = EventFactory::factory()->getEvent("$");
510  pta.insertEvent(endEvt);
511  mask.makeUnobservable(endEvt);
513  pta.outputEventTransitionBegin(current,endEvt);
514  if(it == pta.outputEventTransitionEnd(current,endEvt))
515  {
516  State target = pta.newState();
517  pta.newTransition(current,target,endEvt);
518  current = target;
519  }
520  }
521  log.close();
522  pta.setMask(mask);
523  pta.component2dot(targetName + ".dot");
524  pta.exportDesCompModel(targetName + ".des_comp");
525  ofstream file(targetName + ".orange");
526  component2Orange(pta,file);
527  file.close();
528 }
529 
530 
531 
532 
533 int main(int argc, char ** argv)
534 {
535  string fileName = argv[1];
536  string targetName = argv[2];
537  MealyEncoding(fileName,targetName);
538 }
539 
540 
void MooreEncoding(const string &filename)
Definition: GeneratePta.cc:90
static Event getEvent(const Label &evtLabel)
virtual bool component2dot(const string &fileName) const
virtual bool exportDesCompModel(const string &filename) const
string stringOf(unsigned &counter)
Definition: GeneratePta.cc:81
OutputEventTransitionIterator outputEventTransitionBegin(State s, Event e) const
Definition: Component.hh:832
string replaceSpace(const string &label)
Definition: GeneratePta.cc:19
Diades::Graph::Edge Transition
Definition: Component.hh:46
void setMask(const ObservableMask &mask)
StateIterator stateBegin() const
Definition: Component.hh:572
const Label & nickname() const
Definition: Event.hh:190
void MealyEncoding(const string &filename, const string &targetName)
Definition: GeneratePta.cc:452
string encodeSourceState(unsigned id, const vector< string > &varNames)
Definition: GeneratePta.cc:343
const StateLabel & getLabel(State state) const
Definition: Component.hh:521
EventTransitionIterator eventTransitionEnd(const Event &e) const
Definition: Component.hh:817
StateIterator stateEnd() const
Definition: Component.hh:579
OutputEventTransitionIterator outputEventTransitionEnd(State s, Event e) const
Definition: Component.hh:850
void MealyEncodingV1(const string &filename)
Definition: GeneratePta.cc:228
void makeUnobservable(const Event &e)
TransitionIterator transitionEnd() const
Definition: Component.hh:904
void deleteState(State state)
static void init(size_t capacity)
Definition: Run.cc:85
An observable Component defined as a automaton.
void setName(const string &name)
Definition: Component.hh:246
AutFsm::State State
Definition: Run.cc:72
const string & name() const
Definition: Component.hh:256
AutFsm::Transition Transition
Definition: Run.cc:73
DdAutFsm::EventPropertyId Event
Definition: TrimState.cc:139
InitialStateIterator initialStateBegin() const
Definition: Component.hh:638
void setInitial(State state)
Definition: Component.hh:589
unsigned numberOfStates() const
Definition: Component.hh:563
EventTransitionIterator eventTransitionBegin(const Event &e) const
Definition: Component.hh:806
OutputTransitionIterator outputTransitionBegin(State s) const
Definition: Component.hh:913
Definition: Run.cc:83
const ObservableMask & mask() const
int main(int argc, char **argv)
Definition: GeneratePta.cc:533
virtual bool component2dot(const string &fileName) const
TransitionIterator transitionBegin() const
Definition: Component.hh:897
unordered_set< Transition >::const_iterator OutputEventTransitionIterator
Definition: Component.hh:74
const Event & getEvent(Transition t) const
Definition: Component.hh:304
virtual bool exportDesCompModel(const string &filename) const
void insertEvent(const Event &event)
string encodeTargetState(unsigned id, const vector< string > &varNames)
Definition: GeneratePta.cc:327
void addMask(const Event &e, const Event &obs)
Log log(Logger logger, const string &msg)
Definition: Log.hh:124
Diades::Graph::Node State
Definition: BeliefState.hh:36
Transition newTransition(State source, State target, Event event)
bool isUnobservable(const Event &e) const
void component2Orange(const ObservableComponent &pta, ostream &os)
Definition: GeneratePta.cc:362
void exportToDesComp(const Component &ptaMoore, const string &fileName)
Definition: GeneratePta.cc:26
const set< Event > & events() const
Definition: Component.hh:318
OutputTransitionIterator outputTransitionEnd(State s) const
Definition: Component.hh:925