Debian Codegraph @

Source/Test/COWLlinkAPIReasonerTestfileTester.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 "COWLlinkAPIReasonerTestfileTester.h"
22
23
24 namespace Konclude {
25
26 namespace Test {
27
28
29
30 COWLlinkAPIReasonerTestfileTester::COWLlinkAPIReasonerTestfileTester(CConfiguration *config)
31 : CLogIdentifier("::Konclude::Test::OWLlinkAPIReasonerTestfileTester",this),mConfig(config) {
32
33
34 // 2 weeks timeout
35 mTransManager = new CQtHttpTransactionManager(1000*60*60*24*14);
36
37 mAddressString = CConfigDataReader::readConfigString(mConfig,"Konclude.OWLlink.OWLlinkAPI.Address","http://127.0.0.1:8080");
38
39 mWriteResponseStdOut = CConfigDataReader::readConfigBoolean(mConfig,"Konclude.OWLlink.WriteResponseToStandardOutput",true);
40 mModifyRequest = CConfigDataReader::readConfigBoolean(mConfig,"Konclude.OWLlink.OWLlinkAPI.RequestFileCompatibleModification",true);
41 mModifyResponseTiming = CConfigDataReader::readConfigBoolean(mConfig,"Konclude.OWLlink.OWLlinkAPI.ResponseFileTimeExtension",true);
42 mModifyRequestAbsolutePaths = CConfigDataReader::readConfigBoolean(mConfig,"Konclude.OWLlink.OWLlinkAPI.RequestFileAbsolutePathsModification",true);
43 mExpectedResult = nullptr;
44 }
45
46
47 COWLlinkAPIReasonerTestfileTester::~COWLlinkAPIReasonerTestfileTester() {
48 delete mTransManager;
49 }
50
51
52
53
54 void COWLlinkAPIReasonerTestfileTester::startTestfileTest(const QString& requestFileName, const QString& responseFileName) {
55
56 QFile file(requestFileName);
57 if (file.open(QIODevice::ReadOnly)) {
58 QDomDocument document;
59 document.setContent(&file,false);
60 file.close();
61 QDomElement rootEl = document.documentElement();
62 if (mModifyRequest) {
63 modifyRequestDomStructure(rootEl);
64 }
65 QByteArray transReqByteArray(document.toByteArray());
66
67 CHttpRequest* webRequest = mTransManager->createRequest(mAddressString,transReqByteArray);
68 QTime timing;
69 timing.start();
70 CHttpResponse* webResponse = mTransManager->getResponse(webRequest);
71 LOG(INFO,getLogDomain(),logTr("Sending OWLlink request from file '%1' to '%2'.").arg(requestFileName).arg(mAddressString),this);
72
73 QString* siteText = nullptr;
74 siteText = mTransManager->getExtractedText(webResponse);
75 cint64 elapsedTime = timing.elapsed();
76
77 if (siteText && !siteText->isEmpty()) {
78 LOG(INFO,getLogDomain(),logTr("Recieved OWLlink response in '%1' ms.").arg(elapsedTime),this);
79 document.setContent(*siteText,false);
80 rootEl = document.documentElement();
81 if (mModifyResponseTiming) {
82 modifyResponseDomStructure(rootEl,elapsedTime);
83 }
84
85 mTransManager->releaseResponse(webResponse);
86
87 if (mWriteResponseStdOut) {
88 QString respString(document.toByteArray());
89 cout<<respString.toLocal8Bit().data();
90 }
91
92
93 if (!responseFileName.isEmpty()) {
94 QFile wFile(responseFileName);
95 if (wFile.open(QIODevice::WriteOnly)) {
96 wFile.write(document.toByteArray());
97 wFile.close();
98 LOG(INFO,getLogDomain(),logTr("Writing OWLlink response to file '%1'.").arg(responseFileName),this);
99 } else {
100 LOG(INFO,getLogDomain(),logTr("Could not write response to '%1'.").arg(responseFileName),this);
101 }
102 }
103 } else {
104 LOG(INFO,getLogDomain(),logTr("Timeout/Error while requesting response from '%1'.").arg(mAddressString),this);
105 }
106 } else {
107 LOG(INFO,getLogDomain(),logTr("File '%1' does not exist or is not readable.").arg(requestFileName),this);
108 }
109
110 }
111
112 void COWLlinkAPIReasonerTestfileTester::modifyResponseDomStructure(QDomElement& el, cint64 respTime) {
113 if (el.tagName() == "BooleanResponse") {
114 el.setAttribute("response-time",respTime);
115 } else if (el.tagName() == "ClassHierarchy") {
116 el.setAttribute("response-time",respTime);
117 if (mExpectedResult) {
118 CClassHierarchyResult* respResult = parseClassHierarchyResultRelocatedTOP(&el);
119 if (!mExpectedResult->isResultEquivalentTo(respResult)) {
120 LOG(WARNING,getLogDomain(),logTr("Result doesn't match expected result."),this);
121 el.setAttribute("warning","Result doesn't match expected result.");
122 }
123 }
124 } else {
125 QDomElement child(el.firstChildElement());
126 while (!child.isNull()) {
127 modifyResponseDomStructure(child,respTime);
128 child = child.nextSiblingElement();
129 }
130 }
131 }
132
133
134 bool COWLlinkAPIReasonerTestfileTester::modifyRequestDomStructure(QDomElement& el) {
135 if (el.tagName() == "ExpectedResult") {
136 QDomElement classHirNode = el.firstChildElement("ClassHierarchy");
137 if (!classHirNode.isNull()) {
138 mExpectedResult = parseClassHierarchyResult(&classHirNode);
139 }
140 return true;
141 } else if (mModifyRequestAbsolutePaths && el.tagName() == "OntologyIRI") {
142 QString ontoIRI = el.attribute("IRI");
143 if (ontoIRI.startsWith("file:") && !ontoIRI.startsWith("file://")) {
144 QString relFileString = ontoIRI;
145 relFileString = relFileString.remove(0,5);
146 QDir currFilePath(QDir::currentPath());
147 QString absFileString = currFilePath.absoluteFilePath(relFileString);
148 absFileString = QUrl::fromLocalFile(absFileString).toString();
149 el.setAttribute("IRI",absFileString);
150 LOG(INFO,getLogDomain(),logTr("Changed relative filepath '%1' to absolute filepath '%2'.").arg(ontoIRI).arg(absFileString),this);
151 }
152 }
153
154 QDomElement child(el.firstChildElement());
155 while (!child.isNull()) {
156 if (modifyRequestDomStructure(child)) {
157 el.removeChild(child);
158 }
159 child = child.nextSiblingElement();
160 }
161 return false;
162 }
163
164
165 CClassHierarchyResult* COWLlinkAPIReasonerTestfileTester::parseClassHierarchyResultRelocatedTOP(QDomElement *node) {
166 // rename BOTTOM
167 QDomElement bottomClassNode = node->firstChildElement("ClassSynset");
168 renameAbbreviatedClassSynset(&bottomClassNode,"owl:Nothing","http://www.w3.org/2002/07/owl#Nothing");
169
170 // relocate and rename TOP
171 QDomElement subClassNode = node->firstChildElement("ClassSubClassesPair");
172 while (!subClassNode.isNull()) {
173 QDomElement classSynsetElm(subClassNode.firstChildElement("ClassSynset"));
174 if (hasClassSynsetTOP(&classSynsetElm)) {
175 renameAbbreviatedClassSynset(&classSynsetElm,"owl:Thing","http://www.w3.org/2002/07/owl#Thing");
176 node->removeChild(subClassNode);
177 node->insertAfter(subClassNode,bottomClassNode);
178 break;
179 }
180 subClassNode = subClassNode.nextSiblingElement("ClassSubClassesPair");
181 }
182
183 return parseClassHierarchyResult(node);
184 }
185
186 bool COWLlinkAPIReasonerTestfileTester::hasClassSynsetTOP(QDomElement *node) {
187 QDomElement classNode = node->firstChildElement("owl:Class");
188 while (!classNode.isNull()) {
189 QString className = classNode.attribute("abbreviatedIRI");
190 if (!className.isEmpty() && className == "owl:Thing") {
191 return true;
192 }
193 classNode = classNode.nextSiblingElement("owl:Class");
194 }
195 return false;
196 }
197
198
199 bool COWLlinkAPIReasonerTestfileTester::renameAbbreviatedClassSynset(QDomElement *node, const QString& abbIRI, const QString& replaceIRI) {
200 QDomElement classNode = node->firstChildElement("owl:Class");
201 while (!classNode.isNull()) {
202 QString className = classNode.attribute("abbreviatedIRI");
203 if (!className.isEmpty() && className == abbIRI) {
204 classNode.removeAttribute("abbreviatedIRI");
205 classNode.setAttribute("IRI",replaceIRI);
206 return true;
207 }
208 classNode = classNode.nextSiblingElement("owl:Class");
209 }
210 return false;
211 }
212
213
214
215 CClassHierarchyResult* COWLlinkAPIReasonerTestfileTester::parseClassHierarchyResult(QDomElement *node) {
216 CClassHierarchyResult *classHierResult = 0;
217 QDomElement bottomClassNode = node->firstChildElement("ClassSynset");
218 CClassSynsetResult *bottomClassSynset = parseClassSynsetResult(&bottomClassNode);
219 CClassSynsetResult *topClassSynset = 0;
220 QDomElement subClassNode = node->firstChildElement("ClassSubClassesPair");
221 while (!subClassNode.isNull()) {
222 CClassSynsetResult *superClass = 0;
223 if (!classHierResult) {
224 QDomElement classSynsetElm(subClassNode.firstChildElement("ClassSynset"));
225 topClassSynset = parseClassSynsetResult(&classSynsetElm);
226 classHierResult = new CClassHierarchyResult(bottomClassSynset,topClassSynset);
227 superClass = topClassSynset;
228 } else {
229 QDomElement classSynsetElm(subClassNode.firstChildElement("ClassSynset"));
230 QStringList classNames = parseClassNames(&classSynsetElm);
231 superClass = classHierResult->getClassSynset(classNames);
232 }
233
234 QDomElement subClassSynsetNode = subClassNode.firstChildElement("SubClassSynsets");
235 while (!subClassSynsetNode.isNull()) {
236 QDomElement classSynsetNode = subClassSynsetNode.firstChildElement("ClassSynset");
237 while (!classSynsetNode.isNull()) {
238 QStringList classNames = parseClassNames(&classSynsetNode);
239 CClassSynsetResult *subClass = classHierResult->getClassSynset(classNames);
240 classHierResult->addSubClassRelation(superClass,subClass);
241 classSynsetNode = classSynsetNode.nextSiblingElement("ClassSynset");
242 }
243 subClassSynsetNode = subClassSynsetNode.nextSiblingElement("SubClassSynsets");
244 }
245
246 subClassNode = subClassNode.nextSiblingElement("ClassSubClassesPair");
247 }
248 return classHierResult;
249 }
250
251
252 CClassSynsetResult *COWLlinkAPIReasonerTestfileTester::parseClassSynsetResult(QDomElement *node) {
253 QStringList classNameList = parseClassNames(node);
254 CClassSynsetResult *classSynset = new CClassSynsetResult(classNameList);
255 return classSynset;
256 }
257
258 QStringList COWLlinkAPIReasonerTestfileTester::parseClassNames(QDomElement *node) {
259 QStringList classNameList;
260 bool err = node->isNull();
261
262 QString tagName = node->tagName();
263
264 QDomElement classNode = node->firstChildElement("owl:Class");
265 while (!classNode.isNull()) {
266 QString className = classNode.attribute("IRI");
267 classNameList.append(className);
268 classNode = classNode.nextSiblingElement("owl:Class");
269 }
270 return classNameList;
271 }
272
273 }; // end namespace Test
274
275 }; // end namespace Konclude