DiaDes  0.1
DIAgnosis of Discrete-Event System
BddStateMachine.hh
Go to the documentation of this file.
1 /*
2  * @file BddStateMachine.hh
3  * @author Yannick Pencole, ypencole@laas.fr, LAAS-CNRS, Univ. Toulouse, FRANCE
4  *
5  * @date 3 November 2017, 12:44
6  */
7 
8 #ifndef __DIADES__SDMDL_EXPERIMENTAL__BDDSTATEMACHINE__HH__
9 #define __DIADES__SDMDL_EXPERIMENTAL__BDDSTATEMACHINE__HH__
10 
11 #include <vector>
12 #include <unordered_map>
14 
16 
17 namespace Diades
18 {
19  namespace Sdmdl
20  {
21  namespace Experimental
22  {
23 
33  {
34  public:
35 
41  static string typeName()
42  {
43  return "Diades::Sdmdl::Experimental::BddStateMachine";
44  }
45 
50 
55 
60 
65  using SlStates = Formula;
66 
72 
77  using SlEvents = Formula;
78 
82  using Event = size_t;
83  private:
84 
93 
100 
101 
105  std::unordered_map<std::string, FormulaVector> _eventOfValue;
106 
110  std::unordered_map<Event, FormulaVector> _formulaOfEvent;
111 
112 
140 
145  mutable std::vector< std::reference_wrapper<FormulaFactory> > _factory;
146 
147  public:
148 
159  _behaviour(),
160  _initialState(),
161  _noTransitions(),
162  _eventOfValue(),
163  _formulaOfEvent(),
164  _events(),
165  _setOfEvents(),
166  _stateVariable(),
167  _nextStateVariable(),
168  _eventVariable(),
169  _internalFactory(),
170  _factory()
171  {
172  _factory.push_back(_internalFactory);
173  setInitialStates(_internalFactory.isFalse());
174  _behaviour.push_back(_internalFactory.isFalse());
175  _events.push_back(_internalFactory.isFalse());
176  _setOfEvents.push_back(_internalFactory.isFalse());
177  };
178 
192  {
193  _factory.clear();
194  _factory.push_back(factory);
195  return _factory[0].get();
196  }
197 
207  {
208  // std::cout << "HELOLLLLjajoue" << std::endl;
209  _factory.clear();
210 
211  _factory.push_back(_internalFactory);
212  VariableVector varSet;
213  if (!_eventVariable.empty())
214  {
215  varSet.push_back(eventVariable());
216  }
217  // std::cout << "_stateVar" << _stateVariable.size() << std::endl;
218  if (!_stateVariable.empty())
219  {
220  varSet.push_back(stateVariable());
221  }
222 
223  if (!_nextStateVariable.empty())
224  {
225  varSet.push_back(nextStateVariable());
226  }
227  _internalFactory.reset(varSet);
228  setInitialStates(_internalFactory.isFalse());
229  _behaviour.clear();
230  _behaviour.push_back(_internalFactory.isFalse());
231  _events.clear();
232  _events.push_back(_internalFactory.isFalse());
233  _setOfEvents.clear();
234  _setOfEvents.push_back(_internalFactory.isFalse());
235  return _factory[0].get();
236  }
237 
242  const Variable & eventVariable() const
243  {
244  return _eventVariable[0].get();
245  }
246 
252  {
253  return _eventVariable[0].get();
254  }
255 
264  {
265  _eventVariable.clear();
266  _eventVariable.push_back(event);
267  return _eventVariable[0].get();
268  }
269 
275  {
276  return _stateVariable[0].get();
277  }
278 
283  const Variable & stateVariable() const
284  {
285  return _stateVariable[0].get();
286  }
287 
296  {
297  _stateVariable.clear();
298  _stateVariable.push_back(stateVar);
299  return _stateVariable[0].get();
300  }
301 
307  {
308  return _nextStateVariable[0].get();
309  }
310 
315  const Variable & nextStateVariable() const
316  {
317  return _nextStateVariable[0].get();
318  }
319 
328  {
329  _nextStateVariable.clear();
330  _nextStateVariable.push_back(nextStateVar);
331  return _nextStateVariable[0].get();
332  }
333 
339  {
340  _factory.clear();
341  _internalFactory.destroy();
342  };
343 
350  const SlStates & initialStates() const
351  {
352  require(Exception, _initialState.size() == 1,
353  "initialStates: not initialised");
354  return _initialState[0].get();
355  }
356 
362  const SlStates & addInitialStates(const SlStates & newInitialStates);
363 
369  const SlStates & setInitialStates(const SlStates & newInitialStates)
370  {
371  _initialState.clear();
372  _initialState.push_back(newInitialStates);
373  return _initialState.back();
374  }
375 
381  const SlEvents & addEvent(Event evt)
382  {
383  stringstream stream;
384  stream << evt;
385  auto & value = eventVariable().getValue(stream.str());
386 
387  _events.push_back(factory().newFormula(eventVariable(), value));
388  _eventOfValue[value.label()].push_back(_events.back());
389  _formulaOfEvent[evt].push_back(_events.back());
390  const SlEvents & currentSetOfEvent = _setOfEvents[0].get();
391  _setOfEvents.clear();
392  _setOfEvents.push_back(currentSetOfEvent || _events.back());
393  return _events.back();
394  }
395 
401  const SlEvents & event(const Value & eventValue) const
402  {
403  auto it = _eventOfValue.find(eventValue.label());
404  return (it->second)[0].get();
405  }
406 
412  const SlEvents & event(Event evt) const
413  {
414  auto it = _formulaOfEvent.find(evt);
415  return (it->second)[0].get();
416  }
417 
423  const SlEvents & sources(const Value & sourceStateValue) const
424  {
425  return _factory[0].get().newFormula(stateVariable(),sourceStateValue);
426  }
427 
434  template<typename ValueIterator>
435  const SlEvents & sources(ValueIterator first, ValueIterator last) const
436  {
437  FormulaVector tmp;
438  if(first!=last)
439  {
440  tmp.push_back(_factory[0].get().isTrue());
441  }
442  else
443  {
444  tmp.push_back(_factory[0].get().isFalse());
445  }
446  while(first != last)
447  {
448  Formula & current = tmp[0].get();
449  tmp.clear();
450  tmp.push_back(current && sources(*first));
451  ++first;
452  }
453  return tmp[0].get();
454  }
455 
456 
462  const SlEvents & targets(const Value & targetStateValue) const
463  {
464  return _factory[0].get().newFormula(nextStateVariable(),targetStateValue);
465  }
466 
467 
474  template<typename ValueIterator>
475  const SlEvents & targets(ValueIterator first, ValueIterator last) const
476  {
477  FormulaVector tmp;
478  if(first!=last)
479  {
480  tmp.push_back(_factory[0].get().isTrue());
481  }
482  else
483  {
484  tmp.push_back(_factory[0].get().isFalse());
485  }
486  while(first != last)
487  {
488  Formula & current = tmp[0].get();
489  tmp.clear();
490  tmp.push_back(current && targets(*first));
491  ++first;
492  }
493  return tmp[0].get();
494  }
495 
501  const SlTransitions & addTransitions(const SlTransitions & transition)
502  {
503  const SlTransitions & currentTransitions = _behaviour[0].get();
504  _behaviour.clear();
505  _behaviour.push_back(currentTransitions
506  ||
507  transition);
508  return transition;
509  }
510 
515  const SlTransitions & transitions() const
516  {
517  require(Exception, _behaviour.size() == 1,
518  "transitions: not initialised");
519  return _behaviour[0].get();
520  }
521 
527  const SlTransitions & transitions(const SlEvents & event) const
528  {
529  require(Exception, _behaviour.size() == 1,
530  "transitions: not initialised");
531  return _behaviour[0].get() && event;
532  }
533 
534 
541  const SlTransitions & noTransitions() const;
542 
549  const SlTransitions & transitions(const SlStates & sources, const SlEvents & events) const
550  {
551  require(Exception, _behaviour.size() == 1,
552  "transitions: not initialised");
553  return (_behaviour[0].get() && sources && events);
554  }
555 
559  const SlEvents & events() const
560  {
561  return _setOfEvents[0].get();
562  }
563 
567  template<typename EventIterator>
568  const SlEvents & events(EventIterator first, EventIterator last)
569  {
570  FormulaVector tmp;
571  tmp.push_back(factory().isFalse());
572  while(first != last)
573  {
574  const SlEvents & current = tmp[0].get();
575  tmp.clear();
576  tmp.push_back(current || event(*first));
577  ++first;
578  }
579  const SlEvents & result = tmp[0].get();
580  tmp.clear();
581  return result;
582  }
592  const SlStates & trigger(const SlStates & sources, const SlEvents & events) const;
593 
594 
605  const SlStates & synchronousTrigger(const SlStates & sources,
606  const SlEvents & events) const;
607 
620  const SlStates & reach(const SlStates & sources, const SlEvents & nonObservables,
621  const SlEvents & observations);
622 
624  {
625  require(Exception, _factory.size() == 1,
626  "BddComponent::factory: not initialised");
627  return _factory[0].get();
628  }
629 
630 
631 
636  void clear();
637 
638  };
639 
640 
641  }
642  }
643 }
644 
645 
646 #endif
647 
const SlEvents & events(EventIterator first, EventIterator last)
void reset(const VariableVector &varSet)
const Value & getValue(const string &label) const
const SlEvents & event(Event evt) const
std::unordered_map< Event, FormulaVector > _formulaOfEvent
const SlStates & addInitialStates(const SlStates &newInitialStates)
Formula class using BDD.
std::vector< ConstReference > FormulaVector
Definition: Formula.hh:74
const SlTransitions & transitions(const SlEvents &event) const
const SlStates & setInitialStates(const SlStates &newInitialStates)
const SlEvents & targets(ValueIterator first, ValueIterator last) const
Variable & setStateVariable(Variable &stateVar)
const SlEvents & targets(const Value &targetStateValue) const
Variable & setEventVariable(Variable &event)
std::vector< Reference > VariableVector
Definition: Variable.hh:33
const SlEvents & sources(ValueIterator first, ValueIterator last) const
const SlEvents & event(const Value &eventValue) const
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
const SlTransitions & transitions(const SlStates &sources, const SlEvents &events) const
std::vector< std::reference_wrapper< FormulaFactory > > _factory
const SlStates & synchronousTrigger(const SlStates &sources, const SlEvents &events) const
Definition: Run.cc:83
const Formula & isFalse() const
Definition: Formula.hh:884
FormulaFactory & setFormulaFactory(FormulaFactory &factory)
std::unordered_map< std::string, FormulaVector > _eventOfValue
const SlStates & trigger(const SlStates &sources, const SlEvents &events) const
const std::string & label() const
Definition: Value.hh:113
const SlTransitions & noTransitions() const
const SlStates & reach(const SlStates &sources, const SlEvents &nonObservables, const SlEvents &observations)
const SlEvents & sources(const Value &sourceStateValue) const
const SlTransitions & transitions() const
Variable & setNextStateVariable(Variable &nextStateVar)
const SlTransitions & addTransitions(const SlTransitions &transition)