Debian Codegraph @

Source/Reasoner/Preprocess/CReverseRoleAssertionGeneratorPreProcess.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 "CReverseRoleAssertionGeneratorPreProcess.h"
22
23
24 namespace Konclude {
25
26 namespace Reasoner {
27
28 namespace Preprocess {
29
30
31 CReverseRoleAssertionGeneratorPreProcess::CReverseRoleAssertionGeneratorPreProcess() {
32 }
33
34
35 CReverseRoleAssertionGeneratorPreProcess::~CReverseRoleAssertionGeneratorPreProcess() {
36 }
37
38
39
40 bool CReverseRoleAssertionGeneratorPreProcess::isAssertionChanged(CIndividual* indi, CIndividual* prevIndi, CRoleAssertionLinker* roleAssLinker, CPreProcessContext* context) {
41 if (!prevIndi) {
42 return true;
43 } else {
44 CIndividual* destIndi = roleAssLinker->getIndividual();
45 CRole* role = roleAssLinker->getRole();
46 for (CRoleAssertionLinker* prevRoleAssLinkerIt = prevIndi->getAssertionRoleLinker(); prevRoleAssLinkerIt; prevRoleAssLinkerIt = prevRoleAssLinkerIt->getNext()) {
47 if (prevRoleAssLinkerIt->getIndividual()->getIndividualID() == destIndi->getIndividualID() && prevRoleAssLinkerIt->getRole() == role) {
48 return false;
49 }
50 }
51 return true;
52 }
53 }
54
55
56
57 CConcreteOntology *CReverseRoleAssertionGeneratorPreProcess::preprocess(CConcreteOntology *ontology, CPreProcessContext* context) {
58
59
60 CTBox* tbox = ontology->getDataBoxes()->getTBox();
61 CABox *abox = ontology->getDataBoxes()->getABox();
62 CRBox *rbox = ontology->getDataBoxes()->getRBox();
63 CMBox *mbox = ontology->getDataBoxes()->getMBox();
64
65 mBoxMemMan = CContext::getMemoryAllocationManager(ontology->getDataBoxes()->getBoxContext());
66
67 mConVec = tbox->getConceptVector();
68 cint64 conceptCount = mConVec->getItemCount();
69
70 mRoleVec = rbox->getRoleVector();
71 cint64 roleCount = rbox->getRoleCount();
72
73
74 mIndiVec = abox->getIndividualVector(false);
75 if (mIndiVec) {
76 cint64 indiCount = mIndiVec->getItemCount();
77
78 if (indiCount > 0) {
79
80 if (ontology->getDataBoxes()->isInitialBuild()) {
81
82 for (qint64 i = 0; i < indiCount; ++i) {
83 CIndividual* indi = mIndiVec->getLocalData(i);
84
85 if (indi) {
86 for (CRoleAssertionLinker* roleAssLinkerIt = indi->getAssertionRoleLinker(); roleAssLinkerIt; roleAssLinkerIt = roleAssLinkerIt->getNext()) {
87 CIndividual* otherIndi = roleAssLinkerIt->getIndividual();
88 CReverseRoleAssertionLinker* revAssLinker = CObjectAllocator<CReverseRoleAssertionLinker>::allocateAndConstruct(mBoxMemMan);
89 revAssLinker->initReverseRoleAssertionLinker(roleAssLinkerIt,indi);
90 otherIndi->addReverseAssertionRoleLinker(revAssLinker);
91 }
92 }
93 }
94 } else {
95 bool deletedABoxAssertions = ontology->getIncrementalRevisionData()->getAxiomChangeData()->hasDeletedABoxAxioms();
96 QSet<cint64> directUpdateIndividualSet;
97 QSet<cint64> reverseAssUpdateIndividualSet;
98 QSet<CReverseRoleAssertionLinker*> validAssIndividualSet;
99
100 for (qint64 i = 0; i < indiCount; ++i) {
101 CIndividual* indi = mIndiVec->getLocalData(i);
102 if (indi) {
103 directUpdateIndividualSet.insert(i);
104 if (deletedABoxAssertions) {
105 CIndividual* prevIndividual = mIndiVec->getReferenceData(i);
106 for (CRoleAssertionLinker* roleAssLinkerIt = indi->getAssertionRoleLinker(); roleAssLinkerIt; roleAssLinkerIt = roleAssLinkerIt->getNext()) {
107 CIndividual* otherIndi = roleAssLinkerIt->getIndividual();
108 if (isAssertionChanged(indi,prevIndividual,roleAssLinkerIt,context)) {
109 reverseAssUpdateIndividualSet.insert(otherIndi->getIndividualID());
110 }
111 }
112 if (prevIndividual) {
113 for (CRoleAssertionLinker* prevRoleAssLinkerIt = prevIndividual->getAssertionRoleLinker(); prevRoleAssLinkerIt; prevRoleAssLinkerIt = prevRoleAssLinkerIt->getNext()) {
114 CIndividual* otherIndi = prevRoleAssLinkerIt->getIndividual();
115 if (isAssertionChanged(prevIndividual,indi,prevRoleAssLinkerIt,context)) {
116 reverseAssUpdateIndividualSet.insert(otherIndi->getIndividualID());
117 }
118 }
119 }
120 if (!indi->getIndividualNominalConcept() && prevIndividual->getIndividualNominalConcept()) {
121 indi->setIndividualNominalConcept(prevIndividual->getIndividualNominalConcept());
122 }
123 }
124 }
125 }
126
127 for (QSet<cint64>::const_iterator indiIt = directUpdateIndividualSet.constBegin(), indiItEnd = directUpdateIndividualSet.constEnd(); indiIt != indiItEnd; ++indiIt) {
128 cint64 indiID = *indiIt;
129 CIndividual* individual = mIndiVec->getLocalData(indiID);
130 CIndividual* prevIndividual = mIndiVec->getReferenceData(individual->getIndividualID());
131 if (prevIndividual) {
132 for (CReverseRoleAssertionLinker* prevRevRoleAssLinkerIt = prevIndividual->getReverseAssertionRoleLinker(); prevRevRoleAssLinkerIt; prevRevRoleAssLinkerIt = prevRevRoleAssLinkerIt->getNext()) {
133 CIndividual* otherIndi = prevRevRoleAssLinkerIt->getIndividual();
134 if (!directUpdateIndividualSet.contains(otherIndi->getIndividualID())) {
135 CReverseRoleAssertionLinker* revAssLinker = CObjectAllocator<CReverseRoleAssertionLinker>::allocateAndConstruct(mBoxMemMan);
136 revAssLinker->initReverseRoleAssertionLinker(prevRevRoleAssLinkerIt->getRoleAssertion(),otherIndi);
137 individual->addReverseAssertionRoleLinker(revAssLinker);
138 }
139 }
140 }
141 }
142
143
144
145 for (QSet<cint64>::const_iterator indiIt = reverseAssUpdateIndividualSet.constBegin(), indiItEnd = reverseAssUpdateIndividualSet.constEnd(); indiIt != indiItEnd; ++indiIt) {
146 cint64 indiID = *indiIt;
147 bool local = false;
148 CIndividual* individual = mIndiVec->getData(indiID,&local);
149 if (!local) {
150 CIndividual* locIndividual = CConceptRoleIndividualLocator::getLocatedIndividual(individual,ontology);
151 locIndividual->setAssertionRoleLinker(individual->getAssertionRoleLinker());
152
153 for (CReverseRoleAssertionLinker* prevRevRoleAssLinkerIt = individual->getReverseAssertionRoleLinker(); prevRevRoleAssLinkerIt; prevRevRoleAssLinkerIt = prevRevRoleAssLinkerIt->getNext()) {
154 CIndividual* otherIndi = prevRevRoleAssLinkerIt->getIndividual();
155 if (!directUpdateIndividualSet.contains(otherIndi->getIndividualID())) {
156 CReverseRoleAssertionLinker* revAssLinker = CObjectAllocator<CReverseRoleAssertionLinker>::allocateAndConstruct(mBoxMemMan);
157 revAssLinker->initReverseRoleAssertionLinker(prevRevRoleAssLinkerIt->getRoleAssertion(),otherIndi);
158 locIndividual->addReverseAssertionRoleLinker(revAssLinker);
159 }
160 }
161 }
162 }
163
164 for (QSet<cint64>::const_iterator indiIt = directUpdateIndividualSet.constBegin(), indiItEnd = directUpdateIndividualSet.constEnd(); indiIt != indiItEnd; ++indiIt) {
165 cint64 indiID = *indiIt;
166 CIndividual* individual = mIndiVec->getLocalData(indiID);
167 CIndividual* prevIndividual = mIndiVec->getReferenceData(indiID);
168 for (CRoleAssertionLinker* roleAssLinkerIt = individual->getAssertionRoleLinker(); roleAssLinkerIt; roleAssLinkerIt = roleAssLinkerIt->getNext()) {
169 CIndividual* otherIndi = roleAssLinkerIt->getIndividual();
170 if (directUpdateIndividualSet.contains(otherIndi->getIndividualID()) || reverseAssUpdateIndividualSet.contains(otherIndi->getIndividualID())) {
171
172 //CIndividual* locOtherIndi = mIndiVec->getLocalData(otherIndi->getIndividualID());
173 CIndividual* locOtherIndi = CConceptRoleIndividualLocator::getLocatedIndividual(otherIndi,ontology);
174 CReverseRoleAssertionLinker* revAssLinker = CObjectAllocator<CReverseRoleAssertionLinker>::allocateAndConstruct(mBoxMemMan);
175 revAssLinker->initReverseRoleAssertionLinker(roleAssLinkerIt,individual);
176 locOtherIndi->addReverseAssertionRoleLinker(revAssLinker);
177 }
178 }
179 }
180
181
182
183 }
184
185 }
186 }
187
188
189
190 return ontology;
191 }
192
193
194
195 }; // end namespace Preprocess
196
197 }; // end namespace Reasoner
198
199 }; // end namespace Konclude