| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CDataLiteralBinaryDataValue.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Ontology { |
| 29 |
|
| 30 |
|
| 31 |
CDataLiteralBinaryDataValue::CDataLiteralBinaryDataValue(CContext* context) { |
| 32 |
mContext = context; |
| 33 |
mByteDataArray = nullptr; |
| 34 |
mByteDataArraySize = 0; |
| 35 |
clearValue(); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
CDataLiteralCompareValue* CDataLiteralBinaryDataValue::initValue(CDataLiteralCompareValue* value) { |
| 41 |
clearValue(); |
| 42 |
CDataLiteralBinaryDataValue* dataValue = dynamic_cast<CDataLiteralBinaryDataValue*>(value); |
| 43 |
if (dataValue) { |
| 44 |
initValue(dataValue); |
| 45 |
} |
| 46 |
return this; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
CDataLiteralBinaryDataValue* CDataLiteralBinaryDataValue::initValueFromInfiniteLength() { |
| 51 |
clearValue(); |
| 52 |
mInfiniteLength = true; |
| 53 |
return this; |
| 54 |
} |
| 55 |
|
| 56 |
CDataLiteralBinaryDataValue* CDataLiteralBinaryDataValue::initValueFromLength(cuint64 length) { |
| 57 |
clearValue(); |
| 58 |
mLength = length; |
| 59 |
return this; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
CDataLiteralBinaryDataValue* CDataLiteralBinaryDataValue::initValueFromData(unsigned char* data, cint64 length) { |
| 64 |
clearValue(); |
| 65 |
if (mByteDataArraySize < length) { |
| 66 |
mByteDataArraySize = length; |
| 67 |
mByteDataLength = length; |
| 68 |
mByteDataArray = CObjectAllocator<unsigned char>::allocateArray(CContext::getMemoryAllocationManager(mContext),mByteDataArraySize); |
| 69 |
} |
| 70 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 71 |
mByteDataArray[i] = data[i]; |
| 72 |
} |
| 73 |
return this; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
CDataLiteralBinaryDataValue* CDataLiteralBinaryDataValue::initValue(CDataLiteralBinaryDataValue* value) { |
| 78 |
clearValue(); |
| 79 |
if (value) { |
| 80 |
mInfiniteLength = value->mInfiniteLength; |
| 81 |
mLength = value->mLength; |
| 82 |
if (value->mByteDataLength > 0) { |
| 83 |
if (mByteDataArraySize < value->mByteDataLength) { |
| 84 |
mByteDataArraySize = value->mByteDataLength; |
| 85 |
mByteDataLength = value->mByteDataLength; |
| 86 |
mByteDataArray = CObjectAllocator<unsigned char>::allocateArray(CContext::getMemoryAllocationManager(mContext),mByteDataArraySize); |
| 87 |
} |
| 88 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 89 |
mByteDataArray[i] = value->mByteDataArray[i]; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return this; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
CDataLiteralBinaryDataValue* CDataLiteralBinaryDataValue::clearValue() { |
| 98 |
mInfiniteLength = false; |
| 99 |
mLength = 0; |
| 100 |
mByteDataLength = 0; |
| 101 |
if (mByteDataArray) { |
| 102 |
for (cint64 i = 0; i < mByteDataArraySize; ++i) { |
| 103 |
mByteDataArray[i] = 0; |
| 104 |
} |
| 105 |
} |
| 106 |
return this; |
| 107 |
} |
| 108 |
|
| 109 |
cuint64 CDataLiteralBinaryDataValue::getBinaryDataID() { |
| 110 |
cuint64 dataID = 0; |
| 111 |
dataID += 1 << getLength()*8; |
| 112 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 113 |
dataID += mByteDataArray[i] << (mByteDataLength-i-1)*8; |
| 114 |
} |
| 115 |
return dataID; |
| 116 |
} |
| 117 |
|
| 118 |
CDataLiteralBinaryDataValue* CDataLiteralBinaryDataValue::incrementBinaryData() { |
| 119 |
if (mLength > 0) { |
| 120 |
if (mByteDataArraySize < mLength) { |
| 121 |
mByteDataArraySize = mLength; |
| 122 |
mByteDataArray = CObjectAllocator<unsigned char>::allocateArray(CContext::getMemoryAllocationManager(mContext),mByteDataArraySize); |
| 123 |
} |
| 124 |
mByteDataLength = mLength; |
| 125 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 126 |
mByteDataArray[i] = 0; |
| 127 |
} |
| 128 |
mLength = 0; |
| 129 |
} |
| 130 |
|
| 131 |
bool overflow = true; |
| 132 |
for (cint64 i = mByteDataLength-1; overflow && i >= 0; --i) { |
| 133 |
overflow = false; |
| 134 |
if (mByteDataArray[i] == 255) { |
| 135 |
mByteDataArray[i] = 0; |
| 136 |
overflow = true; |
| 137 |
} else { |
| 138 |
mByteDataArray[i] = mByteDataArray[i]+1; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (overflow) { |
| 143 |
if (mByteDataArraySize < mLength+1) { |
| 144 |
mByteDataArraySize = mLength+1; |
| 145 |
mByteDataArray = CObjectAllocator<unsigned char>::allocateArray(CContext::getMemoryAllocationManager(mContext),mByteDataArraySize); |
| 146 |
} |
| 147 |
mByteDataLength = mLength+1; |
| 148 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 149 |
mByteDataArray[i] = 0; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return this; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
bool CDataLiteralBinaryDataValue::isInfiniteLength() { |
| 159 |
return mInfiniteLength; |
| 160 |
} |
| 161 |
|
| 162 |
cuint64 CDataLiteralBinaryDataValue::getLength() { |
| 163 |
if (mLength > 0) { |
| 164 |
return mLength; |
| 165 |
} else { |
| 166 |
return mByteDataLength; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
unsigned char* CDataLiteralBinaryDataValue::getByteDataArray() { |
| 172 |
return mByteDataArray; |
| 173 |
} |
| 174 |
|
| 175 |
cint64 CDataLiteralBinaryDataValue::getByteDataLength() { |
| 176 |
return mByteDataLength; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
bool CDataLiteralBinaryDataValue::isEqualTo(CDataLiteralValue* value) { |
| 181 |
CDataLiteralBinaryDataValue* dataValue = dynamic_cast<CDataLiteralBinaryDataValue*>(value); |
| 182 |
if (dataValue) { |
| 183 |
return isEqualTo(dataValue); |
| 184 |
} |
| 185 |
return false; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
bool CDataLiteralBinaryDataValue::isLessEqualThan(CDataLiteralCompareValue* value) { |
| 191 |
CDataLiteralBinaryDataValue* dataValue = dynamic_cast<CDataLiteralBinaryDataValue*>(value); |
| 192 |
if (dataValue) { |
| 193 |
return isEqualTo(dataValue) || isLessThan(dataValue); |
| 194 |
} |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
bool CDataLiteralBinaryDataValue::isLessEqualThan(CDataLiteralBinaryDataValue* dataValue) { |
| 199 |
return isEqualTo(dataValue) || isLessThan(dataValue); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
bool CDataLiteralBinaryDataValue::isEqualTo(CDataLiteralCompareValue* value) { |
| 204 |
CDataLiteralBinaryDataValue* dataValue = dynamic_cast<CDataLiteralBinaryDataValue*>(value); |
| 205 |
if (dataValue) { |
| 206 |
return isEqualTo(dataValue); |
| 207 |
} |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
bool CDataLiteralBinaryDataValue::isZeroData() { |
| 213 |
if (mLength > 0) { |
| 214 |
return true; |
| 215 |
} |
| 216 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 217 |
unsigned char data = mByteDataArray[i]; |
| 218 |
if (data != 0) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
} |
| 222 |
return true; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
bool CDataLiteralBinaryDataValue::isEqualTo(CDataLiteralBinaryDataValue* dataValue) { |
| 227 |
if (getDataValueType() != dataValue->getDataValueType()) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
if (mInfiniteLength && dataValue->mInfiniteLength) { |
| 231 |
return true; |
| 232 |
} |
| 233 |
if (mInfiniteLength || dataValue->mInfiniteLength) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
if (getLength() != dataValue->getLength()) { |
| 237 |
return false; |
| 238 |
} |
| 239 |
if (mLength > 0 && mLength == dataValue->mLength) { |
| 240 |
return true; |
| 241 |
} |
| 242 |
bool zD = isZeroData(); |
| 243 |
bool dataValueZD = dataValue->isZeroData(); |
| 244 |
if (zD && dataValueZD) { |
| 245 |
return true; |
| 246 |
} else if (zD != dataValueZD) { |
| 247 |
return false; |
| 248 |
} else { |
| 249 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 250 |
if (mByteDataArray[i] != dataValue->mByteDataArray[i]) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
} |
| 254 |
return true; |
| 255 |
} |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
bool CDataLiteralBinaryDataValue::isGreaterEqualThan(CDataLiteralBinaryDataValue* dataValue) { |
| 260 |
if (getDataValueType() != dataValue->getDataValueType()) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
return isEqualTo(dataValue) || isGreaterThan(dataValue); |
| 264 |
} |
| 265 |
|
| 266 |
bool CDataLiteralBinaryDataValue::isGreaterEqualThan(CDataLiteralCompareValue* value) { |
| 267 |
CDataLiteralBinaryDataValue* dataValue = dynamic_cast<CDataLiteralBinaryDataValue*>(value); |
| 268 |
if (dataValue) { |
| 269 |
return isEqualTo(dataValue) || isGreaterThan(dataValue); |
| 270 |
} |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
bool CDataLiteralBinaryDataValue::isGreaterThan(CDataLiteralCompareValue* value) { |
| 275 |
if (getDataValueType() != value->getDataValueType()) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
return value->isLessThan(this); |
| 279 |
} |
| 280 |
|
| 281 |
bool CDataLiteralBinaryDataValue::isGreaterThan(CDataLiteralBinaryDataValue* dataValue) { |
| 282 |
if (getDataValueType() != dataValue->getDataValueType()) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
return dataValue->isLessThan(this); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
bool CDataLiteralBinaryDataValue::isLessThan(CDataLiteralCompareValue* value) { |
| 290 |
CDataLiteralBinaryDataValue* dataValue = dynamic_cast<CDataLiteralBinaryDataValue*>(value); |
| 291 |
if (dataValue) { |
| 292 |
return isLessThan(dataValue); |
| 293 |
} |
| 294 |
return false; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
bool CDataLiteralBinaryDataValue::isLessThan(CDataLiteralBinaryDataValue* dataValue) { |
| 302 |
if (getDataValueType() != dataValue->getDataValueType()) { |
| 303 |
return false; |
| 304 |
} |
| 305 |
if (mInfiniteLength) { |
| 306 |
return false; |
| 307 |
} |
| 308 |
if (!mInfiniteLength && dataValue->mInfiniteLength) { |
| 309 |
return true; |
| 310 |
} |
| 311 |
if (getLength() < dataValue->getLength()) { |
| 312 |
return true; |
| 313 |
} else if (getLength() > dataValue->getLength()) { |
| 314 |
return false; |
| 315 |
} else { |
| 316 |
|
| 317 |
if (dataValue->isZeroData()) { |
| 318 |
return false; |
| 319 |
} else if (isZeroData()) { |
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
for (cint64 i = 0; i < mByteDataLength; ++i) { |
| 324 |
if (mByteDataArray[i] < dataValue->mByteDataArray[i]) { |
| 325 |
return true; |
| 326 |
} else if (mByteDataArray[i] > dataValue->mByteDataArray[i]) { |
| 327 |
return false; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
} |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
}; // end namespace Ontology |
| 338 |
|
| 339 |
}; // end namespace Reasoner |
| 340 |
|
| 341 |
}; // end namespace Konclude |