| 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 "CSPARQLQtXMLResultParser.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Parser { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CSPARQLQtXMLResultParser::CSPARQLQtXMLResultParser() { |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
CSPARQLQtXMLResultParser::~CSPARQLQtXMLResultParser() { |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
CQueryResult *CSPARQLQtXMLResultParser::parseQueryResult(QDomElement *resultNode) { |
| 41 |
QDomElement node = resultNode->firstChildElement(); |
| 42 |
CQueryResult *queryResult = nullptr; |
| 43 |
while (!node.isNull() && !queryResult) { |
| 44 |
QString docElemName = node.tagName(); |
| 45 |
if (docElemName == "results") { |
| 46 |
queryResult = parseVariableBindingsSet(&node); |
| 47 |
} |
| 48 |
|
| 49 |
} |
| 50 |
return queryResult; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
CVariableBindingsAnswersSetResult* CSPARQLQtXMLResultParser::parseVariableBindingsSet(QDomElement *node, QHash<QString, cint64>* variableNameIndexHash, bool locallyRenameBlankNamesBindings) { |
| 57 |
return (CVariableBindingsAnswersSetResult*)parseVariableBindings(node, variableNameIndexHash, locallyRenameBlankNamesBindings, true); |
| 58 |
} |
| 59 |
|
| 60 |
CVariableBindingsAnswersListResult* CSPARQLQtXMLResultParser::parseVariableBindingsList(QDomElement *node, QHash<QString, cint64>* variableNameIndexHash, bool locallyRenameBlankNamesBindings) { |
| 61 |
return (CVariableBindingsAnswersListResult*)parseVariableBindings(node, variableNameIndexHash, locallyRenameBlankNamesBindings, false); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
CVariableBindingsAnswersResult* CSPARQLQtXMLResultParser::parseVariableBindings(QDomElement *node, QHash<QString, cint64>* variableNameIndexHash, bool locallyRenameBlankNamesBindings, bool parseAsSet) { |
| 67 |
|
| 68 |
QStringList classNameList; |
| 69 |
bool err = node->isNull(); |
| 70 |
|
| 71 |
QString tagName = node->tagName(); |
| 72 |
|
| 73 |
CVariableBindingsAnswersResult* bindingAnswersResult = nullptr; |
| 74 |
|
| 75 |
QStringList variableNameList; |
| 76 |
cint64 nextVariableIndex = 0; |
| 77 |
|
| 78 |
QHash<QString, cint64>* deleteVariableNameIndexHash = nullptr; |
| 79 |
if (!variableNameIndexHash) { |
| 80 |
deleteVariableNameIndexHash = variableNameIndexHash = new QHash<QString, cint64>(); |
| 81 |
} |
| 82 |
|
| 83 |
QDomElement resultNode = node->firstChildElement(); |
| 84 |
while (!resultNode.isNull()) { |
| 85 |
if (resultNode.tagName() == "result") { |
| 86 |
|
| 87 |
CVariableBindingsListAnswerResult* bindingResult = new CVariableBindingsListAnswerResult(); |
| 88 |
QVector<CVariableBindingResult*> bindingsVector(nextVariableIndex); |
| 89 |
|
| 90 |
|
| 91 |
QDomElement bindingNode = resultNode.firstChildElement(); |
| 92 |
while (!bindingNode.isNull()) { |
| 93 |
if (bindingNode.tagName() == "binding") { |
| 94 |
|
| 95 |
QString variableName = bindingNode.attribute("name"); |
| 96 |
if (!variableName.isEmpty()) { |
| 97 |
|
| 98 |
if (!variableNameIndexHash->contains(variableName)) { |
| 99 |
variableNameIndexHash->insert(variableName, nextVariableIndex++); |
| 100 |
variableNameList.append(variableName); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
cint64 index = variableNameIndexHash->value(variableName); |
| 105 |
if (index >= bindingsVector.size()) { |
| 106 |
bindingsVector.resize(index + 1); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
QDomElement boundNode = bindingNode.firstChildElement(); |
| 112 |
while (!boundNode.isNull()) { |
| 113 |
if (boundNode.tagName() == "uri") { |
| 114 |
QString uri = boundNode.text(); |
| 115 |
CVariableBindingResult* varBindingResult = new CVariableBindingStringResult(uri, CVariableBindingResult::VBTNAMEDINDIVIDUAL); |
| 116 |
bindingsVector[index] = varBindingResult; |
| 117 |
} else if (boundNode.tagName() == "literal") { |
| 118 |
QString lexicalValue = boundNode.text(); |
| 119 |
QString datatypeIRI = boundNode.attribute("datatypeIRI", PREFIX_XML_PLAINLITERAL_DATATYPE); |
| 120 |
CVariableBindingResult* varBindingResult = new CVariableBindingStringResult("\"" + lexicalValue + "\"^^" + datatypeIRI, CVariableBindingResult::VBTLITERAL); |
| 121 |
bindingsVector[index] = varBindingResult; |
| 122 |
} else if (boundNode.tagName() == "bnode") { |
| 123 |
QString bname = boundNode.text(); |
| 124 |
if (locallyRenameBlankNamesBindings) { |
| 125 |
bname = variableName + "_bound_blank_node"; |
| 126 |
} |
| 127 |
CVariableBindingResult* varBindingResult = new CVariableBindingStringResult(bname, CVariableBindingResult::VBTANONYMOUSINDIVIDUAL); |
| 128 |
bindingsVector[index] = varBindingResult; |
| 129 |
} |
| 130 |
|
| 131 |
boundNode = boundNode.nextSiblingElement(); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
bindingNode = bindingNode.nextSiblingElement(); |
| 136 |
} |
| 137 |
|
| 138 |
if (bindingsVector.count() > 0) { |
| 139 |
bindingResult->setResultVariableBindings(bindingsVector.toList()); |
| 140 |
if (!bindingAnswersResult) { |
| 141 |
if (parseAsSet) { |
| 142 |
bindingAnswersResult = new CVariableBindingsAnswersSetResult(variableNameList); |
| 143 |
} else { |
| 144 |
bindingAnswersResult = new CVariableBindingsAnswersListResult(variableNameList); |
| 145 |
} |
| 146 |
} |
| 147 |
bindingAnswersResult->addResultVariableBindings(bindingResult); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
resultNode = resultNode.nextSiblingElement(); |
| 152 |
} |
| 153 |
|
| 154 |
if (!bindingAnswersResult) { |
| 155 |
if (parseAsSet) { |
| 156 |
bindingAnswersResult = new CVariableBindingsAnswersSetResult(QStringList()); |
| 157 |
} else { |
| 158 |
bindingAnswersResult = new CVariableBindingsAnswersListResult(QStringList()); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
if (deleteVariableNameIndexHash) { |
| 163 |
delete deleteVariableNameIndexHash; |
| 164 |
} |
| 165 |
|
| 166 |
return bindingAnswersResult; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
}; // end namespace Parser |
| 173 |
|
| 174 |
}; // end namespace Konclude |