Skip to content

Commit 90b45e3

Browse files
committed
DTMC/MDP toString().
1 parent be9a9bb commit 90b45e3

7 files changed

Lines changed: 84 additions & 110 deletions

File tree

prism/src/explicit/DTMC.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,4 +803,36 @@ public default void vmMultPowerSteadyState(double vect[], double[] result, doubl
803803
}
804804
}
805805

806+
/**
807+
* Generate a string representation for a DTMC.
808+
*/
809+
default String toStringDTMC()
810+
{
811+
StringBuilder str = new StringBuilder("[ ");
812+
int numStates = getNumStates();
813+
for (int s = 0; s < numStates; s++) {
814+
if (s > 0) {
815+
str.append(", ");
816+
}
817+
str.append(s).append(": ");
818+
str.append("{");
819+
Iterator<Entry<Integer, Pair<Value, Object>>> iter = getTransitionsAndActionsIterator(s);
820+
boolean first = true;
821+
while (iter.hasNext()) {
822+
if (first) {
823+
first = false;
824+
} else {
825+
str.append(",");
826+
}
827+
Entry<Integer, Pair<Value, Object>> entry = iter.next();
828+
str.append(entry.getValue().first).append(":").append(entry.getKey());
829+
if (entry.getValue().second != null) {
830+
str.append(":").append(entry.getValue().second);
831+
}
832+
}
833+
str.append("}");
834+
}
835+
str.append(" ]\n");
836+
return str.toString();
837+
}
806838
}

prism/src/explicit/DTMCSimple.java

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -425,41 +425,7 @@ public Distribution<Value> getTransitions(int s)
425425
@Override
426426
public String toString()
427427
{
428-
int i;
429-
boolean first;
430-
String s = "";
431-
s = "[ ";
432-
first = true;
433-
for (i = 0; i < numStates; i++) {
434-
if (first)
435-
first = false;
436-
else
437-
s += ", ";
438-
s += i + ": " + toStringDistr(i);
439-
}
440-
s += " ]";
441-
return s;
442-
}
443-
444-
protected String toStringDistr(int i)
445-
{
446-
String s = "";
447-
boolean first = true;
448-
s = "{";
449-
int numSucc = succ.get(i).size();
450-
for (int j = 0; j < numSucc; j++) {
451-
if (first)
452-
first = false;
453-
else
454-
s += ", ";
455-
s += succ.get(i).get(j) + "=" + trans.get(i).get(j);
456-
Object action = actions.getAction(i, j);
457-
if (action != null) {
458-
s += ":" + action;
459-
}
460-
}
461-
s += "}";
462-
return s;
428+
return toStringDTMC();
463429
}
464430

465431
@Override

prism/src/explicit/DTMCSparse.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -530,33 +530,12 @@ public void vmMultPowerSteadyState(double vect[], double result[], double[] diag
530530
}
531531
}
532532

533-
534-
535533
//--- Object ---
536534

537535
@Override
538536
public String toString()
539537
{
540-
final Function<Integer, Entry<Integer, Distribution<Double>>> getDistribution = new Function<Integer, Entry<Integer, Distribution<Double>>>()
541-
{
542-
@Override
543-
public final Entry<Integer, Distribution<Double>> apply(final Integer state)
544-
{
545-
final Distribution<Double> distribution = new Distribution<>(getTransitionsIterator(state), getEvaluator());
546-
return new AbstractMap.SimpleImmutableEntry<>(state, distribution);
547-
}
548-
};
549-
String s = "trans: [ ";
550-
IterableStateSet states = new IterableStateSet(numStates);
551-
Iterator<Entry<Integer, Distribution<Double>>> distributions = states.iterator().map(getDistribution);
552-
while (distributions.hasNext()) {
553-
final Entry<Integer, Distribution<Double>> dist = distributions.next();
554-
s += dist.getKey() + ": " + dist.getValue();
555-
if (distributions.hasNext()) {
556-
s += ", ";
557-
}
558-
}
559-
return s + " ]";
538+
return toStringDTMC();
560539
}
561540

562541
@Override

