-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrismPolicy.java
251 lines (212 loc) · 8.21 KB
/
PrismPolicy.java
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;
//import com.google.common.base.Objects;
/**
* @author ashutosh
*
*/
public class PrismPolicy {
private static String m_policyFile;
public ArrayList<String> m_plan = new ArrayList<String>();
public PrismPolicy(String policyFile) {
m_policyFile = policyFile;
}
/**
* Generates a linear plan from a policy
* @param initial_state
* @param stateActionMap
* @param startEndStateMap
*/
private void extractPolicy(String initial_state,
Map<String, LinkedList<String>> stateActionMap, Map<String, LinkedList<String>> startEndStateMap) {
String state = initial_state;
String action = "";
while (startEndStateMap.containsKey(state)) { // While current state is mapped to something
boolean foundState = false;
String previousState = state;
for (String e: startEndStateMap.get(state)){ // For each of the alternative states to which a source state can be mapped (probabilistic branches)
if (startEndStateMap.containsKey(e)){ // Lookahead
action = stateActionMap.get(state).get(0);
state = e;
foundState=true;
}
}
if (!foundState) {
if ((startEndStateMap.get(state).size()==1) && (!startEndStateMap.containsKey(startEndStateMap.get(state).get(0)))){ // Special case for final state
action = stateActionMap.get(state).get(0);
state = startEndStateMap.get(state).get(0);
}
else if ((startEndStateMap.get(state).size() > 1 && !startEndStateMap.containsKey(startEndStateMap.get(state).get(0)) && !startEndStateMap.containsKey(startEndStateMap.get(state).get(1)) )) {
action = stateActionMap.get(state).get(0);
state = startEndStateMap.get(state).get(0);
}
}
if (action != "") {
m_plan.add(action);
}
if (previousState.equals(state)) break;
}
}
/**
* Obtains the initial state in an adversary/strategy file
* @return String id of the initial state
*/
public static String findInitialState(){
String initialState="";
Map<String, LinkedList<String>> t = new HashMap<String, LinkedList<String>>();
Scanner sc = null;
try {
sc = new Scanner(new File(m_policyFile));
} catch (FileNotFoundException e){
System.out.println("Error determining initial state in policy. File not found "+m_policyFile);
}
String l;
if (sc.hasNextLine())
{
l=sc.nextLine(); // Eliminate header line in adversary file
}
while (sc.hasNextLine()){
l = sc.nextLine();
String[] chunks = l.split(" ");
if (!t.containsKey(chunks[0])){
t.put(chunks[0], new LinkedList<String>());
}
t.get(chunks[0]).add(chunks[1]);
}
for (Map.Entry<String, LinkedList<String>> e : t.entrySet()){
if (!isHit(t,e.getKey()) && t.get(e.getKey()).size()==1){
return e.getKey();
}
}
return initialState;
}
// Checks if a state id is mapped from something in the data structure
public static boolean isHit( Map<String, LinkedList<String>> l, String s){
for (Map.Entry<String, LinkedList<String>> e : l.entrySet()){
if (e.getValue().contains(s))
return true;
}
return false;
}
/**
* Reads an adversary/strategy file into a policy
* Fixed!! (feb 25), initial state was incorrectly detected and method would return wrong policy
* @return
*/
public boolean readPolicy() {
Map<String, LinkedList<String>> stateActionMap = new HashMap<String, LinkedList<String>>();
Map<String, LinkedList<String>> startEndStateMap = new HashMap<String, LinkedList<String>>();
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(m_policyFile);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
int line_no = 0;
boolean firstActionFound = false;
String startState="";
String endState="";
while((line = bufferedReader.readLine()) != null) {
++line_no;
if (line_no == 1)
{
continue; // Skip the first line
}
String[] elements = line.split(" ");
startState=elements[0];
endState = elements[1];
String action = "";
if (elements.length == 4) {
action = elements[3];
}
if (elements.length == 5) {
action = elements[4];
}
if (!stateActionMap.containsKey(startState)){
stateActionMap.put(startState, new LinkedList<String>());
}
stateActionMap.get(startState).add(action);
if (!startEndStateMap.containsKey(startState)){
startEndStateMap.put(startState, new LinkedList<String>());
}
startEndStateMap.get(startState).add(endState);
if (!firstActionFound && elements.length==4) {
firstActionFound = true;
}
}
bufferedReader.close();
this.extractPolicy (findInitialState (), stateActionMap, startEndStateMap);
}
catch(FileNotFoundException ex) {
System.out.println("Unable to open file '" + m_policyFile + "'");
}
catch(IOException ex) {
System.out.println("Error reading file '" + m_policyFile + "'");
}
return true;
}
/**
* Returns the linear plan associated with the policy
* @return List of strings encoding sequence of action labels, e.g., [action1, ..., actionN]
*/
public ArrayList<String> getPlan() {
return m_plan;
}
/**
* Returns the list of allowed reconfiguration actions in a plan
* @return
*/
public ArrayList<String> getAllowedReconfigurations(){
ArrayList<String> allowed = new ArrayList<String>();
for (int i=0; i< m_plan.size(); i++){
if (m_plan.get(i).startsWith("t_set")){
allowed.add(m_plan.get(i));
}
}
return allowed;
}
/*
public ArrayList<String> getPlan(ConfigurationProvider cp, String fromConfiguration){
String confSetPrefix = "t_set_";
ArrayList<String> res = new ArrayList<String>();
HashMap<String,List<String>> reconfs = cp.getLegalReconfigurationsFrom(fromConfiguration);
for (int i=0; i<m_plan.size(); i++){
if (m_plan.get(i).startsWith(confSetPrefix)){
res.addAll(reconfs.get(m_plan.get(i).replace(confSetPrefix, "")));
} else
res.add(m_plan.get(i));
}
return res;
}
*/
/**
* Class test
*/
public static void main (String[] args) throws Exception { // Class test
// PrismConnector conn = new PrismConnector (null);
// conn.invoke(8, 7);
// PrismPolicy prismPolicy = new PrismPolicy("/Users/jcamara/Dropbox/Documents/Work/Projects/BRASS/rainbow-prototype/trunk/rainbow-brass/prismtmp/botpolicy.adv");
//
System.out.println("Arguments from command line:");
for (String s: args) {
System.out.println(s);
}
System.out.println("Extracting the policy:");
String policyFilePath = args[0];
PrismPolicy prismPolicy = new PrismPolicy(policyFilePath);
prismPolicy.readPolicy();
System.out.println(prismPolicy.getPlan().toString());
}
}