| 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" |
| 21 | #include "tRole.h" |
| 22 | |
| 23 | /// check whether transition is TopRole one |
| 24 | bool |
| 25 | RATransition :: isTop ( void ) const |
| 26 | { |
| 27 | return label.size() == 1 && unlikely(label.front()->isTop()); |
| 28 | } |
| 29 | |
| 30 | /// set up state transitions: no more additions to the structure |
| 31 | void |
| 32 | RAStateTransitions :: finalise ( RAState state, size_t nRoles, bool data ) |
| 33 | { |
| 34 | from = state; |
| 35 | DataRole = data; |
| 36 | ApplicableRoles.ensureMaxSetSize(nRoles); |
| 37 | // fills the set of recognisable roles |
| 38 | for ( const auto& trans: Base ) |
| 39 | std::for_each ( trans.begin(), trans.end(), [&] (const TRole* R) { ApplicableRoles.add(R->getIndex()); } ); |
| 40 | } |
| 41 | |
| 42 | /// add information from TRANS to existing transition between the same states. @return false if no such transition found |
| 43 | bool |
| 44 | RAStateTransitions :: addToExisting ( const RATransition& trans ) |
| 45 | { |
| 46 | RAState to = trans.final(); |
| 47 | bool tEmpty = trans.empty(); |
| 48 | for ( auto& transition: Base ) |
| 49 | if ( transition.final() == to && transition.empty() == tEmpty ) |
| 50 | { // found existing transition |
| 51 | transition.addIfNew(trans); |
| 52 | return true; |
| 53 | } |
| 54 | // no transition from->to found |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | RATransition::Print ( std::ostream& o, RAState from ) const |
| 60 | { |
| 61 | o << "\n" << from << " -- "; |
| 62 | if ( empty() ) |
| 63 | o << "e"; |
| 64 | else |
| 65 | { |
| 66 | const_iterator p = label.begin(); |
| 67 | o << '"' << (*p)->getName() << '"'; |
| 68 | |
| 69 | for ( ++p; p != label.end(); ++p ) |
| 70 | o << ",\"" << (*p)->getName() << '"'; |
| 71 | } |
| 72 | o << " -> " << final(); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | RoleAutomaton :: addCopy ( const RoleAutomaton& RA ) |
| 77 | { |
| 78 | for ( RAState i = 0; i < RA.size(); ++i ) |
| 79 | { |
| 80 | RAState from = map[i]; |
| 81 | RAStateTransitions& RST = Base[from]; |
| 82 | const RAStateTransitions& RSTOrig = RA[i]; |
| 83 | |
| 84 | if ( RSTOrig.empty() ) |
| 85 | continue; |
| 86 | |
| 87 | for ( auto& origTrans: RSTOrig ) |
| 88 | { |
| 89 | // find a map from original transitions' end to the updated transitions' end |
| 90 | RAState to = origTrans.final(); |
| 91 | RAState final = map[to]; |
| 92 | checkTransition ( from, final ); |
| 93 | RATransition trans(final); |
| 94 | trans.add(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.addToExisting(trans) ) |
| 99 | RST.add(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 | RoleAutomaton :: initMap ( size_t RASize, RAState fRA ) |
| 107 | { |
| 108 | map.resize(RASize); |
| 109 | // new state in the automaton |
| 110 | RAState newState = (RAState) size()-1; |
| 111 | |
| 112 | // fill initial state; it is always known in the automaton |
| 113 | map[0] = iRA; |
| 114 | |
| 115 | // fills the final state; if it is not known -- adjust newState |
| 116 | if ( fRA >= size() ) |
| 117 | { |
| 118 | fRA = (RAState) size(); // make sure we don't create an extra unused state |
| 119 | ++newState; |
| 120 | } |
| 121 | map[1] = fRA; |
| 122 | |
| 123 | // check transitions as it may turns out to be a single transition |
| 124 | checkTransition ( iRA, fRA ); |
| 125 | |
| 126 | // set new initial state |
| 127 | iRA = fRA; |
| 128 | |
| 129 | // fills the rest of map |
| 130 | for ( unsigned int i = 2; i < RASize; ++i ) |
| 131 | map[i] = ++newState; |
| 132 | |
| 133 | // reserve enough space for the new automaton |
| 134 | ensureState(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 | RoleAutomaton :: addToChain ( const RoleAutomaton& RA, bool oSafe, RAState fRA ) |
| 140 | { |
| 141 | fpp_assert(!isCompleted()); |
| 142 | bool needFinalTrans = ( fRA < size() && !RA.isOSafe() ); |
| 143 | // we can skip transition if chaining automaton are i- and o-safe |
| 144 | if ( !oSafe && !RA.isISafe() ) |
| 145 | nextChainTransition(newState()); |
| 146 | // check whether we need an output transition |
| 147 | initMap ( RA.size(), needFinalTrans ? (RAState) size() : fRA ); |
| 148 | addCopy(RA); |
| 149 | if ( needFinalTrans ) |
| 150 | nextChainTransition(fRA); |
| 151 | |
| 152 | return RA.isOSafe(); |
| 153 | } |