Debian Codegraph @

Source/Reasoner/Query/CVariableBindingsListAnswerResult.cpp

raw
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 "CVariableBindingsListAnswerResult.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Query {
29
30
31 CVariableBindingsListAnswerResult::CVariableBindingsListAnswerResult() {
32 }
33
34 CVariableBindingsListAnswerResult::CVariableBindingsListAnswerResult(cint64 count) {
35 for (cint64 i = 0; i < count; ++i) {
36 mBindingsList.push_back(nullptr);
37 }
38 }
39
40
41 CVariableBindingsListAnswerResult::~CVariableBindingsListAnswerResult() {
42 for (auto binding : mBindingsList) {
43 delete binding;
44 }
45 }
46
47
48 CVariableBindingsListAnswerResult::CVariableBindingsListAnswerResult(const CVariableBindingsListAnswerResult& varListResult) {
49 for (auto binding : varListResult.mBindingsList) {
50 mBindingsList.append(binding->createCopy());
51 }
52 }
53
54 CVariableBindingsListAnswerResult* CVariableBindingsListAnswerResult::clearResultVariableBindings() {
55 mBindingsList.clear();
56 return this;
57 }
58
59
60
61 CVariableBindingsListAnswerResult* CVariableBindingsListAnswerResult::addResultVariableBinding(CVariableBindingResult* varBinding) {
62 mBindingsList.append(varBinding);
63 return this;
64 }
65
66 CVariableBindingsListAnswerResult* CVariableBindingsListAnswerResult::setResultVariableBindings(const QList<CVariableBindingResult*>& bindingsList) {
67 mBindingsList = bindingsList;
68 return this;
69 }
70
71 CVariableBindingResult* CVariableBindingsListAnswerResult::getResult(cint64 index) {
72 return mBindingsList.at(index);
73 }
74
75 cint64 CVariableBindingsListAnswerResult::getResultCount() {
76 return mBindingsList.count();
77 }
78
79
80 CVariableBindingsListAnswerResult* CVariableBindingsListAnswerResult::setResult(cint64 index, CVariableBindingResult* varBinding) {
81 mBindingsList[index] = varBinding;
82 return this;
83 }
84
85
86 QList<CVariableBindingResult*>* CVariableBindingsListAnswerResult::getResult() {
87 return &mBindingsList;
88 }
89
90
91
92 QString CVariableBindingsListAnswerResult::getQueryResultString() {
93 QStringList bindingsStringList;
94 for (auto binding : mBindingsList) {
95 bindingsStringList.append(binding->getBindingString());
96 }
97 return bindingsStringList.join(", ");
98 }
99
100
101 bool CVariableBindingsListAnswerResult::isResultEquivalentTo(CQueryResult *otherQueryResult) {
102 if (otherQueryResult) {
103 CVariableBindingsListAnswerResult *otherBoolQueryResult = dynamic_cast<CVariableBindingsListAnswerResult *>(otherQueryResult);
104 if (otherBoolQueryResult) {
105 return isResultEquivalentTo(otherBoolQueryResult);
106 }
107 }
108 return false;
109 }
110
111
112 bool CVariableBindingsListAnswerResult::isResultEquivalentTo(CVariableBindingsListAnswerResult *otherQueryResult) {
113 if (otherQueryResult) {
114 if (mBindingsList.count() != otherQueryResult->mBindingsList.count()) {
115 return false;
116 }
117 for (QList<CVariableBindingResult*>::const_iterator it1 = mBindingsList.constBegin(), it2 = otherQueryResult->mBindingsList.constBegin(), it1End = mBindingsList.constEnd(), it2ENd = otherQueryResult->mBindingsList.constEnd(); it1 != it1End && it2 != it2ENd; ++it1, ++it2) {
118 CVariableBindingResult* bind1(*it1);
119 CVariableBindingResult* bind2(*it2);
120 if ((bind1 == nullptr || bind2 == nullptr) && bind2 != bind1) {
121 return false;
122 }
123 if (bind1 && !bind1->isResultEquivalentTo(bind2)) {
124 return false;
125 }
126 }
127 return true;
128 } else {
129 return false;
130 }
131 }
132
133
134 CVariableBindingsResultIterator* CVariableBindingsListAnswerResult::getVariableBindingsIterator(CVariableBindingsResultIterator* reuseIt) {
135 class CVariableBindingsListAnswerResultIterator : public CVariableBindingsResultIterator {
136 public:
137 CVariableBindingsListAnswerResultIterator(const QList<CVariableBindingResult*>& list) {
138 init(list);
139 }
140
141 CVariableBindingsListAnswerResultIterator* init(const QList<CVariableBindingResult*>& list) {
142 mIt = list.constBegin();
143 mItEnd = list.constEnd();
144 return this;
145 }
146
147
148 virtual CVariableBindingResult* getNext(bool moveNext = true) {
149 QList<CVariableBindingResult*>::const_iterator it = mIt;
150 if (it != mItEnd) {
151 if (moveNext) {
152 ++mIt;
153 }
154 return *it;
155 }
156 return nullptr;
157 }
158
159 virtual bool hasNext() {
160 return mIt != mItEnd;
161 }
162
163 QList<CVariableBindingResult*>::const_iterator mIt, mItEnd;
164
165 };
166 if (reuseIt) {
167 CVariableBindingsListAnswerResultIterator* listReuseIt = (CVariableBindingsListAnswerResultIterator*)reuseIt;
168 return listReuseIt->init(mBindingsList);
169 }
170 return new CVariableBindingsListAnswerResultIterator(mBindingsList);
171 }
172
173
174 CVariableBindingsAnswerResult* CVariableBindingsListAnswerResult::getCopy() {
175 return new CVariableBindingsListAnswerResult(*this);
176 }
177
178
179 cint64 CVariableBindingsListAnswerResult::getHashCode() {
180 cint64 hashCode = mBindingsList.count();
181 for (CVariableBindingResult* binding : mBindingsList) {
182 cint64 bindingType = 0;
183 if (binding) {
184 bindingType = (cint64)binding->getVariableBindingType();
185 bindingType += qHash(binding->getQueryResultString());
186 }
187 hashCode = bindingType * 13 + hashCode;
188 }
189 return hashCode;
190 }
191
192
193
194 }; // end namespace Query
195
196 }; // end namespace Reasoner
197
198 }; // end namespace Konclude