Debian Codegraph @

Source/Parser/CSPARQLSimpleParser.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 "CSPARQLSimpleParser.h"
22
23
24 namespace Konclude {
25
26 namespace Parser {
27
28
29
30 CSPARQLSimpleParser::CSPARQLSimpleParser() {
31
32 mKeywordSet.insert("BASE");
33 mKeywordSet.insert("@BASE");
34 mKeywordSet.insert("PREFIX");
35 mKeywordSet.insert("@PREFIX");
36 mKeywordSet.insert("SELECT");
37 mKeywordSet.insert("CONSTRUCT");
38 mKeywordSet.insert("DESCRIBE");
39 mKeywordSet.insert("ASK");
40
41 }
42
43
44 CSPARQLSimpleParser::~CSPARQLSimpleParser() {
45 }
46
47
48
49 bool CSPARQLSimpleParser::isKeyword(const QString& part) {
50 return mKeywordSet.contains(part.toUpper());
51 }
52
53
54
55 bool CSPARQLSimpleParser::getNextRDFStringTriple(QStringList& partList, CRDFStringTriple& lastNextTriple, QList<CRDFStringTriple>& nextTripleList, QList<QStringList>& filterList, int startingSopPos) {
56 int sopPos = startingSopPos;
57 while (!partList.isEmpty()) {
58 if (partList.first() == "." && !nextTripleList.isEmpty()) {
59 lastNextTriple = nextTripleList.takeFirst();
60 return true;
61 }
62 QString part = partList.takeFirst();
63 bool isTriplePart = false;
64 if (part == ".") {
65 sopPos = 0;
66 } else if (part == ";") {
67 sopPos = 1;
68 } else if (part == ",") {
69 sopPos = 2;
70 } else if (part.startsWith("?") || part.startsWith("$")) {
71 isTriplePart = true;
72 } else if (part.toUpper().startsWith("FILTER")) {
73 QStringList filterParts = getBracketContent(partList, '(', ')', false);
74 filterList.append(filterParts);
75 sopPos = 0;
76 } else if (part == "{") {
77 getBracketContent(partList, '{', '}', true);
78 sopPos = 0;
79 } else if (part == "[") {
80 part = getNextAnonymousNode();
81 QStringList anonymousNodeTripleParts = getBracketContent(partList, '[', ']', true);
82 QList<CRDFStringTriple> tmpNextTripleList;
83 CRDFStringTriple lastTriple;
84 lastTriple.string[0] = part;
85 while (getNextRDFStringTriple(anonymousNodeTripleParts, lastTriple, tmpNextTripleList, filterList, 1)) {
86 nextTripleList.append(lastTriple);
87 }
88 isTriplePart = true;
89 } else if (part == "_:") {
90 part = getBlankNode(part);
91 isTriplePart = true;
92 } else {
93 part = getIRI(part);
94 isTriplePart = true;
95 }
96 if (isTriplePart) {
97 lastNextTriple.string[sopPos] = part;
98 ++sopPos;
99 if (sopPos >= 3) {
100 return true;
101 }
102 }
103 }
104 if (!nextTripleList.isEmpty()) {
105 lastNextTriple = nextTripleList.takeFirst();
106 return true;
107 }
108 return false;
109
110 }
111
112 QHash<QString, CRDFHahsedSubjectData>* CSPARQLSimpleParser::getRDFSubjectHash(QStringList& partList) {
113 return CRDFSimpleTurtleParser::getRDFSubjectHash(partList);
114 }
115
116
117 QHash<QString, CRDFHahsedSubjectData>* CSPARQLSimpleParser::getRDFSubjectHash(QStringList& partList, QList<QStringList>& filterList) {
118 QHash<QString, CRDFHahsedSubjectData>* subjectHash = new QHash<QString, CRDFHahsedSubjectData>();
119 QList<CRDFStringTriple> nextTripleList;
120
121 CRDFStringTriple lastTriple;
122 while (getNextRDFStringTriple(partList, lastTriple, nextTripleList, filterList)) {
123 subjectHash->insertMulti(lastTriple.string[0], CRDFHahsedSubjectData(lastTriple));
124 }
125
126 return subjectHash;
127 }
128
129
130
131 QList<CRDFStringTriple>* CSPARQLSimpleParser::getRDFStringTriples(QStringList& partList, QList<QStringList>& filterList) {
132 QList<CRDFStringTriple>* tripleList = new QList<CRDFStringTriple>();
133 QList<CRDFStringTriple> nextTripleList;
134
135 CRDFStringTriple lastTriple;
136 while (getNextRDFStringTriple(partList, lastTriple, nextTripleList, filterList)) {
137 tripleList->append(lastTriple);
138 }
139
140 return tripleList;
141 }
142
143
144
145
146
147 }; // end namespace Parser
148
149 }; // end namespace Konclude