Debian Codegraph @

Source/Parser/COWLlinkQtXMLSimpleQueryParser.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 "COWLlinkQtXMLSimpleQueryParser.h"
22
23
24 namespace Konclude {
25
26 namespace Parser {
27
28
29
30 COWLlinkQtXMLSimpleQueryParser::COWLlinkQtXMLSimpleQueryParser(CQueryBuilder *queryBuilder, CConcreteOntology* conOntology) : COWLlinkQueryParser(queryBuilder) {
31 mConOntology = conOntology;
32 LOG(INFO,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("OWLlink Query Parser initialized."),this);
33
34 }
35
36
37 COWLlinkQtXMLSimpleQueryParser::~COWLlinkQtXMLSimpleQueryParser() {
38 }
39
40
41 QList<CBuildExpression *> COWLlinkQtXMLSimpleQueryParser::parseOntologyChildNodes(QDomElement *node) {
42 QList<CBuildExpression *> builds;
43 QDomElement childNode = node->firstChildElement();
44 while (!childNode.isNull()) {
45 CBuildExpression *expression = nullptr;
46 QString nodeString(childNode.tagName());
47 if (nodeString == "Class" || nodeString == "owl:Class" ) {
48 QString className(getEntityStringName(&childNode));
49 CBUILDHASH<CStringRefStringHasher,CClassExpression*>* classStringBuildHash = mConOntology->getBuildData()->getClassEntityBuildHash();
50 expression = classStringBuildHash->value(className,nullptr);
51 if (!expression) {
52 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Class name '%1' cannot be resolved in ontology '%2'.").arg(className).arg(mConOntology->getOntologyName()),this);
53 }
54 } else if (nodeString == "ObjectProperty" || nodeString == "owl:ObjectProperty" ) {
55 QString objPropName(getEntityStringName(&childNode));
56 CBUILDHASH<CStringRefStringHasher,CObjectPropertyExpression*>* objPropBuildHash = mConOntology->getBuildData()->getObjectPropertyEntityBuildHash();
57 expression = objPropBuildHash->value(objPropName,nullptr);
58 if (!expression) {
59 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Object property name '%1' cannot be resolved in ontology '%2'.").arg(objPropName).arg(mConOntology->getOntologyName()),this);
60 }
61 } else if (nodeString == "DataProperty" || nodeString == "owl:DataProperty" ) {
62 QString dataPropName(getEntityStringName(&childNode));
63 CBUILDHASH<CStringRefStringHasher,CDataPropertyExpression*>* dataPropBuildHash = mConOntology->getBuildData()->getDataPropertyEntityBuildHash();
64 expression = dataPropBuildHash->value(dataPropName,nullptr);
65 if (!expression) {
66 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Data property name '%1' cannot be resolved in ontology '%2'.").arg(dataPropName).arg(mConOntology->getOntologyName()),this);
67 }
68 } else if (nodeString == "NamedIndividual" || nodeString == "owl:NamedIndividual" ) {
69 QString indiName(getEntityStringName(&childNode));
70 CBUILDHASH<CStringRefStringHasher,CNamedIndividualExpression*>* indiStringBuildHash = mConOntology->getBuildData()->getIndividualEntityBuildHash();
71 expression = indiStringBuildHash->value(indiName,nullptr);
72
73 if (!expression) {
74 expression = mQueryBuilder->getQueryingTemporaryNamedIndividual(indiName);
75 }
76 //if (!expression && mConOntology->getOntologyTriplesData()->getTripleAssertionAccessor()) {
77 // // TODO: check existence of individual
78 // cint64 exIndiId = -1;
79 // mConOntology->getOntologyTriplesData()->getTripleAssertionAccessor()->visitIndividualId(indiName, [&](cint64 indiId)->bool {
80 // exIndiId = indiId;
81 // return true;
82 // });
83 // if (exIndiId >= 0) {
84 // expression = mQueryBuilder->getQueryingTemporaryNamedIndividual(indiName);
85 // }
86 //}
87
88 if (!expression) {
89 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Individual name '%1' cannot be resolved in ontology '%2'.").arg(indiName).arg(mConOntology->getOntologyName()),this);
90 }
91 }
92
93 if (expression) {
94 builds.append(expression);
95 }
96 childNode = childNode.nextSiblingElement();
97 }
98 return builds;
99 }
100
101
102
103
104 QList<CBuildExpression*> COWLlinkQtXMLSimpleQueryParser::parseOntologyChildNodesInverseHandling(QDomElement *node, bool& inversed) {
105 QList<CBuildExpression *> builds;
106 QDomElement childNode = node->firstChildElement();
107 inversed = false;
108 bool inverseNodeParsed = false;
109 while (!childNode.isNull()) {
110 CBuildExpression *expression = nullptr;
111 QString nodeString(childNode.tagName());
112 if (nodeString == "ObjectInverseOf" || nodeString == "owl:ObjectInverseOf" ) {
113 bool nextInversed = false;
114 builds.append(parseOntologyChildNodesInverseHandling(&childNode,nextInversed));
115 if (!builds.isEmpty()) {
116 inverseNodeParsed = true;
117 if (nextInversed) {
118 inversed = false;
119 } else {
120 inversed = true;
121 }
122 }
123 } else if (nodeString == "Class" || nodeString == "owl:Class" ) {
124 QString className(getEntityStringName(&childNode));
125 CBUILDHASH<CStringRefStringHasher,CClassExpression*>* classStringBuildHash = mConOntology->getBuildData()->getClassEntityBuildHash();
126 expression = classStringBuildHash->value(className,nullptr);
127 if (!expression) {
128 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Class name '%1' cannot be resolved in ontology '%2'.").arg(className).arg(mConOntology->getOntologyName()),this);
129 }
130 } else if (nodeString == "ObjectProperty" || nodeString == "owl:ObjectProperty" ) {
131 QString objPropName(getEntityStringName(&childNode));
132 CBUILDHASH<CStringRefStringHasher,CObjectPropertyExpression*>* objPropBuildHash = mConOntology->getBuildData()->getObjectPropertyEntityBuildHash();
133 expression = objPropBuildHash->value(objPropName,nullptr);
134 if (!expression) {
135 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Object property name '%1' cannot be resolved in ontology '%2'.").arg(objPropName).arg(mConOntology->getOntologyName()),this);
136 }
137 } else if (nodeString == "DataProperty" || nodeString == "owl:DataProperty" ) {
138 QString dataPropName(getEntityStringName(&childNode));
139 CBUILDHASH<CStringRefStringHasher,CDataPropertyExpression*>* dataPropBuildHash = mConOntology->getBuildData()->getDataPropertyEntityBuildHash();
140 expression = dataPropBuildHash->value(dataPropName,nullptr);
141 if (!expression) {
142 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Data property name '%1' cannot be resolved in ontology '%2'.").arg(dataPropName).arg(mConOntology->getOntologyName()),this);
143 }
144 } else if (nodeString == "NamedIndividual" || nodeString == "owl:NamedIndividual" ) {
145 QString indiName(getEntityStringName(&childNode));
146 CBUILDHASH<CStringRefStringHasher, CNamedIndividualExpression*>* indiStringBuildHash = mConOntology->getBuildData()->getIndividualEntityBuildHash();
147 expression = indiStringBuildHash->value(indiName, nullptr);
148
149 if (!expression) {
150 expression = mQueryBuilder->getQueryingTemporaryNamedIndividual(indiName);
151 }
152
153 //if (!expression && mConOntology->getOntologyTriplesData()->getTripleAssertionAccessor()) {
154 // // TODO: check existence of individual
155 // cint64 exIndiId = -1;
156 // mConOntology->getOntologyTriplesData()->getTripleAssertionAccessor()->visitIndividualId(indiName, [&](cint64 indiId)->bool {
157 // exIndiId = indiId;
158 // return true;
159 // });
160 // if (exIndiId >= 0) {
161 // expression = mQueryBuilder->getQueryingTemporaryNamedIndividual(indiName);
162 // }
163 //}
164 if (!expression) {
165 LOG(ERROR,"::Konclude::Parser::OWLlinkQtXMLSimpleQueryParser",logTr("Individual name '%1' cannot be resolved in ontology '%2'.").arg(indiName).arg(mConOntology->getOntologyName()),this);
166 }
167 }
168 if (expression) {
169 builds.append(expression);
170 }
171 childNode = childNode.nextSiblingElement();
172 }
173
174 return builds;
175 }
176
177
178
179 QString COWLlinkQtXMLSimpleQueryParser::getEntityStringName(QDomElement *node) {
180 QString entityName;
181
182 bool abbreviated = false;
183
184 QString attrName = node->attribute(QString("IRI"));
185 if (attrName.isEmpty()) {
186 attrName = node->attribute(QString("abbreviatedIRI"));
187 abbreviated = true;
188 }
189 if (attrName.isEmpty()) {
190 attrName = node->attribute(QString("owl:IRI"));
191 }
192 if (attrName.isEmpty()) {
193 attrName = node->attribute(QString("owl:abbreviatedIRI"));
194 abbreviated = true;
195 }
196 if (attrName.isEmpty()) {
197 attrName = node->attribute(QString("URI"));
198 }
199 if (attrName.isEmpty()) {
200 attrName = node->attribute(QString("owl:URI"));
201 }
202 if (abbreviated) {
203 entityName = resolveAbbreviationName(attrName);
204 } else {
205 entityName = attrName;
206 }
207
208 return entityName;
209 }
210
211 QString COWLlinkQtXMLSimpleQueryParser::resolveAbbreviationName(const QString& name) {
212 QString resolvedString(name);
213 QStringList nameList(name.split(":"));
214 if (nameList.size() > 1) {
215 QString abbName = nameList.first();
216 QString idName = nameList.last();
217 CMAPPINGHASH<QString,CNamePrefix*>* abbNameHash = mConOntology->getStringMapping()->getAbbreviatedNamePrefixHash(false);
218 CNamePrefix* namePrefix = abbNameHash->value(abbName,nullptr);
219 if (namePrefix) {
220 resolvedString = namePrefix->getPrefixString()+idName;
221 }
222 }
223 return resolvedString;
224 }
225
226
227
228
229 }; // end namespace Parser
230
231 }; // end namespace Konclude