-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathSpecSubsets.java
More file actions
208 lines (168 loc) · 7.34 KB
/
SpecSubsets.java
File metadata and controls
208 lines (168 loc) · 7.34 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
import net.sf.javabdd.BDD;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import edu.wis.jtlv.env.Env;
import edu.wis.jtlv.env.module.SMVModule;
import edu.wis.jtlv.env.spec.Spec;
import edu.wis.jtlv.env.spec.Operator;
import edu.wis.jtlv.env.spec.SpecBDD;
import edu.wis.jtlv.env.spec.SpecExp;
public class SpecSubsets {
public enum ConjunctType {
INITIAL, SAFETY, LIVENESS, OTHER;
}
public static void main(String[] args) throws Exception {
// Check that we have enough arguments
if (args.length < 2) {
System.err.println("Usage: java SpecSubsets <smv_file> <ltl_file>");
System.exit(1);
}
// Load SMV and LTL files
Env.loadModule(args[0]);
Spec[] spcs = Env.loadSpecFile(args[1]);
// Parse the specs
Spec[] e_conjuncts = GROneParser.parseConjuncts(spcs[0]);
Spec[] s_conjuncts = GROneParser.parseConjuncts(spcs[1]);
// Load all of the environment spec
SMVModule m_env = (SMVModule) Env.getModule("main.e");
GROneParser.addReactiveBehavior(m_env, e_conjuncts);
SMVModule m_sys = (SMVModule) Env.getModule("main.s");
// Load all of the system initials and goals
GROneParser.addReactiveBehavior(m_sys, getConjunctsByType(s_conjuncts, ConjunctType.INITIAL));
GROneParser.addReactiveBehavior(m_sys, getConjunctsByType(s_conjuncts, ConjunctType.LIVENESS));
m_sys.initial().printSet();
// Incrementally load the system safeties
for (Spec safety_conjunct : getConjunctsByType(s_conjuncts, ConjunctType.SAFETY)) {
clearSpecPartFromModule(m_sys, ConjunctType.SAFETY); // Remove this line if you want to accumulate
System.out.println("Adding " + safety_conjunct);
GROneParser.addReactiveBehavior(m_sys, new Spec[]{safety_conjunct});
m_sys.trans().printSet();
System.out.println("=====================================");
}
}
public static String iteratedCoreSafetyIndices (String[] args) throws Exception {
return (iteratedCoreSafetyIndices (args, false));
}
public static String iteratedCoreSafetyIndices (String[] args, boolean verbose) throws Exception {
// Check that we have enough arguments
if (args.length < 2) {
System.err.println("Usage: java SpecSubsets <smv_file> <ltl_file>");
System.exit(1);
}
// Load SMV and LTL files
Spec[] spcs = Env.loadSpecFile(args[1]);
// Parse the specs
Spec[] e_conjuncts = GROneParser.parseConjuncts(spcs[0]);
Spec[] s_conjuncts = GROneParser.parseConjuncts(spcs[1]);
// Load all of the environment spec
SMVModule m_env = (SMVModule) Env.getModule("main.e");
GROneParser.addReactiveBehavior(m_env, e_conjuncts);
SMVModule m_sys = (SMVModule) Env.getModule("main.s");
// Load all of the system initials and goals
GROneParser.addReactiveBehavior(m_sys, getConjunctsByType(s_conjuncts, ConjunctType.INITIAL));
GROneParser.addReactiveBehavior(m_sys, getConjunctsByType(s_conjuncts, ConjunctType.LIVENESS));
Spec[] all_safeties = getConjunctsByType(s_conjuncts, ConjunctType.SAFETY);
GROneParser.addReactiveBehavior(m_sys, all_safeties);
BDD all_init = m_sys.initial().and(m_env.initial());
if (verbose) {
System.out.println("Initial Conditions " + all_init);
System.out.println("Full safety specification " + m_sys.trans());
}
BDD counter_exmple;
GROneGame g;
int i = 0;
int j = 0;
List<Integer> list = new LinkedList<Integer>();
for (int k = 0; k <all_safeties.length; k++) {
list.add(k);
}
//populate list of core candidates with all conjuncts
ArrayList<Integer> candidates = new ArrayList<Integer>(list);
// Remove the system safeties one at a time
for (Spec safety_conjunct : all_safeties) {
if (verbose) System.out.println("Removing conjunct " + i + " = " + safety_conjunct);
clearSpecPartFromModule(m_sys, ConjunctType.SAFETY);
j = 0;
for (Spec safety_conjunct2 : all_safeties) {
//add only those conjuncts that are being considered, excluding the current conjunct
if (i != j && candidates.contains(new Integer(j))) {
GROneParser.addReactiveBehavior(m_sys, new Spec[]{safety_conjunct2});
}
j++;
}
//check synthesizability
g = new GROneGame(m_env,m_sys);
counter_exmple = g.envWinningStates().and(all_init);
//if removing this conjunct has made the specification synthesizable, add it back in
if (counter_exmple.isZero()) {
if (verbose) System.out.println("Added it back in" + safety_conjunct);
} else {
//this is no longer a candidate guilty conjunct
candidates.remove(new Integer(i));
}
if (verbose) System.out.println("=====================================");
i++;
}
int[] ret = new int[candidates.size()];
for(int n = 0;n < ret.length;n++) {
ret[n] = ((Integer)candidates.get(n)).intValue();
}
return Arrays.toString(ret);
}
public static Spec[] getConjunctsByType(Spec[] sp, ConjunctType t) throws Exception {
ArrayList<Spec> conjuncts = new ArrayList<Spec>();
for (Spec conjunct : sp) {
if (getConjunctType(conjunct) == t) {
conjuncts.add(conjunct);
}
}
return conjuncts.toArray(new Spec[0]);
}
public static String getOutermostTemporalOperators(Spec sp) throws Exception {
// Stop recursion when we reach a pure BDD
if (sp instanceof SpecBDD) {
return "";
}
// This should never happen
if (!(sp instanceof SpecExp)) {
throw new Exception("Encountered subconjunct that is neither SpecBDD or SpecExp");
}
// Cast to SpecExp
SpecExp spe = (SpecExp) sp;
// Add the temporal operator we see and recurse
if (spe.getOperator() == Operator.GLOBALLY) {
return "G" + getOutermostTemporalOperators(spe.getChildren()[0]);
} else if (spe.getOperator() == Operator.FINALLY) {
return "F" + getOutermostTemporalOperators(spe.getChildren()[0]);
} else {
throw new Exception("Encountered SpecExp that is neither GLOBALLY or FINALLY");
}
}
public static ConjunctType getConjunctType(Spec sp) throws Exception {
String operators = getOutermostTemporalOperators(sp);
if (operators.equals("")) {
return ConjunctType.INITIAL;
} else if (operators.equals("G")) {
return ConjunctType.SAFETY;
} else if (operators.equals("GF")) {
return ConjunctType.LIVENESS;
} else {
return ConjunctType.OTHER;
}
}
private static void clearSpecPartFromModule(SMVModule m, ConjunctType t) throws Exception {
// For some unknown reason, removeAllIniRestrictions() and removeAllTransRestrictions() don't seem to work
if (t == ConjunctType.INITIAL) {
m.setInitial(Env.TRUE());
} else if (t == ConjunctType.SAFETY) {
m.disjunctTrans(Env.TRUE());
} else if (t == ConjunctType.LIVENESS) {
// Gotta clear these out one by one, apparently
while (m.justiceNum() > 0) {
m.popLastJustice();
}
}
}
}