-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTableau.cpp
187 lines (147 loc) · 3.9 KB
/
Tableau.cpp
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
#include "Tableau.h"
#include <algorithm>
#include "LTLFormula.h"
#include "PreTableau.h"
//
//#pragma region TableauState
//
//TableauState::TableauState(int state, LTLFormulaSet&& formulas, const InterpretationFunction& interpretation) :
// state(state),
// formulas(std::move(formulas)),
// interpretation(interpretation)
//{
// this->id = TableauState::lastID++;
//}
//
//TableauState::TableauState(int state, LTLFormulaSet&& formulas, InterpretationFunction&& interpretation) :
// state(state),
// formulas(std::move(formulas)),
// interpretation(std::move(interpretation))
//{
// this->id = TableauState::lastID++;
//}
//
//
//TableauState::~TableauState()
//{
//}
//
//
//bool TableauState::operator==(const TableauState & other) const
//{
// return this->state == other.state && this->formulas == other.formulas && this->interpretation == other.interpretation;
//}
//
//int TableauState::lastID = 0;
//
//#pragma endregion
//
#pragma region Tableau
Tableau::Tableau(const Model* model, LTLFormula* formula, std::vector<std::unique_ptr<TableauState>>&& states, std::vector<TableauState*>&& rootStates):
model(model),
formula(formula),
states(std::move(states)),
roots(rootStates)
{
}
Tableau::~Tableau()
{
}
void Tableau::stateElim1()
{
//suppr all states without any sucessors
for (const auto& state : this->states)
{
if (!state->isDissociated() && state->getChildren().size() == 0)
{
state->dissociate(true, false);
//remove from roots
if (auto it = std::find(this->roots.begin(), this->roots.end(), state.get()); it != this->roots.end())
{
this->roots.erase(it);
}
}
}
}
void Tableau::stateElim2()
{
for (const auto& state : this->states)
{
if (state->isDissociated())
continue;
for (const auto& formula : state->getFormulas())
{
LTLFormula* f = formula->getEventuality();
if (f != nullptr)
{
std::vector<const TableauState*> alreadyVisitedStates;
if (!this->isEventualityRealised(f, state.get(), alreadyVisitedStates))
{
state->dissociate(true, true);
//remove from roots
if (auto it = std::find(this->roots.begin(), this->roots.end(), state.get()); it != this->roots.end())
{
this->roots.erase(it);
}
break;
}
}
}
}
}
bool Tableau::isEventualityRealised(const LTLFormula * eventuality, const TableauState * fromState, std::vector<const TableauState*>& alreadyVisitedStates) const
{
alreadyVisitedStates.push_back(fromState);
if (fromState->getFormulas().containsFormula(eventuality))
return true;
for (const TableauState* child : fromState->getChildren())
{
if (std::find(alreadyVisitedStates.begin(), alreadyVisitedStates.end(), child) == alreadyVisitedStates.end())
{
if (isEventualityRealised(eventuality, child, alreadyVisitedStates))
return true;
}
}
return false;
}
void Tableau::tableauComputation()
{
size_t oldStateCount = 0;
size_t oldOldStateCount = 0;
while (oldStateCount != this->states.size() || oldOldStateCount != this->states.size())
{
oldOldStateCount = oldStateCount;
oldStateCount = this->states.size();
this->stateElim1();
this->stateElim2();
this->states.erase(std::remove_if(this->states.begin(), this->states.end(), [](const std::unique_ptr<TableauState>& x)
{
return x->isDissociated();
}), this->states.end());
}
}
Tableau::operator std::string() const
{
std::string str;
str += "\n\nTableau : \n" + std::to_string(this->states.size()) + " States : \n";
for (const auto& state : this->states)
{
str += "\t" + std::to_string(state->id) + " : ";
for (const auto& formula : state->getFormulas())
{
str += formula->operator std::string() + ", ";
}
str += "\n\t\t" + std::to_string(state->getChildren().size()) + " Children : ";
for (const auto& child : state->getChildren())
{
str += std::to_string(child->id) + ", ";
}
str += "\n";
}
return str;
}
bool Tableau::isOpen() const
{
return this->roots.size() > 0;
}
#pragma endregion