syn_ctrlpfx.cpp
Go to the documentation of this file.
1 /** @file syn_ctrlpfx.cpp Controllability prefix */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5  Copyright (C) 2025 Thomas Moor
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 
23 
24 #include "syn_ctrlpfx.h"
25 
26 namespace faudes {
27 
28 /*
29 *********************************************************************
30 
31 Implementation of operator on state sets: virtual base
32 
33 *********************************************************************
34 */
35 
36 // static member
38 
39 // get dummy domain
40 const StateSet& StateSetOperator::Domain(void) const {
41  static StateSet empty;
42  return empty;
43 }
44 
45 // API wrapper, multiple arguments
47  if(rArgs.Size()!=mArgCount) {
48  std::stringstream errstr;
49  errstr << "signature mismatch: expected arguments #" << mArgCount <<
50  " provided argumenst #" << rArgs.Size();
51  throw Exception("StateSetOperator::Evaluate", errstr.str(), 80);
52  }
53  DoEvaluate(rArgs,rRes);
54 }
55 
56 // API wrapper, single argument
57 void StateSetOperator::Evaluate(StateSet& rArg, StateSet& rRes) const {
58  if(mArgCount!=1) {
59  std::stringstream errstr;
60  errstr << "signature mismatch: expected arguments #" << mArgCount <<
61  " provided argumenst #1";
62  throw Exception("StateSetOperator::Evaluate", errstr.str(), 80);
63  }
64  StateSetVector args;
65  args.PushBack(&rArg);
66  DoEvaluate(args,rRes);
67 }
68 
69 // API wrapper, no arguments
71  if(mArgCount!=0) {
72  std::stringstream errstr;
73  errstr << "signature mismatch: expected arguments #" << mArgCount <<
74  " provided argumenst #0";
75  throw Exception("StateSetOperator::Evaluate", errstr.str(), 80);
76  }
77  static StateSetVector args;
78  DoEvaluate(args,rRes);
79 }
80 
81 // signature, i.e., the number of arguments */
83  return mArgCount;
84 };
85 
86 // signature, i.e., argument names (cosmetic) */
87 const std::string& StateSetOperator::ArgName(StateSetVector::Position pos) const {
88  static std::string range="?";
89  if(pos>= mArgNames.size()) return range;
90  return mArgNames.at(pos);
91 }
92 
93 // stats
94 std::string StateSetOperator::ArgStatistics(const StateSetVector& rArgs) const {
95  std::stringstream res;
96  for(StateSetVector::Position pos=0; pos< rArgs.Size(); ++pos) {
97  res << " " << ArgName(pos) << " #" << rArgs.At(pos).Size();
98  }
99  return res.str();
100 }
101 
102 // indentation for nested iterations
103 const std::string& StateSetOperator::Indent(void) const {
104  return mIndent;
105 }
106 
107 // indentation for nested iterations (fake const)
108 void StateSetOperator::Indent(const std::string& indent) const {
109  StateSetOperator* rwp = const_cast<StateSetOperator*>(this);
110  rwp->mIndent = indent;
111 }
112 
113 // logging
114 void StateSetOperator::LogMuNu(bool logon) {
115  mLogMuNu=logon;
116 }
117 
118 /*
119 *********************************************************************
120 
121 Implementation of operator on state sets: controllability prefix
122 
123 *********************************************************************
124 */
125 
126 // constructor
127 CtrlPfxOperator::CtrlPfxOperator(const vGenerator& rGenerator, const EventSet& rSigmaCtrl) :
129  rGen(rGenerator),
130  mSigmaCtrl(rSigmaCtrl),
131  rTransRel(rGen.TransRel()),
132  mRevTransRel(rGen.TransRel())
133 {
134  FD_DF("CtrlPfxOperator(): instantiated from " << mrGen.Name());
135  rGen.SWrite();
136  Name("cpx_op([Y,X])");
137  mArgNames= std::vector<std::string>{"Y","X"};
138  mArgCount=2;
139 };
140 
141 // domain
142 const StateSet& CtrlPfxOperator::Domain(void) const {
143  return rGen.States();
144 }
145 
146 // evaluation
148  FD_DF("CtrlPfxOperator::DoEvaluate(): " << Name());
149  // prepare result
150  rRes.Clear();
151  // have neat accessors
152  StateSet& Y=rArgs.At(0);
153  StateSet& X=rArgs.At(1);
154  //FD_DF("CtrlPfxOperator::DoEvaluate(): Y " << rGen.StateSetToString(Y));
155  //FD_DF("CtrlPfxOperator::DoEvaluate(): X " << rGen.StateSetToString(X));
156 
157  // actual implementation comes here, aka
158  // eval([Y,X]) =
159  // (pre_exisntial(X) union marked_states) intersectted with pre_universal(Y)
160 
161  /*
162  // variant 1: by the book
163  StateSet lhs;
164  lhs.Assign(mRevTransRel.PredecessorStates(X));
165  lhs.InsertSet(rGen.MarkedStates());
166  StateSet rhs;
167  StateSet Ycmp= rGen.States() - Y;
168  rhs.Assign(rGen.States());
169  rhs.EraseSet(mRevTransRel.PredecessorStates(Ycmp,mSigmaUc) );
170  rRes=lhs * rhs;
171  */
172 
173  // variant 2: perhaps gain some performance
175  rRes.InsertSet(rGen.MarkedStates());
176  StateSet::Iterator sit=rRes.Begin();
177  StateSet::Iterator sit_end=rRes.End();
178  while(sit!=sit_end){
180  TransSet::Iterator tit_end=rTransRel.End(*sit);
181  for(;tit!=tit_end;++tit){
182  if(mSigmaCtrl.Exists(tit->Ev)) continue;
183  if(Y.Exists(tit->X2)) continue;
184  break;
185  }
186  if(tit!=tit_end) rRes.Erase(sit++);
187  else++sit;
188  }
189 
190  //FD_DF("CtrlPfxOperator::DoEvaluate(): R " << rGen.StateSetToString(rRes));
191 };
192 
193 
194 /*
195 *********************************************************************
196 
197 Implementation of fixpoint iterations: mu
198 
199 *********************************************************************
200 */
201 
204  mrOp(rOp)
205 {
206  if(rOp.ArgCount()<1) {
207  std::stringstream errstr;
208  errstr << "operator \"" << rOp.Name() << "\" takes no arguments";
209  throw Exception("MuIteration", errstr.str(), 80);
210  }
211  Name("mu " + rOp.ArgName(rOp.ArgCount()-1) + " . " + rOp.Name());
212  mArgCount=rOp.ArgCount()-1;
213  for(StateSetVector::Position pos=0; pos<rOp.ArgCount(); ++pos)
214  mArgNames.push_back(rOp.ArgName(pos));
215  mIndent=mrOp.Indent();
216  mrOp.Indent(mIndent+" ");
217  FD_DF("MuIteration(): instantiated to evaluate " << Name());
218 };
219 
220 
221 // pass on indent to inner loops
222 void MuIteration::Indent(const std::string& indent) const {
223  StateSetOperator::Indent(indent);
224  mrOp.Indent(indent+" ");
225 }
226 
227 
228 // inherit domain
229 const StateSet& MuIteration::Domain(void) const {
230  return mrOp.Domain();
231 }
232 
233 // evaluation
235  // prepare progress message
236  std::string prog="MuIteration::DoEvaluate(): " + Indent() + Name() + ": " + ArgStatistics(rArgs);
237  if(mLogMuNu) {
238  FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
239  } else {
240  FD_DF(prog);
241  }
242  // prepare result
243  rRes.Clear();
244  // actual implementation comes here
245  StateSetVector xargs;
246  xargs.AssignByReference(rArgs);
247  xargs.PushBack(&rRes);
248  StateSet R;
249  while(true) {
250  Idx xsz=rRes.Size();
251  mrOp.Evaluate(xargs,R);
252  FD_DF("MuIteration::DoEvaluate(): " << Indent() << xsz << "# -> #" << R.Size());
253  rRes.Assign(R);
254  if(rRes.Size()==xsz) break;
255  FD_WPC(1,2,prog);
256  }
257  // say goodby
258  prog=prog + " -> " + mrOp.ArgName(mrOp.ArgCount()-1) + " #" + faudes::ToStringInteger(rRes.Size());
259  if(mLogMuNu) {
260  FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
261  } else {
262  FD_DF(prog);
263  }
264 };
265 
266 
267 
268 /*
269 *********************************************************************
270 
271 Implementation of fixpoint iterations: nu
272 
273 *********************************************************************
274 */
275 
278  mrOp(rOp)
279 {
280  if(rOp.ArgCount()<1) {
281  std::stringstream errstr;
282  errstr << "operator \"" << rOp.Name() << "\" takes no arguments";
283  throw Exception("MuIteration", errstr.str(), 80);
284  }
285  Name("nu " + rOp.ArgName(rOp.ArgCount()-1) + " . " + rOp.Name());
286  mArgCount=rOp.ArgCount()-1;
287  for(StateSetVector::Position pos=0; pos<rOp.ArgCount(); ++pos)
288  mArgNames.push_back(rOp.ArgName(pos));
289  mIndent=mrOp.Indent();
290  mrOp.Indent(mIndent+" ");
291  FD_DF("NuIteration(): instantiated to evaluate " << Name());
292 }
293 
294 // pass on indent to inner loops
295 void NuIteration::Indent(const std::string& indent) const {
296  StateSetOperator::Indent(indent);
297  mrOp.Indent(indent+" ");
298 }
299 
300 // inherit domain
301 const StateSet& NuIteration::Domain(void) const {
302  return mrOp.Domain();
303 }
304 
305 // evaluation
307  // prepare progress message
308  std::string prog="NuIteration::DoEvaluate(): " + Indent() + Name() + ": " + ArgStatistics(rArgs);
309  if(mLogMuNu) {
310  FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
311  } else {
312  FD_DF(prog);
313  }
314  // prepare result
315  rRes.Clear();
316  // actual implementation comes here
317  StateSetVector xargs;
318  xargs.AssignByReference(rArgs);
319  xargs.PushBack(&rRes);
320  rRes=Domain();
321  StateSet R;
322  while(true) {
323  Idx xsz=rRes.Size();
324  mrOp.Evaluate(xargs,R);
325  FD_DF("NuIteration::DoEvaluate(): " << Indent() << xsz << "# -> #" << R.Size());
326  rRes.Assign(R);
327  if(rRes.Size()==xsz) break;
328  FD_WPC(1,2,prog);
329  }
330  // say goodby
331  prog=prog + " -> " + mrOp.ArgName(mrOp.ArgCount()-1) + " #" + faudes::ToStringInteger(rRes.Size());
332  if(mLogMuNu) {
333  FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
334  } else {
335  FD_DF(prog);
336  }
337 };
338 
339 } // namespace faudes
340 
341 
#define FD_WPC(cntnow, contdone, message)
#define FAUDES_WRITE_CONSOLE(message)
#define FD_DF(message)
CtrlPfxOperator(const vGenerator &rGenerator, const EventSet &rSigmaCtrl)
virtual const StateSet & Domain(void) const
const TransSet & rTransRel
Definition: syn_ctrlpfx.h:205
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes) const
const vGenerator & rGen
Definition: syn_ctrlpfx.h:199
TransSetX2EvX1 mRevTransRel
Definition: syn_ctrlpfx.h:208
const std::string & Name(void) const
Definition: cfl_types.cpp:422
const StateSetOperator & mrOp
Definition: syn_ctrlpfx.h:277
virtual const std::string & Indent(void) const
virtual const StateSet & Domain(void) const
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes) const
MuIteration(const StateSetOperator &rOp)
bool Exists(const Idx &rIndex) const
virtual const std::string & Indent(void) const
NuIteration(const StateSetOperator &rOp)
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes) const
virtual const StateSet & Domain(void) const
const StateSetOperator & mrOp
Definition: syn_ctrlpfx.h:347
virtual const StateSet & Domain(void) const
Definition: syn_ctrlpfx.cpp:40
StateSetVector::Position ArgCount(void) const
Definition: syn_ctrlpfx.cpp:82
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes) const =0
virtual const std::string & Indent(void) const
std::string ArgStatistics(const StateSetVector &rArgs) const
Definition: syn_ctrlpfx.cpp:94
std::vector< std::string > mArgNames
Definition: syn_ctrlpfx.h:126
StateSetVector::Position mArgCount
Definition: syn_ctrlpfx.h:123
const std::string & ArgName(StateSetVector::Position pos) const
Definition: syn_ctrlpfx.cpp:87
static void LogMuNu(bool on)
void Evaluate(StateSetVector &rArgs, StateSet &rRes) const
Definition: syn_ctrlpfx.cpp:46
std::vector< int >::size_type Position
virtual const T & At(const Position &pos) const
StateSet PredecessorStates(Idx x2) const
Iterator Begin(void) const
Iterator End(void) const
TBaseSet< Transition, TransSort::X1EvX2 >::Iterator Iterator
Definition: cfl_transset.h:273
virtual Type & Assign(const Type &rSrc)
Definition: cfl_types.cpp:82
void SWrite(TokenWriter &rTw) const
Definition: cfl_types.cpp:262
void AssignByReference(vBaseVector &rSourceVector)
virtual void PushBack(const Type &rElem)
Idx Size(void) const
const StateSet & MarkedStates(void) const
const StateSet & States(void) const
bool Exists(const T &rElem) const
Definition: cfl_baseset.h:2180
virtual void Clear(void)
Definition: cfl_baseset.h:1962
Iterator End(void) const
Definition: cfl_baseset.h:1956
virtual void InsertSet(const TBaseSet &rOtherSet)
Definition: cfl_baseset.h:2052
Iterator Begin(void) const
Definition: cfl_baseset.h:1951
virtual bool Erase(const T &rElem)
Definition: cfl_baseset.h:2084
Idx Size(void) const
Definition: cfl_baseset.h:1782
uint32_t Idx
std::string ToStringInteger(Int number)
Definition: cfl_utils.cpp:43

libFAUDES 2.33h --- 2025.06.18 --- c++ api documentaion by doxygen