-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersector.cpp
183 lines (156 loc) · 6.79 KB
/
Intersector.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
/**
CSE355 optional project, Intersected.h Intersected.cpp
Purpose: To generate an intersection of two tuples.
@author Suhail Ghafoor
@version 0.1 04/04/18
*/
#include "Intersector.h"
/**
* This class takes in 2 tuples, generates the compliment of one of them
* and then generates an intersection of them.
* @param spec - The tuple for the specifications automaton
* @param system - The tuple for the system model
* @param nicefy - Boolean value, if true renames the state names from string to integers
*/
Intersector::Intersector(DFA_flat_tuple *spec, DFA_flat_tuple *system, bool nicefy) {
this->spec = spec;
this->system = system;
intersected = new DFA_flat_tuple();
makeCompliment();
encodeStateNames();
makeIntersection();
if(nicefy){
nicefyNames();
}
}
/**
* Generates the compliment of the specifications automaton finding all states
* that are not currently in final states and then replacing the old final states
* with the newly filtered states
*/
void Intersector::makeCompliment() {
vector<string> nFinStates;
for(vector<string>::size_type z = 0; z != this->spec->states.size(); z++){
if(find(this->spec->finStates.begin(), this->spec->finStates.end(), this->spec->states[z]) != this->spec->finStates.end()) {
/* v contains x */
} else {
nFinStates.push_back(this->spec->states[z]);
}
}
this->spec->finStates = nFinStates;
}
/**
* Generates the intersection of two tuples by making a list of new states.
* This function is relatively spmple in complexity.
*/
void Intersector::makeIntersection() {
//intersect states
for(vector<string>::size_type z = 0; z != this->spec->states.size(); z++){
for(vector<string>::size_type x = 0; x != this->system->states.size(); x++){
string intersectedStateName = this->spec->states[z] + this->system->states[x];
intersected->states.push_back(intersectedStateName);
}
}
//add alphabets which are same as original machine
intersected->alphabet = this->spec->alphabet;
//initial state
string intersectedInitialState = this->spec->initialState + this->system->initialState;
intersected->initialState = intersectedInitialState;
//final states
for(vector<string>::size_type z = 0; z != this->spec->finStates.size(); z++){
for(vector<string>::size_type x = 0; x != this->system->finStates.size(); x++){
string intersectedFinState = this->spec->finStates[z] + this->system->finStates[x];
intersected->finStates.push_back(intersectedFinState);
}
}
//intersectioned transitions
for(vector<string>::size_type z = 0; z != this->spec->states.size(); z++){
for(vector<string>::size_type x = 0; x != this->system->states.size(); x++){
for(vector<string>::size_type y = 0; y != this->spec->alphabet.size(); y ++){
string nStateSpec = findTransition(this->spec->states[z], this->spec->alphabet[y], this->spec->transitions);
string nStateSys = findTransition(this->system->states[x], this->spec->alphabet[y], this->system->transitions);
auto * trans = new Flat_transition();
trans->initState = this->spec->states[z] + this->system->states[x];
trans->alphabet = this->spec->alphabet[y];
trans->nState = nStateSpec + nStateSys;
intersected->transitions.push_back(trans);
}
}
}//all done
}
/**
* Encodes state names so the combination of state names do not generate duplicates.
*/
void Intersector::encodeStateNames() {
string specMachine = "SP#";
string sysMachine = "#SYS";
//encode the state names of specifications tuple
for(vector<string>::size_type z = 0; z != this->spec->states.size(); z++){
this->spec->states[z] += specMachine;
}
//encode the final state names of specifications tuple
for(vector<string>::size_type z = 0; z != this->spec->finStates.size(); z++){
this->spec->finStates[z] += specMachine;
}
//encode the transitions of the specifications tuple
for(vector<Flat_transition*>::size_type z = 0; z != this->spec->transitions.size(); z++){
this->spec->transitions[z]->initState += specMachine;
this->spec->transitions[z]->nState += specMachine;
}
//encode the initial state of specifications tuple
this->spec->initialState += specMachine;
//encode the state names of system model tuple
for(vector<string>::size_type z = 0; z != this->system->states.size(); z++){
this->system->states[z].insert(0, sysMachine);
}
//encode the final state names of system model tuple
for(vector<string>::size_type z = 0; z != this->system->finStates.size(); z++){
this->system->finStates[z].insert(0, sysMachine);
}
//encode the transitions of the system model tuple
for(vector<Flat_transition*>::size_type z = 0; z != this->system->transitions.size(); z++){
this->system->transitions[z]->initState.insert(0, sysMachine);
this->system->transitions[z]->nState.insert(0, sysMachine);
}
//encode the initial state of system model tuple
this->system->initialState.insert(0, sysMachine);
}
/**
* Given a state name, alphabet and a transitions vector, returns the name of the new state
* @param state
* @param alpha
* @param ft
* @return
*/
string Intersector::findTransition(string state, string alpha, vector<Flat_transition*> ft){
for(vector<Flat_transition*>::size_type z = 0; z != ft.size(); z++){
if(ft[z]->initState == state && ft[z]->alphabet == alpha){
return ft[z]->nState;
}
}
return "ERROR";
}
DFA_flat_tuple * Intersector::giveIntersected() {
return intersected;
}
void Intersector::nicefyNames() {
for(vector<string>::size_type z = 0; z != intersected->states.size(); z++){
intersected->states[z] = to_string(giveStateNum(intersected->states[z]));
}
for(vector<Flat_transition*>::size_type z = 0; z != intersected->transitions.size(); z++){
intersected->transitions[z]->initState = to_string(giveStateNum(intersected->transitions[z]->initState));
intersected->transitions[z]->nState = to_string(giveStateNum(intersected->transitions[z]->nState));
}
for(vector<string>::size_type z = 0; z != intersected->finStates.size(); z++){
intersected->finStates[z] = to_string(giveStateNum(intersected->finStates[z]));
}
intersected->initialState = to_string(giveStateNum(intersected->initialState));
}
int Intersector::giveStateNum(string name) {
if(find(niceNames.begin(), niceNames.end(), name) != niceNames.end()) {
} else {
niceNames.push_back(name);
}
auto pos = std::distance(niceNames.begin(), find(niceNames.begin(), niceNames.end(), name));
return (int)pos;
}