Skip to content

Commit 91084e9

Browse files
merkstemaxkurze1
authored andcommitted
Use predicates for label evaluation instead of maps
1 parent b837698 commit 91084e9

11 files changed

Lines changed: 53 additions & 31 deletions

prism/src/param/SymbolicEngine.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.List;
3535
import java.util.Map;
3636
import java.util.Vector;
37+
import java.util.function.Predicate;
3738

3839
import parser.State;
3940
import parser.ast.Command;
@@ -141,7 +142,7 @@ static boolean hasMoreThanOneVariable(Expression exp)
141142
}
142143
}
143144

144-
public TransitionList calculateTransitions(State state, Map<String, Boolean> labelValues, boolean noWarnings) throws PrismException
145+
public TransitionList calculateTransitions(State state, Predicate<String> labelValues, boolean noWarnings) throws PrismException
145146
{
146147
List<ChoiceListFlexi> chs;
147148
int i, j, k, l, n, count;
@@ -249,7 +250,7 @@ public TransitionList calculateTransitions(State state, Map<String, Boolean> lab
249250
* @param m The module index
250251
* @param state State from which to explore
251252
*/
252-
protected void calculateUpdatesForModule(int m, State state, Map<String, Boolean> labelValues) throws PrismLangException
253+
protected void calculateUpdatesForModule(int m, State state, Predicate<String> labelValues) throws PrismLangException
253254
{
254255
Module module;
255256
Command command;

prism/src/parser/EvaluateContextState.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package parser;
2828

2929
import java.util.Map;
30+
import java.util.function.Predicate;
3031

3132
/**
3233
* Information required to evaluate an expression: a State object.
@@ -37,7 +38,7 @@ public class EvaluateContextState implements EvaluateContext
3738
{
3839
protected final Values constantValues;
3940
protected final Object[] varValues;
40-
protected final Map<String, Boolean> labelValues;
41+
protected final Predicate<String> labelValues;
4142

4243
public EvaluateContextState(State state)
4344
{
@@ -49,7 +50,7 @@ public EvaluateContextState(Values constantValues, State state)
4950
this(constantValues, null, state);
5051
}
5152

52-
public EvaluateContextState(Values constantValues, Map<String, Boolean> labelValues, State state)
53+
public EvaluateContextState(Values constantValues, Predicate<String> labelValues, State state)
5354
{
5455
this.constantValues = constantValues;
5556
this.labelValues = labelValues;
@@ -74,6 +75,6 @@ public Object getVarValue(String name, int index)
7475

7576
public Boolean getLabelValue(String name)
7677
{
77-
return labelValues.get(name);
78+
return labelValues.test(name);
7879
}
7980
}

prism/src/parser/ast/Expression.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import prism.PrismLangException;
5050

5151
import java.util.Map;
52+
import java.util.function.Predicate;
5253

5354
// Abstract class for PRISM language expressions
5455

@@ -545,7 +546,7 @@ public boolean evaluateBoolean(Values constantValues, State state) throws PrismL
545546
* Variable values are supplied as a State object, i.e. array of variable values.
546547
* Note: assumes that type checking has been done.
547548
*/
548-
public boolean evaluateBoolean(Values constantValues, Map<String, Boolean> labelValues, State state) throws PrismLangException
549+
public boolean evaluateBoolean(Values constantValues, Predicate<String> labelValues, State state) throws PrismLangException
549550
{
550551
return evaluateBoolean(new EvaluateContextState(constantValues, labelValues, state));
551552
}
@@ -627,7 +628,7 @@ public BigRational evaluateExact(State state) throws PrismLangException
627628
* Variable values are supplied as a State object, i.e. array of variable values.
628629
* Note: assumes that type checking has been done.
629630
*/
630-
public BigRational evaluateExact(Values constantValues, Map<String, Boolean> labelValues, State state) throws PrismLangException
631+
public BigRational evaluateExact(Values constantValues, Predicate<String> labelValues, State state) throws PrismLangException
631632
{
632633
return evaluateExact(new EvaluateContextState(constantValues, labelValues, state));
633634
}

prism/src/parser/ast/ExpressionLabel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public boolean isProposition()
8181
}
8282

8383
@Override
84-
public Object evaluate(EvaluateContext ec) throws PrismLangException
84+
public Boolean evaluate(EvaluateContext ec) throws PrismLangException
8585
{
86-
Object value = ec.getLabelValue(this.name);
86+
Boolean value = ec.getLabelValue(this.name);
8787
if (value == null)
8888
throw new PrismLangException("Failed to evaluate label", this);
8989
return value;

prism/src/prism/ModelGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Collections;
3434
import java.util.List;
3535
import java.util.Map;
36+
import java.util.function.Predicate;
3637

3738
/**
3839
* Interface for classes that generate a probabilistic model:
@@ -313,7 +314,7 @@ public default String getTransitionUpdateStringFull(int i, int offset) throws Pr
313314
/**
314315
* Creates a {@link Map} of (label-name -> label-value) for all labels in the current state.
315316
*/
316-
default Map<String, Boolean> getLabelValues(State state) throws PrismException
317+
default Predicate<String> getLabelValues(State state) throws PrismException
317318
{
318319
// No labels by default
319320
throw new PrismException("Labels not defined");

prism/src/prism/ModelGenerator2MTBDD.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.LinkedList;
3030
import java.util.Map;
3131
import java.util.Vector;
32+
import java.util.function.Predicate;
3233

3334
import jdd.JDD;
3435
import jdd.JDDNode;
@@ -513,9 +514,9 @@ private void buildTransAndRewards() throws PrismException
513514
// TODO progress.updateIfReady(src + 1);
514515
}
515516

516-
Map<String, Boolean> labelValues = modelGen.getLabelValues(state);
517+
Predicate<String> labelValues = modelGen.getLabelValues(state);
517518
for (int l = 0; l < numLabels; l++) {
518-
if (labelValues.get(modelGen.getLabelName(l))) {
519+
if (labelValues.test(modelGen.getLabelName(l))) {
519520
labelsArray[l] = JDD.Or(labelsArray[l], ddState.copy());
520521
}
521522
}

prism/src/simulator/LabelEvaluator.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,10 @@ public boolean getLabelValue(State state, String label) throws PrismLangExceptio
8787
* @return Returns a {@link Map} containing all label-values for the given state, indexed by their name.
8888
* @throws PrismLangException In case a label couldn't be evaluated.
8989
*/
90-
public Map<String, Boolean> getLabelValues(State state) throws PrismLangException
90+
public Predicate<String> getLabelValues(State state) throws PrismLangException
9191
{
92-
// convert BitSet to Map to provide intuitiv interface
93-
BitSet value = getStateValues(state);
94-
Map<String, Boolean> labelValueMap = new HashMap<>();
95-
for (int i = 0, numLabels = labelList.size(); i < numLabels; i++){
96-
labelValueMap.put(labelList.getLabelName(i), value.get(i));
97-
}
98-
return labelValueMap;
99-
}
92+
return asPredicate(getStateValues(state));
93+
}
10094

10195
/**
10296
* This method provides all values of a certain label. They will be ordered by the given statesList. <br>
@@ -146,15 +140,35 @@ protected BitSet getStateValues(State state) throws PrismLangException
146140
private BitSet evaluateState(State state) throws PrismLangException
147141
{
148142
int numLabels = labelList.size();
149-
Map<String, Boolean> labelValues = new HashMap<>(numLabels);
150143
BitSet values = new BitSet();
144+
Predicate<String> labelValues = asPredicate(values);
151145
for (int i : evaluationOrder) {
152146
Expression label = labelList.getLabel(i);
153147
boolean labelValue = label.evaluateBoolean(null, labelValues, state);
154-
labelValues.put(labelList.getLabelName(i), labelValue);
155148
values.set(i, labelValue);
156149
}
157150
stateValues.put(state, values);
158151
return values;
159152
}
153+
154+
/**
155+
* Convert the label values for a state to a predicate on the label names.
156+
*
157+
* @param values the label values for some state
158+
* @return a Predicate evaluating to {@code true} for a label name iff the Bit at the corresponding index in {@code values} is set
159+
*/
160+
public Predicate<String> asPredicate(BitSet values)
161+
{
162+
return new Predicate<String>() {
163+
@Override
164+
public boolean test(String name)
165+
{
166+
int index = labelList.getLabelIndex(name);
167+
if (index == -1) {
168+
throw new NoSuchElementException("Unknown label \"" + name + "\"");
169+
}
170+
return values.get(index);
171+
}
172+
};
173+
}
160174
}

prism/src/simulator/ModulesFileModelGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.BitSet;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.function.Predicate;
2324

2425
public class ModulesFileModelGenerator implements ModelGenerator, RewardGenerator
2526
{
@@ -417,7 +418,7 @@ public State computeTransitionTarget(int index, int offset) throws PrismExceptio
417418
}
418419

419420
@Override
420-
public Map<String, Boolean> getLabelValues(State state) throws PrismLangException
421+
public Predicate<String> getLabelValues(State state) throws PrismLangException
421422
{
422423
return labelEvaluator.getLabelValues(state);
423424
}

prism/src/simulator/ModulesFileModelGeneratorSymbolic.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import java.util.List;
55
import java.util.Map;
6+
import java.util.function.Predicate;
67

78
import param.Function;
89
import param.FunctionFactory;
@@ -449,7 +450,7 @@ public State computeTransitionTarget(int index, int offset) throws PrismExceptio
449450
}
450451

451452
@Override
452-
public Map<String, Boolean> getLabelValues(State state) throws PrismLangException
453+
public Predicate<String> getLabelValues(State state) throws PrismLangException
453454
{
454455
return labelsEvaluator.getLabelValues(state);
455456
}

prism/src/simulator/Updater.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.List;
3333
import java.util.Vector;
3434
import java.util.Map;
35+
import java.util.function.Predicate;
3536

3637
import parser.State;
3738
import parser.VarList;
@@ -151,7 +152,7 @@ public double getSumRoundOff()
151152
* @param state State from which to explore
152153
* @param transitionList TransitionList object in which to store result
153154
*/
154-
public void calculateTransitions(State state, Map<String, Boolean> labelValues, TransitionList transitionList) throws PrismException
155+
public void calculateTransitions(State state, Predicate<String> labelValues, TransitionList transitionList) throws PrismException
155156
{
156157
List<ChoiceListFlexi> chs;
157158
int i, j, k, l, n, count;
@@ -269,7 +270,7 @@ public void calculateTransitions(State state, Map<String, Boolean> labelValues,
269270
* @param m The module index
270271
* @param state State from which to explore
271272
*/
272-
protected void calculateUpdatesForModule(int m, State state, Map<String, Boolean> labelValues) throws PrismLangException
273+
protected void calculateUpdatesForModule(int m, State state, Predicate<String> labelValues) throws PrismLangException
273274
{
274275
Module module;
275276
Command command;

0 commit comments

Comments
 (0)