1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2005-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef TDATAENTRY_H
#define TDATAENTRY_H
#include <cstdlib>
#include "tNamedEntry.h"
#include "BiPointer.h"
#include "tLabeller.h"
#include "DataTypeComparator.h"
// if min() and max() are defined, then they conflicts with a TDI members
#undef min
#undef max
/// class for representing general data restriction
class TDataInterval
{
public: // members
/// left border of the interval
ComparableDT min;
/// right border of the interval
ComparableDT max;
/// type of the left border
bool minExcl;
/// type of the right border
bool maxExcl;
public: // interface
/// empty c'tor
TDataInterval ( void ) {}
/// copy c'tor
TDataInterval ( const TDataInterval& copy )
: min(copy.min)
, max(copy.max)
, minExcl(copy.minExcl)
, maxExcl(copy.maxExcl)
{}
/// assignment
TDataInterval& operator = ( const TDataInterval& copy )
{
min = copy.min;
max = copy.max;
minExcl = copy.minExcl;
maxExcl = copy.maxExcl;
return *this;
}
/// empty d'tor
~TDataInterval ( void ) {}
/// clear an interval
void clear ( void ) { min = max = ComparableDT(); }
/// check if min value range have been set
bool hasMin ( void ) const { return min.inited(); }
/// check if max value range have been set
bool hasMax ( void ) const { return max.inited(); }
/// no constraints
bool empty ( void ) const { return !hasMin() && !hasMax(); }
/// closed interval
bool closed ( void ) const { return hasMin() && hasMax(); }
/// update MIN border of an interval with VALUE wrt EXCL
bool updateMin ( bool excl, const ComparableDT& value )
{
if ( hasMin() ) // another min value: check if we need update
{
// constraint is >= or >
if ( min > value ) // was: {7,}; now: {5,}: no update needed
return false;
if ( min == value && // was: (5,}; now: [5,}: no update needed
minExcl && !excl )
return false;
// fallthrough: update is necessary for everything else
}
min = value;
minExcl = min.correctMin(excl);
return true;
}
/// update MAX border of an interval with VALUE wrt EXCL
bool updateMax ( bool excl, const ComparableDT& value )
{
if ( hasMax() ) // another max value: check if we need update
{
// constraint is <= or <
if ( max < value ) // was: {,5}; now: {,7}: no update needed
return false;
if ( max == value && // was: {,5); now: {,5]: no update needed
maxExcl && !excl )
return false;
// fallthrough: update is necessary for everything else
}
max = value;
maxExcl = max.correctMax(excl);
return true;
}
/// update given border of an interval with VALUE wrt EXCL
bool update ( bool min, bool excl, const ComparableDT& value )
{ return min ? updateMin ( excl, value ) : updateMax ( excl, value ); }
/// update wrt another interval
bool update ( const TDataInterval& Int )
{
bool ret = false;
if ( Int.hasMin() )
ret |= updateMin ( Int.minExcl, Int.min );
if ( Int.hasMax() )
ret |= updateMax ( Int.maxExcl, Int.max );
return ret;
}
/// @return true iff all the data is consistent wrt given TYPE
bool consistent ( const ComparableDT& dtype ) const
{
if ( hasMin() && !min.compatible(dtype) )
return false;
if ( hasMax() && !max.compatible(dtype) )
return false;
return true;
}
/// print an interval
void Print ( std::ostream& o ) const
{
if ( hasMin() )
o << (minExcl ? '(' : '[') << min;
else
o << '{';
o << ',';
if ( hasMax() )
o << max << (maxExcl ? ')' : ']');
else
o << '}';
}
/// print an interval as a LISP; FIXME!! hack
void printLISP ( std::ostream& o, const char* type ) const
{
if ( hasMin() && hasMax() )
o << "(and ";
if ( hasMin() )
{
o << "(g" << (minExcl ? 't' : 'e') << " (" << type;
min.printValue(o);
o << "))";
}
if ( hasMax() )
{
o << "(l" << (maxExcl ? 't' : 'e') << " (" << type;
max.printValue(o);
o << "))";
}
if ( hasMin() && hasMax() )
o << ")";
}
}; // TDataInterval
/// class for representing general data entry ("name" of data type or data value)
class TDataEntry: public TNamedEntry
{
private: // members
/// label to use in relevant-only checks
TLabeller::LabelType rel;
protected: // members
/// corresponding type (Type has NULL in the field)
const TDataEntry* Type;
/// DAG index of the entry
BipolarPointer pName;
/// ComparableDT, used only for values
ComparableDT comp;
/// restriction to the entry
TDataInterval Constraints;
private: // no copy
/// no copy c'tor
TDataEntry ( const TDataEntry& );
/// no assignment
TDataEntry& operator = ( const TDataEntry& );
protected: // methods
/// set COMP for the (typed) data value
void setComp ( const std::string& typeName )
{
// FIXME!! do the thing properly; unify the usage of DT names
if ( typeName == "string" )
comp = ComparableDT(getName());
else if ( typeName == "number" )
comp = ComparableDT(atol(getName()));
else if ( typeName == "real" )
comp = ComparableDT((float)atof(getName()));
else if ( typeName == "bool" ) // FIXME!! dirty hack
comp = ComparableDT(getName());
else if ( typeName == "time" )
comp = ComparableDT ( atol(getName()), 0 );
else // no more types available
fpp_unreachable();
}
public: // interface
/// create data entry with given name
TDataEntry ( const std::string& name )
: TNamedEntry(name)
, Type(NULL)
, pName(bpINVALID)
, comp()
{}
/// empty d'tor
~TDataEntry ( void ) {}
// type/value part
/// check if data entry represents basic data type
bool isBasicDataType ( void ) const { return Type == NULL && Constraints.empty(); }
/// check if data entry represents restricted data type
bool isRestrictedDataType ( void ) const { return !Constraints.empty(); }
/// check if data entry represents data value
bool isDataValue ( void ) const { return Type != NULL && Constraints.empty(); }
/// set host data type for the data value
void setHostType ( const TDataEntry* type ) { Type = type; setComp(type->getName()); }
/// get host type
const TDataEntry* getType ( void ) const { return Type; }
/// get comparable variant of DE
const ComparableDT& getComp ( void ) const { return comp; }
// facet part
/// get RW access to constraints of the DE
TDataInterval* getFacet ( void ) { return &Constraints; }
/// get RO access to constraints of the DE
const TDataInterval* getFacet ( void ) const { return &Constraints; }
// relevance part
/// is given concept relevant to given Labeller's state
bool isRelevant ( const TLabeller& lab ) const { return lab.isLabelled(rel); }
/// make given concept relevant to given Labeller's state
void setRelevant ( const TLabeller& lab ) { lab.set(rel); }
// BP part
/// get pointer to DAG entry correstonding to the data entry
BipolarPointer getBP ( void ) const { return pName; }
/// set DAG index of the data entry
void setBP ( BipolarPointer p ) { pName = p; }
// printing LISP FIXME!!
void printLISP ( std::ostream& o ) const
{
o << ' ';
if ( isBasicDataType() )
o << "(" << getName() << ")";
else if ( isDataValue() )
{
o << "(" << getType()->getName();
comp.printValue(o);
o << ")";
}
else if ( isRestrictedDataType() )
Constraints.printLISP ( o, getType()->getName() );
else
fpp_unreachable();
}
}; // TDataEntry
inline std::ostream&
operator << ( std::ostream& o, const TDataInterval& c )
{
c.Print(o);
return o;
}
#endif
|