Debian Codegraph @

Source/Parser/CSPARQLSimpleUpdateParser.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 "CSPARQLSimpleUpdateParser.h"
22
23
24 namespace Konclude {
25
26 namespace Parser {
27
28
29
30 CSPARQLSimpleUpdateParser::CSPARQLSimpleUpdateParser(COntologyBuilder* ontoBuilder, CConcreteOntology* ontology) : CSPARQLSimpleBuildingParser(ontoBuilder,ontology) {
31 mKeywordSet.insert("INSERT");
32 mKeywordSet.insert("DELETE");
33 }
34
35
36 CSPARQLSimpleUpdateParser::~CSPARQLSimpleUpdateParser() {
37 }
38
39
40
41 bool CSPARQLSimpleUpdateParser::parseUpdateFile(const QString& filename) {
42 bool successfullParsed = true;
43
44 // parse file
45 QFile file(filename);
46 if (file.open(QIODevice::ReadOnly)) {
47
48 QStringList partStringList;
49 while (!file.atEnd()) {
50 QString line(file.readLine());
51 cint64 linePos = 0;
52 while (linePos != line.length()) {
53 QString partString = getNextPart(line, linePos);
54 if (!partString.isEmpty()) {
55 if (isKeyword(partString) && !partStringList.isEmpty()) {
56 processParts(partStringList);
57 partStringList.clear();
58 }
59 partStringList.append(partString);
60 }
61 }
62 }
63 if (!partStringList.isEmpty()) {
64 processParts(partStringList);
65 }
66
67 file.close();
68
69 } else {
70 successfullParsed = false;
71 LOG(ERROR,"::Konclude::Parser::SPARQLSimpleUpdateParser",logTr("File '%1' not found or couldn't be opened.").arg(filename),this);
72 }
73
74 return successfullParsed;
75 }
76
77
78 bool CSPARQLSimpleUpdateParser::parseUpdateText(const QString& filetext) {
79 QStringList partStringList;
80 QString line(filetext);
81 cint64 linePos = 0;
82 while (linePos != line.length()) {
83 QString partString = getNextPart(line, linePos);
84 if (!partString.isEmpty()) {
85 if (isKeyword(partString) && !partStringList.isEmpty()) {
86 processParts(partStringList);
87 partStringList.clear();
88 }
89 partStringList.append(partString);
90 }
91 }
92 if (!partStringList.isEmpty()) {
93 processParts(partStringList);
94 }
95 return true;
96 }
97
98
99 bool CSPARQLSimpleUpdateParser::parseUpdateTextList(const QStringList& queryTextList) {
100 QStringList partStringList;
101 QStringList tmpQueryTextList = queryTextList;
102 cint64 linePos = 0;
103 while (!tmpQueryTextList.isEmpty()) {
104 QString partString = tmpQueryTextList.takeFirst();
105 if (!partString.isEmpty()) {
106 if (isKeyword(partString) && !partStringList.isEmpty()) {
107 processParts(partStringList);
108 partStringList.clear();
109 }
110 partStringList.append(partString);
111 }
112 }
113 if (!partStringList.isEmpty()) {
114 processParts(partStringList);
115 }
116 return true;
117 }
118
119
120
121
122 bool CSPARQLSimpleUpdateParser::processParts(QStringList& parsedParts) {
123 if (parsedParts.size() > 0) {
124 QString keyword = parsedParts.takeFirst().toUpper();
125 if (keyword == "BASE" || keyword == "@BASE") {
126 mBaseIRI = "";
127 if (parsedParts.size() >= 1) {
128 mBaseIRI = getIRI(parsedParts.takeFirst());
129 }
130 } else if (keyword == "PREFIX" || keyword == "@PREFIX") {
131 if (parsedParts.size() >= 2) {
132 QString prefix = parsedParts.takeFirst();
133 while (prefix.endsWith(":")) {
134 prefix = prefix.mid(0, prefix.length() - 1);
135 }
136 QString iri = getIRI(parsedParts.takeFirst());
137 mPrefixHash.insert(prefix, iri);
138 }
139 } else if (keyword == "INSERT" || keyword == "DELETE") {
140
141 bool insertAxioms = keyword == "INSERT";
142 bool deleteAxioms = keyword == "DELETE";
143
144 QStringList dataClauseStringList = getBracketContent(parsedParts, '{', '}', false);
145
146 for (QStringList::iterator it = dataClauseStringList.begin(), itEnd = dataClauseStringList.end(); it != itEnd;) {
147 const QString& graphPartString(*it);
148 if (graphPartString.toUpper() == "GRAPH") {
149 it = dataClauseStringList.erase(it);
150 const QString& namedPartString(*it);
151 if (namedPartString.toUpper() == "NAMED") {
152 it = dataClauseStringList.erase(it);
153 }
154 it = dataClauseStringList.erase(it);
155 } else {
156 break;
157 }
158 }
159
160 QHash<QString, CRDFHahsedSubjectData>* subjectTripleHash = getRDFSubjectHash(dataClauseStringList);
161 QList<CAxiomExpression*> axiomList = getTripleOWLAxioms(subjectTripleHash, false);
162 delete subjectTripleHash;
163 for (CAxiomExpression* axiom : axiomList) {
164 if (insertAxioms) {
165 mOntoBuilder->tellOntologyAxiom(axiom);
166 } else {
167 mOntoBuilder->retractOntologyAxiom(axiom);
168 }
169 }
170
171 }
172 }
173 return true;
174 }
175
176
177
178
179 }; // end namespace Parser
180
181 }; // end namespace Konclude