Debian Codegraph @

Source/Test/Evaluation/CReasonerEvaluationRequestClientOWLlinkThread.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 "CReasonerEvaluationRequestClientOWLlinkThread.h"
22
23
24 namespace Konclude {
25
26 namespace Test {
27
28 namespace Evaluation {
29
30
31
32 CReasonerEvaluationRequestClientOWLlinkThread::CReasonerEvaluationRequestClientOWLlinkThread() : CReasonerEvaluationRequestClientBaseThread("Reasoner-Evaluation-OWLlink-Request-Client", "::Konclude::Test::ReasonerEvaluationOWLlinkRequestClient") {
33 }
34
35
36 CReasonerEvaluationRequestClientOWLlinkThread::~CReasonerEvaluationRequestClientOWLlinkThread() {
37 }
38
39
40 bool CReasonerEvaluationRequestClientOWLlinkThread::canHandleRequestFile(const QString& testFileString) {
41 if (testFileString.endsWith(".xml")) {
42 return true;
43 }
44 return false;
45 }
46
47
48 bool CReasonerEvaluationRequestClientOWLlinkThread::parseResponse(QIODevice* device) {
49 bool responseError = false;
50 QDomDocument document;
51 if (document.setContent(device)) {
52
53 QDomElement rootNode = document.documentElement();
54 QDomElement responseNode = rootNode.firstChildElement();
55 ((CReasonerEvaluationRequestResponseOWLlink*)mNextReqRes)->setResponseNode(responseNode);
56 if (responseNode.isNull()) {
57 responseError = true;
58 }
59
60 } else {
61 responseError = true;
62 }
63 return responseError;
64 }
65
66
67 bool CReasonerEvaluationRequestClientOWLlinkThread::sendNextRequest() {
68 if (mRemainingRequestList.isEmpty()) {
69 return false;
70 }
71 mNextReqRes = mRemainingRequestList.takeFirst();
72 CReasonerEvaluationRequestResponseOWLlink* owllinkNextReqRes = (CReasonerEvaluationRequestResponseOWLlink*)mNextReqRes;
73 QDomElement requestElement = owllinkNextReqRes->getModifiedNode();
74 QString nodeTagString(requestElement.tagName());
75 if (nodeTagString == "#comment" || nodeTagString == "#text") {
76 LOG(INFO,getLogDomain(),logTr("Skipped OWLlink request %1 of %2, because it is text or comment.").arg(mCurrentRequestCount+1).arg(mTotalRequestCount),this);
77 owllinkNextReqRes->setResponseNode(requestElement);
78 mResponse->addResponse(owllinkNextReqRes,mNumberInitRequests-- > 0);
79 return false;
80 } else {
81 QDomDocument document = createRequestDocument(getAdabtedRequestNode(requestElement));
82
83 QByteArray transReqByteArray(document.toByteArray());
84
85 if (mNumberInitRequests == 0) {
86 mTestingTiming.start();
87 }
88
89 mWebRequest = mTransManager->createRequest(QString("http://%1").arg(mAddressString),transReqByteArray);
90 mNextRequestTiming.start();
91 startTimerWithIntervalLimited(TIMERIDREQUESTTIMEOUT,mRequestTimeout,1);
92
93 mWebResponse = mTransManager->getResponse(mWebRequest);
94 mTransManager->callbackFinishedRequest(mWebResponse,new CReasonerEvaluationNextEvent(this,mCurrentOperationNumber));
95 LOG(INFO,getLogDomain(),logTr("Sending OWLlink request %1 of %2 with command '%3' to '%4'.").arg(mCurrentRequestCount+1).arg(mTotalRequestCount).arg(requestElement.tagName()).arg(mAddressString),this);
96 return true;
97 }
98 }
99
100
101
102
103 QDomElement CReasonerEvaluationRequestClientOWLlinkThread::getAdabtedRequestNode(QDomElement& el) {
104 if (mModifyRequestAbsolutePaths && el.tagName() == "OntologyIRI") {
105 QString ontoIRI = el.attribute("IRI");
106 QString relFileString = ontoIRI;
107 relFileString = relFileString.remove(0,5);
108 QDir currFilePath(QDir::currentPath());
109 QString absFileString = currFilePath.absoluteFilePath(relFileString);
110 if (ontoIRI.startsWith("file:") && !ontoIRI.startsWith("file://")) {
111 absFileString = QUrl::fromPercentEncoding(QUrl::fromLocalFile(absFileString).toString().toUtf8());
112 el.setAttribute("IRI",absFileString);
113 LOG(INFO,getLogDomain(),logTr("Changed relative filepath '%1' to absolute filepath '%2'.").arg(ontoIRI).arg(absFileString),this);
114 }
115 return el;
116 }
117
118 if (mReplaceLoadOntologiesWithTells && el.tagName() == "LoadOntologies") {
119 QString kbString = el.attribute("kb");
120 QDomDocument document;
121 QString owllinkNS = "http://www.owllink.org/owllink#";
122 QString owlNS = "http://www.w3.org/2002/07/owl#";
123 QDomElement newTellNode;
124 newTellNode = document.createElementNS(owllinkNS,"Tell");
125 //newTellNode.setAttributeNS("xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd");
126 newTellNode.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance","xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd");
127 newTellNode.setAttribute("xmlns:owl",owlNS);
128 document.appendChild(newTellNode);
129 newTellNode.setAttribute("kb",kbString);
130
131 QDomElement ontNode = el.firstChildElement("OntologyIRI");
132 while (!ontNode.isNull()) {
133 QString ontoIRI = ontNode.attribute("IRI");
134
135 QHash<QString,QString> appreviatePrefixHash;
136
137 QString relFileString = ontoIRI;
138 relFileString = relFileString.remove(0,5);
139 QFile ontAxFile(relFileString);
140 if (ontAxFile.open(QIODevice::ReadOnly)) {
141 QDomDocument ontAxDoc;
142 ontAxDoc.setContent(&ontAxFile,true);
143 QDomElement rootNode = ontAxDoc.firstChildElement();
144 QString xmlBase = rootNode.attribute("base");
145
146 QDomElement prefixNode = rootNode.firstChildElement("Prefix");
147 while (!prefixNode.isNull()) {
148 const QString& prefixName = prefixNode.attribute("name");
149 const QString& prefixIRI = prefixNode.attribute("IRI");
150 appreviatePrefixHash.insert(prefixName,prefixIRI);
151 prefixNode = prefixNode.nextSiblingElement("Prefix");
152 }
153
154 QDomElement axiomNode = rootNode.firstChildElement();
155 while (!axiomNode.isNull()) {
156 if (!mResolveAppreviatedIRIsForReplacedTells || axiomNode.tagName() != "Prefix") {
157 QDomElement importedAxiomNode = document.importNode(axiomNode,true).toElement();
158 if (mResolveAppreviatedIRIsForReplacedTells) {
159 importedAxiomNode = getResolvedAbbreviatedIRIsNode(importedAxiomNode,appreviatePrefixHash,xmlBase);
160 }
161 newTellNode.appendChild(importedAxiomNode);
162 }
163 axiomNode = axiomNode.nextSiblingElement();
164 }
165 ontAxFile.close();
166 }
167 ontNode = ontNode.nextSiblingElement("OntologyIRI");
168 }
169 return newTellNode;
170 }
171
172 QDomElement child(el.firstChildElement());
173 while (!child.isNull()) {
174 getAdabtedRequestNode(child);
175 child = child.nextSiblingElement();
176 }
177 return el;
178 }
179
180
181
182
183 bool CReasonerEvaluationRequestClientOWLlinkThread::startsWithScheme(const QString& uriString) {
184 for (int i = 0; i < uriString.count()-1; ++i) {
185 const QChar& c = uriString.at(i);
186 if (!c.isLetterOrNumber() && c != '.') {
187 if (c == ':') {
188 return i != 0;
189 }
190 return false;
191 }
192 }
193 return false;
194 }
195
196 QDomElement CReasonerEvaluationRequestClientOWLlinkThread::getResolvedAbbreviatedIRIsNode(QDomElement& el, const QHash<QString,QString>& appreviatePrefixHash, const QString& xmlBase) {
197 QString nextXMLBase = xmlBase;
198 if (el.hasAttribute("base")) {
199 nextXMLBase = el.attribute("base");
200 }
201 if (el.hasAttribute("IRI")) {
202 QString iriString = el.attribute("IRI");
203 if (!startsWithScheme(iriString)) {
204 QString iriStringWithXMLBase = nextXMLBase+iriString;
205 el.setAttribute("IRI",iriStringWithXMLBase);
206 }
207 }
208 if (el.hasAttribute("abbreviatedIRI")) {
209 QString abbreviatedIRIString = el.attribute("abbreviatedIRI");
210 el.removeAttribute("abbreviatedIRI");
211 cint64 colonPos = abbreviatedIRIString.indexOf(":");
212 QString prefixName = abbreviatedIRIString.mid(0,colonPos);
213 QString extensionName = appreviatePrefixHash.value(prefixName);
214 QString resolvedName = extensionName+abbreviatedIRIString.mid(colonPos+1);
215 el.setAttribute("IRI",resolvedName);
216 }
217 QDomElement childNode = el.firstChildElement();
218 while (!childNode.isNull()) {
219 getResolvedAbbreviatedIRIsNode(childNode,appreviatePrefixHash,nextXMLBase);
220 childNode = childNode.nextSiblingElement();
221 }
222 return el;
223 }
224
225
226 bool CReasonerEvaluationRequestClientOWLlinkThread::loadReasonerRequests(const QString& initFileString, const QString& testFileString) {
227 mCurrentRequestCount = 0;
228 mTotalRequestCount = 0;
229 bool loaded = loadReasonerRequest(initFileString);
230 mNumberInitRequests = mRemainingRequestList.count();
231 loaded &= loadReasonerRequest(testFileString);
232 return loaded;
233 }
234
235
236 bool CReasonerEvaluationRequestClientOWLlinkThread::loadReasonerRequest(const QString& requestFileString) {
237 QFile file(requestFileString);
238 if (file.open(QIODevice::ReadOnly)) {
239 QDomDocument document;
240 document.setContent(&file,false);
241 file.close();
242 QDomElement rootEl = document.documentElement();
243 cint64 loadedRequestCount = 0;
244
245 QDomElement childEl = rootEl.firstChildElement();
246 while (!childEl.isNull()) {
247 CReasonerEvaluationRequestResponseOWLlink* requestRespone = new CReasonerEvaluationRequestResponseOWLlink();
248 requestRespone->setRequestNode(childEl);
249 QDomElement modifiedEl = getModifiedRequestNode(childEl);
250 requestRespone->setModifiedNode(modifiedEl);
251 mRemainingRequestList.append(requestRespone);
252 ++mTotalRequestCount;
253 ++loadedRequestCount;
254 childEl = childEl.nextSiblingElement();
255 }
256 LOG(INFO,getLogDomain(),logTr("Loaded %1 OWLlink requests from file '%2'.").arg(loadedRequestCount).arg(requestFileString),this);
257
258 }
259 return true;
260 }
261
262
263 QDomDocument CReasonerEvaluationRequestClientOWLlinkThread::createRequestDocument(const QDomElement& reqNode) {
264 QDomDocument document;
265
266 QString owllinkNS = "http://www.owllink.org/owllink#";
267 QString owlNS = "http://www.w3.org/2002/07/owl#";
268
269 QDomElement rootNode;
270
271 rootNode = document.createElementNS(owllinkNS,"RequestMessage");
272 //rootNode.setAttributeNS("xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd");
273 rootNode.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance","xsi:schemaLocation","http://www.owllink.org/owllink# http://www.owllink.org/owllink-20091116.xsd");
274 rootNode.setAttribute("xmlns:owl",owlNS);
275 document.appendChild(rootNode);
276
277 QDomNode importedReqNode = document.importNode(reqNode,true);
278
279 rootNode.appendChild(importedReqNode);
280
281 return document;
282 }
283
284
285
286 QDomElement CReasonerEvaluationRequestClientOWLlinkThread::getModifiedRequestNode(const QDomElement& el) {
287 QDomElement modNode = el;
288 QDomElement exResNode = modNode.firstChildElement("ExpectedResult");
289 while (!exResNode.isNull()) {
290 modNode.removeChild(exResNode);
291 exResNode = modNode.firstChildElement("ExpectedResult");
292 }
293 return modNode;
294 }
295
296
297
298 }; // end namespace Evaluation
299
300 }; // end namespace Test
301
302 }; // end namespace Konclude