From c072eb0bd311bcec030eee8a8d3eb95f45a3f5a3 Mon Sep 17 00:00:00 2001 From: Dave Parker Date: Thu, 15 Sep 2011 07:52:07 +0000 Subject: [PATCH] More methods in explicit StateValues. git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@3733 bbc10eb1-c90d-0410-af57-cb519fbb1720 --- prism/src/explicit/StateValues.java | 69 +++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/prism/src/explicit/StateValues.java b/prism/src/explicit/StateValues.java index 1978e303..ed2a33e1 100644 --- a/prism/src/explicit/StateValues.java +++ b/prism/src/explicit/StateValues.java @@ -26,6 +26,10 @@ package explicit; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.util.BitSet; import java.util.List; @@ -292,6 +296,55 @@ public class StateValues valuesB.and(sv.valuesB); } + /** + * Set the elements of this vector by reading them in from a file. + */ + public void readFromFile(File file) throws PrismException + { + BufferedReader in; + String s; + int lineNum = 0, count = 0; + + try { + // open file for reading + in = new BufferedReader(new FileReader(file)); + // read remaining lines + s = in.readLine(); lineNum++; + while (s != null) { + s = s.trim(); + if (!("".equals(s))) { + if (count + 1 > size) + throw new PrismException("Too many values in file \"" + file + "\" (more than " + size + ")"); + if (type instanceof TypeInt) { + int i = Integer.parseInt(s); + setIntValue(count, i); + } + else if (type instanceof TypeDouble) { + double d = Double.parseDouble(s); + setDoubleValue(count, d); + } + else if (type instanceof TypeBool) { + boolean b = Boolean.parseBoolean(s); + setBooleanValue(count, b); + } + count++; + } + s = in.readLine(); lineNum++; + } + // close file + in.close(); + // check size + if (count < size) + throw new PrismException("Too few values in file \"" + file + "\" (" + count + ", not " + size + ")"); + } + catch (IOException e) { + throw new PrismException("File I/O error reading from \"" + file + "\""); + } + catch (NumberFormatException e) { + throw new PrismException("Error detected at line " + lineNum + " of file \"" + file + "\""); + } + } + // ... /** @@ -339,6 +392,22 @@ public class StateValues } } + /** + * For integer-valued vectors, get the int array storing the data. + */ + public int[] getIntArray() + { + return valuesI; + } + + /** + * For double-valued vectors, get the double array storing the data. + */ + public double[] getDoubleArray() + { + return valuesD; + } + /** * For Boolean-valued vectors, get the BitSet storing the data. */