00001
00002
00003
00004
00005
00006
00007 package edu.tum.cs.bayesnets.core.io;
00008
00009 import java.io.BufferedReader;
00010 import java.io.IOException;
00011 import java.io.InputStream;
00012 import java.io.InputStreamReader;
00013
00014 import edu.ksu.cis.bnj.ver3.streams.OmniFormatV1;
00015
00020 public class Converter_ergo implements edu.ksu.cis.bnj.ver3.streams.Importer {
00021
00022 public String getDesc() {
00023 return "Ergo";
00024 }
00025
00026 public String getExt() {
00027 return "*.erg";
00028 }
00029
00030 public void load(InputStream stream, OmniFormatV1 writer) {
00031 BufferedReader br = new BufferedReader(new InputStreamReader(stream));
00032 String line;
00033 try {
00034
00035 int numVars = readLineOfInts(br)[0];
00036 int[] domSizes = readLineOfInts(br);
00037 int[][] parents = new int[numVars][];
00038 for(int i = 0; i < numVars; i++) {
00039 parents[i] = readLineOfInts(br);
00040 }
00041
00042
00043 line = nextLine(br);
00044 if(!line.contains("Probabilities"))
00045 throw new IOException("Expected 'Probabilities' section, got this: " + line);
00046 double[][] cpfs = new double[numVars][];
00047 for(int i = 0; i < numVars; i++) {
00048 int numEntries = readLineOfInts(br)[0];
00049 int parentConfigs = 1;
00050 for(int j = 1; j < parents[i].length; j++)
00051 parentConfigs *= domSizes[parents[i][j]];
00052 int entriesPerLine = numEntries / parentConfigs;
00053 double[] cpf = new double[numEntries];
00054 for(int j = 0; j < parentConfigs; j++) {
00055 if(readCPF(br, cpf, j*entriesPerLine) != entriesPerLine)
00056 throw new IOException("CPF line contained unexpected number of entries");
00057 }
00058 cpfs[i] = cpf;
00059 }
00060
00061
00062 line = nextLine(br);
00063 if(!line.contains("Names"))
00064 throw new IOException("Expected 'Names' section, got this: " + line);
00065 String[] names = new String[numVars];
00066 for(int i = 0; i < numVars; i++) {
00067 names[i] = nextLine(br);
00068 }
00069
00070
00071 line = nextLine(br);
00072 if(!line.contains("Labels"))
00073 throw new IOException("Expected 'Labels' section, got this: " + line);
00074 String[][] outcomes = new String[numVars][];
00075 for(int i = 0; i < numVars; i++) {
00076 outcomes[i] = readLineOfStrings(br);
00077 if(outcomes[i].length != domSizes[i])
00078 throw new IOException("Unexpected domain size");
00079 }
00080
00081
00082 writer.Start();
00083 writer.CreateBeliefNetwork(0);
00084
00085 for(int i = 0; i < numVars; i++) {
00086 writer.BeginBeliefNode(i);
00087 writer.SetType("chance");
00088 for(int j = 0; j < outcomes[i].length; j++)
00089 writer.BeliefNodeOutcome(outcomes[i][j]);
00090 writer.SetBeliefNodeName(names[i]);
00091 writer.EndBeliefNode();
00092 }
00093
00094 for(int i = 0; i < numVars; i++) {
00095
00096 for(int j = 1; j < parents[i].length; j++)
00097 writer.Connect(parents[i][j], i);
00098
00099 writer.BeginCPF(i);
00100 for(int j = 0; j < cpfs[i].length; j++)
00101 writer.ForwardFlat_CPFWriteValue(Double.toString(cpfs[i][j]));
00102 writer.EndCPF();
00103 }
00104 writer.Finish();
00105 }
00106 catch (IOException e) {
00107 throw new RuntimeException(e);
00108 }
00109 }
00110
00111 protected String nextLine(BufferedReader br) throws IOException {
00112 String l;
00113 do
00114 l = br.readLine().trim();
00115 while(l.length() == 0);
00116 return l;
00117 }
00118
00119 protected String[] readLineOfStrings(BufferedReader br) throws IOException {
00120 return nextLine(br).split("\\s+");
00121 }
00122
00123 protected int[] readLineOfInts(BufferedReader br) throws IOException {
00124 String[] elems = readLineOfStrings(br);
00125 int[] ret = new int[elems.length];
00126 for(int i = 0; i < elems.length; i++)
00127 ret[i] = Integer.parseInt(elems[i]);
00128 return ret;
00129 }
00130
00131 protected int readCPF(BufferedReader br, double[] cpf, int i) throws IOException {
00132 String l = nextLine(br);
00133 String[] elems = l.split("\\s+");
00134 for(int j = 0; j < elems.length; j++)
00135 cpf[i++] = Double.parseDouble(elems[j]);
00136 return elems.length;
00137 }
00138 }