| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef RAUTOMATON_H |
| 21 |
#define RAUTOMATON_H |
| 22 |
|
| 23 |
#include <vector> |
| 24 |
#include <iosfwd> |
| 25 |
|
| 26 |
#include "fpp_assert.h" |
| 27 |
#include "tFastSet.h" |
| 28 |
|
| 29 |
class TRole; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
typedef unsigned int RAState; |
| 34 |
|
| 35 |
|
| 36 |
class RATransition |
| 37 |
{ |
| 38 |
protected: |
| 39 |
|
| 40 |
typedef std::vector<const TRole*> TLabel; |
| 41 |
|
| 42 |
public: |
| 43 |
|
| 44 |
typedef TLabel::const_iterator const_iterator; |
| 45 |
|
| 46 |
protected: |
| 47 |
|
| 48 |
TLabel label; |
| 49 |
|
| 50 |
RAState state; |
| 51 |
|
| 52 |
public: |
| 53 |
|
| 54 |
explicit RATransition ( RAState st ) : state(st) {} |
| 55 |
|
| 56 |
RATransition ( RAState st, const TRole* R ) : state(st) { add(R); } |
| 57 |
|
| 58 |
RATransition ( const RATransition& ) = default; |
| 59 |
|
| 60 |
RATransition ( RATransition&& ) = default; |
| 61 |
|
| 62 |
RATransition& operator = ( const RATransition& ) = default; |
| 63 |
|
| 64 |
RATransition& operator = ( RATransition&& ) = default; |
| 65 |
|
| 66 |
|
| 67 |
bool applicable ( const TRole* R ) const |
| 68 |
{ |
| 69 |
return std::any_of ( label.cbegin(), label.cend(), [=] (const TRole* S) { return S == R; } ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
void add ( const TRole* R ) { label.push_back(R); } |
| 76 |
|
| 77 |
void addIfNew ( const TRole* R ) { if ( !applicable(R) ) add(R); } |
| 78 |
|
| 79 |
void add ( const RATransition& trans ) |
| 80 |
{ label.insert ( label.end(), trans.label.cbegin(), trans.label.cend() ); } |
| 81 |
|
| 82 |
void addIfNew ( const RATransition& trans ) |
| 83 |
{ |
| 84 |
std::for_each ( trans.label.cbegin(), trans.label.cend(), [=] (const TRole* R) { addIfNew(R); } ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
const_iterator begin ( void ) const { return label.begin(); } |
| 91 |
|
| 92 |
const_iterator end ( void ) const { return label.end(); } |
| 93 |
|
| 94 |
|
| 95 |
RAState final ( void ) const { return state; } |
| 96 |
|
| 97 |
bool empty ( void ) const { return label.empty(); } |
| 98 |
|
| 99 |
bool isTop ( void ) const; |
| 100 |
|
| 101 |
void Print ( std::ostream& o, RAState from ) const; |
| 102 |
}; |
| 103 |
|
| 104 |
|
| 105 |
class RAStateTransitions |
| 106 |
{ |
| 107 |
protected: |
| 108 |
|
| 109 |
typedef std::vector<RATransition> RTBase; |
| 110 |
|
| 111 |
typedef RTBase::iterator iterator; |
| 112 |
|
| 113 |
public: |
| 114 |
|
| 115 |
typedef RTBase::const_iterator const_iterator; |
| 116 |
|
| 117 |
protected: |
| 118 |
|
| 119 |
RTBase Base; |
| 120 |
|
| 121 |
TFastSet<unsigned int> ApplicableRoles; |
| 122 |
|
| 123 |
RAState from; |
| 124 |
|
| 125 |
bool EmptyTransition = false; |
| 126 |
|
| 127 |
bool TopTransition = false; |
| 128 |
|
| 129 |
bool DataRole; |
| 130 |
|
| 131 |
public: |
| 132 |
|
| 133 |
RAStateTransitions()= default; |
| 134 |
|
| 135 |
RAStateTransitions ( const RAStateTransitions& ) = default; |
| 136 |
|
| 137 |
RAStateTransitions ( RAStateTransitions&& ) = default; |
| 138 |
|
| 139 |
RAStateTransitions& operator = ( const RAStateTransitions& ) = default; |
| 140 |
|
| 141 |
RAStateTransitions& operator = ( RAStateTransitions&& ) = default; |
| 142 |
|
| 143 |
|
| 144 |
void finalise ( RAState state, size_t nRoles, bool data ); |
| 145 |
|
| 146 |
|
| 147 |
void add ( RATransition&& trans ) |
| 148 |
{ |
| 149 |
if ( trans.empty() ) |
| 150 |
EmptyTransition = true; |
| 151 |
if ( trans.isTop() ) |
| 152 |
TopTransition = true; |
| 153 |
Base.push_back(std::move(trans)); |
| 154 |
} |
| 155 |
|
| 156 |
bool addToExisting ( const RATransition& trans ); |
| 157 |
|
| 158 |
|
| 159 |
bool empty ( void ) const { return Base.empty(); } |
| 160 |
|
| 161 |
bool hasEmptyTransition ( void ) const { return EmptyTransition; } |
| 162 |
|
| 163 |
bool hasTopTransition ( void ) const { return TopTransition; } |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
const_iterator begin ( void ) const { return Base.begin(); } |
| 169 |
|
| 170 |
const_iterator end ( void ) const { return Base.end(); } |
| 171 |
|
| 172 |
RAState getFrom ( void ) const { return from; } |
| 173 |
|
| 174 |
bool recognise ( const TRole* R ) const; |
| 175 |
|
| 176 |
bool isSingleton ( void ) const { return Base.size() == 1; } |
| 177 |
|
| 178 |
RAState getTransitionEnd ( void ) const { return Base.front().final(); } |
| 179 |
|
| 180 |
|
| 181 |
void Print ( std::ostream& o ) const |
| 182 |
{ |
| 183 |
for ( auto& trans: Base ) |
| 184 |
trans.Print ( o, from ); |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
class RoleAutomaton |
| 190 |
{ |
| 191 |
protected: |
| 192 |
|
| 193 |
std::vector<RAStateTransitions> Base; |
| 194 |
|
| 195 |
std::vector<unsigned int> map; |
| 196 |
|
| 197 |
RAState iRA = 0; |
| 198 |
|
| 199 |
bool ISafe = true; |
| 200 |
|
| 201 |
bool OSafe = true; |
| 202 |
|
| 203 |
bool Complete = false; |
| 204 |
|
| 205 |
protected: |
| 206 |
|
| 207 |
void ensureState ( RAState state ) |
| 208 |
{ |
| 209 |
if ( state >= Base.size() ) |
| 210 |
Base.resize(state+1); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
void setIUnsafe ( void ) { ISafe = false; } |
| 215 |
|
| 216 |
void setOUnsafe ( void ) { OSafe = false; } |
| 217 |
|
| 218 |
void checkTransition ( RAState from, RAState to ) |
| 219 |
{ |
| 220 |
if ( from == final() ) |
| 221 |
setOUnsafe(); |
| 222 |
if ( to == initial() ) |
| 223 |
setIUnsafe(); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
void addTransition ( RAState from, RATransition&& trans ) |
| 228 |
{ |
| 229 |
checkTransition ( from, trans.final() ); |
| 230 |
Base[from].add(std::move(trans)); |
| 231 |
} |
| 232 |
|
| 233 |
void nextChainTransition ( RAState to ) |
| 234 |
{ |
| 235 |
addTransition ( iRA, RATransition(to) ); |
| 236 |
iRA = to; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
void addCopy ( const RoleAutomaton& RA ); |
| 241 |
|
| 242 |
void initMap ( size_t RASize, RAState fRA ); |
| 243 |
|
| 244 |
public: |
| 245 |
|
| 246 |
RoleAutomaton() { ensureState(1); } |
| 247 |
|
| 248 |
RoleAutomaton ( const RoleAutomaton& ) = default; |
| 249 |
|
| 250 |
RoleAutomaton ( RoleAutomaton&& ) = default; |
| 251 |
|
| 252 |
RoleAutomaton& operator= ( const RoleAutomaton& ) = default; |
| 253 |
|
| 254 |
RoleAutomaton& operator= ( RoleAutomaton&& ) = default; |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
RAState initial ( void ) const { return 0; } |
| 260 |
|
| 261 |
RAState final ( void ) const { return 1; } |
| 262 |
|
| 263 |
RAState newState ( void ) |
| 264 |
{ |
| 265 |
RAState ret = (RAState) Base.size(); |
| 266 |
ensureState(ret); |
| 267 |
return ret; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
const RAStateTransitions& operator [] ( RAState state ) const { return Base[state]; } |
| 272 |
|
| 273 |
void setup ( size_t nRoles, bool data ) |
| 274 |
{ |
| 275 |
for ( RAState i = 0; i < Base.size(); ++i ) |
| 276 |
Base[i].finalise ( i, nRoles, data ); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
void addTransitionSafe ( RAState state, RATransition&& trans ) |
| 283 |
{ |
| 284 |
ensureState(state); |
| 285 |
ensureState(trans.final()); |
| 286 |
addTransition ( state, std::move(trans) ); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
void initChain ( RAState from ) { iRA = from; } |
| 293 |
|
| 294 |
bool addToChain ( const RoleAutomaton& RA, bool oSafe, RAState fRA ); |
| 295 |
|
| 296 |
bool addToChain ( const RoleAutomaton& RA, bool oSafe ) { return addToChain ( RA, oSafe, (RAState) size()+1 ); } |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
bool isISafe ( void ) const { return ISafe; } |
| 302 |
|
| 303 |
bool isOSafe ( void ) const { return OSafe; } |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
void setCompleted ( void ) { Complete = true; } |
| 309 |
|
| 310 |
bool isCompleted ( void ) const { return Complete; } |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
size_t size ( void ) const { return Base.size(); } |
| 316 |
|
| 317 |
bool isSimple ( void ) const |
| 318 |
{ |
| 319 |
fpp_assert(isCompleted()); |
| 320 |
return size() == 2 && ISafe && OSafe; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
void addRA ( const RoleAutomaton& RA ) |
| 327 |
{ |
| 328 |
fpp_assert(!isCompleted()); |
| 329 |
if ( RA.isSimple() ) |
| 330 |
{ |
| 331 |
bool ok = Base[initial()].addToExisting(*RA[initial()].begin()); |
| 332 |
fpp_assert(ok); |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
initChain(initial()); |
| 337 |
addToChain ( RA, false, final() ); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
void Print ( std::ostream& o ) const |
| 343 |
{ |
| 344 |
for ( auto& StateTransitions: Base ) |
| 345 |
StateTransitions.Print(o); |
| 346 |
} |
| 347 |
}; |
| 348 |
|
| 349 |
#endif |
| 350 |
|