From 58a8184c752eade6fc52d483eece9d1901e2c3a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20M=C3=A4rcker?= Date: Thu, 9 May 2019 10:26:52 +0200 Subject: [PATCH] Replace home-grown iterators and map entries with proper types --- prism/src/explicit/DTMCEmbeddedSimple.java | 35 +++------- prism/src/explicit/DiracDistribution.java | 81 ++++++++++++++++++++++ 2 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 prism/src/explicit/DiracDistribution.java diff --git a/prism/src/explicit/DTMCEmbeddedSimple.java b/prism/src/explicit/DTMCEmbeddedSimple.java index 717d6e9234..d7b168b94d 100644 --- a/prism/src/explicit/DTMCEmbeddedSimple.java +++ b/prism/src/explicit/DTMCEmbeddedSimple.java @@ -27,6 +27,7 @@ package explicit; import java.util.*; +import java.util.AbstractMap.SimpleImmutableEntry; import java.util.Map.Entry; import explicit.rewards.MCRewards; @@ -214,12 +215,11 @@ public Iterator> getTransitionsIterator(int s) { if (exitRates[s] == 0) { // return prob-1 self-loop - return Collections.singletonMap(s, 1.0).entrySet().iterator(); + return DiracDistribution.iterator(s); } else { - final Iterator> ctmcIterator = ctmc.getTransitionsIterator(s); - + Iterator> ctmcIterator = ctmc.getTransitionsIterator(s); // return iterator over entries, with probabilities divided by exitRates[s] - final double er = exitRates[s]; + double er = exitRates[s]; return new Iterator>() { @Override public boolean hasNext() @@ -230,27 +230,10 @@ public boolean hasNext() @Override public Entry next() { - final Entry ctmcEntry = ctmcIterator.next(); - - return new Entry() { - @Override - public Integer getKey() - { - return ctmcEntry.getKey(); - } - - @Override - public Double getValue() - { - return ctmcEntry.getValue() / er; - } - - @Override - public Double setValue(Double value) - { - throw new UnsupportedOperationException(); - } - }; + Entry transition = ctmcIterator.next(); + Integer state = transition.getKey(); + double probability = transition.getValue() / er; + return new SimpleImmutableEntry<>(state, probability); } }; } @@ -259,7 +242,7 @@ public Double setValue(Double value) @Override public void forEachTransition(int s, TransitionConsumer c) { - final double er = exitRates[s]; + double er = exitRates[s]; if (er == 0) { // exit rate = 0 -> prob 1 self loop c.accept(s, s, 1.0); diff --git a/prism/src/explicit/DiracDistribution.java b/prism/src/explicit/DiracDistribution.java new file mode 100644 index 0000000000..e4abfaa276 --- /dev/null +++ b/prism/src/explicit/DiracDistribution.java @@ -0,0 +1,81 @@ +//============================================================================== +// +// Copyright (c) 2019- +// Authors: +// * Steffen Maercker (TU Dresden) +// +//------------------------------------------------------------------------------ +// +// This file is part of PRISM. +// +// PRISM is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// PRISM is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with PRISM; if not, write to the Free Software Foundation, +// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +//============================================================================== + +package explicit; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import common.iterable.SingletonIterator; + +public class DiracDistribution implements Iterable> +{ + private final Entry transition; + + public DiracDistribution(final int state) + { + this.transition = new Transition(state); + } + + public Iterator> iterator() + { + return new SingletonIterator.Of<>(transition); + } + + public static Iterator> iterator(final int state) + { + return new SingletonIterator.Of<>((Entry) new Transition(state)); + } + + public static class Transition implements Map.Entry + { + private final Integer state; + + public Transition(final int state) + { + this.state = state; + } + + @Override + public Integer getKey() + { + return state; + } + + @Override + public Double getValue() + { + return 1.0; + } + + @Override + public Double setValue(final Double value) + { + throw new UnsupportedOperationException("immutable entry"); + } + } +} \ No newline at end of file