-
Notifications
You must be signed in to change notification settings - Fork 1
/
DependencyIndex.c
258 lines (229 loc) · 7.69 KB
/
DependencyIndex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "DependencyIndex.h"
#include "Clash.h"
#include "DependencyEntry.h"
#include "KnowledgeBase.h"
#include "DependencySet.h"
#include "Branch.h"
DependencyIndex::DependencyIndex(KnowledgeBase* pKB)
{
m_pKB = pKB;
}
void DependencyIndex::setClashDependencies(Clash* pClash)
{
for(SetOfDependency::iterator i = m_setClashIndex.begin(); i != m_setClashIndex.end(); i++ )
{
ClashDependency* pClashDependency = (ClashDependency*)*i;
//remove the dependency
Expr2DependencyEntry::iterator ii = m_mapDependencies.find(pClashDependency->m_pAssertion);
if( ii != m_mapDependencies.end() )
{
DependencyEntry* pDE = (DependencyEntry*)ii->second;
if( pDE ) pDE->m_pClashDependency = NULL;
}
//clear the old index
m_setClashIndex.clear();
if( pClash == NULL )
return;
for( ExprNodeSet::iterator i = pClash->m_pDepends->m_setExplain.begin(); i != pClash->m_pDepends->m_setExplain.end(); i++ )
{
ExprNode* pAtom = (ExprNode*)*i;
//check if this assertion exists
if( m_pKB->m_setSyntacticAssertions.find(pAtom) != m_pKB->m_setSyntacticAssertions.end() )
{
//if this entry does not exist then create it
if( m_mapDependencies.find(pAtom) == m_mapDependencies.end() )
m_mapDependencies[pAtom] = new DependencyEntry();
ClashDependency* pNewDep = new ClashDependency(pAtom, pClash);
//set the dependency
DependencyEntry* pDEntry = (DependencyEntry*)(m_mapDependencies.find(pAtom)->second);
pDEntry->m_pClashDependency = pNewDep;
//update index
m_setClashIndex.insert(pNewDep);
}
}
}
}
void DependencyIndex::addEdgeDependency(Edge* pEdge, DependencySet* pDS)
{
//loop over ds
for(ExprNodeSet::iterator i = pDS->m_setExplain.begin(); i != pDS->m_setExplain.end(); i++ )
{
ExprNode* pAtom = (ExprNode*)*i;
//check if this assertion exists
if( m_pKB->m_setSyntacticAssertions.find(pAtom) != m_pKB->m_setSyntacticAssertions.end() )
{
//if this entry does not exist then create it
DependencyEntry* pDE = NULL;
if( m_mapDependencies.find(pAtom) == m_mapDependencies.end() )
{
pDE = new DependencyEntry();
m_mapDependencies[pAtom] = pDE;
}
else
pDE = (DependencyEntry*)m_mapDependencies.find(pAtom)->second;
//add the dependency
pDE->addEdgeDependency(pEdge);
}
}
}
// Add a new merge dependency
void DependencyIndex::addMergeDependency(ExprNode* pInd, ExprNode* pMergeTo, DependencySet* pDS)
{
//loop over ds
for(ExprNodeSet::iterator i = pDS->m_setExplain.begin(); i != pDS->m_setExplain.end(); i++ )
{
//get explain atom
ExprNode* pAtom = (ExprNode*)*i;
//check if this assertion exists
ExprNodeSet::iterator iFind = m_pKB->m_setSyntacticAssertions.find(pAtom);
if( iFind != m_pKB->m_setSyntacticAssertions.end() )
{
//if this entry does not exist then create it
DependencyEntry* pDE = NULL;
Expr2DependencyEntry::iterator iFind2 = m_mapDependencies.find(pAtom);
if( iFind2 == m_mapDependencies.end() )
{
pDE = new DependencyEntry();
m_mapDependencies[pAtom] = pDE;
}
else
pDE = (DependencyEntry*)iFind2->second;
pDE->addMergeDependency(pInd, pMergeTo);
}
}
}
void DependencyIndex::addTypeDependency(ExprNode* pInd, ExprNode* pType, DependencySet* pDS)
{
for(ExprNodeSet::iterator i = pDS->m_setExplain.begin(); i != pDS->m_setExplain.end(); i++ )
{
//get explain atom
ExprNode* pAtom = (ExprNode*)*i;
//check if this assertion exists
ExprNodeSet::iterator iFind = m_pKB->m_setSyntacticAssertions.find(pAtom);
if( iFind != m_pKB->m_setSyntacticAssertions.end() )
{
//if this entry does not exist then create it
DependencyEntry* pDE = NULL;
Expr2DependencyEntry::iterator iFind2 = m_mapDependencies.find(pAtom);
if( iFind2 == m_mapDependencies.end() )
{
pDE = new DependencyEntry();
m_mapDependencies[pAtom] = pDE;
}
else
pDE = (DependencyEntry*)iFind2->second;
pDE->addTypeDependency(pInd, pType);
}
}
}
void DependencyIndex::addBranchAddDependency(Branch* pBranch)
{
for(ExprNodeSet::iterator i = pBranch->m_pTermDepends->m_setExplain.begin(); i != pBranch->m_pTermDepends->m_setExplain.end(); i++ )
{
ExprNode* pAtom = (ExprNode*)*i;
//check if this assertion exists
ExprNodeSet::iterator iFind = m_pKB->m_setSyntacticAssertions.find(pAtom);
if( iFind != m_pKB->m_setSyntacticAssertions.end() )
{
//if this entry does not exist then create it
DependencyEntry* pDE = NULL;
Expr2DependencyEntry::iterator iFind2 = m_mapDependencies.find(pAtom);
if( iFind2 == m_mapDependencies.end() )
{
pDE = new DependencyEntry();
m_mapDependencies[pAtom] = pDE;
}
else
pDE = (DependencyEntry*)iFind2->second;
//add the dependency
pDE->addBranchAddDependency(pAtom, pBranch->m_iBranch, pBranch);
// add depedency to index so that backjumping can be supported
// (ie, we need a fast way to remove the branch dependencies
#if 0
if(!branchIndex.containsKey(branch))
{
Set<BranchDependency> newS = new HashSet<BranchDependency>();
newS.add(newDep);
branchIndex.put(branch, newS);
}
else
{
branchIndex.get(branch).add(newDep);
}
#endif
}
}
}
void DependencyIndex::addCloseBranchDependency(Branch* pBranch, DependencySet* pDS)
{
for(ExprNodeSet::iterator i = pDS->m_setExplain.begin(); i != pDS->m_setExplain.end(); i++ )
{
//get explain atom
ExprNode* pAtom = (ExprNode*)*i;
//check if this assertion exists
ExprNodeSet::iterator iFind = m_pKB->m_setSyntacticAssertions.find(pAtom);
if( iFind != m_pKB->m_setSyntacticAssertions.end() )
{
//if this entry does not exist then create it
DependencyEntry* pDE = NULL;
Expr2DependencyEntry::iterator iFind2 = m_mapDependencies.find(pAtom);
if( iFind2 == m_mapDependencies.end() )
{
pDE = new DependencyEntry();
m_mapDependencies[pAtom] = pDE;
}
else
pDE = (DependencyEntry*)iFind2->second;
BranchDependency* pNewDep = pDE->addCloseBranchDependency(pAtom, pBranch);
// add depedency to index so that backjumping can be supported
// (ie, we need a fast way to remove the branch dependencies
#if 0
if(!branchIndex.containsKey(branch))
{
Set<BranchDependency> newS = new HashSet<BranchDependency>();
newS.add(newDep);
branchIndex.put(branch, newS);
}
else
{
branchIndex.get(branch).add(newDep);
}
#endif
}
}
}
DependencyEntry* DependencyIndex::getDependencies(ExprNode* pExprNode)
{
Expr2DependencyEntry::iterator i = m_mapDependencies.find(pExprNode);
if( i != m_mapDependencies.end() )
return (DependencyEntry*)i->second;
return NULL;
}
void DependencyIndex::removeDependencies(ExprNode* pExprNode)
{
m_mapDependencies.erase(pExprNode);
}
// Remove branch dependencies - this is needed due to backjumping!
void DependencyIndex::removeBranchDependencies(Branch* pBranch)
{
Branch2BranchDependencySetMap::iterator i = m_mapBranchIndex.find(pBranch);
if( i != m_mapBranchIndex.end() )
{
BranchDependencySet* pDeps = (BranchDependencySet*)i->second;
for(BranchDependencySet::iterator j = pDeps->begin(); j != pDeps->end(); j++)
{
BranchDependency* pBD = (BranchDependency*)*j;
if( pBD->m_iDependencyType == DEPENDENCY_BRANCHADD )
{
//remove the dependency
DependencyEntry* pDE = getDependencies(pBD->m_pAssertion);
pDE->m_setBranchAdds.erase(pBD);
}
}
}
else
{
//TODO: why is this null? is this because of duplicate entries in the index set?
//This seems to creep up in WebOntTest-I5.8-Manifest004 and 5 among others...
}
}