Skip to content

Commit 05a35e8

Browse files
committed
new label evaluation ordering algorithm
1 parent 81dafa5 commit 05a35e8

1 file changed

Lines changed: 40 additions & 24 deletions

File tree

prism/src/simulator/LabelEvaluator.java

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package simulator;
22

33

4-
import common.iterable.IterableArray;
5-
import common.iterable.Range;
6-
import common.iterable.Reducible;
74
import parser.State;
85
import parser.ast.Expression;
96
import parser.ast.LabelList;
107
import prism.PrismException;
118
import prism.PrismLangException;
129
import prism.PrismUtils;
1310

14-
import java.util.*;
11+
import java.util.BitSet;
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
import java.util.NoSuchElementException;
1516
import java.util.function.Predicate;
1617

1718
/**
@@ -41,34 +42,48 @@ public LabelEvaluator(LabelList labelList) throws PrismLangException
4142
*
4243
* @param labels a {@link LabelList} of labels to be evaluated
4344
* @return an int array of the label indices in ascending order
44-
* @throws PrismLangException In case a label depencies could not be computed.
45+
* @throws PrismLangException In case a label dependencies could not be computed.
4546
*/
4647
private int[] fixEvaluationOrder(LabelList labels) throws PrismLangException
4748
{
4849
boolean[][] deps = labels.getLabelDependencies();
4950
assert PrismUtils.findCycle(deps) == -1 : "label dependencies must not contain a cylce";
5051

5152
int numLabels = labels.size();
52-
Integer[] order = new Range(numLabels).collect(new Integer[numLabels]);
53-
Arrays.sort(order, new Comparator<Integer>()
54-
{
55-
@Override
56-
public int compare(Integer row, Integer col)
57-
{
58-
int i = row;
59-
int j = col;
60-
if (deps[i][j]) { // i depends on j
61-
return +1;
62-
}
63-
if (deps[j][i]) { // j depends on i
64-
return -1;
53+
BitSet done = new BitSet(numLabels);
54+
int[] order = new int[numLabels];
55+
for (int l = done.nextClearBit(0); l < numLabels; l = done.nextClearBit(l)) {
56+
traverseDfs(deps, l, order, -1, done);
57+
}
58+
return order;
59+
}
60+
61+
/**
62+
* Do a depth-first search in a graph given by an adjacency matrix starting at a given node.
63+
*
64+
* @param adj an adjacency matrix with adj[i][j] iff i->j
65+
* @param start a start node
66+
* @param trace an int array to record the trace, will be modified
67+
* @param last index of last element of the trace
68+
* @param done a {@link BitSet} of already visited nodes, will be modified
69+
* @return the index of the last element of the extended trace, i.e., of the start node
70+
*/
71+
private int traverseDfs(boolean[][] adj, int start, int[] trace, int last, BitSet done)
72+
{
73+
if (!done.get(start)) {
74+
boolean[] isSuccessor = adj[start];
75+
for (int node = isSuccessor.length - 1; node >= 0; node--) {
76+
if (isSuccessor[node]) {
77+
last = traverseDfs(adj, node, trace, last, done);
6578
}
66-
return Integer.compare(i, j);
6779
}
68-
});
69-
return Reducible.unboxInt(new IterableArray.Of<>(order)).collect(new int[numLabels]);
80+
trace[++last] = start;
81+
done.set(start);
82+
}
83+
return last;
7084
}
7185

86+
7287
/**
7388
* Returns the value of a specific label in a specific state.
7489
*
@@ -99,7 +114,7 @@ public Predicate<String> getLabelValues(State state) throws PrismLangException
99114
* This method provides all values of a certain label. They will be ordered by the given statesList. <br>
100115
* States will be evaluated if necessary.
101116
*
102-
* @param label The name of the label.
117+
* @param label The name of the label.
103118
* @param statesList The states to evaluate against in correct order.
104119
* @return All values of the label.
105120
* @throws PrismException In case a label evaluation fails.
@@ -109,7 +124,7 @@ public BitSet getLabel(String label, List<State> statesList) throws PrismExcepti
109124
int statesNum = statesList.size();
110125
int labelIndex = labelList.getLabelIndex(label);
111126
BitSet labelValues = new BitSet();
112-
for (int i = statesNum-1; i >= 0; i--) {
127+
for (int i = statesNum - 1; i >= 0; i--) {
113128
State state = statesList.get(i);
114129
BitSet valuesOfState = getStateValues(state);
115130
labelValues.set(i, valuesOfState.get(labelIndex));
@@ -163,7 +178,8 @@ private BitSet evaluateState(State state) throws PrismLangException
163178
*/
164179
public Predicate<String> asPredicate(BitSet values)
165180
{
166-
return new Predicate<String>() {
181+
return new Predicate<String>()
182+
{
167183
@Override
168184
public boolean test(String name)
169185
{

0 commit comments

Comments
 (0)