Debian Codegraph @

Source/Reasoner/Preprocess/CLexicalNormalisationPreProcess.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 "CLexicalNormalisationPreProcess.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Preprocess {
29
30
31 CLexicalNormalisationPreProcess::CLexicalNormalisationPreProcess() {
32 mOntology = nullptr;
33 }
34
35
36 CLexicalNormalisationPreProcess::~CLexicalNormalisationPreProcess() {
37 }
38
39
40 void CLexicalNormalisationPreProcess::negateOperandConcepts(CConcept *concept, int firstNegationOperand) {
41
42 CSortedNegLinker<CConcept *> *operands = concept->getOperandList();
43 int operand = 0;
44 while (operands) {
45 if (operand++ >= firstNegationOperand) {
46 operands->setNegated(!operands->isNegated());
47 }
48 operands = operands->getNext();
49 }
50 }
51
52
53
54
55 CConcreteOntology *CLexicalNormalisationPreProcess::preprocess(CConcreteOntology *ontology, CPreProcessContext* context) {
56
57 bool skipForELOntologies = CConfigDataReader::readConfigBoolean(context->getConfiguration(),"Konclude.Calculation.Preprocessing.LexicalNormalization.SkipForELFragment",true);
58 bool nonELConstructsUsed = ontology->getDataBoxes()->getExpressionDataBoxMapping()->getBuildConstructFlags()->isNonELConstructUsed();
59
60 mOntology = nullptr;
61
62 if (nonELConstructsUsed || !skipForELOntologies) {
63
64 mOntology = ontology;
65
66 CTBox* tbox = ontology->getDataBoxes()->getTBox();
67 CABox *abox = ontology->getDataBoxes()->getABox();
68 CRBox *rbox = ontology->getDataBoxes()->getRBox();
69 CMBox *mbox = ontology->getDataBoxes()->getMBox();
70
71 mBoxMemMan = CContext::getMemoryAllocationManager(ontology->getDataBoxes()->getBoxContext());
72 CNominalSchemaTemplateVector* nomSchTemplVector = mbox->getNominalSchemaTemplateVector(false);
73
74
75 concepts = tbox->getConceptVector();
76 qint64 conceptCount = concepts->getItemCount();
77
78 roles = rbox->getRoleVector();
79 qint64 roleCount = rbox->getRoleCount();
80
81
82 for (qint64 i = 0; i < conceptCount; ++i) {
83 CConcept *concept = concepts->getLocalData(i);
84
85 if (concept) {
86
87 qint64 cOpCode = concept->getDefinitionOperatorTag();
88
89 if (cOpCode == CCOR) {
90 concept->setMappingNegation(true);
91 concept->setOperatorTag(CCAND);
92 negateOperandConcepts(concept,0);
93 } else if (cOpCode == CCSOME) {
94 concept->setMappingNegation(true);
95 concept->setOperatorTag(CCALL);
96 negateOperandConcepts(concept,0);
97 }
98 }
99 }
100
101 topConcept = concepts->getData(1);
102
103 for (qint64 i = 0; i < conceptCount; ++i) {
104 CConcept *concept = concepts->getLocalData(i);
105
106 if (concept) {
107
108 CSortedNegLinker<CConcept *> *operands = concept->getOperandList();
109 while (operands) {
110 CConcept *opConcept = operands->getData();
111 if (opConcept->hasMappingNegation()) {
112 operands->setNegated(!operands->isNegated());
113 }
114 if (opConcept->getDefinitionOperatorTag() == CCBOTTOM) {
115 operands->setData(topConcept);
116 operands->setNegated(!operands->isNegated());
117 }
118 if (nomSchTemplVector) {
119 if (opConcept->getOperatorCode() == CCNOMTEMPLREF || opConcept->getOperatorCode() == CCPBINDGROUND || opConcept->getOperatorCode() == CCVARBINDGROUND) {
120 CNominalSchemaTemplate* nsTemplate = nomSchTemplVector->getData(opConcept->getParameter());
121 if (nsTemplate) {
122 if (nsTemplate->getTemplateConcept()->hasMappingNegation()) {
123 operands->setNegated(!operands->isNegated());
124 }
125 }
126 }
127 }
128 operands = operands->getNext();
129 }
130 }
131 }
132 for (qint64 i = 0; i < roleCount; ++i) {
133 CRole *role = roles->getLocalData(i);
134 if (role) {
135 CSortedNegLinker<CConcept *> *domainConIt = role->getDomainConceptList();
136 while (domainConIt) {
137 CConcept *domainConcept = domainConIt->getData();
138 if (domainConcept->hasMappingNegation()) {
139 domainConIt->setNegated(!domainConIt->isNegated());
140 }
141 domainConIt = domainConIt->getNext();
142 }
143 CSortedNegLinker<CConcept *> *rangeConIt = role->getRangeConceptList();
144 while (rangeConIt) {
145 CConcept *rangeConcept = rangeConIt->getData();
146 if (rangeConcept->hasMappingNegation()) {
147 rangeConIt->setNegated(!rangeConIt->isNegated());
148 }
149 rangeConIt = rangeConIt->getNext();
150 }
151 }
152 }
153
154
155 if (abox) {
156 individuals = abox->getIndividualVector();
157 qint64 individualCount = abox->getIndividualCount();
158 for (qint64 i = 0; i < individualCount; ++i) {
159 CIndividual *indi = individuals->getLocalData(i);
160 if (indi) {
161 CConceptAssertionLinker* assertionConLinker = indi->getAssertionConceptLinker();
162 while (assertionConLinker) {
163 if (assertionConLinker->getData()->hasMappingNegation()) {
164 assertionConLinker->setNegation(!assertionConLinker->isNegated());
165 }
166 assertionConLinker = assertionConLinker->getNext();
167 }
168 }
169 }
170 }
171
172
173 replacementHash = new QHash<CConceptUnambiguousHasher,CConcept*>();
174 processedSet = new QSet<CConcept*>();
175 conceptReplaceHash = new QHash<CConcept*,QPair<bool,CConcept*> >();
176 processingList = new QList<CConcept*>();
177
178 for (qint64 i = 0; i < conceptCount; ++i) {
179 CConcept *concept = concepts->getLocalData(i);
180 if (concept) {
181 if (!processedSet->contains(concept)) {
182 //QString iriClassNameString2 = CIRIName::getRecentIRIName(concept->getClassNameLinker());
183 //if (iriClassNameString2 == "http://www.owllink.org/testsuite/particle-D#Proton") {
184 // bool bug = true;
185 //}
186 addNormalizeConcept(concept);
187 processedSet->insert(concept);
188 normalizeListedConcepts();
189 }
190 }
191 }
192
193 for (qint64 i = 0; i < roleCount; ++i) {
194 CRole *role = roles->getLocalData(i);
195 if (role) {
196 CSortedNegLinker<CConcept *> *domainConIt = role->getDomainConceptList();
197 bool domainModified = false;
198 while (domainConIt) {
199 CConcept *domainConcept = domainConIt->getData();
200 if (conceptReplaceHash->contains(domainConcept)) {
201 QPair<bool,CConcept *> negRepConcept(conceptReplaceHash->value(domainConcept));
202 domainConcept = negRepConcept.second;
203 if (negRepConcept.first) {
204 domainConIt->setNegated(!domainConIt->isNegated());
205 }
206 domainConIt->setData(domainConcept);
207 domainModified = true;
208 }
209 domainConIt = domainConIt->getNext();
210 }
211 if (domainModified) {
212 domainConIt = role->getDomainConceptList();
213 domainConIt->resortLinkerData();
214 }
215 CSortedNegLinker<CConcept *> *rangeConIt = role->getRangeConceptList();
216 bool rangeModified = false;
217 while (rangeConIt) {
218 CConcept *rangeConcept = rangeConIt->getData();
219 if (conceptReplaceHash->contains(rangeConcept)) {
220 QPair<bool,CConcept *> negRepConcept(conceptReplaceHash->value(rangeConcept));
221 rangeConcept = negRepConcept.second;
222 if (negRepConcept.first) {
223 rangeConIt->setNegated(!rangeConIt->isNegated());
224 }
225 rangeConIt->setData(rangeConcept);
226 domainModified = true;
227 }
228 rangeConIt = rangeConIt->getNext();
229 }
230 if (rangeModified) {
231 rangeConIt = role->getRangeConceptList();
232 rangeConIt->resortLinkerData();
233 }
234
235 }
236 }
237
238 if (abox) {
239 individuals = abox->getIndividualVector();
240 qint64 individualCount = abox->getIndividualCount();
241 for (qint64 i = 0; i < individualCount; ++i) {
242 CIndividual *indi = individuals->getLocalData(i);
243 if (indi) {
244
245 CConceptAssertionLinker* assertionConLinker = indi->getAssertionConceptLinker();
246 while (assertionConLinker) {
247 if (conceptReplaceHash->contains(assertionConLinker->getData())) {
248 QPair<bool,CConcept *> negRepConcept(conceptReplaceHash->value(assertionConLinker->getData()));
249 assertionConLinker->setData(negRepConcept.second);
250 if (negRepConcept.first) {
251 assertionConLinker->setNegation(!assertionConLinker->getNegation());
252 }
253 }
254 assertionConLinker = assertionConLinker->getNext();
255 }
256
257 }
258 }
259 }
260
261 delete replacementHash;
262 delete processedSet;
263 delete conceptReplaceHash;
264 delete processingList;
265 }
266
267
268 return ontology;
269 }
270
271
272 CConcreteOntology* CLexicalNormalisationPreProcess::continuePreprocessing() {
273 if (mOntology) {
274 }
275 return mOntology;
276 }
277
278 CSortedNegLinker<CConcept*>* CLexicalNormalisationPreProcess::resortConceptLinkersByTagging(CSortedNegLinker<CConcept*>* conceptLinkers) {
279 CSortedNegLinker<CConcept*>* newConceptLinkers = nullptr;
280 while (conceptLinkers) {
281 CSortedNegLinker<CConcept*>* tmpConceptLinker = conceptLinkers;
282 conceptLinkers = conceptLinkers->getNext();
283 tmpConceptLinker->setNext(nullptr);
284 if (!newConceptLinkers) {
285 newConceptLinkers = tmpConceptLinker;
286 } else {
287 newConceptLinkers = newConceptLinkers->insertSortedNextSorted(tmpConceptLinker);
288 }
289 }
290 return newConceptLinkers;
291 }
292
293 void CLexicalNormalisationPreProcess::addNormalizeConcept(CConcept *concept) {
294 processingList->prepend(concept);
295 }
296
297
298 void CLexicalNormalisationPreProcess::normalizeListedConcepts() {
299 while (!processingList->isEmpty()) {
300 CConcept* concept = processingList->first();
301 cint64 conceptTag = concept->getConceptTag();
302 bool isLocal = concepts->hasLocalData(conceptTag);
303 bool conceptNormalizationAdded = false;
304 CSortedNegLinker<CConcept*>* opLinkerIt = concept->getOperandList();
305 if (isLocal) {
306 while (opLinkerIt) {
307 CConcept* opConcept = concepts->getData(opLinkerIt->getData()->getConceptTag());
308 if (!processedSet->contains(opConcept)) {
309 conceptNormalizationAdded = true;
310 addNormalizeConcept(opConcept);
311 processedSet->insert(opConcept);
312 }
313 opLinkerIt = opLinkerIt->getNext();
314 }
315 }
316
317 if (!conceptNormalizationAdded) {
318 processingList->removeFirst();
319 normalizeConcept(concept,isLocal);
320 }
321 }
322 }
323
324
325 void CLexicalNormalisationPreProcess::normalizeConcept(CConcept *concept, bool isLocal) {
326 CSortedNegLinker<CConcept *> *opList = 0;
327
328 cint64 conceptTag = concept->getConceptTag();
329 bool implicTriggers = concept->getOperatorCode() == CCIMPL || concept->getOperatorCode() == CCIMPLALL;
330 bool nonReplace = false;
331 if (implicTriggers) {
332 nonReplace = true;
333 }
334
335 if (isLocal) {
336
337 CSortedNegLinker<CConcept *> *conceptAddList = 0;
338
339 CSortedNegLinker<CConcept *> *lastOpList = 0;
340 bool opConTagsModified = false;
341 opList = concept->getOperandList();
342 while (opList) {
343 CConcept *opConcept = concepts->getData(opList->getData()->getConceptTag());
344
345 if (!nonReplace && conceptReplaceHash->contains(opConcept)) {
346 QPair<bool,CConcept *> negRepConcept(conceptReplaceHash->value(opConcept));
347 opConcept = negRepConcept.second;
348 if (negRepConcept.first) {
349 opList->setNegated(!opList->isNegated());
350 }
351 opList->setData(opConcept);
352 opConTagsModified = true;
353 }
354
355 bool removeThisOp = false;
356
357 cint64 opCode = concept->getOperatorCode();
358
359
360 if (opCode == CCAND) {
361
362 cint64 operandOpCode = opConcept->getOperatorCode();
363
364 if ((operandOpCode == CCAND) && !opList->isNegated()) {
365 // remove concept from operand list and copy operand concepts from removed concept to this concept as operands
366 CSortedNegLinker<CConcept *> *opOpList = opConcept->getOperandList();
367 while (opOpList) {
368 concept->incOperandCount();
369 conceptAddList = CObjectAllocator< CSortedNegLinker<CConcept*> >::allocateAndConstruct(mBoxMemMan)->init(opOpList->getData(),opOpList->isNegated())->insertSortedNextSorted(conceptAddList);
370 opOpList = opOpList->getNext();
371 }
372 removeThisOp = true;
373 } else if (opConcept->getDefinitionOperatorTag() == CCTOP && !opList->isNegated()) {
374 removeThisOp = true;
375 }
376
377 }
378
379 if (removeThisOp) {
380 concept->incOperandCount(-1);
381 if (!lastOpList) {
382 concept->setOperandList(opList->getNext());
383 opList = opList->getNext();
384 } else {
385 opList = opList->getNext();
386 lastOpList->removeNext();
387 }
388 } else {
389 lastOpList = opList;
390 opList = opList->getNext();
391 }
392
393 }
394 if (conceptAddList) {
395 concept->setOperandList(conceptAddList->insertSortedNextSorted(concept->getOperandList()));
396 }
397 if (opConTagsModified) {
398 concept->setOperandList(resortConceptLinkersByTagging(concept->getOperandList()));
399 }
400
401 }
402
403 bool isClash = false;
404
405 CSortedNegLinker<CConcept *> *lastOpList = 0;
406 opList = concept->getOperandList();
407 while (opList) {
408 CConcept *opConcept = concepts->getData(opList->getData()->getConceptTag());
409
410 if (isLocal) {
411 if (!nonReplace && conceptReplaceHash->contains(opConcept)) {
412 QPair<bool,CConcept *> negRepConcept(conceptReplaceHash->value(opConcept));
413 opConcept = negRepConcept.second;
414 if (negRepConcept.first) {
415 opList->setNegated(!opList->isNegated());
416 }
417 opList->setData(opConcept);
418 }
419 }
420
421 qint64 opCCode = opConcept->getDefinitionOperatorTag();
422 if (concept->getProcessingOperatorTag() == CCAND && opCCode == CCTOP && opList->isNegated()) {
423 isClash = true;
424 }
425
426 if (concept->getProcessingOperatorTag() == CCSOME && opCCode == CCTOP && opList->isNegated()) {
427 isClash = true;
428 }
429 if (concept->getProcessingOperatorTag() == CCATLEAST && opCCode == CCTOP && opList->isNegated()) {
430 isClash = true;
431 }
432
433 if (concept->getProcessingOperatorTag() == CCAND && lastOpList && lastOpList->getData()->getConceptTag() == opList->getData()->getConceptTag()) {
434 if (lastOpList->isNegated() != opList->isNegated()) {
435 isClash = true;
436 }
437 }
438
439 lastOpList = opList;
440 opList = opList->getNext();
441 }
442
443 if (isClash) {
444 conceptReplaceHash->insert(concept,QPair<bool,CConcept *>(true,topConcept));
445 } else {
446 CConceptUnambiguousHasher conUnHasher(getConceptHasher(concept));
447 if (!replacementHash->contains(conUnHasher)) {
448 replacementHash->insert(conUnHasher,concept);
449 } else {
450 conceptReplaceHash->insert(concept,QPair<bool,CConcept *>(false,replacementHash->value(conUnHasher)));
451 }
452 }
453 }
454
455
456
457 CConceptUnambiguousHasher CLexicalNormalisationPreProcess::getConceptHasher(CConcept *concept) {
458 cint64 opCode = concept->getOperatorCode();
459 CConceptOperator* conOperator = concept->getConceptOperator();
460 if (opCode == CCATOM || opCode == CCSUB || opCode == CCEQ || opCode == CCDATATYPE || opCode == CCDATALITERAL || opCode == CCDATARESTRICTION || conOperator->hasPartialOperatorCodeFlag(CConceptOperator::CCFS_TRIG_TYPE)) {
461 return CConceptUnambiguousHasher(concept,true);
462 } else {
463 return CConceptUnambiguousHasher(concept,false);
464 }
465 }
466
467
468
469 }; // end namespace Preprocess
470
471 }; // end namespace Reasoner
472
473 }; // end namespace Konclude