Skip to content

Commit 7a488a5

Browse files
author
Steffen Märcker
committed
Replace home-grown iterators and map entries with proper types
1 parent ed62b2c commit 7a488a5

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;
@@ -214,12 +215,11 @@ public Iterator<Entry<Integer,Double>> getTransitionsIterator(int s)
214215
{
215216
if (exitRates[s] == 0) {
216217
// return prob-1 self-loop
217-
return Collections.singletonMap(s, 1.0).entrySet().iterator();
218+
return DiracDistribution.iterator(s);
218219
} else {
219-
final Iterator<Entry<Integer,Double>> ctmcIterator = ctmc.getTransitionsIterator(s);
220-
220+
Iterator<Entry<Integer,Double>> ctmcIterator = ctmc.getTransitionsIterator(s);
221221
// return iterator over entries, with probabilities divided by exitRates[s]
222-
final double er = exitRates[s];
222+
double er = exitRates[s];
223223
return new Iterator<Entry<Integer,Double>>() {
224224
@Override
225225
public boolean hasNext()
@@ -230,27 +230,10 @@ public boolean hasNext()
230230
@Override
231231
public Entry<Integer, Double> next()
232232
{
233-
final Entry<Integer, Double> ctmcEntry = ctmcIterator.next();
234-
235-
return new Entry<Integer, Double>() {
236-
@Override
237-
public Integer getKey()
238-
{
239-
return ctmcEntry.getKey();
240-
}
241-
242-
@Override
243-
public Double getValue()
244-
{
245-
return ctmcEntry.getValue() / er;
246-
}
247-
248-
@Override
249-
public Double setValue(Double value)
250-
{
251-
throw new UnsupportedOperationException();
252-
}
253-
};
233+
Entry<Integer, Double> transition = ctmcIterator.next();
234+
Integer state = transition.getKey();
235+
double probability = transition.getValue() / er;
236+
return new SimpleImmutableEntry<>(state, probability);
254237
}
255238
};
256239
}
@@ -259,7 +242,7 @@ public Double setValue(Double value)
259242
@Override
260243
public void forEachTransition(int s, TransitionConsumer c)
261244
{
262-
final double er = exitRates[s];
245+
double er = exitRates[s];
263246
if (er == 0) {
264247
// exit rate = 0 -> prob 1 self loop
265248
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)