DiaDes  0.1
DIAgnosis of Discrete-Event System
Abstract.hh
Go to the documentation of this file.
1 
9 #ifndef __DIADES__AUTOMATA__EXPERIMENTAL__ABSTRACT__HH
10 #define __DIADES__AUTOMATA__EXPERIMENTAL__ABSTRACT__HH
11 
12 #include<unordered_map>
13 #include<unordered_set>
17 
18 namespace Diades {
19  namespace Automata {
20  namespace Experimental {
21 
43  template<typename Fsm, typename BeliefStateIterator, typename StateCreation>
44  bool
45  abstract(const Fsm & machine,
46  Fsm & abstractedMachine,
48  std::vector<typename Fsm::State> & abstractedStates,
49  StateCreation & creator) {
50  using State = typename Fsm::State;
51  using Transition = typename Fsm::Transition;
52  if (machine == abstractedMachine) {
53  return false;
54  }
55  std::unordered_map<State, std::unordered_set < size_t>> bsOfState;
56  std::vector<BeliefStateIterator> iterators;
57 
58  for (auto it = bfBegin; it != bfEnd; ++it) {
59  bool result;
60  typename Fsm::State newState;
61  std::tie(newState,result) = creator.newState(*it);
62  iterators.push_back(it);
63  abstractedStates.push_back(newState);
64  std::for_each(it->begin(), it->end(), [&](State s) {
65  bsOfState[s].insert(iterators.size() - 1);
66  if (machine.isInitial(s)) {
67  abstractedMachine.setInitial(abstractedStates.back());
68  }
69  });
70  }
71  abstractedMachine.copyEventPropertyIds(machine);
72  std::for_each(bsOfState.begin(), bsOfState.end(),
73  [&](const std::pair<State, std::unordered_set < size_t> > & bsState) {
74  std::for_each(machine.outputTransitionBegin(bsState.first),
75  machine.outputTransitionEnd(bsState.first),
76  [&](Transition t) {
77  auto itTarget = bsOfState.find(t.target());
78  if (itTarget != bsOfState.end()) {
79  std::for_each(bsState.second.begin(),
80  bsState.second.end(),
81  [&](size_t sourceId) {
82  std::for_each(itTarget->second.begin(),
83  itTarget->second.end(),
84  [&](size_t targetId) {
85  abstractedMachine.newTransition(abstractedStates[sourceId],
86  abstractedStates[targetId],
87  machine.getEvent(t));
88  });
89  });
90  }
91  });
92  });
93  return true;
94  }
95 
96 
97 // /**
98 // *
99 // * @param machine a StateMachine
100 // * @param abstractMachine the resulting abstracted StateMachine
101 // * @param bfBegin the start iterator of a BeliefState<Property,EventInfo> range
102 // * @param bfEnd the end iterator of a BeliefState<Property,EventInfo> range
103 // * @param abstractedStates the created states of abstractMachine in the order of the BeliefState range.
104 // * @return true if the abstraction is a success. If not the state of the abstractedMachine
105 // * is undetermined.
106 // *
107 // * @pre machine should not be abstractedMachine
108 // * @p [bfBegin,bfEnd) represents a set of belief states bs1, bs2, ... bsn.
109 // * Each bsi is a set of State from the initial StateMachine 'machine'.
110 // * The abstracted machine will contain n states. State si will represent
111 // * the states of bsi. A transition t of event e between state si and sj exists
112 // * in the abstracted machine iff there exists a transition t of event e
113 // * between a state from bsi and a state from bsj in the original machine.
114 // * The state si of the abtracted machine is initial as soon as one of
115 // * the state of bsi is initial.
116 // *
117 // */
118 // template<typename Property, typename EventInfo, typename BeliefStateIterator>
119 // bool
120 // abstract(const StateMachine<Property, EventInfo> & machine,
121 // StateMachine<Property, EventInfo> & abstractedMachine,
122 // BeliefStateIterator bfBegin, BeliefStateIterator bfEnd,
123 // std::vector<typename StateMachine<Property, EventInfo>::State> & abstractedStates) {
124 // using Fsm = StateMachine<Property, EventInfo>;
125 // using State = typename Fsm::State;
126 // using Transition = typename Fsm::Transition;
127 // if (machine == abstractedMachine) {
128 // return false;
129 // }
130 // std::unordered_map<State, std::unordered_set < size_t>> bsOfState;
131 // std::vector<BeliefStateIterator> iterators;
132 //
133 // for (auto it = bfBegin; it != bfEnd; ++it) {
134 // iterators.push_back(it);
135 // abstractedStates.push_back(abstractedMachine.newState());
136 // std::for_each(it->begin(), it->end(), [&](State s) {
137 // bsOfState[s].insert(iterators.size() - 1);
138 // if (machine.isInitial(s)) {
139 // abstractedMachine.setInitial(abstractedStates.back());
140 // }
141 // });
142 // }
143 // abstractedMachine.copyEventPropertyIds(machine);
144 // std::for_each(bsOfState.begin(), bsOfState.end(),
145 // [&](const std::pair<State, std::unordered_set < size_t> > & bsState) {
146 // std::for_each(machine.outputTransitionBegin(bsState.first),
147 // machine.outputTransitionEnd(bsState.first),
148 // [&](Transition t) {
149 // auto itTarget = bsOfState.find(t.target());
150 // if (itTarget != bsOfState.end()) {
151 // std::for_each(bsState.second.begin(),
152 // bsState.second.end(),
153 // [&](size_t sourceId) {
154 // std::for_each(itTarget->second.begin(),
155 // itTarget->second.end(),
156 // [&](size_t targetId) {
157 // abstractedMachine.newTransition(abstractedStates[sourceId],
158 // abstractedStates[targetId],
159 // machine.getEvent(t));
160 // });
161 // });
162 // }
163 // });
164 // });
165 // return true;
166 // }
167 
188  template<typename Fsm, typename BeliefStateIterator, typename StateCreation>
189  bool
190  abstract(const Fsm & machine,
191  Fsm & abstractedMachine,
193  StateCreation & creator) {
194  std::vector<typename Fsm::State> abstractedStates;
195  return abstract(machine, abstractedMachine, bfBegin, bfEnd, abstractedStates,creator);
196  }
197 
198 // /**
199 // *
200 // * @param machine a StateMachine
201 // * @param abstractMachine the resulting abstracted StateMachine
202 // * @param bfBegin the start iterator of a BeliefState<Property,EventInfo> range
203 // * @param bfEnd the end iterator of a BeliefState<Property,EventInfo> range
204 // * @param stateManager StateInfoManger of the original machine
205 // * @param abstractedStateManager StateInfoManger of the abstracted machine
206 // * @param disjunction Definition of the way the minimal machine will hold the disjunction states from the
207 // * original machine.
208 // * @return true if the abstraction is a success. If not the state of the abstractedMachine
209 // * is undetermined.
210 // *
211 // * @pre machine should not be abstractedMachine
212 // * @p [bfBegin,bfEnd) represents a set of belief states bs1, bs2, ... bsn.
213 // * Each bsi is a set of State from the initial StateMachine 'machine'.
214 // * The abstracted machine will contain n states. State si will represent
215 // * the states of bsi. A transition t of event e between state si and sj exists
216 // * in the abstracted machine iff there exists a transition t of event e
217 // * between a state from bsi and a state from bsj in the original machine.
218 // * The state si of the abtracted machine is initial as soon as one of
219 // * the state of bsi is initial.
220 // *
221 // */
222 // template<typename Fsm,typename BeliefStateIterator>
223 // bool
224 // abstract(const StateMachine<StateProperty, EventId> & machine,
225 // StateMachine<StateProperty, EventId> & abstractedMachine,
226 // BeliefStateIterator bfBegin, BeliefStateIterator bfEnd,
227 // const StatePropertyManager<StateInfo, StateProperty> & stateManager,
228 // StatePropertyManager<StateInfo, StateProperty> & abstractedStateManager,
229 // const Disjunction & disjunction) {
230 // using Fsm = StateMachine<StateProperty, EventId>;
231 // std::vector<typename Fsm::State> abstractedStates;
232 // bool result = abstract(machine, abstractedMachine, bfBegin, bfEnd, abstractedStates);
233 //
234 // if (result) {
235 // size_t counter = 0;
236 // for (auto it = bfBegin; it != bfEnd; ++it) {
237 // auto begin = stateManager.statePropertyIdRangeStatePropertyIterator(machine.statePropertyIdRangeIterator(it->begin()));
238 // auto end = stateManager.statePropertyIdRangeStatePropertyIterator(machine.statePropertyIdRangeIterator(it->end()));
239 // abstractedMachine.setStatePropertyId(abstractedStates[counter],
240 // abstractedStateManager.statePropertyId(disjunction.operator()(begin, end)));
241 // ++counter;
242 // }
243 // }
244 // return result;
245 // }
246 
247  }
248  }
249 }
250 
251 
252 
253 #endif /* __DIADES__AUTOMATA__EXPERIMENTAL__ABSTRACT__HH */
254 
Diades::Graph::Edge Transition
Definition: Component.hh:46
FaultyEventStateMachine< CandidateId, EventInfoId > Fsm
Diades::Graph::GraphNodeSet::Iterator BeliefStateIterator
Definition: BeliefState.hh:40
AutFsm::State State
Definition: Run.cc:72
AutFsm::Transition Transition
Definition: Run.cc:73
Namespace of the Diades project.
Diades::Graph::Node State
Definition: BeliefState.hh:36