-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAggregatedTransitionSystem.java
More file actions
258 lines (213 loc) · 9.43 KB
/
AggregatedTransitionSystem.java
File metadata and controls
258 lines (213 loc) · 9.43 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
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
252
253
254
255
256
257
258
package logic;
import models.*;
import java.util.*;
import java.util.stream.Collectors;
public abstract class AggregatedTransitionSystem extends TransitionSystem {
protected final TransitionSystem[] systems;
private final HashMap<Clock, Integer> maxBounds = new HashMap<>();
private final HashSet<State> passed = new HashSet<>();
private final Queue<State> worklist = new ArrayDeque<>();
private Automaton resultant = null;
public AggregatedTransitionSystem(TransitionSystem... systems)
throws IllegalArgumentException {
if (systems.length == 0) {
throw new IllegalArgumentException("Aggregated transition system must consists of least one transition system");
}
this.systems = systems;
for (TransitionSystem system : systems) {
clocks.addAll(
system.getClocks()
);
BVs.addAll(
system.getBVs()
);
maxBounds.putAll(
system.getMaxBounds()
);
}
}
@Override
public List<SimpleTransitionSystem> getSystems() {
return Arrays.stream(systems)
.map(TransitionSystem::getSystems)
.flatMap(List::stream)
.collect(Collectors.toList());
}
@Override
public Location getInitialLocation() {
return getInitialLocation(systems);
}
@Override
public Automaton getAutomaton() {
// No need for recomputing the same composition
if (resultant != null) {
return resultant;
}
/* Before creating the composition and thereby initialising the CDD.
* We must ensure that the underlying operands (Transition systems),
* have processed their automaton such that we won't start multiple
* CDDs by invoking "GetAutomaton" on the underlying TransitionSystems */
Automaton[] automata = new Automaton[systems.length];
for (int i = 0; i < systems.length; i++) {
automata[i] = systems[i].getAutomaton();
}
/* We utilise a try-finally such that we can correctly clean up whilst still immediately
* rethrow the exceptions as we can't handle a failure (most likely from the CDD).
* This especially helps increase the meaning of failing tests */
try {
resultant = aggregate(automata);
} finally {
CDDRuntime.done();
}
return resultant;
}
@Override
public List<Transition> getNextTransitions(State currentState, Channel channel, List<Clock> allClocks) {
return createNewTransitions(
currentState, getNextMoves(currentState.getLocation(), channel), allClocks
);
}
@Override
public List<Move> getNextMoves(Location location, Channel channel) {
// Check if action belongs to this transition system at all before proceeding
if (!getOutputs().contains(channel) && !getInputs().contains(channel)) {
return new ArrayList<>();
}
// Check that the location is ComplexLocation
if (!location.isComposed()) {
throw new IllegalArgumentException(
"The location type must be ComplexLocation as aggregated transition systems requires multiple locations"
);
}
List<Location> locations = location.getChildren();
/* Check that the complex locations size is the same as the systems
* This is because the index of the system,
* determines also the location. Meaning that,
* the i'th system has the i'th location. */
if (locations.size() != getRootSystems().size()) {
throw new IllegalStateException(
"The amount of locations in the complex location must be exactly the same as the amount of systems"
);
}
return computeResultMoves(locations, channel);
}
protected List<Move> computeResultMoves(List<Location> locations, Channel channel) {
return new ArrayList<>();
}
protected List<TransitionSystem> getRootSystems() {
return Arrays.asList(systems);
}
protected boolean in(Channel element, Set<Channel> set) {
return set.contains(element);
}
protected Set<Channel> intersect(Set<Channel> set1, Set<Channel> set2) {
Set<Channel> intersection = new HashSet<>(set1);
intersection.retainAll(set2);
return intersection;
}
protected Set<Channel> difference(Set<Channel> set1, Set<Channel> set2) {
Set<Channel> difference = new HashSet<>(set1);
difference.removeAll(set2);
return difference;
}
protected Set<Channel> union(Set<Channel> set1, Set<Channel> set2) {
Set<Channel> union = new HashSet<>(set1);
union.addAll(set2);
return union;
}
private Automaton aggregate(Automaton[] automata) {
boolean initialisedCdd = CDDRuntime.tryInit(getClocks(), BVs.getItems());
String name = getName();
Set<Edge> edges = new HashSet<>();
Set<Location> locations = new HashSet<>();
Map<String, Location> locationMap = new HashMap<>();
State initialState = getInitialState();
Location initial = initialState.getLocation();
locations.add(initial);
locationMap.put(initial.getName(), initial);
Set<Channel> channels = new HashSet<>();
channels.addAll(getOutputs());
channels.addAll(getInputs());
worklist.add(
getInitialState()
);
while (!worklist.isEmpty()) {
State state = worklist.remove();
passed.add(state);
for (Channel channel : channels) {
List<Transition> transitions = getNextTransitions(state, channel, clocks.getItems());
for (Transition transition : transitions) {
/* Get the state following the transition and then extrapolate. If we have not
* already visited the location, this is equivalent to simulating the arrival
* at that location following this transition with the current "channel". */
State targetState = transition.getTarget();
if (!havePassed(targetState) && !isWaitingFor(targetState)) {
targetState.extrapolateMaxBounds(maxBounds, getClocks());
worklist.add(targetState);
}
/* If we don't already have the "targetState" location added
* To the set of locations for the conjunction then add it. */
String targetName = targetState.getLocation().getName();
locationMap.computeIfAbsent(
targetName, key -> {
Location newLocation = Location.createFromState(targetState);
locations.add(newLocation);
return newLocation;
}
);
// Create and add the edge connecting the conjoined locations
String sourceName = transition.getSource().getLocation().getName();
assert locationMap.containsKey(sourceName);
assert locationMap.containsKey(targetName);
Edge edge = createEdgeFromTransition(
transition,
locationMap.get(sourceName),
locationMap.get(targetName),
channel
);
if (!containsEdge(edges, edge)) {
edges.add(edge);
}
}
}
}
List<Location> updatedLocations = updateLocations(
locations, getClocks(), getClocks(), getBVs(), getBVs()
);
List<Edge> edgesWithNewClocks = updateEdges(edges, clocks.getItems(), clocks.getItems(), BVs.getItems(), BVs.getItems());
Automaton resAut = new Automaton(name, updatedLocations, edgesWithNewClocks, clocks.getItems(), BVs.getItems(), false);
if (initialisedCdd) {
CDDRuntime.done();
}
return resAut;
}
private boolean havePassed(State element) {
for (State state : passed) {
if (element.getLocation().getName().equals(state.getLocation().getName()) &&
element.getInvariant().isSubset(state.getInvariant())) {
return true;
}
}
return false;
}
private boolean isWaitingFor(State element) {
for (State state : worklist) {
if (element.getLocation().getName().equals(state.getLocation().getName()) &&
element.getInvariant().isSubset(state.getInvariant())) {
return true;
}
}
return false;
}
private boolean containsEdge(Set<Edge> set, Edge edge) {
return set.stream().anyMatch(other -> other.equals(edge) &&
other.getGuardCDD().equals(edge.getGuardCDD())
);
}
private Edge createEdgeFromTransition(Transition transition, Location source, Location target, Channel channel) {
Guard guard = transition.getGuards(getClocks());
List<Update> updates = transition.getUpdates();
boolean isInput = getInputs().contains(channel);
return new Edge(source, target, channel, isInput, guard, updates);
}
}