| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2006-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 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 | /// state of the role automaton |
| 32 | // TODO: think to use short |
| 33 | typedef unsigned int RAState; |
| 34 | |
| 35 | /// transition in the automaton for the role in RIQ-like languages |
| 36 | class RATransition |
| 37 | { |
| 38 | protected: // typedefs |
| 39 | /// set or roles that labels transition |
| 40 | typedef std::vector<const TRole*> TLabel; |
| 41 | |
| 42 | public: // typedefs |
| 43 | /// iterator over roles |
| 44 | typedef TLabel::const_iterator const_iterator; |
| 45 | |
| 46 | protected: // members |
| 47 | /// set of roles that may affect the transition |
| 48 | TLabel label; |
| 49 | /// final state of the transition |
| 50 | RAState state; |
| 51 | |
| 52 | public: // interface |
| 53 | /// create a transition to given state |
| 54 | explicit RATransition ( RAState st ) : state(st) {} |
| 55 | /// create a transition with a given label R to given state ST |
| 56 | RATransition ( RAState st, const TRole* R ) : state(st) { add(R); } |
| 57 | /// copy c'tor |
| 58 | RATransition ( const RATransition& ) = default; |
| 59 | /// move c'tor |
| 60 | RATransition ( RATransition&& ) = default; |
| 61 | /// assignment |
| 62 | RATransition& operator = ( const RATransition& ) = default; |
| 63 | /// move assignment |
| 64 | RATransition& operator = ( RATransition&& ) = default; |
| 65 | |
| 66 | /// check whether transition is applicable wrt role R |
| 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 | // update the transition |
| 73 | |
| 74 | /// add role R to transition's label |
| 75 | void add ( const TRole* R ) { label.push_back(R); } |
| 76 | /// add role R to transition's label |
| 77 | void addIfNew ( const TRole* R ) { if ( !applicable(R) ) add(R); } |
| 78 | /// add label of transition TRANS to transition's label |
| 79 | void add ( const RATransition& trans ) |
| 80 | { label.insert ( label.end(), trans.label.cbegin(), trans.label.cend() ); } |
| 81 | /// add label of transition TRANS to transition's label only if they are new |
| 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 | // query the transition |
| 88 | |
| 89 | /// get the 1st role in (multi-)transition |
| 90 | const_iterator begin ( void ) const { return label.begin(); } |
| 91 | /// get the last role in (multi-)transition |
| 92 | const_iterator end ( void ) const { return label.end(); } |
| 93 | |
| 94 | /// give a final point of the transition |
| 95 | RAState final ( void ) const { return state; } |
| 96 | /// check whether transition is empty |
| 97 | bool empty ( void ) const { return label.empty(); } |
| 98 | /// check whether transition is TopRole one |
| 99 | bool isTop ( void ) const; |
| 100 | /// print the transition starting from FROM |
| 101 | void Print ( std::ostream& o, RAState from ) const; |
| 102 | }; // RATransition |
| 103 | |
| 104 | /// class to represent transitions from a single state in an automaton |
| 105 | class RAStateTransitions |
| 106 | { |
| 107 | protected: // types |
| 108 | /// keep all the transitions |
| 109 | typedef std::vector<RATransition> RTBase; |
| 110 | /// RW iterators |
| 111 | typedef RTBase::iterator iterator; |
| 112 | |
| 113 | public: // type interface |
| 114 | /// RO iterators |
| 115 | typedef RTBase::const_iterator const_iterator; |
| 116 | |
| 117 | protected: // members |
| 118 | /// all transitions |
| 119 | RTBase Base; |
| 120 | /// set of all roles that can be applied by one of the transitions |
| 121 | TFastSet<unsigned int> ApplicableRoles; |
| 122 | /// state from which all the transition starts |
| 123 | RAState from; |
| 124 | /// check whether there is an empty transition going from this state |
| 125 | bool EmptyTransition = false; |
| 126 | /// true iff there is a top transition going from this state |
| 127 | bool TopTransition = false; |
| 128 | /// flag whether the role is data or not (valid only for simple automata) |
| 129 | bool DataRole; |
| 130 | |
| 131 | public: // interface |
| 132 | /// empty c'tor |
| 133 | RAStateTransitions()= default; |
| 134 | /// copy c'tor |
| 135 | RAStateTransitions ( const RAStateTransitions& ) = default; |
| 136 | /// move c'tor |
| 137 | RAStateTransitions ( RAStateTransitions&& ) = default; |
| 138 | /// assignment |
| 139 | RAStateTransitions& operator = ( const RAStateTransitions& ) = default; |
| 140 | /// move assignment |
| 141 | RAStateTransitions& operator = ( RAStateTransitions&& ) = default; |
| 142 | |
| 143 | /// set up state transitions: no more additions to the structure |
| 144 | void finalise ( RAState state, size_t nRoles, bool data ); |
| 145 | |
| 146 | /// add a transition from a given state |
| 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 | /// copy information from TRANS to existing transition between the same states. @return false if no such transition found |
| 156 | bool addToExisting ( const RATransition& trans ); |
| 157 | |
| 158 | /// @return true iff there are no transitions from this state |
| 159 | bool empty ( void ) const { return Base.empty(); } |
| 160 | /// @return true iff there is an empty transition from the state |
| 161 | bool hasEmptyTransition ( void ) const { return EmptyTransition; } |
| 162 | /// @return true iff there is a top-role transition from the state |
| 163 | bool hasTopTransition ( void ) const { return TopTransition; } |
| 164 | |
| 165 | // RO access |
| 166 | |
| 167 | /// RO begin |
| 168 | const_iterator begin ( void ) const { return Base.begin(); } |
| 169 | /// RO end |
| 170 | const_iterator end ( void ) const { return Base.end(); } |
| 171 | /// get FROM state |
| 172 | RAState getFrom ( void ) const { return from; } |
| 173 | /// check whether one of the transitions accept R; implementation is in tRole.h |
| 174 | bool recognise ( const TRole* R ) const; |
| 175 | /// @return true iff there is only one transition |
| 176 | bool isSingleton ( void ) const { return Base.size() == 1; } |
| 177 | /// @return final state of the 1st transition; used for singletons |
| 178 | RAState getTransitionEnd ( void ) const { return Base.front().final(); } |
| 179 | |
| 180 | /// print all the transitions starting from the state FROM |
| 181 | void Print ( std::ostream& o ) const |
| 182 | { |
| 183 | for ( auto& trans: Base ) |
| 184 | trans.Print ( o, from ); |
| 185 | } |
| 186 | }; // RAStateTransitions |
| 187 | |
| 188 | /// automaton for the role in RIQ-like languages |
| 189 | class RoleAutomaton |
| 190 | { |
| 191 | protected: // members |
| 192 | /// all transitions of the automaton, grouped by a starting state |
| 193 | std::vector<RAStateTransitions> Base; |
| 194 | /// maps original automata state into the new ones (used in copyRA) |
| 195 | std::vector<unsigned int> map; |
| 196 | /// initial state of the next automaton in chain |
| 197 | RAState iRA = 0; |
| 198 | /// flag whether automaton is input safe |
| 199 | bool ISafe = true; |
| 200 | /// flag whether automaton is output safe |
| 201 | bool OSafe = true; |
| 202 | /// flag for the automaton to be completed |
| 203 | bool Complete = false; |
| 204 | |
| 205 | protected: // methods |
| 206 | /// make sure that STATE exists in the automaton (update ton's size) |
| 207 | void ensureState ( RAState state ) |
| 208 | { |
| 209 | if ( state >= Base.size() ) |
| 210 | Base.resize(state+1); |
| 211 | } |
| 212 | |
| 213 | /// state that the automaton is i-unsafe |
| 214 | void setIUnsafe ( void ) { ISafe = false; } |
| 215 | /// state that the automaton is o-unsafe |
| 216 | void setOUnsafe ( void ) { OSafe = false; } |
| 217 | /// check whether transition between FROM and TO breaks safety |
| 218 | void checkTransition ( RAState from, RAState to ) |
| 219 | { |
| 220 | if ( from == final() ) |
| 221 | setOUnsafe(); |
| 222 | if ( to == initial() ) |
| 223 | setIUnsafe(); |
| 224 | } |
| 225 | |
| 226 | /// add TRANSition leading from a state FROM; all states are known to fit the ton |
| 227 | void addTransition ( RAState from, RATransition&& trans ) |
| 228 | { |
| 229 | checkTransition ( from, trans.final() ); |
| 230 | Base[from].add(std::move(trans)); |
| 231 | } |
| 232 | /// make the internal chain transition (between chainState and TO) |
| 233 | void nextChainTransition ( RAState to ) |
| 234 | { |
| 235 | addTransition ( iRA, RATransition(to) ); |
| 236 | iRA = to; |
| 237 | } |
| 238 | |
| 239 | /// add copy of the RA to given one; use internal MAP to renumber the states |
| 240 | void addCopy ( const RoleAutomaton& RA ); |
| 241 | /// init internal map according to RA size and final (FRA) states |
| 242 | void initMap ( size_t RASize, RAState fRA ); |
| 243 | |
| 244 | public: // interface |
| 245 | /// empty c'tor |
| 246 | RoleAutomaton() { ensureState(1); } |
| 247 | /// copy c'tor |
| 248 | RoleAutomaton ( const RoleAutomaton& ) = default; |
| 249 | /// move c'tor |
| 250 | RoleAutomaton ( RoleAutomaton&& ) = default; |
| 251 | /// assignment |
| 252 | RoleAutomaton& operator= ( const RoleAutomaton& ) = default; |
| 253 | /// move assignment |
| 254 | RoleAutomaton& operator= ( RoleAutomaton&& ) = default; |
| 255 | |
| 256 | // access to states |
| 257 | |
| 258 | /// get the initial state |
| 259 | RAState initial ( void ) const { return 0; } |
| 260 | /// get the final state |
| 261 | RAState final ( void ) const { return 1; } |
| 262 | /// create new state |
| 263 | RAState newState ( void ) |
| 264 | { |
| 265 | RAState ret = (RAState) Base.size(); |
| 266 | ensureState(ret); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | /// get access to the transitions starting from STATE |
| 271 | const RAStateTransitions& operator [] ( RAState state ) const { return Base[state]; } |
| 272 | /// set up all transitions passing number of roles |
| 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 | // automaton's construction |
| 280 | |
| 281 | /// add TRANSition leading from a given STATE; check whether all states are correct |
| 282 | void addTransitionSafe ( RAState state, RATransition&& trans ) |
| 283 | { |
| 284 | ensureState(state); |
| 285 | ensureState(trans.final()); |
| 286 | addTransition ( state, std::move(trans) ); |
| 287 | } |
| 288 | |
| 289 | // chain automaton creation |
| 290 | |
| 291 | /// make the beginning of the chain |
| 292 | void initChain ( RAState from ) { iRA = from; } |
| 293 | /// add an Automaton to the chain that would start from the iRA; OSAFE shows the safety of a previous automaton in a chain |
| 294 | bool addToChain ( const RoleAutomaton& RA, bool oSafe, RAState fRA ); |
| 295 | /// add an Automaton to the chain with a default final state |
| 296 | bool addToChain ( const RoleAutomaton& RA, bool oSafe ) { return addToChain ( RA, oSafe, (RAState) size()+1 ); } |
| 297 | |
| 298 | // i/o safety |
| 299 | |
| 300 | /// get the i-safe value |
| 301 | bool isISafe ( void ) const { return ISafe; } |
| 302 | /// get the o-safe value |
| 303 | bool isOSafe ( void ) const { return OSafe; } |
| 304 | |
| 305 | // automaton completeness |
| 306 | |
| 307 | /// mark an automaton as completed |
| 308 | void setCompleted ( void ) { Complete = true; } |
| 309 | /// check whether automaton is completed |
| 310 | bool isCompleted ( void ) const { return Complete; } |
| 311 | |
| 312 | // get some stats |
| 313 | |
| 314 | /// return number of distinct states |
| 315 | size_t size ( void ) const { return Base.size(); } |
| 316 | /// @return true iff the automaton is simple |
| 317 | bool isSimple ( void ) const |
| 318 | { |
| 319 | fpp_assert(isCompleted()); |
| 320 | return size() == 2 && ISafe && OSafe; |
| 321 | } |
| 322 | |
| 323 | // add single RA |
| 324 | |
| 325 | /// add RA from a subrole to given one |
| 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, /*oSafe=*/false, final() ); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /// print an automaton |
| 342 | void Print ( std::ostream& o ) const |
| 343 | { |
| 344 | for ( auto& StateTransitions: Base ) |
| 345 | StateTransitions.Print(o); |
| 346 | } |
| 347 | }; // RoleAutomaton |
| 348 | |
| 349 | #endif |
| 350 |