|
1
|
|
|
package it.cnr.istc.pst.platinum.executive.dc.strategy; |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
import java.util.ArrayList; |
|
5
|
|
|
import java.util.HashMap; |
|
6
|
|
|
import java.util.HashSet; |
|
7
|
|
|
import java.util.List; |
|
8
|
|
|
import java.util.Map; |
|
9
|
|
|
import java.util.Set; |
|
10
|
|
|
|
|
11
|
|
|
import it.cnr.istc.pst.platinum.executive.dc.strategy.result.Action; |
|
12
|
|
|
import it.cnr.istc.pst.platinum.executive.dc.strategy.result.Transition; |
|
13
|
|
|
import it.cnr.istc.pst.platinum.executive.dc.strategy.result.Wait; |
|
14
|
|
|
|
|
15
|
|
|
public class TreeStrategy implements Strategy { |
|
16
|
|
|
private static int MAX_STRIKE = 999999; |
|
17
|
|
|
|
|
18
|
|
|
private long horizon; |
|
19
|
|
|
private long time; |
|
20
|
|
|
private int strikeTimer; |
|
21
|
|
|
private int strikeMaxReached; |
|
22
|
|
|
private TreeNodeState rootState; |
|
23
|
|
|
private Map<String,Long> timelineClocks; |
|
24
|
|
|
private Map<String,String> initialState; //substituted with root of tree, maintained for compatibility with older version |
|
25
|
|
|
private Set<String> uStates; |
|
26
|
|
|
private Map<Transition,Map<String,String>> uPostConditions; |
|
27
|
|
|
|
|
28
|
|
|
private Map<Long,Integer> out; |
|
29
|
|
|
|
|
30
|
|
|
private Set<StateSet> listStrategy; |
|
31
|
|
|
//private PrintWriter writer; |
|
32
|
|
|
|
|
33
|
|
|
// ------------------------------ CONSTRUCTORS ------------------ |
|
34
|
|
|
|
|
35
|
|
|
public TreeStrategy(long horizon) { |
|
36
|
|
|
this.horizon = horizon; |
|
37
|
|
|
//this.rootState = new TreeNodeState(); |
|
38
|
|
|
this.initialState = new HashMap<>(); |
|
39
|
|
|
this.uPostConditions = new HashMap<>(); |
|
40
|
|
|
this.listStrategy = new HashSet<>(); |
|
41
|
|
|
this.time = 0; |
|
42
|
|
|
this.out = new HashMap<>(); |
|
43
|
|
|
this.strikeTimer = 0; |
|
44
|
|
|
this.strikeMaxReached = 0 ; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
//---------------------------------- METHODS -------------------- |
|
48
|
|
|
|
|
49
|
|
|
// ! MUST BE CALLED AFTER ALL DATA IS IN |
|
50
|
|
|
@Override |
|
51
|
|
|
public void buildStrategyStructure() { |
|
52
|
|
|
for( StateSet set : this.listStrategy){ |
|
53
|
|
|
if(set.isStateSetStatus(this.initialState)) { |
|
54
|
|
|
this.listStrategy.remove(set); //should not exist more |
|
55
|
|
|
this.rootState = new TreeNodeState(set,listStrategy,uStates); |
|
56
|
|
|
//this.listStrategy = null; |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
//returns next strategy step(s) |
|
63
|
|
|
@Override |
|
64
|
|
|
public List<Action> askAllStrategySteps(long plan_clock, Map<String,String> actualState, boolean isPlanClock) { //throws Exception { |
|
65
|
|
|
//System.out.println(actualState + "plan clock " + plan_clock + "\n" + "clock " + this.timelineClocks); |
|
66
|
|
|
long time = System.currentTimeMillis(); |
|
|
|
|
|
|
67
|
|
|
List<Action> actions = new ArrayList<>(); |
|
68
|
|
|
this.updateExpectedState(actualState); |
|
69
|
|
|
this.updateClocks(plan_clock-(this.timelineClocks.get("plan"))); |
|
70
|
|
|
//System.out.println(initialState + "plan clock " + plan_clock + "clock " + this.timelineClocks); |
|
71
|
|
|
Action action = askSingleStrategyStep(initialState); |
|
72
|
|
|
//System.out.println(action + "\n"); |
|
73
|
|
|
actions.add(action); |
|
74
|
|
|
while(action.getClass().equals(Transition.class)) { |
|
75
|
|
|
action = askSingleStrategyStep(this.initialState); |
|
76
|
|
|
actions.add(action); |
|
77
|
|
|
} |
|
78
|
|
|
time = System.currentTimeMillis() - time; |
|
79
|
|
|
this.time = this.time + time; |
|
80
|
|
|
if(this.out.containsKey(time)) this.out.put(time, this.out.get(time)+1); |
|
81
|
|
|
else this.out.put(time, 1); |
|
82
|
|
|
System.out.println(">>>>>>> Answer all strategy steps: " + time + "ms, " + "current time: " + this.time + "\n" + this.out + "\n"); |
|
83
|
|
|
return actions; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
//returns next strategy step (repeat until wait!!) using tic |
|
87
|
|
View Code Duplication |
@Override |
|
|
|
|
|
|
88
|
|
|
public List<Action> askAllStrategySteps(long tic, Map<String,String> actualState) { // throws Exception { |
|
89
|
|
|
System.out.println(initialState + "tic " + tic + "\n\n"); |
|
90
|
|
|
long time = System.currentTimeMillis(); |
|
|
|
|
|
|
91
|
|
|
List<Action> actions = new ArrayList<>(); |
|
92
|
|
|
this.updateExpectedState(actualState); |
|
93
|
|
|
this.updateClocks(tic); |
|
94
|
|
|
Action action = askSingleStrategyStep(this.initialState); |
|
95
|
|
|
actions.add(action); |
|
96
|
|
|
while(action.getClass().equals(Transition.class)) { |
|
97
|
|
|
action = askSingleStrategyStep(this.initialState); |
|
98
|
|
|
actions.add(action); |
|
99
|
|
|
} |
|
100
|
|
|
time = System.currentTimeMillis() - time; |
|
101
|
|
|
System.out.println("\n"+ "Answer all strategy steps: " + time + "ms, " + "\n"); |
|
102
|
|
|
return actions; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
//returns one action predicted for next step ***** |
|
106
|
|
|
private Action askSingleStrategyStep(Map<String, String> actualState) { // throws Exception { |
|
107
|
|
|
//System.out.println("ASK SINGLE STRATEGY STEP: "+ actualState + "\n\n"); |
|
108
|
|
|
long time = System.currentTimeMillis(); |
|
|
|
|
|
|
109
|
|
|
try |
|
110
|
|
|
{ |
|
111
|
|
|
|
|
112
|
|
|
if(this.rootState.isStateSetStatus(actualState)) { |
|
113
|
|
|
System.out.println("Searching for next step of " + this.rootState); |
|
114
|
|
|
StateStrategy win = this.rootState.searchNextStepStrategy(timelineClocks); |
|
115
|
|
|
this.timelineClocks = win.applyPostConditions(this.timelineClocks, this.horizon); |
|
116
|
|
|
updateExpectedState(win.getAction()); |
|
117
|
|
|
time = System.currentTimeMillis() - time; |
|
118
|
|
|
//System.out.println("\n"+ "Answer single strategy steps: " + time + "ms\n"); |
|
119
|
|
|
return win.getAction(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
catch (Exception ex) { |
|
123
|
|
|
System.out.println("Warning: no state or clock found -> return default action WAIT\n"); |
|
124
|
|
|
time = System.currentTimeMillis() - time; |
|
|
|
|
|
|
125
|
|
|
//System.out.println("\n"+ "Answer single strategy steps: " + time + "ms\n"); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
// default action |
|
129
|
|
|
return new Wait(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
//update list of expected states with list of actual states given, resets local clocks if state changed |
|
133
|
|
|
private void updateExpectedState(Map<String,String> actualStates) { |
|
134
|
|
|
for(String timeline : actualStates.keySet()) { |
|
|
|
|
|
|
135
|
|
|
if(!this.initialState.get(timeline).equals(actualStates.get(timeline))) { |
|
136
|
|
|
resetClocks(timeline,this.initialState.get(timeline),actualStates.get(timeline)); |
|
137
|
|
|
this.initialState.put(timeline, actualStates.get(timeline)); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// Resets the local clocks of uncontrollable events that took place in the tic |
|
143
|
|
View Code Duplication |
private void resetClocks(String timeline,String from, String to) { |
|
|
|
|
|
|
144
|
|
|
//this.timelineClocks.put(timeline+"."+timeline+"_clock",0); |
|
145
|
|
|
//System.out.println(" RESET CLOCK : " + timeline + " " + token + "<<<<<<<<<<<<<<<<<<<\n"); |
|
146
|
|
|
Map<String,String> condToken = this.uPostConditions.get(new Transition(timeline,from,to)); |
|
147
|
|
|
//System.out.println("CONDTOKEN : " + condToken + "\n"); |
|
148
|
|
|
if(condToken!=null) { |
|
149
|
|
|
for(String cond : condToken.keySet()) { |
|
|
|
|
|
|
150
|
|
|
if(condToken.get(cond).contains("0")) { |
|
151
|
|
|
this.timelineClocks.put(cond, 0l); |
|
152
|
|
|
} |
|
153
|
|
|
if(condToken.get(cond).contains("H")) { |
|
154
|
|
|
this.timelineClocks.put(cond, this.horizon*2); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
this.timelineClocks.put(timeline, 0l); |
|
158
|
|
|
}} |
|
159
|
|
|
|
|
160
|
|
|
//updates, after a winning transition, the next expected state |
|
161
|
|
|
private void updateExpectedState(Action action) { |
|
162
|
|
|
if(action.getClass().equals(Transition.class)) { |
|
163
|
|
|
Transition t = (Transition) action; |
|
164
|
|
|
Map<String, String> rootStates = this.rootState.getParentState().getStateSet(); |
|
165
|
|
|
rootStates.put(t.getTimeline(),t.getTransitionTo().getToken()); |
|
166
|
|
|
this.rootState = this.rootState.getChild(rootStates); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
//updates clocks through plan clock |
|
171
|
|
|
private void updateClocks(long n) { |
|
172
|
|
|
for(String c : this.timelineClocks.keySet()) { |
|
|
|
|
|
|
173
|
|
|
//System.out.println(">>> INCREMENT CLOCK BY " + n + ":::: CLOCK " + c + "\n"); |
|
174
|
|
|
this.timelineClocks.put(c, this.timelineClocks.get(c)+n); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
@Override |
|
180
|
|
|
public void updateUncontrollable(Map<String, String> updatedState) { |
|
181
|
|
|
// TODO Auto-generated method stub |
|
182
|
|
|
System.out.println("Unimplemented method 'updateUncontrollable'\n"); |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
@Override |
|
187
|
|
|
public String toString() { |
|
188
|
|
|
return "Strategy [STRATEGY = " + this.rootState + "]"; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
//---------------------- GETTERS&SETTERS ------------------------ |
|
192
|
|
|
|
|
193
|
|
|
public Map<String,Long> getTimelineClocks() { |
|
194
|
|
|
return this.timelineClocks; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
@Override |
|
198
|
|
|
public void setTimelineClocks(Map<String,Long> tc) { |
|
199
|
|
|
this.timelineClocks = tc; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public Map<String, String> getExpectedState() { |
|
203
|
|
|
return initialState; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
@Override |
|
207
|
|
|
public void setInitialState(Map<String, String> expectedState) { |
|
208
|
|
|
this.initialState = expectedState; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public Map<Transition, Map<String, String>> getuPostConditions() { |
|
212
|
|
|
return uPostConditions; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
@Override |
|
216
|
|
|
public void setuPostConditions(Map<Transition, Map<String, String>> uPostConditions) { |
|
217
|
|
|
this.uPostConditions = uPostConditions; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
// @Override //may be obsolete |
|
221
|
|
|
// public void getStrategyAsList(Set<StateSet> accumulatedStates) { |
|
222
|
|
|
// this.listStrategy = accumulatedStates; |
|
223
|
|
|
// } |
|
224
|
|
|
|
|
225
|
|
|
@Override |
|
226
|
|
|
public boolean isOutOfBounds() { |
|
227
|
|
|
return (this.strikeTimer > TreeStrategy.MAX_STRIKE); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public Set<String> getuStates() { |
|
231
|
|
|
return uStates; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
@Override |
|
235
|
|
|
public void setuStates(Set<String> uStates) { |
|
236
|
|
|
this.uStates = uStates; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
@Override |
|
240
|
|
|
public int getStrikeMaxReached() { |
|
241
|
|
|
return this.strikeMaxReached; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
|
|
245
|
|
|
@Override |
|
246
|
|
|
public void getStrategyAsStrings(String ss, List<String> statesLine) { |
|
247
|
|
|
StateSet stateSet = new StateSet(ss,statesLine); |
|
248
|
|
|
this.listStrategy.add(stateSet); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/*public void setWriter(PrintWriter writer) { |
|
252
|
|
|
this.writer = writer; |
|
253
|
|
|
|
|
254
|
|
|
}*/ |
|
255
|
|
|
} |
|
256
|
|
|
|