prism/src/explicit/MDP.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,4 +884,44 @@ public default void mvMultRight(int[] states, int[] strat, double[] source, doub
884884
}
885885
}
886886

887+
/**
888+
* Generate a string representation for an MDP.
889+
*/
890+
default String toStringMDP()
891+
{
892+
StringBuilder str = new StringBuilder("[ ");
893+
int numStates = getNumStates();
894+
for (int s = 0; s < numStates; s++) {
895+
if (s > 0) {
896+
str.append(", ");
897+
}
898+
str.append(s).append(": ").append("[");
899+
int numChoices = getNumChoices(s);
900+
for (int i = 0; i < numChoices; i++) {
901+
if (i > 0) {
902+
str.append(",");
903+
}
904+
Object action = getAction(s, i);
905+
if (action != null) {
906+
str.append(action).append(":");
907+
}
908+
str.append("{");
909+
Iterator<Entry<Integer, Value>> iter = getTransitionsIterator(s, i);
910+
boolean first = true;
911+
while (iter.hasNext()) {
912+
if (first) {
913+
first = false;
914+
} else {
915+
str.append(",");
916+
}
917+
Entry<Integer, Value> entry = iter.next();
918+
str.append(entry.getValue()).append(":").append(entry.getKey());
919+
}
920+
str.append("}");
921+
}
922+
str.append("]");
923+
}
924+
str.append(" ]\n");
925+
return str.toString();
926+
}
887927
}

prism/src/explicit/MDPSimple.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -553,28 +553,7 @@ public int indexOfActionLabelledChoice(int s, Distribution<Value> distr, Object
553553
@Override
554554
public String toString()
555555
{
556-
int i, j, n;
557-
Object o;
558-
String s = "";
559-
s = "[ ";
560-
for (i = 0; i < numStates; i++) {
561-
if (i > 0)
562-
s += ", ";
563-
s += i + ": ";
564-
s += "[";
565-
n = getNumChoices(i);
566-
for (j = 0; j < n; j++) {
567-
if (j > 0)
568-
s += ",";
569-
o = getAction(i, j);
570-
if (o != null)
571-
s += o + ":";
572-
s += trans.get(i).get(j);
573-
}
574-
s += "]";
575-
}
576-
s += " ]\n";
577-
return s;
556+
return toStringMDP();
578557
}
579558

580559
@Override

prism/src/explicit/MDPSparse.java

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,37 +1134,7 @@ public void mvMultRight(int[] states, int[] strat, double[] source, double[] des
11341134
@Override
11351135
public String toString()
11361136
{
1137-
int i, j, k, l1, h1, l2, h2;
1138-
Object o;
1139-
String s = "";
1140-
s = "[ ";
1141-
for (i = 0; i < numStates; i++) {
1142-
if (i > 0)
1143-
s += ", ";
1144-
s += i + ": [";
1145-
l1 = rowStarts[i];
1146-
h1 = rowStarts[i + 1];
1147-
for (j = l1; j < h1; j++) {
1148-
if (j > l1)
1149-
s += ",";
1150-
o = getAction(i, j - l1);
1151-
if (o != null)
1152-
s += o + ":";
1153-
s += "{";
1154-
l2 = choiceStarts[j];
1155-
h2 = choiceStarts[j + 1];
1156-
for (k = l2; k < h2; k++) {
1157-
if (k > l2)
1158-
s += ", ";
1159-
s += cols[k] + ":" + nonZeros[k];
1160-
}
1161-
s += "}";
1162-
}
1163-
s += "]";
1164-
}
1165-
s += " ]";
1166-
1167-
return s;
1137+
return toStringMDP();
11681138
}
11691139

11701140
@Override

prism/src/explicit/ModelExplicitWrapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,12 @@ public void exportToPrismLanguage(String filename, int precision) throws PrismEx
274274
{
275275
this.model.exportToPrismLanguage(filename, precision);
276276
}
277+
278+
// Standard methods
279+
280+
@Override
281+
public String toString()
282+
{
283+
return model.toString();
284+
}
277285
}

0 commit comments

Comments
 (0)