| 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 | #ifndef DATATYPECENTER_H |
| 21 | #define DATATYPECENTER_H |
| 22 | |
| 23 | #include "tDataType.h" |
| 24 | #include "dltree.h" |
| 25 | #include "tDataTypeBool.h" |
| 26 | |
| 27 | class DataTypeReasoner; |
| 28 | |
| 29 | class DataTypeCenter |
| 30 | { |
| 31 | public: // interface |
| 32 | /// vector of DATATYPEs |
| 33 | typedef std::vector<TDataType*> TypesVector; |
| 34 | |
| 35 | /// iterator (RW) to access types |
| 36 | typedef TypesVector::iterator iterator; |
| 37 | /// iterator (RO) to access types |
| 38 | typedef TypesVector::const_iterator const_iterator; |
| 39 | |
| 40 | protected: // members |
| 41 | /// vector of registered data types; initially contains unrestricted NUMBER and STRING |
| 42 | TypesVector Types; |
| 43 | |
| 44 | protected: // methods |
| 45 | /// register new data type |
| 46 | void RegisterDataType ( TDataType* p ) { Types.push_back(p); } |
| 47 | |
| 48 | // access to datatypes |
| 49 | |
| 50 | /// get type corresponding to Numbers |
| 51 | TDataType* getNumberDataType ( void ) const { return *begin(); } |
| 52 | /// get type corresponding to Strings |
| 53 | TDataType* getStringDataType ( void ) const { return *(begin()+1); } |
| 54 | /// get type corresponding to Real |
| 55 | TDataType* getRealDataType ( void ) const { return *(begin()+2); } |
| 56 | /// get type corresponding to Bool |
| 57 | TDataType* getBoolDataType ( void ) const { return *(begin()+3); } |
| 58 | /// get type corresponding to date-time |
| 59 | TDataType* getTimeDataType ( void ) const { return *(begin()+4); } |
| 60 | /// get type by name |
| 61 | TDataType* getTypeByName ( const std::string& name ) const; |
| 62 | |
| 63 | /// get type corresponding to given sample |
| 64 | TDataType* getTypeBySample ( const TDataEntry* sample ) const |
| 65 | { |
| 66 | const TDataEntry* type = sample->isBasicDataType() ? sample : sample->getType(); |
| 67 | |
| 68 | for ( const_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 69 | if ( type == (*p)->getType() ) |
| 70 | return *p; |
| 71 | |
| 72 | return nullptr; |
| 73 | } |
| 74 | |
| 75 | // DLTree wrapping interface |
| 76 | |
| 77 | /// get DLTree by a given TDE |
| 78 | static DLTree* wrap ( const TDataEntry* t ) { return createEntry ( DATAEXPR, const_cast<TDataEntry*>(t) ); } |
| 79 | /// get TDE by a given DLTree |
| 80 | static TDataEntry* unwrap ( const DLTree* t ) { return static_cast<TDataEntry*>(t->Element().getNE()); } |
| 81 | |
| 82 | public: // interface |
| 83 | // c'tor: create NUMBER and STRING datatypes |
| 84 | DataTypeCenter ( void ) |
| 85 | { |
| 86 | // register primitive DataTypes |
| 87 | RegisterDataType(new TDataType("number")); |
| 88 | RegisterDataType(new TDataType("string")); |
| 89 | RegisterDataType(new TDataType("real")); |
| 90 | RegisterDataType(new TDataTypeBool()); |
| 91 | RegisterDataType(new TDataType("time")); |
| 92 | // fresh DT that doesn't appear in KB. FIXME!! make it as TOP later on |
| 93 | RegisterDataType(new TDataType(" ")); |
| 94 | } |
| 95 | /// d'tor: delete all datatypes |
| 96 | ~DataTypeCenter(); |
| 97 | |
| 98 | // iterators |
| 99 | |
| 100 | /// begin (RW) |
| 101 | iterator begin ( void ) { return Types.begin(); } |
| 102 | /// end (RW) |
| 103 | iterator end ( void ) { return Types.end(); } |
| 104 | /// begin (RO) |
| 105 | const_iterator begin ( void ) const { return Types.begin(); } |
| 106 | /// end (RO) |
| 107 | const_iterator end ( void ) const { return Types.end(); } |
| 108 | |
| 109 | // DLTree interface |
| 110 | |
| 111 | /// get NUMBER DT that can be used in TBox |
| 112 | DLTree* getNumberType ( void ) const { return wrap(getNumberDataType()->getType()); } |
| 113 | /// get STRING DT that can be used in TBox |
| 114 | DLTree* getStringType ( void ) const { return wrap(getStringDataType()->getType()); } |
| 115 | /// get REAL DT that can be used in TBox |
| 116 | DLTree* getRealType ( void ) const { return wrap(getRealDataType()->getType()); } |
| 117 | /// get BOOL DT that can be used in TBox |
| 118 | DLTree* getBoolType ( void ) const { return wrap(getBoolDataType()->getType()); } |
| 119 | /// get TIME DT that can be used in TBox |
| 120 | DLTree* getTimeType ( void ) const { return wrap(getTimeDataType()->getType()); } |
| 121 | /// get fresh DT that can be used in TBox |
| 122 | DLTree* getFreshDataType ( void ) const { return wrap((*(begin()+5))->getType()); } |
| 123 | |
| 124 | /// return registered data value by given NAME of a Type, given by SAMPLE |
| 125 | DLTree* getDataValue ( const std::string& name, const DLTree* sample ) const |
| 126 | { |
| 127 | TDataType* type = isConst(sample) ? |
| 128 | getStringDataType() : // data top |
| 129 | getTypeBySample(unwrap(sample)); |
| 130 | |
| 131 | return wrap(type->get(name)); // may throw |
| 132 | } |
| 133 | /// get datatype by its name |
| 134 | DLTree* getDataType ( const std::string& name ) |
| 135 | { return wrap(getTypeByName(name)->getType()); } |
| 136 | /// define datatype by NAME as equal to given EXPR. FIXME!! stub for JNI for now |
| 137 | DLTree* getDataType ( const std::string&, DLTree* expr ) |
| 138 | { return expr; } |
| 139 | |
| 140 | /// facet for >=/>/</<= |
| 141 | DLTree* getIntervalFacetExpr ( DLTree* val, bool min, bool excl ) |
| 142 | { |
| 143 | /// get value as an DTE |
| 144 | TDataEntry* value = unwrap(val); |
| 145 | // create new (unnamed) expression |
| 146 | TDataEntry* ret = getTypeBySample(value)->getExpr(); |
| 147 | // apply appropriate facet to it |
| 148 | ret->getFacet()->update ( min, excl, value->getComp() ); |
| 149 | deleteTree(val); |
| 150 | return wrap(ret); |
| 151 | } |
| 152 | |
| 153 | // reasoning interface |
| 154 | |
| 155 | /// init DT reasoner |
| 156 | void initDataTypeReasoner ( DataTypeReasoner& DTReasoner ) const; |
| 157 | /// lock/unlock types for additional elements |
| 158 | void setLocked ( bool val ); |
| 159 | }; // DataTypeCenter |
| 160 | |
| 161 | #endif |