Browse Source

Improved explicit StateValues class.

git-svn-id: https://www.prismmodelchecker.org/svn/prism/prism/trunk@3589 bbc10eb1-c90d-0410-af57-cb519fbb1720
master
Dave Parker 14 years ago
parent
commit
3204326ebc
  1. 126
      prism/src/explicit/StateValues.java
  2. 21
      prism/src/explicit/Utils.java

126
prism/src/explicit/StateValues.java

@ -27,23 +27,20 @@
package explicit;
import java.util.BitSet;
import java.util.List;
import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration;
import jdd.JDDNode;
import parser.State;
import parser.type.Type;
import parser.type.TypeBool;
import parser.type.TypeDouble;
import parser.type.TypeInt;
import prism.PrismException;
import prism.PrismFileLog;
import prism.PrismLangException;
import prism.PrismLog;
import prism.PrismUtils;
/**
* Class for explicit-state storage of a state-indexed vector of (integer or double) values.
* Class for explicit-state storage of a state-indexed vector of values (int, double, boolean).
*/
public class StateValues
{
@ -56,6 +53,9 @@ public class StateValues
protected double[] valuesD;
protected BitSet valuesB;
// Model info
protected List<State> statesList;
// CONSTRUCTORS, etc.
/**
@ -162,22 +162,6 @@ public class StateValues
return createFromDoubleArray(array);
}
// CONVERSION METHODS
/*// convert to StateValuesDV (nothing to do)
public StateValuesDV convertToStateValuesDV()
{
return this;
}
// convert to StateValuesMTBDD, destroy (clear) old vector
public StateValuesMTBDD convertToStateValuesMTBDD()
{
StateValuesMTBDD res = new StateValuesMTBDD(values.convertToMTBDD(vars, odd), model);
clear();
return res;
}*/
/**
* Generate BitSet for states in the given interval
* (interval specified as relational operator and bound)
@ -278,8 +262,6 @@ public class StateValues
// ...
// clear (free memory)
/**
* Clear the vector, i.e. free any used memory.
* (Well, actually, just set pointer to null and wait for later garbage collection.)
@ -309,6 +291,22 @@ public class StateValues
}
}
/**
* Is the ith element of the vector non-zero (or non-false)?
*/
public boolean isNonZero(int i)
{
if (type instanceof TypeInt) {
return valuesI[i] != 0;
} else if (type instanceof TypeDouble) {
return valuesD[i] != 0.0;
} else if (type instanceof TypeBool) {
return valuesB.get(i);
} else {
return false;
}
}
/**
* For Boolean-valued vectors, get the BitSet storing the data.
*/
@ -317,19 +315,30 @@ public class StateValues
return valuesB;
}
/*
// get num non zeros
/**
* Get the number of states for which the value is non-zero/non-false.
*/
public int getNNZ()
{
return values.getNNZ();
int count = 0;
if (type instanceof TypeBool) {
count = valuesB.cardinality();
} else {
for (int i = 0; i < size; i++) {
if (isNonZero(i))
count++;
}
}
return count;
}
/**
* Get (as a string) the number of states for which the value is non-zero/non-false.
*/
public String getNNZString()
{
return "" + getNNZ();
}
*/
// Filter operations
@ -481,16 +490,51 @@ public class StateValues
* Print vector to a log/file (non-zero entries only)
*/
public void print(PrismLog log) throws PrismException
{
print(log, true, false, true);
}
/**
* Print vector to a log/file.
* @param log The log
* @param printSparse Print non-zero elements only?
* @param printMatlab Print in Matlab format?
* @param printStates Print states (variable values) for each element?
*/
public void print(PrismLog log, boolean printSparse, boolean printMatlab, boolean printStates) throws PrismException
{
int i;
// Header for Matlab format
if (printMatlab)
log.println(!printSparse ? "v = [" : "v = sparse(" + size + ",1);");
// Check if all zero
if (printSparse && !printMatlab && getNNZ() == 0) {
log.println("(all zero)");
return;
}
// Print vector
for (i = 0; i < size; i++) {
log.println(getValue(i));
if (!printSparse || isNonZero(i)) {
if (printSparse)
log.print(printMatlab ? "v(" + (i + 1) + ")" : i);
if (printStates && !printMatlab && statesList != null) {
log.print(":" + statesList.get(i).toString());
}
if (printSparse)
log.print("=");
log.print(getValue(i));
if (printMatlab && printSparse)
log.print(";");
log.println();
}
}
public void print(PrismLog log, boolean printSparse, boolean printMatlab, boolean printStates) throws PrismException
{
print(log); //TODO
// Footer for Matlab format
if (printMatlab && !printSparse)
log.println("];");
}
/**
@ -498,7 +542,19 @@ public class StateValues
*/
public StateValues deepCopy() throws PrismException
{
// TODO
throw new PrismException("Not implemented yet");
StateValues sv = new StateValues();
sv.type = type;
sv.size = size;
if (valuesI != null) {
sv.valuesI = Utils.cloneIntArray(valuesI);
}
if (valuesD != null) {
sv.valuesD = Utils.cloneDoubleArray(valuesD);
}
if (valuesB != null) {
sv.valuesB = (BitSet) valuesB.clone();
}
sv.statesList = statesList;
return sv;
}
}

21
prism/src/explicit/Utils.java

@ -144,6 +144,27 @@ public class Utils
return arrayNew;
}
/**
* Clone an integer array.
* @param array The array to be cloned
* @return The new array
*/
public static int[] cloneIntArray(int array[])
{
int i, n;
int[] arrayNew;
// Do nothing for null pointers
if (array == null)
return null;
// Otherwise copy and return
n = array.length;
arrayNew = new int[n];
for (i = 0; i < n; i++) {
arrayNew[i] = array[i];
}
return arrayNew;
}
/**
* Test if two double arrays are equal.
*/

Loading…
Cancel
Save