00001 package edu.tum.cs.srl.bayesnets.bln.py;
00002
00003 import java.util.Arrays;
00004 import java.util.Vector;
00005
00006 import edu.ksu.cis.bnj.ver3.core.BeliefNode;
00007 import edu.ksu.cis.bnj.ver3.core.CPF;
00008 import edu.ksu.cis.bnj.ver3.core.Discrete;
00009 import edu.ksu.cis.bnj.ver3.core.values.ValueDouble;
00010 import edu.tum.cs.bayesnets.core.BeliefNetworkEx;
00011 import edu.tum.cs.srl.Database;
00012 import edu.tum.cs.srl.bayesnets.BLOGModel;
00013 import edu.tum.cs.srl.bayesnets.RelationalBeliefNetwork;
00014 import edu.tum.cs.srl.bayesnets.RelationalNode;
00015 import edu.tum.cs.srl.bayesnets.bln.AbstractGroundBLN;
00016 import edu.tum.cs.srl.bayesnets.inference.Algorithm;
00017 import edu.tum.cs.util.Stopwatch;
00018 import edu.tum.cs.util.datastruct.Pair;
00019
00020 public class GroundBLN extends AbstractGroundBLN {
00021 protected BeliefNetworkEx groundBN;
00022 protected Vector<String> hardFormulaNodes;
00023 protected Database db;
00024
00025 public GroundBLN(BayesianLogicNetworkPy bln, String databaseFile) throws Exception {
00026 super(bln, databaseFile);
00027 }
00028
00029 public GroundBLN(BayesianLogicNetworkPy bln, Database db) throws Exception {
00030 super(bln, db);
00031 }
00032
00033 @Override
00034 public void groundFormulaicNodes() throws Exception {
00035 BayesianLogicNetworkPy bln = (BayesianLogicNetworkPy)this.bln;
00036
00037 System.out.println(" grounding formulas...");
00038 bln.generateGroundFormulas(this.databaseFile);
00039 GroundFormulaIteration gfIter = bln.iterGroundFormulas();
00040 System.out.printf(" %d formulas instantiated\n", gfIter.getCount());
00041 System.out.println(" instantiating nodes and CPFs...");
00042 Stopwatch sw_structure = new Stopwatch();
00043 Stopwatch sw_cpt = new Stopwatch();
00044 for(GroundFormula gf : gfIter) {
00045
00046 sw_structure.start();
00047 String nodeName = "GF" + gf.idxGF;
00048 System.out.println(nodeName + ": " + gf);
00049 Vector<String> GAs = gf.getGroundAtoms();
00050 BeliefNode node = addHardFormulaNode(nodeName, GAs);
00051
00052 sw_structure.stop();
00053
00054
00055 sw_cpt.start();
00056 fillFormulaCPF(gf, node.getCPF(),GAs);
00057 sw_cpt.stop();
00058 }
00059 System.out.println(" structure time: " + sw_structure.getElapsedTimeSecs() + "s");
00060 System.out.println(" cpf time: " + sw_cpt.getElapsedTimeSecs() + "s");
00061 }
00062
00071 protected void fillFormulaCPF(GroundFormula gf, CPF cpf, Vector<String> parentGAs) throws Exception {
00072 BeliefNode[] nodes = cpf.getDomainProduct();
00073 int[] addr = new int[nodes.length];
00074 fillFormulaCPF(gf, cpf, parentGAs, 1, addr);
00075 }
00076
00077 protected void fillFormulaCPF(GroundFormula gf, CPF cpf, Vector<String> parentGAs, int iDomProd, int[] addr) throws Exception {
00078 BeliefNode[] domprod = cpf.getDomainProduct();
00079
00080
00081 State state = ((BayesianLogicNetworkPy)bln).getState();
00082 if(iDomProd == domprod.length) {
00083
00084 double value = gf.isTrue(state) ? 1 : 0;
00085
00086
00087
00088
00089
00090
00091
00092 addr[0] = 0;
00093 cpf.put(addr, new ValueDouble(value));
00094
00095 addr[0] = 1;
00096 cpf.put(addr, new ValueDouble(1.0-value));
00097 return;
00098 }
00099
00100 BeliefNode parent = domprod[iDomProd];
00101 String parentGA = parentGAs.get(iDomProd-1);
00102 Discrete domain = (Discrete)parent.getDomain();
00103 boolean isBoolean = RelationalBeliefNetwork.isBooleanDomain(domain);
00104
00105 int trueIndex = 0;
00106 if(!isBoolean) {
00107 int iStart = parentGA.lastIndexOf(',')+1;
00108 int iEnd = parentGA.lastIndexOf(')');
00109 String outcome = parentGA.substring(iStart, iEnd);
00110 trueIndex = domain.findName(outcome);
00111 if(trueIndex == -1)
00112 throw new Exception("'" + outcome + "' not found in domain of " + parentGA);
00113 }
00114
00115 for(int i = 0; i < domain.getOrder(); i++) {
00116
00117 addr[iDomProd] = i;
00118
00119 if(i == trueIndex)
00120 state.set(parentGA, true);
00121 else
00122 state.set(parentGA, false);
00123
00124 fillFormulaCPF(gf, cpf, parentGAs, iDomProd+1, addr);
00125 }
00126 }
00127
00132 public BeliefNetworkEx getGroundBN() {
00133 return this.groundBN;
00134 }
00135
00136 public static void main(String[] args) {
00137 try {
00138 int test = 1;
00139
00140 if(test == 0) {
00141 String dir = "/usr/wiss/jain/work/code/SRLDB/bln/test/";
00142 BayesianLogicNetworkPy bln = new BayesianLogicNetworkPy(new BLOGModel(dir + "relxy.blog", dir + "relxy.xml"), dir + "relxy.bln");
00143 GroundBLN gbln = new GroundBLN(bln, dir + "relxy.blogdb");
00144 Stopwatch sw = new Stopwatch();
00145 sw.start();
00146
00147
00148
00149 sw.stop();
00150 System.out.println("Inference time: " + sw.getElapsedTimeSecs() + " seconds");
00151 }
00152 if(test == 1) {
00153 String dir = "/usr/wiss/jain/work/code/SRLDB/blog/kitchen/meal_goods2/";
00154 BayesianLogicNetworkPy bln = new BayesianLogicNetworkPy(new BLOGModel(dir + "meals_any_names.blog", dir + "meals_any.learnt.xml"), dir + "meals_any.bln");
00155 GroundBLN gbln = new GroundBLN(bln, dir + "query2.blogdb");
00156 gbln.show();
00157
00158 }
00159 }
00160 catch(Exception e) {
00161 e.printStackTrace();
00162 }
00163 }
00164
00165 @Override
00166 protected void onAddGroundAtomNode(RelationalNode relNode, String[] params,
00167 BeliefNode instance) {
00168
00169
00170 }
00171 }