-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPreTableau.cpp
More file actions
291 lines (218 loc) · 7.49 KB
/
PreTableau.cpp
File metadata and controls
291 lines (218 loc) · 7.49 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "PreTableau.h"
#include "LTLFormula.h"
#include <iostream>
#pragma region PreTableauState
PreTableauState::PreTableauState(int state, LTLFormulaSet&& formulas, const InterpretationFunction& interpretation) :
state(state),
formulas(formulas),
interpretation(interpretation)
{
}
PreTableauState::PreTableauState(int state, LTLFormulaSet && formulas, InterpretationFunction && interpretation) :
state(state),
formulas(std::move(formulas)),
interpretation(std::move(interpretation))
{
}
PreTableauState::~PreTableauState()
{
}
void PreTableauState::addChild(PreTableauState& child)
{
this->children.push_back(&child);
child.parents.push_back(this);
}
void PreTableauState::dissociate(bool fromParents, bool fromChildren)
{
if (fromParents)
{
for (auto parent : this->parents)
{
parent->children.erase(std::find(parent->children.begin(), parent->children.end(), this));
}
this->parents.clear();
}
if (fromChildren)
{
for (auto child : this->children)
{
child->parents.erase(std::find(child->parents.begin(), child->parents.end(), this));
}
this->children.clear();
}
}
bool PreTableauState::operator==(const PreTableauState & other) const
{
// std::cout << "\t" << a << " " << (this->state == other.state) << " " << (this->formulas == other.formulas) << " " << (this->interpretation == other.interpretation) << std::endl;;
return this->state == other.state && this->formulas == other.formulas && this->interpretation == other.interpretation;
}
//bool PreTableauState::operator==(const TableauState & other) const
//{
// return this->state == other.getState() && this->formulas == other.getFormulas() && this->interpretation == other.getInterpretationFunction();
//}
int PreTableauState::lastID = 0;
#pragma endregion
#pragma region PreTableau
PreTableau::PreTableau(const Model* model, LTLFormula* formula) :
model(model),
formula(std::move(formula))
{
}
PreTableau::~PreTableau()
{
}
void PreTableau::createInitialState()
{
LTLFormulaSet set;
set.addFormula(this->formula->copy());
const auto& ints = this->model->getInterpretationFunction().getInterpretations(0);
std::unique_ptr<LTLFormula>* formula = set.addFormulas(ints.size());
for (const auto& interp : ints)
{
formula++->reset(new VariableOp(interp.first, interp.second == Interpretation::FALSE));
}
this->preStates.push_back(std::make_unique<PreTableauState>(0, std::move(set), this->model->getInterpretationFunction()));
this->rootState = this->preStates.back().get();
this->rootState->setId();
this->currentPreStates.push_back(this->rootState);
}
void PreTableau::expRule()
{
for (const auto& prestate : this->currentPreStates)
{
std::vector<LTLFormulaSet> fullExp = prestate->getFormulas().fullExpansion();
for (LTLFormulaSet& formulaSet : fullExp)
{
//update interpretation function
InterpretationFunction inter = prestate->getInterpretationFunction();
for (VariableOp* var : formulaSet.getLitterals())
{
if (inter.get(prestate->getState(), var->getName()) == Interpretation::UNKNOWN)
inter.set(prestate->getState(), var->getName(), var->isNeg() ? Interpretation::FALSE : Interpretation::TRUE);
}
PreTableauState newState(prestate->getState(), std::move(formulaSet), std::move(inter));
//check if state already present
bool stateAlreadyPresent = false;
for (auto& state : this->states)
{
if (*state == newState)
{
prestate->addChild(*state);
stateAlreadyPresent = true;
break;
}
}
//don't add offspring State if model state has no successors
if (!stateAlreadyPresent && this->model->getSuccessors(prestate->getState()).size() > 0)
{
newState.setId();
this->states.push_back(std::make_unique<PreTableauState>(std::move(newState)));
PreTableauState* newStatePointer = this->states.back().get();
prestate->addChild(*newStatePointer);
this->currentStates.push_back(newStatePointer);
}
}
}
this->currentPreStates.clear();
}
void PreTableau::nextRule()
{
for (const auto& state : this->currentStates)
{
for (int modelState : this->model->getSuccessors(state->getState()))
{
//calculate scomp
std::vector<std::unique_ptr<LTLFormula>> scomps;
for (const auto& formula : state->getFormulas())
{
if (formula->getOperatorType() == OperatorType::SUCCESSOR)
for (auto& comp : formula->getComponents())
scomps.push_back(std::move(comp));
}
//create formula set
LTLFormulaSet formulaSet;
std::unique_ptr<LTLFormula>* currFormula = formulaSet.addFormulas(scomps.size() + state->getInterpretationFunction().getInterpretations(modelState).size());
for (auto& comp : scomps)
currFormula++->swap(comp);
//add litterals
for (const auto& litteral : state->getInterpretationFunction().getInterpretations(modelState))
{
currFormula++->reset(new VariableOp(litteral.first, litteral.second == Interpretation::FALSE));
}
PreTableauState newPreState(modelState, std::move(formulaSet), state->getInterpretationFunction());
//check if prestate is already present
bool preStateAlreadyPresent = false;
for (auto& alreadyPresentPrestate : this->preStates)
{
if (*alreadyPresentPrestate == newPreState)
{
state->addChild(*alreadyPresentPrestate);
preStateAlreadyPresent = true;
break;
}
}
if (!preStateAlreadyPresent)
{
newPreState.setId();
this->preStates.push_back(std::make_unique<PreTableauState>(std::move(newPreState)));
PreTableauState* newPreStatePointer = this->preStates.back().get();
state->addChild(*newPreStatePointer);
this->currentPreStates.push_back(newPreStatePointer);
}
}
}
this->currentStates.clear();
}
void PreTableau::preTableauComputation()
{
this->createInitialState();
while (this->currentPreStates.size() > 0 || this->currentStates.size() > 0)
{
this->expRule();
this->nextRule();
}
}
Tableau PreTableau::convertToTableau()
{
//prestateElim + StateElim1
std::vector<std::unique_ptr<TableauState>> states;
std::map<PreTableauState*, TableauState*> correspondingState;
std::vector<TableauState*> roots;
roots.reserve(this->rootState->getChildren().size());
PreTableauState::resetNextIds();
for (const std::unique_ptr<PreTableauState>& prestate : this->preStates)
{
for (PreTableauState* parent : prestate->getParents())
{
TableauState* currentparentTableau;
if (auto it = correspondingState.find(parent); it == correspondingState.end())
{
currentparentTableau = states.emplace_back(new TableauState(parent->getState(), std::move(parent->getFormulas()), std::move(parent->getInterpretationFunction()))).get();
currentparentTableau->setId();
correspondingState.insert_or_assign(parent, currentparentTableau);
}
else
currentparentTableau = it->second;
for (PreTableauState* child : prestate->getChildren())
{
TableauState* currentChildTableau;
if (auto it = correspondingState.find(child); it == correspondingState.end())
{
currentChildTableau = states.emplace_back(new TableauState(child->getState(), std::move(child->getFormulas()), std::move(child->getInterpretationFunction()))).get();
currentChildTableau->setId();
correspondingState.insert_or_assign(child, currentChildTableau);
}
else
currentChildTableau = it->second;
currentparentTableau->addChild(*currentChildTableau);
}
}
}
for (const auto& s : states)
{
if (s->getFormulas().containsFormula(this->formula))
roots.push_back(s.get());
}
return Tableau(this->model, std::move(this->formula), std::move(states), std::move(roots));
}
#pragma endregion