Skip to content

Commit e9a48eb

Browse files
Steffen Märckermerkste
authored andcommitted
Replace home-grown iterators and map entries with proper types
1 parent 42df06d commit e9a48eb

2 files changed

Lines changed: 90 additions & 26 deletions

File tree

prism/src/explicit/DTMCEmbeddedSimple.java

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package explicit;
2828

2929
import java.util.*;
30+
import java.util.AbstractMap.SimpleImmutableEntry;
3031
import java.util.Map.Entry;
3132

3233
import explicit.rewards.MCRewards;
@@ -208,12 +209,11 @@ public Iterator<Entry<Integer,Double>> getTransitionsIterator(int s)
208209
{
209210
if (exitRates[s] == 0) {
210211
// return prob-1 self-loop
211-
return Collections.singletonMap(s, 1.0).entrySet().iterator();
212+
return DiracDistribution.iterator(s);
212213
} else {
213-
final Iterator<Entry<Integer,Double>> ctmcIterator = ctmc.getTransitionsIterator(s);
214-
214+
Iterator<Entry<Integer,Double>> ctmcIterator = ctmc.getTransitionsIterator(s);
215215
// return iterator over entries, with probabilities divided by exitRates[s]
216-
final double er = exitRates[s];
216+
double er = exitRates[s];
217217
return new Iterator<Entry<Integer,Double>>() {
218218
@Override
219219
public boolean hasNext()
@@ -224,27 +224,10 @@ public boolean hasNext()
224224
@Override
225225
public Entry<Integer, Double> next()
226226
{
227-
final Entry<Integer, Double> ctmcEntry = ctmcIterator.next();
228-
229-
return new Entry<Integer, Double>() {
230-
@Override
231-
public Integer getKey()
232-
{
233-
return ctmcEntry.getKey();
234-
}
235-
236-
@Override
237-
public Double getValue()
238-
{
239-
return ctmcEntry.getValue() / er;
240-
}
241-
242-
@Override
243-
public Double setValue(Double value)
244-
{
245-
throw new UnsupportedOperationException();
246-
}
247-
};
227+
Entry<Integer, Double> transition = ctmcIterator.next();
228+
Integer state = transition.getKey();
229+
double probability = transition.getValue() / er;
230+
return new SimpleImmutableEntry<>(state, probability);
248231
}
249232
};
250233
}
@@ -253,7 +236,7 @@ public Double setValue(Double value)
253236
@Override
254237
public void forEachTransition(int s, TransitionConsumer c)
255238
{
256-
final double er = exitRates[s];
239+
double er = exitRates[s];
257240
if (er == 0) {
258241
// exit rate = 0 -> prob 1 self loop
259242
c.accept(s, s, 1.0);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//==============================================================================
2+
//
3+
// Copyright (c) 2019-
4+
// Authors:
5+
// * Steffen Maercker <steffen.maercker@tu-dresden.de> (TU Dresden)
6+
//
7+
//------------------------------------------------------------------------------
8+
//
9+
// This file is part of PRISM.
10+
//
11+
// PRISM is free software; you can redistribute it and/or modify
12+
// it under the terms of the GNU General Public License as published by
13+
// the Free Software Foundation; either version 2 of the License, or
14+
// (at your option) any later version.
15+
//
16+
// PRISM is distributed in the hope that it will be useful,
17+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
// GNU General Public License for more details.
20+
//
21+
// You should have received a copy of the GNU General Public License
22+
// along with PRISM; if not, write to the Free Software Foundation,
23+
// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24+
//
25+
//==============================================================================
26+
27+
package explicit;
28+
29+
import java.util.Iterator;
30+
import java.util.Map;
31+
import java.util.Map.Entry;
32+
33+
import common.iterable.SingletonIterator;
34+
35+
public class DiracDistribution implements Iterable<Entry<Integer, Double>>
36+
{
37+
private final Entry<Integer, Double> transition;
38+
39+
public DiracDistribution(final int state)
40+
{
41+
this.transition = new Transition(state);
42+
}
43+
44+
public Iterator<Entry<Integer, Double>> iterator()
45+
{
46+
return new SingletonIterator.Of<>(transition);
47+
}
48+
49+
public static Iterator<Entry<Integer, Double>> iterator(final int state)
50+
{
51+
return new SingletonIterator.Of<>((Entry<Integer, Double>) new Transition(state));
52+
}
53+
54+
public static class Transition implements Map.Entry<Integer, Double>
55+
{
56+
private final Integer state;
57+
58+
public Transition(final int state)
59+
{
60+
this.state = state;
61+
}
62+
63+
@Override
64+
public Integer getKey()
65+
{
66+
return state;
67+
}
68+
69+
@Override
70+
public Double getValue()
71+
{
72+
return 1.0;
73+
}
74+
75+
@Override
76+
public Double setValue(final Double value)
77+
{
78+
throw new UnsupportedOperationException("immutable entry");
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)