Debian Codegraph @

Source/Parser/CSPARQLSimpleBuildingParser.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 "CSPARQLSimpleBuildingParser.h"
22
23
24 namespace Konclude {
25
26 namespace Parser {
27
28
29
30 CSPARQLSimpleBuildingParser::CSPARQLSimpleBuildingParser(COntologyBuilder* ontoBuilder, CConcreteOntology* ontology) {
31 mOntoBuilder = ontoBuilder;
32 mOntology = ontology;
33
34 }
35
36
37 CSPARQLSimpleBuildingParser::~CSPARQLSimpleBuildingParser() {
38 }
39
40
41
42 QList<CAxiomExpression*> CSPARQLSimpleBuildingParser::getTripleOWLAxioms(QHash<QString, CRDFHahsedSubjectData>* subjectTripleHash, bool anonymousIndividualsAsVariables) {
43 QList<CAxiomExpression*> axiomList;
44 for (QHash<QString, CRDFHahsedSubjectData>::const_iterator it = subjectTripleHash->begin(), itEnd = subjectTripleHash->end(); it != itEnd;) {
45 QString subjectString(it.key());
46 const CRDFHahsedSubjectData& predObjData = it.value();
47 QList<CRDFHahsedSubjectData> predObjDataList;
48 predObjDataList.append(predObjData);
49
50 ++it;
51 if (it != itEnd) {
52 QString nextSubject(it.key());
53 while (nextSubject == subjectString && it != itEnd) {
54 const CRDFHahsedSubjectData& predObjData = it.value();
55 predObjDataList.append(predObjData);
56 ++it;
57 if (it != itEnd) {
58 nextSubject = it.key();
59 }
60 }
61 }
62
63 CBuildExpression::ExpressionType subjectType;
64 bool hasType = false;
65 for (QList<CRDFHahsedSubjectData>::iterator it = predObjDataList.begin(), itEnd = predObjDataList.end(); it != itEnd && !hasType; ++it) {
66 const CRDFHahsedSubjectData& predObjData = *it;
67
68 const QString& predicateString = predObjData.string[0];
69 const QString& objectString = predObjData.string[1];
70
71 if (predicateString == "a" || predicateString == PREFIX_RDF_TYPE) {
72 if (objectString == PREFIX_OWL_NAMED_INDIVIDUAL) {
73 subjectType = CBuildExpression::BETNAMEDINDIVIDUAL;
74 hasType = true;
75 predObjDataList.erase(it);
76 } else if (objectString == PREFIX_OWL_CLASS || objectString == "http://www.w3.org/1999/02/22-rdf-syntax-ns#Class") {
77 subjectType = CBuildExpression::BETCLASS;
78 hasType = true;
79 predObjDataList.erase(it);
80 } else if (objectString == PREFIX_OWL_OBJECT_PROPERTY) {
81 subjectType = CBuildExpression::BETOBJECTPROPERTY;
82 hasType = true;
83 predObjDataList.erase(it);
84 } else if (objectString == PREFIX_OWL_DATATYPE_PROPERTY) {
85 subjectType = CBuildExpression::BETDATAPROPERTY;
86 hasType = true;
87 predObjDataList.erase(it);
88 }
89 }
90 }
91
92
93 for (QList<CRDFHahsedSubjectData>::iterator it = predObjDataList.begin(), itEnd = predObjDataList.end(); it != itEnd; ++it) {
94 const CRDFHahsedSubjectData& predObjData = *it;
95
96 const QString& predicateString = predObjData.string[0];
97 const QString& objectString = predObjData.string[1];
98
99 if (!hasType || subjectType == CBuildExpression::BETNAMEDINDIVIDUAL) {
100
101 CIndividualTermExpression* indiExpression = getIndividualTermExpression(subjectString, anonymousIndividualsAsVariables);
102
103 if (predicateString == "a" || predicateString == PREFIX_RDF_TYPE) {
104 CClassTermExpression* classTermExp = getClassTermExpression(objectString);
105 CAxiomExpression* assExp = mOntoBuilder->getClassAssertion(indiExpression, classTermExp);
106 axiomList.append(assExp);
107 } else if (predicateString == PREFIX_RDFS_SUBCLASS_OF) {
108 CClassTermExpression* class1Expression = getClassTermExpression(subjectString);
109 CClassTermExpression* class2Expression = getClassTermExpression(objectString);
110 CAxiomExpression* assExp = mOntoBuilder->getSubClassOf(class1Expression, class2Expression);
111 axiomList.append(assExp);
112 } else if (predicateString == PREFIX_RDFS_SUBPROPERTY_OF) {
113 bool dataProps = subjectType == CBuildExpression::BETDATAPROPERTY || isDataProperty(subjectString) || isDataProperty(objectString);
114 if (dataProps) {
115 CDataPropertyTermExpression* dataProp1Expression = getDataPropertyTermExpression(subjectString);
116 CDataPropertyTermExpression* dataProp2Expression = getDataPropertyTermExpression(objectString);
117 CAxiomExpression* assExp = mOntoBuilder->getSubDataPropertyOf(dataProp1Expression, dataProp2Expression);
118 axiomList.append(assExp);
119 } else {
120 CObjectPropertyTermExpression* objProp1Expression = getObjectPropertyTermExpression(subjectString);
121 CObjectPropertyTermExpression* objProp2Expression = getObjectPropertyTermExpression(objectString);
122 CAxiomExpression* assExp = mOntoBuilder->getSubObjectPropertyOf((CBuildExpression*)objProp1Expression, (CBuildExpression*)objProp2Expression);
123 axiomList.append(assExp);
124 }
125 } else {
126 if (!isDataProperty(predicateString)) {
127 CIndividualTermExpression* otherIndiExpression = getIndividualTermExpression(objectString, anonymousIndividualsAsVariables);
128 CObjectPropertyTermExpression* propExp = getObjectPropertyTermExpression(predicateString);
129 CObjectPropertyAssertionExpression* propAss = mOntoBuilder->getObjectPropertyAssertion(indiExpression, propExp, otherIndiExpression);
130 axiomList.append(propAss);
131 } else {
132 CDataLiteralTermExpression* dataRangeExpr = getDataLiteralTermExpression(objectString);
133 CDataPropertyExpression* propExp = mOntoBuilder->getDataProberty(predicateString);
134 CDataPropertyAssertionExpression* propAss = mOntoBuilder->getDataPropertyAssertion(indiExpression, propExp, dataRangeExpr);
135 axiomList.append(propAss);
136 }
137 }
138 }
139 }
140
141 }
142 return axiomList;
143 }
144
145
146
147 QString CSPARQLSimpleBuildingParser::getVariableName(const QString& variableName) {
148 return variableName.mid(1);
149 }
150
151
152 QList<CAxiomExpression*> CSPARQLSimpleBuildingParser::getTripleOWLAxioms(QList<CRDFStringTriple>* tripleStringList, bool anonymousIndividualsAsVariables, QSet<CExpressionVariable*>* varExpSet) {
153 QList<CAxiomExpression*> axiomList;
154 for (auto triple : *tripleStringList) {
155
156 QString subjectString = triple.string[0];
157
158 QString predicateString = triple.string[1];
159 QString objectString = triple.string[2];
160
161 if (predicateString == "a" || predicateString == PREFIX_RDF_TYPE) {
162 CIndividualTermExpression* indiExpression = getIndividualTermExpression(subjectString, anonymousIndividualsAsVariables, varExpSet);
163 CClassTermExpression* classTermExp = getClassTermExpression(objectString, varExpSet);
164 CAxiomExpression* assExp = mOntoBuilder->getClassAssertion(indiExpression, classTermExp);
165 axiomList.append(assExp);
166 } else if (predicateString == PREFIX_RDFS_SUBCLASS_OF) {
167 CClassTermExpression* class1Expression = getClassTermExpression(subjectString, varExpSet);
168 CClassTermExpression* class2Expression = getClassTermExpression(objectString, varExpSet);
169 CAxiomExpression* assExp = mOntoBuilder->getSubClassOf(class1Expression, class2Expression);
170 axiomList.append(assExp);
171 } else if (predicateString == PREFIX_RDFS_SUBPROPERTY_OF) {
172 bool dataProps = isDataProperty(subjectString) || isDataProperty(objectString);
173 if (dataProps) {
174 CDataPropertyTermExpression* dataProp1Expression = getDataPropertyTermExpression(subjectString, varExpSet);
175 CDataPropertyTermExpression* dataProp2Expression = getDataPropertyTermExpression(objectString, varExpSet);
176 CAxiomExpression* assExp = mOntoBuilder->getSubDataPropertyOf(dataProp1Expression, dataProp2Expression);
177 axiomList.append(assExp);
178 } else {
179 CObjectPropertyTermExpression* objProp1Expression = getObjectPropertyTermExpression(subjectString, varExpSet);
180 CObjectPropertyTermExpression* objProp2Expression = getObjectPropertyTermExpression(objectString, varExpSet);
181 CAxiomExpression* assExp = mOntoBuilder->getSubObjectPropertyOf((CBuildExpression*)objProp1Expression, (CBuildExpression*)objProp2Expression);
182 axiomList.append(assExp);
183 }
184 } else {
185 CIndividualTermExpression* indiExpression = getIndividualTermExpression(subjectString, anonymousIndividualsAsVariables, varExpSet);
186 if (!isDataProperty(predicateString)) {
187 CIndividualTermExpression* otherIndiExpression = getIndividualTermExpression(objectString, anonymousIndividualsAsVariables, varExpSet);
188 CObjectPropertyTermExpression* propExp = getObjectPropertyTermExpression(predicateString, varExpSet);
189 CObjectPropertyAssertionExpression* propAss = mOntoBuilder->getObjectPropertyAssertion(indiExpression, propExp, otherIndiExpression);
190 axiomList.append(propAss);
191 } else {
192 CDataLiteralTermExpression* dataRangeExpr = getDataLiteralTermExpression(objectString, varExpSet);
193
194 CDataPropertyExpression* propExp = mOntoBuilder->getDataProberty(predicateString);
195 CDataPropertyAssertionExpression* propAss = mOntoBuilder->getDataPropertyAssertion(indiExpression, propExp, dataRangeExpr);
196 axiomList.append(propAss);
197 }
198 }
199 }
200 return axiomList;
201 }
202
203
204 bool CSPARQLSimpleBuildingParser::isDataProperty(const QString& propertyName) {
205 return mOntology->getBuildData()->getDataPropertyEntityBuildHash()->contains(propertyName);
206 }
207
208 CClassTermExpression* CSPARQLSimpleBuildingParser::getClassTermExpression(const QString &subjectString, QSet<CExpressionVariable*>* varExpSet) {
209 CClassTermExpression* classExpression = nullptr;
210 if (subjectString.startsWith("?") || subjectString.startsWith("$")) {
211 QString variableName = getVariableName(subjectString);
212 CClassVariableExpression* varClassExpression = mOntoBuilder->getClassVariable(variableName);
213 mVarExpSet.insert(varClassExpression);
214 mNameVarHash.insert(variableName, varClassExpression);
215 classExpression = varClassExpression;
216 } else {
217 classExpression = mOntoBuilder->getClass(subjectString);
218 }
219 if (varExpSet && dynamic_cast<CClassVariableExpression*>(classExpression)) {
220 varExpSet->insert((CClassVariableExpression*)classExpression);
221 }
222 return classExpression;
223 }
224
225
226
227
228 CDataPropertyTermExpression* CSPARQLSimpleBuildingParser::getDataPropertyTermExpression(const QString &subjectString, QSet<CExpressionVariable*>* varExpSet) {
229 CDataPropertyTermExpression* dataPropExpression = nullptr;
230 if (subjectString.startsWith("?") || subjectString.startsWith("$")) {
231 QString variableName = getVariableName(subjectString);
232 CDataPropertyVariableExpression* varDataPropExpression = mOntoBuilder->getDataPropertyVariable(variableName);
233 mVarExpSet.insert(varDataPropExpression);
234 mNameVarHash.insert(variableName, varDataPropExpression);
235 dataPropExpression = varDataPropExpression;
236 } else {
237 dataPropExpression = mOntoBuilder->getDataProberty(subjectString);
238 }
239 if (varExpSet && dynamic_cast<CDataPropertyVariableExpression*>(dataPropExpression)) {
240 varExpSet->insert((CDataPropertyVariableExpression*)dataPropExpression);
241 }
242 return dataPropExpression;
243 }
244
245
246
247 CObjectPropertyTermExpression* CSPARQLSimpleBuildingParser::getObjectPropertyTermExpression(const QString &subjectString, QSet<CExpressionVariable*>* varExpSet) {
248 CObjectPropertyTermExpression* objPropExpression = nullptr;
249 if (subjectString.startsWith("?") || subjectString.startsWith("$")) {
250 QString variableName = getVariableName(subjectString);
251 CObjectPropertyVariableExpression* varObjPropExpression = mOntoBuilder->getObjectPropertyVariable(variableName);
252 mVarExpSet.insert(varObjPropExpression);
253 mNameVarHash.insert(variableName, varObjPropExpression);
254 objPropExpression = varObjPropExpression;
255 } else {
256 objPropExpression = mOntoBuilder->getObjectProberty(subjectString);
257 }
258 if (varExpSet && dynamic_cast<CObjectPropertyVariableExpression*>(objPropExpression)) {
259 varExpSet->insert((CObjectPropertyVariableExpression*)objPropExpression);
260 }
261 return objPropExpression;
262 }
263
264 CIndividualTermExpression* CSPARQLSimpleBuildingParser::getIndividualTermExpression(const QString& subjectString, bool anonymousIndividualsAsVariables, QSet<CExpressionVariable*>* varExpSet) {
265 CIndividualTermExpression* indiExpression = nullptr;
266 if (subjectString.startsWith("?") || subjectString.startsWith("$")) {
267 QString variableName = getVariableName(subjectString);
268 CIndividualVariableExpression* varIndiExpression = mOntoBuilder->getIndividualVariable(variableName, false);
269 mVarExpSet.insert(varIndiExpression);
270 mNameVarHash.insert(variableName, varIndiExpression);
271 indiExpression = varIndiExpression;
272 } else if (subjectString.startsWith("_:")) {
273 if (anonymousIndividualsAsVariables) {
274 indiExpression = mOntoBuilder->getIndividualVariable(subjectString.mid(2), true);
275 } else {
276 indiExpression = mOntoBuilder->getAnonymousIndividual(mOntoBuilder->getOntologyAnonymousIdentifier(mOntoBuilder->getOntologyName()), subjectString.mid(2));
277 }
278 } else {
279 indiExpression = mOntoBuilder->getNamedIndividual(subjectString);
280 }
281 if (varExpSet && dynamic_cast<CIndividualVariableExpression*>(indiExpression)) {
282 varExpSet->insert((CIndividualVariableExpression*)indiExpression);
283 }
284 return indiExpression;
285 }
286
287 CDataLiteralTermExpression* CSPARQLSimpleBuildingParser::getDataLiteralTermExpression(const QString &objectString, QSet<CExpressionVariable*>* varExpSet) {
288 CDataLiteralTermExpression* dataRangeExpr = nullptr;
289 if (objectString.startsWith("?") || objectString.startsWith("$")) {
290 QString variableName = getVariableName(objectString);
291 CDataLiteralVariableExpression* dataValueVarExpression = mOntoBuilder->getDataLiteralVariable(variableName);
292 mVarExpSet.insert(dataValueVarExpression);
293 mNameVarHash.insert(variableName, dataValueVarExpression);
294 dataRangeExpr = dataValueVarExpression;
295 } else {
296 QPair<QString, QString> literalDatatypeStringPair = extractDataLiteral(objectString);
297 CDataLexicalValueExpression* literalValueExp = mOntoBuilder->getDataLexicalValue(literalDatatypeStringPair.first);
298 CDatatypeExpression* datatypeExp = mOntoBuilder->getDatatype(literalDatatypeStringPair.second);
299 dataRangeExpr = mOntoBuilder->getDataLiteral(literalValueExp, datatypeExp);
300 }
301 if (varExpSet && dynamic_cast<CDataLiteralVariableExpression*>(dataRangeExpr)) {
302 varExpSet->insert((CDataLiteralVariableExpression*)dataRangeExpr);
303 }
304 return dataRangeExpr;
305 }
306
307
308 }; // end namespace Parser
309
310 }; // end namespace Konclude