-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.go
209 lines (168 loc) · 4.99 KB
/
utilities.go
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
/*
utilities.go
Description:
*/
package modelchecking
import (
"fmt"
)
/*
Subset
Description:
Determines if apSliceA is a subset of apSliceB
*/
func SliceSubset(slice1, slice2 interface{}) (bool, error) {
switch x := slice1.(type) {
case []AtomicProposition:
apSlice1, err1 := ToSliceOfAtomicPropositions(slice1)
apSlice2, err2 := ToSliceOfAtomicPropositions(slice2)
if (err1 != nil) || (err2 != nil) {
return false, fmt.Errorf("Error converting slice1: %v ; Error converting slice2: %v", err1, err2)
}
//Iterate through all AtomicPropositions in apSliceA and make sure that they are in B.
for _, apFrom1 := range apSlice1 {
if !(apFrom1.In(apSlice2)) {
return false, nil
}
}
// If all elements of slice1 are in slice2 then return true!
return true, nil
case []TransitionSystemState:
stateSlice1, ok1 := slice1.([]TransitionSystemState)
stateSlice2, ok2 := slice2.([]TransitionSystemState)
if (!ok1) || (!ok2) {
return false, fmt.Errorf("Error converting slice1 (%v) or slice2 (%v).", ok1, ok2)
}
//Iterate through all TransitionSystemState in stateSlice1 and make sure that they are in 2.
for _, stateFrom1 := range stateSlice1 {
if !(stateFrom1.In(stateSlice2)) {
return false, nil
}
}
// If all elements of slice1 are in slice2 then return true!
return true, nil
default:
return false, fmt.Errorf("Unexpected type given to SliceSubset(): %v", x)
}
}
/*
SliceEquals
Description:
*/
func SliceEquals(slice1, slice2 interface{}) (bool, error) {
//Determine if both slices are of the same type.
// if slice1.(type) != slice2.(type) {
// fmt.Println("Types of the two slices are different!")
// return false
// }
oneSubsetTwo, err := SliceSubset(slice1, slice2)
if err != nil {
return false, fmt.Errorf("There was an issue computing SliceSubset(slice1,slice2): %v", err)
}
twoSubsetOne, err := SliceSubset(slice2, slice1)
if err != nil {
return false, fmt.Errorf("There was an issue computing SliceSubset(slice2,slice1): %v", err)
}
return oneSubsetTwo && twoSubsetOne, nil
}
/*
FindInSlice
Description:
Identifies if the input xIn is in the slice sliceIn.
If it is, then this function returns the index such that xIn = sliceIn[index] and the bool value true.
If it is not, then this function returns the index -1 and the boolean value false.
*/
func FindInSlice(xIn interface{}, sliceIn interface{}) (int, bool) {
x := xIn.(string)
slice := sliceIn.([]string)
xLocationInSliceIn := -1
for sliceIndex, sliceValue := range slice {
if x == sliceValue {
xLocationInSliceIn = sliceIndex
}
}
return xLocationInSliceIn, xLocationInSliceIn >= 0
}
/*
GetBeverageVendingMachineTS
Description:
Creates the beloved Vending Machine example which is used in a lot of Principles of Model Checking.
*/
func GetBeverageVendingMachineTS() TransitionSystem {
ts0, err := GetTransitionSystem(
[]string{"pay", "select", "beer", "soda"}, []string{"", "insert_coin", "get_beer", "get_soda"},
map[string]map[string][]string{
"pay": map[string][]string{
"insert_coin": []string{"select"},
},
"select": map[string][]string{
"": []string{"beer", "soda"},
},
"beer": map[string][]string{
"get_beer": []string{"pay"},
},
"soda": map[string][]string{
"get_soda": []string{"pay"},
},
},
[]string{"pay"},
[]string{"paid", "drink"},
map[string][]string{
"pay": []string{},
"soda": []string{"paid", "drink"},
"beer": []string{"paid", "drink"},
"select": []string{"paid"},
},
)
if err != nil {
fmt.Println(fmt.Sprintf("There was an issue constructing the beverage vending machine! %v", err.Error()))
}
return ts0
}
/*
SliceCartesianProduct
Description:
*/
func SliceCartesianProduct(slice1, slice2 interface{}) (interface{}, error) {
switch x := slice1.(type) {
// case []AtomicProposition:
case []TransitionSystemState:
var productSet [][]TransitionSystemState
slice1Converted, ok1 := slice1.([]TransitionSystemState)
slice2Converted, ok2 := slice2.([]TransitionSystemState)
if (!ok1) || (!ok2) {
return nil, fmt.Errorf("There was an issue converting either slice1 (%v) or slice2 (%v) to []TransitionSystemState objects.", ok1, ok2)
}
for _, item1 := range slice1Converted {
for _, item2 := range slice2Converted {
productSet = append(productSet, []TransitionSystemState{item1, item2})
}
}
return productSet, nil
default:
return false, fmt.Errorf("Unexpected type given to SliceSubset(): %v", x)
}
}
/*
AppendIfUnique
Description:
Appends to the input slice sliceIn if and only if the new state
is actually a unique state.
*/
func AppendIfUnique(sliceIn []string, xIn ...string) []string {
newSlice := sliceIn
var xElementInSlice bool
for _, xElement := range xIn {
xElementInSlice = false
// Check to see if the State is equal to any of the ones in the list.
for _, stringFromSlice := range sliceIn {
if stringFromSlice == xElement {
xElementInSlice = true
}
}
if !xElementInSlice {
newSlice = append(newSlice, xElement)
}
}
return newSlice
}