00001 package edu.tum.cs.bayesnets.core.io; 00002 00003 import java.io.OutputStream; 00004 import java.io.PrintStream; 00005 00006 import edu.ksu.cis.bnj.ver3.core.BeliefNetwork; 00007 import edu.ksu.cis.bnj.ver3.core.BeliefNode; 00008 import edu.ksu.cis.bnj.ver3.core.CPF; 00009 import edu.ksu.cis.bnj.ver3.core.Discrete; 00010 import edu.ksu.cis.bnj.ver3.streams.Exporter; 00011 import edu.tum.cs.bayesnets.core.BeliefNetworkEx; 00012 00018 public class Converter_hugin implements Exporter { 00019 00020 public String getDesc() { 00021 return "Hugin"; 00022 } 00023 00024 public String getExt() { 00025 return "*.net;*.hugin"; 00026 } 00027 00028 public void save(BeliefNetwork bn, OutputStream out) { 00029 BeliefNetworkEx bnx = new BeliefNetworkEx(bn); 00030 PrintStream ps = new PrintStream(out); 00031 ps.println("net\n{\n node_size = (70 28);\n}\n"); 00032 BeliefNode[] nodes = bn.getNodes(); 00033 for(int i = 0; i < nodes.length; i++) { 00034 BeliefNode node = nodes[i]; 00035 ps.printf("node N%d\n{\n", i); 00036 ps.printf(" label = \"%s\";\n", node.getName()); 00037 ps.printf(" position = (%d %d);\n", node.getOwner().getx(), node.getOwner().gety()); 00038 ps.printf(" states = ("); 00039 Discrete dom = (Discrete)node.getDomain(); 00040 for(int j = 0; j < dom.getOrder(); j++) { 00041 if(j > 0) ps.print(" "); 00042 ps.printf("\"%s\"", dom.getName(j)); 00043 } 00044 ps.println(");\n}\n"); 00045 } 00046 for(int i = 0; i < nodes.length; i++) { 00047 BeliefNode node = nodes[i]; 00048 CPF cpf = node.getCPF(); 00049 ps.printf("potential (N%d", i); 00050 BeliefNode[] domprod = cpf.getDomainProduct(); 00051 if(domprod.length > 1) { 00052 ps.print(" |"); 00053 for(int j = 1; j < domprod.length; j++) { 00054 ps.printf(" N%d", bnx.getNodeIndex(domprod[j])); 00055 } 00056 } 00057 ps.printf(")\n{\n data = "); 00058 int[] addr = new int[domprod.length]; 00059 writeCPF(cpf, 1, addr, ps); 00060 ps.println(";\n}\n"); 00061 } 00062 } 00063 00064 protected void writeCPF(CPF cpf, int i, int[] addr, PrintStream ps) { 00065 BeliefNode[] domprod = cpf.getDomainProduct(); 00066 if(i == addr.length) { 00067 ps.print("("); 00068 BeliefNode n = domprod[0]; 00069 Discrete dom = (Discrete)n.getDomain(); 00070 for(int j = 0; j < dom.getOrder(); j++) { 00071 addr[0] = j; 00072 if(j > 0) ps.print(" "); 00073 ps.print(cpf.getDouble(addr)); 00074 } 00075 ps.print(")"); 00076 return; 00077 } 00078 ps.print("("); 00079 BeliefNode n = domprod[i]; 00080 Discrete dom = (Discrete)n.getDomain(); 00081 for(int j = 0; j < dom.getOrder(); j++) { 00082 addr[i] = j; 00083 writeCPF(cpf, i+1, addr, ps); 00084 } 00085 ps.print(")"); 00086 } 00087 }