| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-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 <algorithm> |
| 21 | #include <iostream> |
| 22 | |
| 23 | #include "dlVertex.h" |
| 24 | #include "dlDag.h" |
| 25 | #include "tDataEntry.h" |
| 26 | |
| 27 | // adds a child to 'AND' vertex. |
| 28 | // if finds a contrary pair of concepts -- returns TRUE |
| 29 | // else return false |
| 30 | bool DLVertex :: addChild ( BipolarPointer bp ) |
| 31 | { |
| 32 | // if adds to broken vertex -- do nothing; |
| 33 | if ( Op == dtBad ) |
| 34 | return true; |
| 35 | |
| 36 | // if adds TOP -- nothing to do |
| 37 | if ( bp == bpTOP ) |
| 38 | return false; |
| 39 | |
| 40 | // if adding BOTTOM -- return clash (empty vertex) immediately |
| 41 | // this can happen in case of nested simplifications; see bNested1 |
| 42 | if ( bp == bpBOTTOM ) |
| 43 | return true; |
| 44 | |
| 45 | // find appropriate place to insert |
| 46 | auto v = getValue(bp); |
| 47 | |
| 48 | auto it = Child.begin(), end = Child.end(); |
| 49 | it = std::find_if_not ( it, end, [=] (BipolarPointer p) { return getValue(p) < v; } ); |
| 50 | |
| 51 | if ( it == end ) // finish |
| 52 | { |
| 53 | Child.push_back(bp); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | // we finds a place with |Child[i]| >= v |
| 58 | if ( *it == bp ) // concept already exists |
| 59 | return false; |
| 60 | else if ( *it == inverse(bp) ) |
| 61 | return true; |
| 62 | |
| 63 | // bp is new, add it to the set |
| 64 | Child.insert ( it, bp ); |
| 65 | |
| 66 | // FIXME: add some simplification (about AR.C1, AR.c2 etc) |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Sort given entry in the order defined by flags in a DAG. |
| 71 | // the overall sorted entry structure looks like |
| 72 | // fffA..M..Zlll if sortAscend set, and |
| 73 | // fffZ..M..Alll if sortAscend cleared. |
| 74 | // here 's' means "always first" entries, like neg-primconcepts, |
| 75 | // and 'l' means "always last" entries, like looped concepts |
| 76 | void DLVertex :: sortEntry ( const DLDag& dag ) |
| 77 | { |
| 78 | // safety check |
| 79 | if ( Type() != dtAnd ) |
| 80 | return; |
| 81 | |
| 82 | // auto comp = [&dag] (const BipolarPointer l, const BipolarPointer r) { return dag.less(l,r); }; |
| 83 | // std::sort ( Child.begin(), Child.end(), comp ); |
| 84 | BipolarPointer x; // value of moved element |
| 85 | size_t size = Child.size(); |
| 86 | |
| 87 | for ( size_t i = 1; i < size; ++i ) |
| 88 | { |
| 89 | x = Child[i]; |
| 90 | |
| 91 | // put x to the place s.t. SxL, where S <= x < L wrt dag.less() |
| 92 | size_t j = i; |
| 93 | for ( ; j > 0 && dag.less ( x, Child[j-1] ); --j ) |
| 94 | Child[j] = Child[j-1]; |
| 95 | |
| 96 | // insert new element on it's place |
| 97 | Child[j] = x; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | // sortirovka vstavkami s minimumom (na budushhee) |
| 103 | template <typename T> |
| 104 | inline void insertSortGuarded(T a[], long size) { |
| 105 | T x; |
| 106 | long i, j; |
| 107 | T backup = a[0]; // ????????? ?????? ?????? ??????? |
| 108 | |
| 109 | setMin(a[0]); // ???????? ?? ??????????? |
| 110 | |
| 111 | // ????????????? ?????? |
| 112 | for ( i=1; i < size; i++) { |
| 113 | x = a[i]; |
| 114 | |
| 115 | for ( j=i-1; a[j] > x; j--) |
| 116 | a[j+1] = a[j]; |
| 117 | |
| 118 | a[j+1] = x; |
| 119 | } |
| 120 | |
| 121 | // ???????? backup ?? ?????????? ????? |
| 122 | for ( j=1; j<size && a[j] < backup; j++) |
| 123 | a[j-1] = a[j]; |
| 124 | |
| 125 | // ??????? ???????? |
| 126 | a[j-1] = backup; |
| 127 | } |
| 128 | */ |
| 129 | |
| 130 | const char* DLVertex :: getTagName ( void ) const |
| 131 | { |
| 132 | switch (Op) |
| 133 | { |
| 134 | case dtTop: return "*TOP*"; |
| 135 | case dtBad: return "bad-tag"; |
| 136 | case dtNConcept:return "concept"; |
| 137 | case dtPConcept:return "primconcept"; |
| 138 | case dtPSingleton:return "prim-singleton"; |
| 139 | case dtNSingleton:return "singleton"; |
| 140 | case dtDataType: return "data-type"; |
| 141 | case dtDataValue: return "data-value"; |
| 142 | case dtDataExpr: return "data-expr"; |
| 143 | case dtAnd: return "and"; |
| 144 | case dtForall: return "all"; |
| 145 | case dtLE: return "at-most"; |
| 146 | case dtIrr: return "irreflexive"; |
| 147 | case dtProj: return "projection"; |
| 148 | case dtNN: return "NN-stopper"; |
| 149 | case dtChoose: return "choose"; |
| 150 | default: return "UNKNOWN"; |
| 151 | }; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | void |
| 156 | DLVertex :: Print ( std::ostream& o ) const |
| 157 | { |
| 158 | o << "[d(" << getDepth(true) << "/" << getDepth(false) |
| 159 | << "),s(" << getSize(true) << "/" << getSize(false) |
| 160 | << "),b(" << getBranch(true) << "/" << getBranch(false) |
| 161 | << "),g(" << getGener(true) << "/" << getGener(false) |
| 162 | << "),f(" << getFreq(true) << "/" << getFreq(false) << ")] "; |
| 163 | o << getTagName(); |
| 164 | |
| 165 | switch ( Type() ) |
| 166 | { |
| 167 | case dtAnd: // nothing to do (except for printing operands) |
| 168 | break; |
| 169 | |
| 170 | case dtTop: // nothing to do |
| 171 | case dtNN: |
| 172 | return; |
| 173 | |
| 174 | case dtDataExpr: |
| 175 | o << ' ' << *static_cast<const TDataEntry*>(getConcept())->getFacet(); |
| 176 | return; |
| 177 | |
| 178 | case dtDataValue: // named entry -- just like concept names |
| 179 | case dtDataType: |
| 180 | |
| 181 | case dtPConcept: |
| 182 | case dtNConcept: |
| 183 | case dtPSingleton: |
| 184 | case dtNSingleton: |
| 185 | o << '(' << getConcept()->getName() << ") " << (isNNameTag(Type()) ? "=" : "[=") << ' ' << getC(); |
| 186 | return; |
| 187 | |
| 188 | case dtLE: |
| 189 | o << ' ' << getNumberLE() << ' ' << getRole()->getName() << ' ' << getC(); |
| 190 | return; |
| 191 | |
| 192 | case dtForall: |
| 193 | o << ' ' << getRole()->getName() << '{' << getState() << '}' << ' ' << getC(); |
| 194 | return; |
| 195 | |
| 196 | case dtIrr: |
| 197 | o << ' ' << getRole()->getName(); |
| 198 | return; |
| 199 | |
| 200 | case dtProj: |
| 201 | o << ' ' << getRole()->getName() << ", " << getC() << " => " << getProjRole()->getName(); |
| 202 | return; |
| 203 | |
| 204 | case dtChoose: |
| 205 | o << ' ' << getC(); |
| 206 | return; |
| 207 | |
| 208 | default: |
| 209 | std::cerr << "Error printing vertex of type " << getTagName() << "(" << Type() << ")"; |
| 210 | fpp_unreachable(); |
| 211 | } |
| 212 | |
| 213 | // print operands of the concept constructor |
| 214 | for ( const_iterator q = begin(); q != end(); ++q ) |
| 215 | o << ' ' << *q; |
| 216 | } |