Debian Codegraph @

Source/Control/Interface/SPARQL/CSPARQLRecordResultStreamingInterpreter.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 "CSPARQLRecordResultStreamingInterpreter.h"
22
23
24 namespace Konclude {
25
26 namespace Control {
27
28 namespace Interface {
29
30 namespace SPARQL {
31
32 CSPARQLRecordResultStreamingInterpreter::CSPARQLRecordResultStreamingInterpreter(CSPARQLStreamingWriter* resultStreamingWriter, CCommandDelegater *commandDelegater, CConfiguration* config)
33 : CCommandRecorder() {
34
35 mResultStreamingWriter = resultStreamingWriter;
36
37
38 mConfBufferSize = 104857600;
39
40 mDelegater = commandDelegater;
41 mCurrentProcessingSeqNumber = 0;
42
43 mConfig = config;
44 mConfBufferSize = CConfigDataReader::readConfigInteger(mConfig, "Konclude.SPARQL.Serialization.ChunkEncodingSize", 104857600);
45 mNextSeqNumber = 0;
46 }
47
48
49 CSPARQLRecordResultStreamingInterpreter::~CSPARQLRecordResultStreamingInterpreter() {
50 }
51
52
53 CCommandDelegater *CSPARQLRecordResultStreamingInterpreter::delegateCommand(CCommand *command) {
54 CCalculateQueryCommand* calcQueryCommand = dynamic_cast<CCalculateQueryCommand*>(command);
55 if (calcQueryCommand) {
56 mSeqSyncMutex.lock();
57 cint64 sequenceNumber = mNextSeqNumber++;
58 CQueryCommandProvider* queryProvider = calcQueryCommand->getQueryCommandProvider();
59 CSPARQLResultStreamingData* data = nullptr;
60 if (queryProvider) {
61 data = new CSPARQLResultStreamingData(queryProvider, sequenceNumber, mConfBufferSize, this);
62 } else {
63 data = new CSPARQLResultStreamingData(calcQueryCommand->getQuery(), sequenceNumber, mConfBufferSize, this);
64 }
65 calcQueryCommand->setQueryCommandProvider(data);
66 mCommandSequenceDataHash.insert(command, data);
67 mSequenceDataHash.insert(sequenceNumber, data);
68 mSeqSyncMutex.unlock();
69 }
70 if (mDelegater) {
71 mDelegater->delegateCommand(command);
72 }
73 return this;
74 }
75
76
77 bool CSPARQLRecordResultStreamingInterpreter::notifyWriteRequest(cint64 sequenceNumber) {
78 bool continueWriting = true;
79 if (mCurrentProcessingSeqNumber == sequenceNumber) {
80 CSPARQLResultStreamingData* seqData = mSequenceDataHash.value(sequenceNumber);
81 continueWriting = writeStreamingData(seqData, false);
82 }
83 return continueWriting;
84 }
85
86
87 bool CSPARQLRecordResultStreamingInterpreter::writeStreamingData(CSPARQLResultStreamingData* seqData, bool lastData) {
88 QList<CSPARQLResultBufferWriteData> bufferList = seqData->takeWriteableBuffers();
89 bool continueWriting = true;
90 if (mResultStreamingWriter) {
91 continueWriting &= mResultStreamingWriter->writeStreamData(bufferList, lastData);
92 } else {
93 continueWriting = false;
94 }
95 return continueWriting;
96 }
97
98 bool CSPARQLRecordResultStreamingInterpreter::canWrite() {
99 return mResultStreamingWriter != nullptr;
100 }
101
102
103 CCommandRecorder* CSPARQLRecordResultStreamingInterpreter::recordData(CCommandRecordData *recData) {
104 CCommandRecorder::recordData(recData);
105 if (dynamic_cast<CClosureProcessCommandRecord *>(recData)) {
106 if (recData) {
107 CCommand *command = recData->getCommand();
108 if (command) {
109
110 if (mCommandSequenceDataHash.contains(command)) {
111
112
113 CCalculateQueryCommand *calcQueryCommand = dynamic_cast<CCalculateQueryCommand *>(command);
114 if (calcQueryCommand) {
115 CQuery *query = calcQueryCommand->getQuery();
116 CSPARQLResultStreamingData* seqData = mCommandSequenceDataHash.value(command);
117
118 if (command->getMaxErrorLevel() <= 0) {
119
120 if (query) {
121 if (query->hasError()) {
122 QString errorString = query->getErrorStringList().join(" ");
123 seqData->handleQueryError(query, errorString);
124 } else {
125 CQueryResult* queryResult = query->getQueryResult();
126 seqData->handleQueryResult(query, queryResult);
127 }
128 }
129
130 } else {
131 QString errorString = getErrorString(recData, "Error");
132 seqData->handleQueryError(query, errorString);
133 }
134 seqData->finalize();
135 writeStreamingData(seqData, true);
136 mSeqSyncMutex.lock();
137 mSequenceDataHash.remove(seqData->getSequenceNumber());
138 mCommandSequenceDataHash.remove(command);
139 mCurrentProcessingSeqNumber = seqData->getSequenceNumber() + 1;
140 delete seqData;
141 mSeqSyncMutex.unlock();
142
143
144 } else {
145 if (command->getMaxErrorLevel() > 0) {
146 QString errorString = getErrorString(recData, "Error");
147 //TODO: report error to stream writer
148 }
149
150 }
151 }
152
153 }
154 }
155 }
156 return this;
157 }
158
159
160
161
162
163 QString CSPARQLRecordResultStreamingInterpreter::getErrorString(CCommandRecordData *recData, const QString &nodeString) {
164 QString errorText;
165 if (recData) {
166 errorText = QString("An error has occurred while processing '%1',\n%2 process protocol:\n").arg(recData->getCommand()->getBriefCommandDescription()).arg(nodeString);
167
168 CCommandRecordData *nextRecData = recData;
169
170 while (nextRecData) {
171 errorText += QString("{%3}, [%1]:%2\n").arg(nextRecData->getRecordDomain()).arg(nextRecData->getRecordMessage()).arg(getErrorLevelString(nextRecData->getRecordErrorLevel()));
172 nextRecData = nextRecData->getNextCommandRecordData();
173 }
174
175 CCommand *command = recData->getCommand();
176 CLinker<CCommand *> *subCommIt = command->getSubCommandLinker();
177 QString subNodeString = QString("Sub-%1").arg(nodeString);
178 while (subCommIt) {
179 CCommand *subCommand = subCommIt->getData();
180 if (subCommand->getMaxErrorLevel() > 0) {
181 CCommandRecordData *subCommandRecData = subCommand->getRecordData();
182 if (subCommandRecData) {
183 QString subErrorString = QString("{%1}, %2").arg(getErrorLevelString(subCommand->getMaxErrorLevel())).arg(getErrorString(subCommandRecData, subNodeString));
184 errorText += subErrorString;
185 }
186 }
187 subCommIt = subCommIt->getNext();
188 }
189
190 }
191 else {
192 errorText = QString("An error has occurred while processing, process interface not available");
193 }
194 return errorText;
195 }
196
197
198
199
200 QString CSPARQLRecordResultStreamingInterpreter::getErrorLevelString(double level) {
201 QString logLevelString;
202 if (level >= 90) {
203 logLevelString = "catastrophic error";
204 }
205 else if (level >= 80) {
206 logLevelString = "exception error";
207 }
208 else if (level >= 70) {
209 logLevelString = "error";
210 }
211 else if (level >= 60) {
212 logLevelString = "warning";
213 }
214 else if (level >= 30) {
215 logLevelString = "info";
216 }
217 else {
218 logLevelString = "notice";
219 }
220 return logLevelString;
221 }
222
223
224
225 }; // end namespace SPARQL
226
227 }; // end namespace Interface
228
229 }; // end namespace Control
230
231 }; // end namespace Konclude