00001
00002
00003
00004
00005
00006
00007 package edu.tum.cs.inference;
00008
00009 import java.io.File;
00010 import java.io.FileInputStream;
00011 import java.io.FileOutputStream;
00012 import java.io.IOException;
00013 import java.io.ObjectInputStream;
00014 import java.io.ObjectOutputStream;
00015 import java.util.HashMap;
00016
00022 public class GeneralSampledDistribution extends BasicSampledDistribution {
00023
00024 protected String[] varNames;
00025 protected String[][] domains;
00026 protected HashMap<String,Integer> varName2Index;
00027
00028 public GeneralSampledDistribution(double[][] values, Double Z, String[] varNames, String[][] domains) throws Exception {
00029 this.values = values;
00030 this.Z = Z;
00031 this.varNames = varNames;
00032 this.domains = domains;
00033 varName2Index = new HashMap<String,Integer>();
00034 for(int i = 0; i < varNames.length; i++)
00035 varName2Index.put(varNames[i], i);
00036 }
00037
00038 @Override
00039 public String[] getDomain(int idx) {
00040 return domains[idx];
00041 }
00042
00043 @Override
00044 public String getVariableName(int idx) {
00045 return varNames[idx];
00046 }
00047
00053 public void write(File f) throws IOException {
00054 FileOutputStream fos = new FileOutputStream(f);
00055 ObjectOutputStream oos = new ObjectOutputStream(fos);
00056 oos.writeObject(this.values);
00057 oos.writeObject(this.Z);
00058 oos.writeObject(varNames);
00059 oos.writeObject(domains);
00060 oos.close();
00061 }
00062
00069 public static GeneralSampledDistribution fromFile(File f) throws Exception {
00070 java.io.ObjectInputStream objstream = new ObjectInputStream(new FileInputStream(f));
00071 double[][] values = (double[][])objstream.readObject();
00072 Double Z = (Double)objstream.readObject();
00073 String[] varNames = (String[])objstream.readObject();
00074 String[][] domains = (String[][])objstream.readObject();
00075 objstream.close();
00076 return new GeneralSampledDistribution(values, Z, varNames, domains);
00077 }
00078
00079 @Override
00080 public int getVariableIndex(String name) {
00081 return varName2Index.get(name);
00082 }
00083
00084 @Override
00085 public Integer getNumSamples() {
00086 throw new RuntimeException("A GeneralizedDistribution represents only the distribution, no additional data is available");
00087 }
00088 }