| 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 |
#ifndef KONCLUDE_REASONER_QUERY_CSTRINGSYNSETRESULT_CPP |
| 22 |
#define KONCLUDE_REASONER_QUERY_CSTRINGSYNSETRESULT_CPP |
| 23 |
|
| 24 |
|
| 25 |
#include "CStringSynsetResult.h" |
| 26 |
|
| 27 |
|
| 28 |
namespace Konclude { |
| 29 |
|
| 30 |
namespace Reasoner { |
| 31 |
|
| 32 |
namespace Query { |
| 33 |
|
| 34 |
|
| 35 |
template<class T> |
| 36 |
CStringSynsetResult<T>::CStringSynsetResult() { |
| 37 |
} |
| 38 |
|
| 39 |
template<class T> |
| 40 |
CStringSynsetResult<T>::CStringSynsetResult(const QString &stringName) { |
| 41 |
addEquivalentStringName(stringName); |
| 42 |
} |
| 43 |
|
| 44 |
template<class T> |
| 45 |
CStringSynsetResult<T>::CStringSynsetResult(const QStringList &stringNameList) { |
| 46 |
addEquivalentStringNames(stringNameList); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
template<class T> |
| 51 |
CStringSynsetResult<T>::~CStringSynsetResult() { |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
template<class T> |
| 56 |
QStringList CStringSynsetResult<T>::getEquivalentStringNameList() { |
| 57 |
return mStringNameList; |
| 58 |
} |
| 59 |
|
| 60 |
template<class T> |
| 61 |
T* CStringSynsetResult<T>::addEquivalentStringName(const QString &stringName) { |
| 62 |
mStringNameList.append(stringName); |
| 63 |
return (T*)this; |
| 64 |
} |
| 65 |
|
| 66 |
template<class T> |
| 67 |
T* CStringSynsetResult<T>::addEquivalentStringNames(const QStringList &stringNameList) { |
| 68 |
mStringNameList += stringNameList; |
| 69 |
return (T*)this; |
| 70 |
} |
| 71 |
|
| 72 |
template<class T> |
| 73 |
qint64 CStringSynsetResult<T>::getStringCount() { |
| 74 |
return mStringNameList.count(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
template<class T> |
| 79 |
bool CStringSynsetResult<T>::hasStringName(const QString &stringName) { |
| 80 |
return mStringNameList.contains(stringName); |
| 81 |
} |
| 82 |
|
| 83 |
template<class T> |
| 84 |
bool CStringSynsetResult<T>::hasStringNames(const QStringList &stringNameList) { |
| 85 |
if (mStringNameList.size() <= 1) { |
| 86 |
return mStringNameList.first() == stringNameList.first(); |
| 87 |
} else if (mStringNameList.size() <= 20) { |
| 88 |
QStringList remEqStringNameList(mStringNameList); |
| 89 |
foreach (const QString& stringName, stringNameList) { |
| 90 |
bool contained = false; |
| 91 |
for (qint64 cnt = remEqStringNameList.count(); cnt > 0; --cnt) { |
| 92 |
contained |= remEqStringNameList.first() == stringName; |
| 93 |
if (contained) { |
| 94 |
remEqStringNameList.takeFirst(); |
| 95 |
break; |
| 96 |
} else { |
| 97 |
remEqStringNameList.append(remEqStringNameList.takeFirst()); |
| 98 |
} |
| 99 |
} |
| 100 |
if (!contained) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
} |
| 104 |
} else { |
| 105 |
QHash<QString,cint64> stringNameCountHash; |
| 106 |
foreach (const QString& stringName, stringNameList) { |
| 107 |
if (stringNameCountHash.contains(stringName)) { |
| 108 |
++stringNameCountHash[stringName]; |
| 109 |
} else { |
| 110 |
stringNameCountHash[stringName] = 1; |
| 111 |
} |
| 112 |
} |
| 113 |
foreach (const QString& stringName, mStringNameList) { |
| 114 |
if (!stringNameCountHash.contains(stringName)) { |
| 115 |
return false; |
| 116 |
} else { |
| 117 |
--stringNameCountHash[stringName]; |
| 118 |
} |
| 119 |
} |
| 120 |
for (QHash<QString,cint64>::const_iterator it = stringNameCountHash.constBegin(), itEnd = stringNameCountHash.constEnd(); it != itEnd; ++it) { |
| 121 |
cint64 diff(it.value()); |
| 122 |
if (diff != 0) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
template<class T> |
| 131 |
QString CStringSynsetResult<T>::getQueryResultString(const QString& stringTypeName) { |
| 132 |
QString stringName = mStringNameList.join(", "); |
| 133 |
if (mStringNameList.count() > 1) { |
| 134 |
return QString("Equivalent %1 Set '%2'").arg(stringTypeName).arg(stringName); |
| 135 |
} else if (mStringNameList.count() > 0) { |
| 136 |
return QString("%1 '%2'").arg(stringTypeName).arg(stringName); |
| 137 |
} else { |
| 138 |
return QString("Empty %1 Set").arg(stringTypeName); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
template<class T> |
| 143 |
bool CStringSynsetResult<T>::isResultEquivalentTo(CQueryResult *otherQueryResult) { |
| 144 |
if (!otherQueryResult) { |
| 145 |
return false; |
| 146 |
} |
| 147 |
T* otherClassSynset = dynamic_cast<T*>(otherQueryResult); |
| 148 |
if (!otherClassSynset) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
if (getStringCount() != otherClassSynset->getStringCount()) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
if (!hasStringNames(otherClassSynset->getEquivalentStringNameList())) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
}; // end namespace Query |
| 162 |
|
| 163 |
}; // end namespace Reasoner |
| 164 |
|
| 165 |
}; // end namespace Konclude |
| 166 |
|
| 167 |
|
| 168 |
#endif // KONCLUDE_REASONER_QUERY_CSTRINGSYNSETRESULT_CPP |