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
26namespace faudes {
27
28/*
29*********************************************************************
30
31Implementation of operator on state sets: virtual base
32
33*********************************************************************
34*/
35
36// get dummy domain
38 static StateSet empty;
39 return empty;
40}
41
42// API wrapper, multiple arguments
44 if(rArgs.Size()!=mArgCount) {
45 std::stringstream errstr;
46 errstr << "signature mismatch: expected arguments #" << mArgCount <<
47 " provided argumenst #" << rArgs.Size();
48 throw Exception("StateSetOperator::Evaluate", errstr.str(), 80);
49 }
50 const_cast<StateSetOperator*>(this)->DoEvaluate(rArgs,rRes);
51}
52
53// API wrapper, single argument
55 if(mArgCount!=1) {
56 std::stringstream errstr;
57 errstr << "signature mismatch: expected arguments #" << mArgCount <<
58 " provided argumenst #1";
59 throw Exception("StateSetOperator::Evaluate", errstr.str(), 80);
60 }
61 StateSetVector args;
62 args.PushBack(&rArg);
63 const_cast<StateSetOperator*>(this)->DoEvaluate(args,rRes);
64}
65
66// API wrapper, no arguments
68 if(mArgCount!=0) {
69 std::stringstream errstr;
70 errstr << "signature mismatch: expected arguments #" << mArgCount <<
71 " provided argumenst #0";
72 throw Exception("StateSetOperator::Evaluate", errstr.str(), 80);
73 }
74 static StateSetVector args;
75 const_cast<StateSetOperator*>(this)->DoEvaluate(args,rRes);
76}
77
78// signature, i.e., the number of arguments */
82
83// signature, i.e., argument names (cosmetic) */
85 static std::string range="?";
86 if(pos>= mArgNames.size()) return range;
87 return mArgNames.at(pos);
88}
89
90// stats
91std::string StateSetOperator::ArgStatistics(const StateSetVector& rArgs) const {
92 std::stringstream res;
93 for(StateSetVector::Position pos=0; pos< rArgs.Size(); ++pos) {
94 res << " " << ArgName(pos) << " #" << rArgs.At(pos).Size();
95 }
96 return res.str();
97}
98
99// indentation for nested iterations
100const std::string& StateSetOperator::Indent(void) const {
101 return mIndent;
102}
103
104// indentation for nested iterations (fake const)
105void StateSetOperator::Indent(const std::string& indent) const {
106 StateSetOperator* rwp = const_cast<StateSetOperator*>(this);
107 rwp->mIndent = indent;
108}
109
110
111/*
112*********************************************************************
113
114Implementation of operator on state sets: controllability prefix
115
116*********************************************************************
117*/
118
119// constructor
120CtrlPfxOperator::CtrlPfxOperator(const vGenerator& rGenerator, const EventSet& rSigmaCtrl) :
122 rGen(rGenerator),
123 mSigmaCtrl(rSigmaCtrl),
124 rTransRel(rGen.TransRel()),
125 mRevTransRel(rGen.TransRel())
126{
127 FD_DF("CtrlPfxOperator(): instantiated from " << mrGen.Name());
128 rGen.SWrite();
129 Name("cpx_op([Y,X])");
130 mArgNames= std::vector<std::string>{"Y","X"};
131 mArgCount=2;
132};
133
134// domain
136 return rGen.States();
137}
138
139// evaluation
141 FD_DF("CtrlPfxOperator::DoEvaluate(): " << Name());
142 // prepare result
143 rRes.Clear();
144 // have neat accessors
145 StateSet& Y=rArgs.At(0);
146 StateSet& X=rArgs.At(1);
147 //FD_DF("CtrlPfxOperator::DoEvaluate(): Y " << rGen.StateSetToString(Y));
148 //FD_DF("CtrlPfxOperator::DoEvaluate(): X " << rGen.StateSetToString(X));
149
150 // actual implementation comes here, aka
151 // eval([Y,X]) =
152 // (pre_exisntial(X) union marked_states) intersectted with pre_universal(Y)
153
154 /*
155 // variant 1: by the book
156 StateSet lhs;
157 lhs.Assign(mRevTransRel.PredecessorStates(X));
158 lhs.InsertSet(rGen.MarkedStates());
159 StateSet rhs;
160 StateSet Ycmp= rGen.States() - Y;
161 rhs.Assign(rGen.States());
162 rhs.EraseSet(mRevTransRel.PredecessorStates(Ycmp,mSigmaUc) );
163 rRes=lhs * rhs;
164 */
165
166 // variant 2: perhaps gain some performance
169 StateSet::Iterator sit=rRes.Begin();
170 StateSet::Iterator sit_end=rRes.End();
171 while(sit!=sit_end){
173 TransSet::Iterator tit_end=rTransRel.End(*sit);
174 for(;tit!=tit_end;++tit){
175 if(mSigmaCtrl.Exists(tit->Ev)) continue;
176 if(Y.Exists(tit->X2)) continue;
177 break;
178 }
179 if(tit!=tit_end) rRes.Erase(sit++);
180 else++sit;
181 }
182
183 //FD_DF("CtrlPfxOperator::DoEvaluate(): R " << rGen.StateSetToString(rRes));
184};
185
186
187/*
188*********************************************************************
189
190Implementation of fixpoint iterations: mu
191
192*********************************************************************
193*/
194
197 mrOp(rOp)
198{
199 if(rOp.ArgCount()<1) {
200 std::stringstream errstr;
201 errstr << "operator \"" << rOp.Name() << "\" takes no arguments";
202 throw Exception("MuIteration", errstr.str(), 80);
203 }
204 Name("mu " + rOp.ArgName(rOp.ArgCount()-1) + " . " + rOp.Name());
205 mArgCount=rOp.ArgCount()-1;
206 for(StateSetVector::Position pos=0; pos<rOp.ArgCount(); ++pos)
207 mArgNames.push_back(rOp.ArgName(pos));
209 mrOp.Indent(mIndent+" ");
210 FD_DF("MuIteration(): instantiated to evaluate " << Name());
211};
212
213
214// pass on indent to inner loops
215void MuIteration::Indent(const std::string& indent) const {
217 mrOp.Indent(indent+" ");
218}
219
220
221// inherit domain
222const StateSet& MuIteration::Domain(void) const {
223 return mrOp.Domain();
224}
225
226// evaluation
228 // prepare progress message
229 std::string prog;
230 if(Verbosity()>=10) {
231 prog="MuIteration::DoEvaluate(): " + Indent() + Name() + ": " + ArgStatistics(rArgs);
232 FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
233 }
234 // prepare result
235 rRes.Clear();
236 // actual implementation comes here
237 StateSetVector xargs;
238 xargs.AssignByReference(rArgs);
239 xargs.PushBack(&rRes);
240 StateSet R;
241 while(true) {
242 Idx xsz=rRes.Size();
243 mrOp.Evaluate(xargs,R);
244 FD_DF("MuIteration::DoEvaluate(): " << Indent() << xsz << "# -> #" << R.Size());
245 rRes.Assign(R);
246 if(rRes.Size()==xsz) break;
247 FD_WPC(1,2,prog);
248 }
249 // say goodby
250 if(Verbosity()>=10) {
251 prog=prog + " -> " + mrOp.ArgName(mrOp.ArgCount()-1) + " #" + faudes::ToStringInteger(rRes.Size());
252 FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
253 }
254};
255
256
257// evaluation incl rank (there are more clever wayz to implement this ...)
258void MuIteration::Rank(StateSetVector& rArgs, std::map<Idx,int>& rRMap) const {
259 // consistency check
260 if(rArgs.Size()!=mArgCount) {
261 std::stringstream errstr;
262 errstr << "signature mismatch: expected arguments #" << mArgCount <<
263 " provided argumenst #" << rArgs.Size();
264 throw Exception("StateSetOperator::Evaluate", errstr.str(), 80);
265 }
266 // prepare progress message
267 std::string prog;
268 if(Verbosity()>=10) {
269 prog="MuIteration::Rank(): " + Indent() + Name() + ": " + ArgStatistics(rArgs);
270 FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
271 }
272 // prepare result
273 rRMap.clear();
274 StateSet res;
275 // actual implementation comes here
276 StateSetVector xargs;
277 xargs.AssignByReference(rArgs);
278 xargs.PushBack(&res);
279 StateSet R;
280 StateSet N;
281 StateSet::Iterator sit;
282 int rank=0;
283 while(true) {
284 mrOp.Evaluate(xargs,R);
285 N=R-res;
286 FD_DF("MuIteration::DoEvaluate(): " << Indent() << res.size() << "# -> #" << R.Size());
287 res.Assign(R);
288 if(N.Empty()) break;
289 for(sit=N.Begin();sit!=N.End();++sit)
290 rRMap[*sit]=rank;
291 ++rank;
292 FD_WPC(1,2,prog);
293 }
294 // say goodby
295 if(Verbosity()>=10) {
296 prog=prog + " -> " + mrOp.ArgName(mrOp.ArgCount()-1) + " #" + faudes::ToStringInteger(res.Size());
297 FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
298 }
299};
300
301// API wrapper, single argument
302void MuIteration::Rank(StateSet& rArg, std::map<Idx,int>& rRMap) const {
303 StateSetVector args;
304 args.PushBack(&rArg);
305 Rank(args,rRMap);
306}
307
308// API wrapper, no arguments
309void MuIteration::Rank(std::map<Idx,int>& rRMap) const {
310 static StateSetVector args;
311 Rank(args,rRMap);
312}
313
314
315/*
316*********************************************************************
317
318Implementation of fixpoint iterations: nu
319
320*********************************************************************
321*/
322
325 mrOp(rOp)
326{
327 if(rOp.ArgCount()<1) {
328 std::stringstream errstr;
329 errstr << "operator \"" << rOp.Name() << "\" takes no arguments";
330 throw Exception("MuIteration", errstr.str(), 80);
331 }
332 Name("nu " + rOp.ArgName(rOp.ArgCount()-1) + " . " + rOp.Name());
333 mArgCount=rOp.ArgCount()-1;
334 for(StateSetVector::Position pos=0; pos<rOp.ArgCount(); ++pos)
335 mArgNames.push_back(rOp.ArgName(pos));
337 mrOp.Indent(mIndent+" ");
338 FD_DF("NuIteration(): instantiated to evaluate " << Name());
339}
340
341// pass on indent to inner loops
342void NuIteration::Indent(const std::string& indent) const {
344 mrOp.Indent(indent+" ");
345}
346
347// inherit domain
348const StateSet& NuIteration::Domain(void) const {
349 return mrOp.Domain();
350}
351
352// evaluation
354 // prepare progress message
355 std::string prog;
356 if(Verbosity()>=10) {
357 prog="NuIteration::DoEvaluate(): " + Indent() + Name() + ": " + ArgStatistics(rArgs);
358 FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
359 }
360 // prepare result
361 rRes.Clear();
362 // actual implementation comes here
363 StateSetVector xargs;
364 xargs.AssignByReference(rArgs);
365 xargs.PushBack(&rRes);
366 rRes=Domain();
367 StateSet R;
368 while(true) {
369 Idx xsz=rRes.Size();
370 mrOp.Evaluate(xargs,R);
371 FD_DF("NuIteration::DoEvaluate(): " << Indent() << xsz << "# -> #" << R.Size());
372 rRes.Assign(R);
373 if(rRes.Size()==xsz) break;
374 FD_WPC(1,2,prog);
375 }
376 // say goodby
377 if(Verbosity()>=10) {
378 prog=prog + " -> " + mrOp.ArgName(mrOp.ArgCount()-1) + " #" + faudes::ToStringInteger(rRes.Size());
379 FAUDES_WRITE_CONSOLE("FAUDES_MUNU: " << prog);
380 }
381};
382
383} // namespace faudes
384
385
#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
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes)
const vGenerator & rGen
TransSetX2EvX1 mRevTransRel
const std::string & Name(void) const
const StateSetOperator & mrOp
virtual const std::string & Indent(void) const
void Rank(StateSetVector &rArgs, std::map< Idx, int > &rRMap) const
virtual const StateSet & Domain(void) const
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes)
MuIteration(const StateSetOperator &rOp)
bool Exists(const Idx &rIndex) const
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes)
virtual const std::string & Indent(void) const
NuIteration(const StateSetOperator &rOp)
virtual const StateSet & Domain(void) const
const StateSetOperator & mrOp
virtual const StateSet & Domain(void) const
StateSetVector::Position ArgCount(void) const
virtual const std::string & Indent(void) const
std::string ArgStatistics(const StateSetVector &rArgs) const
std::vector< std::string > mArgNames
virtual void DoEvaluate(StateSetVector &rArgs, StateSet &rRes)=0
StateSetVector::Position mArgCount
const std::string & ArgName(StateSetVector::Position pos) const
void Evaluate(StateSetVector &rArgs, StateSet &rRes) const
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
virtual Type & Assign(const Type &rSrc)
Definition cfl_types.cpp:82
void SWrite(TokenWriter &rTw) const
void AssignByReference(vBaseVector &rSourceVector)
virtual void PushBack(const Type &rElem)
const StateSet & MarkedStates(void) const
const StateSet & States(void) const
bool Empty(void) const
bool Exists(const T &rElem) const
virtual void Clear(void)
Iterator End(void) const
virtual void InsertSet(const TBaseSet &rOtherSet)
Iterator Begin(void) const
virtual bool Erase(const T &rElem)
Idx Size(void) const
uint32_t Idx
int Verbosity(void)
std::string ToStringInteger(Int number)
Definition cfl_utils.cpp:43

libFAUDES 2.33k --- 2025.09.16 --- c++ api documentaion by doxygen