Debian Codegraph @

Source/Reasoner/Revision/Persistence/COntologyRevisionDirectoryFilePersistencerBase.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 "COntologyRevisionDirectoryFilePersistencerBase.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Revision {
29
30 namespace Persistence {
31
32 COntologyRevisionDirectoryFilePersistencerBase::COntologyRevisionDirectoryFilePersistencerBase(CConfiguration* config) : CLogIdentifier("::Konclude::Reasoner::Persistence::DirectoryOWL2XMLFilePersistencer", this) {
33 mConfig = config;
34 mOWLNS = "http://www.w3.org/2002/07/owl#";
35 mConfBaseDirectory = CConfigDataReader::readConfigString(mConfig, "Konclude.Persistence.DirectoryFilePersistencer.BaseDirectory","./Data/Ontologies/Persistence/");
36
37 mFileEndingWritingName = ".owl";
38
39 mFileEndingLoadingName = CConfigDataReader::readConfigString(mConfig, "Konclude.Persistence.OntologyRevisionPersistencer.LoadingFileEnding", ".*");
40
41 mKeepingVersionLimit = CConfigDataReader::readConfigInteger(mConfig, "Konclude.Persistence.OntologyRevisionPersistencer.KeepingVersionsLimit", -1);
42 }
43
44 COntologyRevisionDirectoryFilePersistencerBase::~COntologyRevisionDirectoryFilePersistencerBase() {
45 }
46
47
48
49
50 QString COntologyRevisionDirectoryFilePersistencerBase::getNextPersistingOntologyRevisionString(const QString& ontologyName, const QStringList& pathSepStrings, COntologyRevision* ontoRev) {
51 QString lastVersionFileNameString = mOntologyNameLastFileHash.value(ontologyName);
52
53 QString dirName = pathSepStrings.join('/');
54 QString fileNameStart = pathSepStrings.join('_') + ".";
55 QString fileNameEnd = mFileEndingWritingName;
56
57 lastVersionFileNameString.remove(0,fileNameStart.length());
58 lastVersionFileNameString.remove(fileNameEnd);
59 lastVersionFileNameString.remove("R");
60 cint64 lastVersion = lastVersionFileNameString.toLongLong();
61
62 cint64 newVersion = lastVersion+1;
63
64 QString newVersionString = QString("R%1").arg(newVersion);
65 return newVersionString;
66 }
67
68
69
70 bool COntologyRevisionDirectoryFilePersistencerBase::persistOntologyRevision(COntologyRevision* ontoRev, COntologyRevision* prevOntoRev) {
71
72
73 if (mOntologyNameFileHash.contains(prevOntoRev) && ontoRev->getOntology()->getBuildData()->getChangeAxiomList()->size() == prevOntoRev->getOntology()->getBuildData()->getChangeAxiomList()->size()) {
74 COntologyTriplesData* triplesData = ontoRev->getOntology()->getOntologyTriplesData();
75 COntologyTriplesData* prevTriplesData = prevOntoRev->getOntology()->getOntologyTriplesData();
76 if (prevTriplesData && triplesData && triplesData->getLatestTriplesData(false) == prevTriplesData->getLatestTriplesData(false) || !prevTriplesData && !triplesData) {
77 mOntologyNameFileHash.insert(ontoRev, mOntologyNameFileHash.value(prevOntoRev));
78 return true;
79 }
80 }
81
82 CConcreteOntology* ontology = ontoRev->getOntology();
83 QString ontName = ontology->getOntologyName();
84 cint64 ontoVers = ontoRev->getOntologyRevisionDepth();
85
86 QStringList pathSepStrings = getOntologyFileNameParts(ontName);
87
88 QString dirName = pathSepStrings.join('/');
89 QString revName = getNextPersistingOntologyRevisionString(ontName, pathSepStrings, ontoRev);
90 QString fileName = pathSepStrings.join('_') + "." + revName;
91 QString owlFileName = fileName + mFileEndingWritingName;
92 QString tmpFileName = fileName + "-tmp-XXXXXX";
93
94 if (QFile::exists(QString("%1%2/%3").arg(mConfBaseDirectory).arg(dirName).arg(owlFileName))) {
95 return false;
96 }
97
98
99 bool successfullyWritten = false;
100 QDir baseDir(mConfBaseDirectory);
101 baseDir.mkpath(dirName);
102 QTemporaryFile tmpFile(QString("%1%2/%3").arg(mConfBaseDirectory).arg(dirName).arg(tmpFileName));
103 tmpFile.setAutoRemove(false);
104 if (tmpFile.open()) {
105 LOG(INFO, getLogDomain(), logTr("Trying saving new revision %3 of ontology '%1' to file '%2'").arg(ontName).arg(owlFileName).arg(ontoVers), this);
106 bool successfulWritten = writeOntologyToFile(&tmpFile, ontoRev);
107 tmpFile.close();
108 if (successfulWritten) {
109 LOG(INFO, getLogDomain(), logTr("Saved new revision %3 of ontology '%1' to file '%2'").arg(ontName).arg(owlFileName).arg(ontoVers), this);
110 successfullyWritten = true;
111 } else {
112 LOG(ERROR, getLogDomain(), logTr("Failed to save new revision %3 of ontology '%1' to file '%2'").arg(ontName).arg(owlFileName).arg(ontoVers), this);
113 }
114 }
115
116 if (successfullyWritten) {
117 successfullyWritten = tmpFile.rename(QString("%1%2/%3").arg(mConfBaseDirectory).arg(dirName).arg(owlFileName));
118 if (!successfullyWritten) {
119 tmpFile.remove();
120 } else {
121 mOntologyNameFileHash.insert(ontoRev, owlFileName);
122 mOntologyNameLastFileHash.insert(ontName, owlFileName);
123 }
124 deletePersistingLimitOntologyVersions(ontName);
125 }
126
127
128 return successfullyWritten;
129 }
130
131
132
133
134
135
136 bool COntologyRevisionDirectoryFilePersistencerBase::hasOntologyPersisted(const QString& ontologyName) {
137 QStringList pathSepStrings = getOntologyFileNameParts(ontologyName);
138 QString dirName = pathSepStrings.join('/');
139 QString fileName = pathSepStrings.join('_');
140 QString owlFileNameFilter = fileName + "*" + mFileEndingLoadingName;
141
142
143 QStringList nameFilter(owlFileNameFilter);
144 QDir directory(mConfBaseDirectory + dirName);
145 if (!directory.exists()) {
146 return false;
147 }
148 QStringList ontoFiles = directory.entryList(nameFilter, QDir::Files);
149 if (ontoFiles.isEmpty()) {
150 return false;
151 }
152 return true;
153 }
154
155
156
157 bool COntologyRevisionDirectoryFilePersistencerBase::deletePersistedOntology(const QString& ontologyName) {
158 QStringList pathSepStrings = getOntologyFileNameParts(ontologyName);
159 QString dirName = pathSepStrings.join('/');
160 QString fileName = pathSepStrings.join('_');
161 QString owlFileNameFilter = fileName + "*" + mFileEndingLoadingName;
162
163
164 QStringList nameFilter(owlFileNameFilter);
165 QDir directory(mConfBaseDirectory + dirName);
166 if (directory.exists()) {
167 return false;
168 }
169 QStringList ontoFiles = directory.entryList(nameFilter, QDir::Files);
170 if (ontoFiles.isEmpty()) {
171 return false;
172 }
173 for (QString ontoFile : ontoFiles) {
174 QFile file(mConfBaseDirectory + dirName + "/" + ontoFile);
175 file.remove();
176 }
177
178 return true;
179 }
180
181
182
183
184
185 cint64 isOntologyFileNameNewer(const QString& currentOntologyName, const QString& newOntologyName) {
186 if (currentOntologyName.length() > newOntologyName.length()) {
187 return -1;
188 }
189 if (currentOntologyName.length() < newOntologyName.length()) {
190 return 1;
191 }
192 int comp = QString::compare(currentOntologyName, newOntologyName, Qt::CaseInsensitive);
193 return comp;
194 }
195
196
197 bool COntologyRevisionDirectoryFilePersistencerBase::hasNewOntologyRevision(const QString& ontologyName, COntologyRevision* ontoRev) {
198
199 QString lastVersionFileNameString = mOntologyNameFileHash.value(ontoRev);
200 QString lastFileNameString = mOntologyNameLastFileHash.value(ontologyName);
201 if (lastVersionFileNameString != lastFileNameString) {
202 return true;
203 }
204
205 QStringList pathSepStrings = getOntologyFileNameParts(ontologyName);
206
207
208 QString dirName = pathSepStrings.join('/');
209 QString fileName = pathSepStrings.join('_');
210 QString owlFileNameFilter = fileName + "*" + mFileEndingLoadingName;
211
212
213 QStringList nameFilter(owlFileNameFilter);
214 QDir directory(mConfBaseDirectory+dirName);
215 QStringList ontoFiles = directory.entryList(nameFilter,QDir::Files);
216
217
218 QString newFileNameString = lastFileNameString;
219 for (const QString& fileName : ontoFiles) {
220 if (isOntologyFileNameNewer(lastFileNameString,fileName) > 0) {
221 newFileNameString = fileName;
222 }
223 }
224 if (newFileNameString != lastFileNameString) {
225 mOntologyNameLastFileHash.insert(ontologyName,newFileNameString);
226 return true;
227 }
228 return false;
229 }
230
231
232
233
234
235
236 QStringList COntologyRevisionDirectoryFilePersistencerBase::getPersistedOntologyVersionFilesSortedLatest(const QString& ontologyName) {
237 QStringList pathSepStrings = getOntologyFileNameParts(ontologyName);
238
239
240 QString dirName = pathSepStrings.join('/');
241 QString fileName = pathSepStrings.join('_');
242 QString owlFileNameFilter = fileName + "*" + mFileEndingLoadingName;
243
244
245 QStringList nameFilter(owlFileNameFilter);
246 QDir directory(mConfBaseDirectory + dirName);
247 QStringList ontoVersionFiles = directory.entryList(nameFilter, QDir::Files);
248
249 qSort(ontoVersionFiles.begin(), ontoVersionFiles.end(), isOntologyFileNameNewer);
250 return ontoVersionFiles;
251 }
252
253
254 bool COntologyRevisionDirectoryFilePersistencerBase::deletePersistingLimitOntologyVersions(const QString& ontologyName) {
255
256 if (mKeepingVersionLimit > 0) {
257 QStringList ontoVersionFiles = getPersistedOntologyVersionFilesSortedLatest(ontologyName);
258 if (ontoVersionFiles.size() > mKeepingVersionLimit) {
259 QStringList pathSepStrings = getOntologyFileNameParts(ontologyName);
260
261 QString dirName = pathSepStrings.join('/');
262 QString fileName = pathSepStrings.join('_');
263
264 QStringList::const_iterator ontoVersionFilesIterator = ontoVersionFiles.constBegin();
265 for (cint64 versionNumber = 0; versionNumber < ontoVersionFiles.size(); ++versionNumber) {
266 if (versionNumber >= mKeepingVersionLimit) {
267 QString ontoFile = *ontoVersionFilesIterator;
268 QFile file(mConfBaseDirectory + dirName + "/" + ontoFile);
269 file.remove();
270 }
271 ++ontoVersionFilesIterator;
272 }
273 }
274 }
275 return false;
276 }
277
278
279
280 bool COntologyRevisionDirectoryFilePersistencerBase::loadLatestOntologyRevision(const QString& ontologyName, COntologyRevision* ontoRev) {
281
282
283 if (hasNewOntologyRevision(ontologyName,ontoRev)) {
284
285 QStringList pathSepStrings = getOntologyFileNameParts(ontologyName);
286 QString dirName = pathSepStrings.join('/');
287
288 QString lastVersionFileNameString = mOntologyNameLastFileHash.value(ontologyName);
289
290 CConcreteOntologyUpdateCollectorBuilder *builder = new CConcreteOntologyUpdateCollectorBuilder(ontoRev->getOntology());
291 builder->initializeBuilding();
292
293 bool parsingSucceeded = false;
294
295 QString fileNmae = mConfBaseDirectory + dirName + "/" + lastVersionFileNameString;
296 QFile ontoVersionFile(fileNmae);
297
298 LOG(INFO, getLogDomain(), logTr("Trying loading last revision of ontology '%1' from file '%2'").arg(ontologyName).arg(fileNmae), this);
299 if (ontoVersionFile.open(QIODevice::ReadOnly)) {
300 ontoVersionFile.close();
301
302 parsingSucceeded = loadOntologyFromFile(ontologyName, fileNmae, &ontoVersionFile, ontoRev, builder);
303 if (parsingSucceeded) {
304 builder->completeBuilding();
305 mOntologyNameFileHash.insert(ontoRev, lastVersionFileNameString);
306 } else {
307 LOG(ERROR, getLogDomain(), logTr("Parsing of file '%1' failed.").arg(lastVersionFileNameString), this);
308 }
309 } else {
310 LOG(ERROR, getLogDomain(), logTr("File '%1' cannot be opened.").arg(lastVersionFileNameString), this);
311 }
312
313 delete builder;
314
315
316 return parsingSucceeded;
317 } else {
318 return false;
319 }
320 }
321
322
323 QStringList COntologyRevisionDirectoryFilePersistencerBase::getOntologyFileNameParts(const QString& ontologyName) {
324 QStringList pathSepStrings;
325 QString currPartString;
326 for (cint64 i = 0; i < ontologyName.length(); ++i) {
327 QChar partChar = ontologyName.at(i);
328 bool separator = false;
329 if (partChar == ':' || partChar == '/' || partChar == '\\' || partChar == '?' || partChar == '*' || partChar == '%' || partChar == '$' || partChar == '<' || partChar == '>' || partChar == '|' || partChar == '\"') {
330 separator = true;
331 }
332 if (separator) {
333 if (!currPartString.isEmpty()) {
334 pathSepStrings += currPartString;
335 }
336 currPartString = QString();
337 } else {
338 currPartString += partChar;
339 }
340 }
341 if (!currPartString.isEmpty()) {
342 pathSepStrings += currPartString;
343 }
344 return pathSepStrings;
345 }
346
347
348
349 bool COntologyRevisionDirectoryFilePersistencerBase::loadOntologyFromFile(const QString& ontologyString, const QString& fileNameString, QFile* file, COntologyRevision* ontoRev, CConcreteOntologyUpdateCollectorBuilder *builder) {
350 COntologyMultiAutoParsingLoader loader;
351 QList<QString> importOntologiesList;
352 QStringList parserErrorList;
353 CCommandRecordRouter commandRecordRouter(nullptr, this);
354 bool loaded = loader.parseOntology(file, ontologyString, fileNameString, ontoRev->getOntology(), builder, ontoRev->getOntologyConfiguration(), importOntologiesList, parserErrorList, commandRecordRouter);
355 return loaded;
356 }
357
358
359 }; // end namespace Persistence
360
361 }; // end namespace Revision
362
363 }; // end namespace Reasoner
364
365 }; // end namespace Konclude