| 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 | #include "RAutomaton.h"cxx . . $ `<file>/Kernel/RAutomaton.h`/ |
| 21 | #include "tRole.h"cxx . . $ `<file>/Kernel/tRole.h`/ |
| 22 | |
| 23 | /// check whether transition is TopRole one |
| 24 | bool |
| 25 | RATransitioncxx . . $ RATransition# :: isTopcxx . . $ RATransition#isTop(50ce9a9e25b4a850). ( void ) const |
| 26 | { |
| 27 | return labelcxx . . $ RATransition#label..sizecxx . . $ std/vector#size(fe4d2d262d4164a8).() == 1 && unlikelycxx . . $ `Kernel/globaldef.h:29:10`!(label.front()->isTop()); |
| 28 | } |
| 29 | |
| 30 | /// set up state transitions: no more additions to the structure |
| 31 | void |
| 32 | RAStateTransitionscxx . . $ RAStateTransitions# :: finalisecxx . . $ RAStateTransitions#finalise(552a8a0739473b9a). ( RAStatecxx . . $ RAState# state, size_t nRoles, bool data ) |
| 33 | { |
| 34 | fromcxx . . $ RAStateTransitions#from. = state; |
| 35 | DataRolecxx . . $ RAStateTransitions#DataRole. = data; |
| 36 | ApplicableRolescxx . . $ RAStateTransitions#ApplicableRoles..ensureMaxSetSizecxx . . $ TFastSet#ensureMaxSetSize(2c44cd906ae4e829).(nRoles); |
| 37 | // fills the set of recognisable roles |
| 38 | for ( const auto& trans: Basecxx . . $ RAStateTransitions#Base. ) |
| 39 | stdcxx . . $ std/::for_eachcxx . . $ std/for_each(88304b602929f6f4). ( trans.begincxx . . $ RATransition#begin(506035cb53656567).(), trans.endcxx . . $ RATransition#end(506035cb53656567).(), [&] (const TRolecxx . . $ TRole#* R) { ApplicableRolescxx . . $ RAStateTransitions#ApplicableRoles..addcxx . . $ TFastSet#add(77d9f17553126ecb).(R->getIndexcxx . . $ TRole#getIndex(ddd8539a2fcdd4ce).()); } ); |
| 40 | } |
| 41 | |
| 42 | /// add information from TRANS to existing transition between the same states. @return false if no such transition found |
| 43 | bool |
| 44 | RAStateTransitionscxx . . $ RAStateTransitions# :: addToExistingcxx . . $ RAStateTransitions#addToExisting(97732fae2d1abfaf). ( const RATransitioncxx . . $ RATransition#& trans ) |
| 45 | { |
| 46 | RAStatecxx . . $ RAState# to = trans.finalcxx . . $ RATransition#final(ddd8539a2fcdd4ce).(); |
| 47 | bool tEmpty = trans.emptycxx . . $ RATransition#empty(50ce9a9e25b4a850).(); |
| 48 | for ( auto& transition: Basecxx . . $ RAStateTransitions#Base. ) |
| 49 | if ( transition.finalcxx . . $ RATransition#final(ddd8539a2fcdd4ce).() == to && transition.emptycxx . . $ RATransition#empty(50ce9a9e25b4a850).() == tEmpty ) |
| 50 | { // found existing transition |
| 51 | transition.addIfNewcxx . . $ RATransition#addIfNew(49cc212b42b37343).(trans); |
| 52 | return true; |
| 53 | } |
| 54 | // no transition from->to found |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | RATransitioncxx . . $ RATransition#::Printcxx . . $ RATransition#Print(88043dd2772042aa). ( stdcxx . . $ std/::ostreamcxx . . $ std/ostream#& o, RAStatecxx . . $ RAState# from ) const |
| 60 | { |
| 61 | o <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "\n" <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). from <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " -- "; |
| 62 | if ( emptycxx . . $ RATransition#empty(50ce9a9e25b4a850).() ) |
| 63 | o <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). "e"; |
| 64 | else |
| 65 | { |
| 66 | const_iteratorcxx . . $ RATransition#const_iterator# p = labelcxx . . $ RATransition#label..begincxx . . $ std/vector#begin(359ca5030082c9f).(); |
| 67 | o <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '"' <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->getNamecxx . . $ TNamedEntry#getName(a9f41ea0e82d88cf).() <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '"'; |
| 68 | |
| 69 | for ( ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p; p !=cxx . . $ __gnu_cxx/`operator!=`(ef3c5749062e825c). labelcxx . . $ RATransition#label..endcxx . . $ std/vector#end(359ca5030082c9f).(); ++cxx . . $ __gnu_cxx/__normal_iterator#operator++(93f2e9c3bc4efc49).p ) |
| 70 | o <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). ",\"" <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). (*cxx . . $ __gnu_cxx/__normal_iterator#`operator*`(c076846f881f3e7e).p)->getNamecxx . . $ TNamedEntry#getName(a9f41ea0e82d88cf).() <<cxx . . $ std/`operator<<`(dd93bb5737bfac24). '"'; |
| 71 | } |
| 72 | o <<cxx . . $ std/`operator<<`(efaf8c13fc75f3c7). " -> " <<cxx . . $ std/basic_ostream#`operator<<`(b4c055c363850233). finalcxx . . $ RATransition#final(ddd8539a2fcdd4ce).(); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | RoleAutomatoncxx . . $ RoleAutomaton# :: addCopycxx . . $ RoleAutomaton#addCopy(12494a229a69a5c3). ( const RoleAutomatoncxx . . $ RoleAutomaton#& RA ) |
| 77 | { |
| 78 | for ( RAStatecxx . . $ RAState# i = 0; i < RA.sizecxx . . $ RoleAutomaton#size(fe4d2d262d4164a8).(); ++i ) |
| 79 | { |
| 80 | RAStatecxx . . $ RAState# from = mapcxx . . $ RoleAutomaton#map.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).i]; |
| 81 | RAStateTransitionscxx . . $ RAStateTransitions#& RST = Basecxx . . $ RoleAutomaton#Base.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).from]; |
| 82 | const RAStateTransitionscxx . . $ RAStateTransitions#& RSTOrig = RA[cxx . . $ RoleAutomaton#`operator[]`(49d1c2a99a57610b).i]; |
| 83 | |
| 84 | if ( RSTOrig.emptycxx . . $ RAStateTransitions#empty(50ce9a9e25b4a850).() ) |
| 85 | continue; |
| 86 | |
| 87 | for ( auto& origTrans: RSTOrig ) |
| 88 | { |
| 89 | // find a map from original transitions' end to the updated transitions' end |
| 90 | RAStatecxx . . $ RAState# to = origTrans.finalcxx . . $ RATransition#final(ddd8539a2fcdd4ce).(); |
| 91 | RAStatecxx . . $ RAState# final = mapcxx . . $ RoleAutomaton#map.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).to]; |
| 92 | checkTransitioncxx . . $ RoleAutomaton#checkTransition(b4d55b1286ff73e8). ( from, final ); |
| 93 | RATransitioncxx . . $ RATransition# transcxx . . $ RATransition#RATransition(cb2f890c2fdad230).(final); |
| 94 | trans.addcxx . . $ RATransition#add(49cc212b42b37343).(origTrans); |
| 95 | |
| 96 | // try to merge transitions going to the original final state |
| 97 | // we will move the TRANS in the ADD case, so the temporary copy |
| 98 | if ( to != 1 || !RST.addToExistingcxx . . $ RAStateTransitions#addToExisting(97732fae2d1abfaf).(trans) ) |
| 99 | RST.addcxx . . $ RAStateTransitions#add(265ed24da56d4f50).(RATransitioncxx . . $ RATransition#(trans)); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// init internal map according to RA size, with new initial state from chainState and final (FRA) states |
| 105 | void |
| 106 | RoleAutomatoncxx . . $ RoleAutomaton# :: initMapcxx . . $ RoleAutomaton#initMap(34e6b58f48c1be0a). ( size_t RASize, RAStatecxx . . $ RAState# fRA ) |
| 107 | { |
| 108 | mapcxx . . $ RoleAutomaton#map..resizecxx . . $ std/vector#resize(2c44cd906ae4e829).(RASize); |
| 109 | // new state in the automaton |
| 110 | RAStatecxx . . $ RAState# newState = (RAStatecxx . . $ RAState#) sizecxx . . $ RoleAutomaton#size(fe4d2d262d4164a8).()-1; |
| 111 | |
| 112 | // fill initial state; it is always known in the automaton |
| 113 | mapcxx . . $ RoleAutomaton#map.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).0] = iRAcxx . . $ RoleAutomaton#iRA.; |
| 114 | |
| 115 | // fills the final state; if it is not known -- adjust newState |
| 116 | if ( fRA >= sizecxx . . $ RoleAutomaton#size(fe4d2d262d4164a8).() ) |
| 117 | { |
| 118 | fRA = (RAStatecxx . . $ RAState#) sizecxx . . $ RoleAutomaton#size(fe4d2d262d4164a8).(); // make sure we don't create an extra unused state |
| 119 | ++newState; |
| 120 | } |
| 121 | mapcxx . . $ RoleAutomaton#map.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).1] = fRA; |
| 122 | |
| 123 | // check transitions as it may turns out to be a single transition |
| 124 | checkTransitioncxx . . $ RoleAutomaton#checkTransition(b4d55b1286ff73e8). ( iRAcxx . . $ RoleAutomaton#iRA., fRA ); |
| 125 | |
| 126 | // set new initial state |
| 127 | iRAcxx . . $ RoleAutomaton#iRA. = fRA; |
| 128 | |
| 129 | // fills the rest of map |
| 130 | for ( unsigned int i = 2; i < RASize; ++i ) |
| 131 | mapcxx . . $ RoleAutomaton#map.[cxx . . $ std/vector#`operator[]`(428bb168810b9e52).i] = ++newState; |
| 132 | |
| 133 | // reserve enough space for the new automaton |
| 134 | ensureStatecxx . . $ RoleAutomaton#ensureState(cb2f890c2fdad230).(newState); |
| 135 | } |
| 136 | |
| 137 | /// add an Automaton to the chain that would start from the iRA; OSAFE shows the safety of a previous automaton in a chain |
| 138 | bool |
| 139 | RoleAutomatoncxx . . $ RoleAutomaton# :: addToChaincxx . . $ RoleAutomaton#addToChain(43b07b70767d9338). ( const RoleAutomatoncxx . . $ RoleAutomaton#& RA, bool oSafe, RAStatecxx . . $ RAState# fRA ) |
| 140 | { |
| 141 | fpp_assertcxx . . $ `Kernel/fpp_assert.h:46:10`!(!isCompleted()); |
| 142 | bool needFinalTrans = ( fRA < sizecxx . . $ RoleAutomaton#size(fe4d2d262d4164a8).() && !RA.isOSafecxx . . $ RoleAutomaton#isOSafe(50ce9a9e25b4a850).() ); |
| 143 | // we can skip transition if chaining automaton are i- and o-safe |
| 144 | if ( !oSafe && !RA.isISafecxx . . $ RoleAutomaton#isISafe(50ce9a9e25b4a850).() ) |
| 145 | nextChainTransitioncxx . . $ RoleAutomaton#nextChainTransition(cb2f890c2fdad230).(newStatecxx . . $ RoleAutomaton#newState(db52760e79bf9054).()); |
| 146 | // check whether we need an output transition |
| 147 | initMapcxx . . $ RoleAutomaton#initMap(34e6b58f48c1be0a). ( RA.sizecxx . . $ RoleAutomaton#size(fe4d2d262d4164a8).(), needFinalTrans ? (RAStatecxx . . $ RAState#) sizecxx . . $ RoleAutomaton#size(fe4d2d262d4164a8).() : fRA ); |
| 148 | addCopycxx . . $ RoleAutomaton#addCopy(12494a229a69a5c3).(RA); |
| 149 | if ( needFinalTrans ) |
| 150 | nextChainTransitioncxx . . $ RoleAutomaton#nextChainTransition(cb2f890c2fdad230).(fRA); |
| 151 | |
| 152 | return RA.isOSafecxx . . $ RoleAutomaton#isOSafe(50ce9a9e25b4a850).(); |
| 153 | } |