cfl_localgen.cpp
Go to the documentation of this file.
1 /** @file cfl_localgen.cpp Helper functions for projected generators */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2006 Bernd Opitz
6  Exclusive copyright is granted to Klaus Schmidt
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
21 
22 #include "cfl_localgen.h"
23 
24 namespace faudes {
25 
26 // LowExitStates(rHighAlph, rEntryStatesMap, rLowRevTransRel, highState)
27 StateSet LowExitStates(const Generator& rLowGen, const EventSet& rHighAlph,
28  const std::map<Idx,StateSet>& rEntryStatesMap, const TransSetX2EvX1& rLowRevTransRel,
29  Idx highState) {
30  // prepare result:
31  StateSet lo_exitstates;
32  // algorithm:
33  LowExitStates(rHighAlph, rEntryStatesMap, rLowRevTransRel, highState, lo_exitstates);
34  // return result:
35  return lo_exitstates;
36 }
37 
38 
39 // LowExitStates(rHighAlph, rEntryStatesMap, rLowRevTransRel, highState, rLowExitStates)
40 void LowExitStates(const EventSet& rHighAlph, const std::map<Idx,StateSet>& rEntryStatesMap,
41  const TransSetX2EvX1& rLowRevTransRel, Idx highState, StateSet& rLowExitStates) {
42  FD_DF("LowExitStates: computing low level exit states for high level"
43  << " state " << rLowExitStates.Str(highState));
44  // helpers:
45  std::map<Idx,StateSet>::const_iterator esmap_it;
46  StateSet::Iterator lit;
48  TransSetX2EvX1::Iterator rtit_end;
49  // algorithm:
50  esmap_it = rEntryStatesMap.find(highState);
51 #ifdef FAUDES_CHECKED
52  if (esmap_it == rEntryStatesMap.end()) {
53  std::stringstream errstr;
54  errstr << "Hi level state " << rHighAlph.Str(highState)
55  << " not found in entry states map";
56  throw Exception("LowExitStates", errstr.str(), 502);
57  }
58 #endif
59  // find predecessor states of low_level entry states
60  for (lit = esmap_it->second.Begin(); lit != esmap_it->second.End(); ++lit) {
61  FD_DF("LowExitStates: current low level entry state "
62  << rLowExitStates.Str(*lit));
63  rtit = rLowRevTransRel.BeginByX2(*lit);
64  rtit_end = rLowRevTransRel.EndByX2(*lit);
65  for (; rtit != rtit_end; ++rtit) {
66  if (rHighAlph.Exists(rtit->Ev)) {
67  FD_DF("LowExitStates: found low level exit state "
68  << rLowExitStates.Str(rtit->X1));
69  rLowExitStates.Insert(rtit->X1);
70  }
71  }
72  }
73 }
74 
75 // ReachableEvents(lo, rHighAlph, lowState)
76 EventSet ReachableEvents(const Generator& rLowGen, const EventSet& rHighAlph,
77  Idx lowState) {
78  // prepare result:
79  EventSet reachable_events = rLowGen.NewEventSet();
80  // algorithm:
81  ReachableEvents(rLowGen, rHighAlph, lowState, reachable_events);
82  // return result:
83  return reachable_events;
84 }
85 
86 
87 // ReachableEvents(lo, rHighAlph, lowState, rReachableEvents)
88 void ReachableEvents(const Generator& rLowGen, const EventSet& rHighAlph,
89  Idx lowState, EventSet& rReachableEvents) {
90  // helpers:
91  // iterators
93  TransSet::Iterator tit_end;
94  // todo list
95  std::stack<Idx> todo;
96  // done set
97  StateSet done;
98 
99  // prepare result:
100  rReachableEvents.Clear();
101 
102  // algorithm:
103  todo.push(lowState);
104  done.Insert(lowState);
105  while (! todo.empty()) {
106  const Idx current = todo.top();
107  todo.pop();
108  tit = rLowGen.TransRelBegin(current);
109  tit_end = rLowGen.TransRelEnd(current);
110  for (; tit != tit_end; ++tit) {
111  if (rHighAlph.Exists(tit->Ev)) {
112  rReachableEvents.Insert(tit->Ev);
113  // if high level event
114  if (rReachableEvents.Size() == rHighAlph.Size()) {
115  return;
116  }
117  }
118  // if low level event and not already in done set
119  else if (! done.Exists(tit->X2)) {
120  todo.push(tit->X2);
121  done.Insert(tit->X2);
122  }
123  }
124  }
125 }
126 
127 
128 // LocalCoaccessibleReach(rRevTransRel, rHighAlph, lowState, rCoaccessibleReach)
129 void LocalCoaccessibleReach(const TransSetX2EvX1& rRevTransRel,
130  const EventSet& rHighAlph, Idx lowState, StateSet& rCoaccessibleReach) {
131  // helpers:
132  // iterators
134  TransSetX2EvX1::Iterator tit_end;
135  // todo list
136  std::stack<Idx> todo;
137 
138  // algorithm:
139  todo.push(lowState);
140  rCoaccessibleReach.Insert(lowState);
141  while (! todo.empty()) {
142  const Idx current = todo.top();
143  todo.pop();
144  tit = rRevTransRel.BeginByX2(current);
145  tit_end = rRevTransRel.EndByX2(current);
146  for (; tit != tit_end; ++tit) {
147  // if high level event skip transition
148  if (rHighAlph.Exists(tit->Ev)) {
149  continue;
150  }
151  // if predecessor notin rCoaccessibleReach set
152  else if (! rCoaccessibleReach.Exists(tit->X1)) {
153  todo.push(tit->X1);
154  rCoaccessibleReach.Insert(tit->X1);
155  }
156  }
157  }
158 }
159 
160 // LocalAccessibleReach(rLowGen, rHighAlph, lowState, rAccessibleReach
161 void LocalAccessibleReach(const Generator& rLowGen, const EventSet& rHighAlph,
162  Idx lowState, StateSet& rAccessibleReach) {
163  // helpers:
164  // iterators
165  TransSet::Iterator tit;
166  TransSet::Iterator tit_end;
167  // todo list
168  std::stack<Idx> todo;
169 
170  // algorithm:
171  todo.push(lowState);
172  rAccessibleReach.Insert(lowState);
173  while (! todo.empty()) {
174  const Idx current = todo.top();
175  todo.pop();
176  tit = rLowGen.TransRelBegin(current);
177  tit_end = rLowGen.TransRelEnd(current);
178  for (; tit != tit_end; ++tit) {
179  // if high level event skip transition
180  if (rHighAlph.Exists(tit->Ev)) {
181  continue;
182  }
183  // if successor not in rAccessibleReach set
184  if (! rAccessibleReach.Exists(tit->X2)) {
185  todo.push(tit->X2);
186  rAccessibleReach.Insert(tit->X2);
187  }
188  }
189  }
190 }
191 
192 } // namespace faudes
#define FD_DF(message)
Debug: optional report on user functions.
Helper functions for projected generators.
Faudes exception class.
Set of indices.
Definition: cfl_indexset.h:78
std::string Str(const Idx &rIndex) const
Return pretty printable index.
Definition: cfl_indexset.h:185
Idx Insert(void)
Insert new index to set.
Set of indices with symbolic names.
Definition: cfl_nameset.h:69
bool Exists(const Idx &rIndex) const
Test existence of index.
std::string Str(const Idx &rIndex) const
Return pretty printable symbolic name for index.
bool Insert(const Idx &rIndex)
Add an element by index.
Iterator class for high-level api to TBaseSet.
Definition: cfl_baseset.h:387
Set of Transitions.
Definition: cfl_transset.h:242
Iterator BeginByX2(Idx x2) const
Iterator to first Transition specified by successor state x2.
Iterator EndByX2(Idx x2) const
Iterator to first Transition after specified successor state x2.
TBaseSet< Transition, TransSort::X1EvX2 >::Iterator Iterator
Iterator on transition.
Definition: cfl_transset.h:269
Base class of all FAUDES generators.
TransSet::Iterator TransRelBegin(void) const
Iterator to Begin() of transition relation.
EventSet NewEventSet(void) const
Create EventSet with generator's EventSymbolTable (on stack).
TransSet::Iterator TransRelEnd(void) const
Iterator to End() of transition relation.
bool Exists(const T &rElem) const
Test existence of element.
Definition: cfl_baseset.h:2115
virtual void Clear(void)
Clear all set.
Definition: cfl_baseset.h:1902
Idx Size(void) const
Get Size of TBaseSet.
Definition: cfl_baseset.h:1819
libFAUDES resides within the namespace faudes.
uint32_t Idx
Type definition for index type (allways 32bit)
EventSet ReachableEvents(const Generator &rLowGen, const EventSet &rHighAlph, Idx lowState)
ReachableEvents return-copy function:
StateSet LowExitStates(const Generator &rLowGen, const EventSet &rHighAlph, const std::map< Idx, StateSet > &rEntryStatesMap, const TransSetX2EvX1 &rLowRevTransRel, Idx highState)
LowExitStates return-copy function:
void LocalCoaccessibleReach(const TransSetX2EvX1 &rRevTransRel, const EventSet &rHighAlph, Idx lowState, StateSet &rCoaccessibleReach)
Compute the coaccessible reach for a local automaton.
void LocalAccessibleReach(const Generator &rLowGen, const EventSet &rHighAlph, Idx lowState, StateSet &rAccessibleReach)
Compute the accessible reach for a local automaton.

libFAUDES 2.32b --- 2024.03.01 --- c++ api documentaion by doxygen