Debian Codegraph @

Source/Test/COWLFileAxiomRemover.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 "COWLFileAxiomRemover.h"
22
23
24 namespace Konclude {
25
26 namespace Test {
27
28
29
30 COWLFileAxiomRemover::COWLFileAxiomRemover(CConfiguration *config)
31 : CLogIdentifier("::Konclude::Test::OWLFileAxiomRemover",this),mConfig(config) {
32
33
34 mConfReplaceDatatypeExpressions = false;
35
36 QList<QString> removeTagList;
37 if (mConfReplaceDatatypeExpressions) {
38 removeTagList.append("DataProperty");
39 removeTagList.append("SubDataPropertyOf");
40 removeTagList.append("EquivalentDataProperties");
41 removeTagList.append("DisjointDataProperties");
42 removeTagList.append("DataPropertyDomain");
43 removeTagList.append("DataPropertyRange");
44 removeTagList.append("FunctionalDataProperty");
45 removeTagList.append("DataAllValuesFrom");
46 removeTagList.append("DataMaxCardinality");
47 removeTagList.append("DataPropertyAssertion");
48 removeTagList.append("DatatypeDefinition");
49 removeTagList.append("DatatypeRestriction");
50 removeTagList.append("Datatype");
51 removeTagList.append("HasKey");
52 removeTagList.append("DataSomeValuesFrom");
53 removeTagList.append("DataMinCardinality");
54 removeTagList.append("DataExactCardinality");
55 removeTagList.append("DataHasValue");
56 removeTagList.append("NegativeDataPropertyAssertion");
57 }
58
59
60
61 foreach (QString removeTagString, removeTagList) {
62 mRemoveTagNameSet.insert(removeTagString);
63 mRemoveTagNameSet.insert(QString("owl:")+removeTagString);
64 }
65
66 QList<QString> alwRemoveTagList;
67 alwRemoveTagList.append("AnnotationAssertion");
68 alwRemoveTagList.append("SubAnnotationPropertyOf");
69 alwRemoveTagList.append("AnnotationPropertyDomain");
70 alwRemoveTagList.append("AnnotationPropertyRange");
71 alwRemoveTagList.append("Annotation");
72 alwRemoveTagList.append("AnnotationProperty");
73 alwRemoveTagList.append("Import");
74
75 foreach (QString removeTagString, alwRemoveTagList) {
76 mRemoveTagNameSet.insert(removeTagString);
77 mRemoveTagNameSet.insert(QString("owl:")+removeTagString);
78
79 mAlwaysRemoveTagNameSet.insert(removeTagString);
80 mAlwaysRemoveTagNameSet.insert(QString("owl:")+removeTagString);
81 }
82
83 QList<QString> removeAttributeList;
84 removeAttributeList.append("versionIRI");
85
86 foreach (QString removeAttributeString, removeAttributeList) {
87 mRemoveAttributeNameSet.insert(removeAttributeString);
88 }
89
90
91 }
92
93
94 COWLFileAxiomRemover::~COWLFileAxiomRemover() {
95 }
96
97
98
99
100 bool COWLFileAxiomRemover::loadRemoveAxiomsAndSave(const QString& requestFileName, const QString& responseFileName) {
101 mStatRemovedAxioms = 0;
102 mStatReplacedExpressions = 0;
103
104 QFile file(requestFileName);
105 if (file.open(QIODevice::ReadOnly)) {
106 QDomDocument document;
107 document.setContent(&file,false);
108 file.close();
109 QDomElement rootEl = document.documentElement();
110
111 removeElements(rootEl,true,document);
112 removeAttributes(rootEl,document);
113
114 QFile wFile(responseFileName);
115 if (wFile.open(QIODevice::WriteOnly)) {
116 wFile.write(document.toByteArray());
117 wFile.close();
118 }
119 LOG(INFO,getLogDomain(),logTr("Removed %1 axioms and replaced %2 expressions with TOP in '%3'.").arg(mStatRemovedAxioms).arg(mStatReplacedExpressions).arg(requestFileName),this);
120 return true;
121
122 } else {
123 LOG(INFO,getLogDomain(),logTr("File '%1' does not exist or is not readable.").arg(requestFileName),this);
124 }
125 return false;
126 }
127
128 void COWLFileAxiomRemover::removeAttributes(QDomElement& domElement, QDomDocument& document) {
129 foreach (QString attString, mRemoveAttributeNameSet) {
130 domElement.removeAttribute(attString);
131 }
132 QDomElement nextEl = domElement.firstChildElement();
133 while (!nextEl.isNull()) {
134 removeAttributes(nextEl,document);
135 nextEl = nextEl.nextSiblingElement();
136 }
137 }
138
139 bool COWLFileAxiomRemover::removeElements(QDomElement& domElement, bool axiomLevel, QDomDocument& document) {
140 bool modified = false;
141 QDomElement nextEl = domElement.firstChildElement();
142 while (!nextEl.isNull()) {
143 QDomElement tmpElNode = nextEl;
144 nextEl = nextEl.nextSiblingElement();
145
146 QString nodeName = tmpElNode.nodeName();
147 if (mRemoveTagNameSet.contains(nodeName)) {
148 if (axiomLevel) {
149 domElement.removeChild(tmpElNode);
150 ++mStatRemovedAxioms;
151 } else if (mAlwaysRemoveTagNameSet.contains(nodeName)) {
152 domElement.removeChild(tmpElNode);
153 ++mStatRemovedAxioms;
154 } else {
155 // replace with top element
156 QDomElement topElement = document.createElement("Class");
157 topElement.setAttribute("IRI","http://www.w3.org/2002/07/owl#Thing");
158 domElement.removeChild(tmpElNode);
159 domElement.appendChild(topElement);
160 ++mStatReplacedExpressions;
161 }
162 modified = true;
163 } else {
164
165 if (nodeName == "Declaration" || nodeName == "owl:Declaration") {
166 removeElements(tmpElNode,true,document);
167 if (tmpElNode.childNodes().isEmpty()) {
168 // remove this node too
169 domElement.removeChild(tmpElNode);
170 ++mStatRemovedAxioms;
171 }
172 }
173
174 modified |= removeElements(tmpElNode,false,document);
175 }
176 }
177 return modified;
178 }
179
180
181 }; // end namespace Test
182
183 }; // end namespace Konclude