00001 package edu.tum.cs.srl.bayesnets.bln;
00002
00003 import java.util.HashMap;
00004 import java.util.Set;
00005 import java.util.Vector;
00006
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.core.Value;
00011 import edu.ksu.cis.bnj.ver3.core.values.ValueDouble;
00012 import edu.tum.cs.logic.Formula;
00013 import edu.tum.cs.logic.GroundAtom;
00014 import edu.tum.cs.logic.GroundLiteral;
00015 import edu.tum.cs.logic.KnowledgeBase;
00016 import edu.tum.cs.logic.PossibleWorld;
00017 import edu.tum.cs.logic.WorldVariables;
00018 import edu.tum.cs.srl.Database;
00019 import edu.tum.cs.srl.bayesnets.RelationalBeliefNetwork;
00020 import edu.tum.cs.srl.bayesnets.RelationalNode;
00021 import edu.tum.cs.srl.bayesnets.bln.coupling.VariableLogicCoupling;
00022 import edu.tum.cs.util.StringTool;
00023 import edu.tum.cs.util.datastruct.OrderedSet;
00024
00029 public class GroundBLN extends AbstractGroundBLN {
00030
00031 protected VariableLogicCoupling coupling;
00035 protected PossibleWorld state;
00039 protected KnowledgeBase gkb;
00040
00041 public GroundBLN(AbstractBayesianLogicNetwork bln, Database db) throws Exception {
00042 super(bln, db);
00043 }
00044
00045 public GroundBLN(AbstractBayesianLogicNetwork bln, String databaseFile) throws Exception {
00046 super(bln, databaseFile);
00047 }
00048
00049 @Override
00050 protected void init(AbstractBayesianLogicNetwork bln, Database db) throws Exception {
00051 super.init(bln, db);
00052 coupling = new VariableLogicCoupling();
00053 }
00054
00055 @Override
00056 protected void onAddGroundAtomNode(RelationalNode relNode, String[] params, BeliefNode var) {
00057 if(relNode.isBoolean()) {
00058 coupling.addBooleanVariable(var, relNode.getFunctionName(), params);
00059 }
00060 else {
00061
00062 coupling.addBlockVariable(var, relNode.getDomain(), relNode.getFunctionName(), params);
00063 }
00064 }
00065
00066 public GroundLiteral getGroundLiteral(BeliefNode var, int domIdx) {
00067 return coupling.getGroundLiteral(var, domIdx);
00068 }
00069
00070 @Override
00071 protected void groundFormulaicNodes() throws Exception {
00072 WorldVariables worldVars = coupling.getWorldVars();
00073 state = new PossibleWorld(worldVars);
00074 boolean useFormulaSimplification = false;
00075 BayesianLogicNetwork bln = (BayesianLogicNetwork)this.bln;
00076 gkb = bln.kb.ground(this.db, worldVars, useFormulaSimplification);
00077 if(verbose) System.out.printf(" %d formulas resulted in %s ground formulas\n", bln.kb.size(), gkb.size());
00078 HashMap<String, Value[]> cpfCache = new HashMap<String, Value[]>();
00079 int i = 0;
00080 for(Formula gf : gkb) {
00081 if(!useFormulaSimplification)
00082 gf = gf.simplify(null);
00083
00084
00085 String nodeName = "GF" + i;
00086 if(verbose) System.out.printf(" %s: %s\n", nodeName, gf.toString());
00087
00088
00089
00090
00091 Set<GroundAtom> gas = new OrderedSet<GroundAtom>();
00092 gf.getGroundAtoms(gas);
00093
00094 Vector<String> parentGAs = new Vector<String>();
00095 for(GroundAtom ga : gas) {
00096 if(ga == null)
00097 throw new Exception("null ground atom encountered");
00098 parentGAs.add(ga.toString());
00099 }
00100 BeliefNode node = addHardFormulaNode(nodeName, parentGAs);
00101
00102
00103
00104 String cpfid;
00105 if(useFormulaSimplification) {
00106 cpfid = "F" + i;
00107 }
00108 else
00109 cpfid = "F" + gkb.getTemplateID(gf);
00110 this.cpfIDs.put(node, cpfid);
00111
00112
00113 Value[] values = cpfCache.get(cpfid);
00114 if(values != null) {
00115
00116
00117
00118 node.getCPF().setValues(values);
00119 }
00120 else {
00121 fillFormulaCPF(gf, node.getCPF(), parentGAs);
00122 }
00123
00124 ++i;
00125 }
00126
00127 state = null;
00128 }
00129
00138 protected void fillFormulaCPF(Formula gf, CPF cpf, Vector<String> parentGAs) throws Exception {
00139 BeliefNode[] nodes = cpf.getDomainProduct();
00140 int[] addr = new int[nodes.length];
00141 assert parentGAs.size() == addr.length;
00142 fillFormulaCPF(gf, cpf, parentGAs, 1, addr);
00143 }
00144
00145 protected void fillFormulaCPF(Formula gf, CPF cpf, Vector<String> parentGAs, int iDomProd, int[] addr) throws Exception {
00146 BeliefNode[] domprod = cpf.getDomainProduct();
00147
00148
00149 if(iDomProd == domprod.length) {
00150
00151 double value = gf.isTrue(state) ? 1 : 0;
00152
00153
00154
00155
00156
00157
00158
00159 addr[0] = 0;
00160 cpf.put(addr, new ValueDouble(value));
00161
00162 addr[0] = 1;
00163 cpf.put(addr, new ValueDouble(1.0-value));
00164 return;
00165 }
00166
00167 BeliefNode parent = domprod[iDomProd];
00168 String parentGA = parentGAs.get(iDomProd-1);
00169 Discrete domain = (Discrete)parent.getDomain();
00170 boolean isBoolean = RelationalBeliefNetwork.isBooleanDomain(domain);
00171
00172 int trueIndex = 0;
00173 if(!isBoolean) {
00174 int iStart = parentGA.lastIndexOf(',')+1;
00175 int iEnd = parentGA.lastIndexOf(')');
00176 String outcome = parentGA.substring(iStart, iEnd);
00177 trueIndex = domain.findName(outcome);
00178 if(trueIndex == -1)
00179 throw new Exception("'" + outcome + "' not found in domain of " + parentGA);
00180 }
00181
00182 for(int i = 0; i < domain.getOrder(); i++) {
00183
00184 addr[iDomProd] = i;
00185
00186 if(i == trueIndex)
00187 state.set(parentGA, true);
00188 else
00189 state.set(parentGA, false);
00190
00191 fillFormulaCPF(gf, cpf, parentGAs, iDomProd+1, addr);
00192 }
00193 }
00194
00199 public KnowledgeBase getKB() {
00200 return gkb;
00201 }
00202
00203 public WorldVariables getWorldVars() {
00204 return coupling.getWorldVars();
00205 }
00206
00212 public String getVariableName(GroundAtom gndAtom) {
00213 if(bln.rbn.isBoolean(gndAtom.predicate))
00214 return gndAtom.toString();
00215 else
00216 return gndAtom.predicate + "(" + StringTool.join(",", gndAtom.args, 0, gndAtom.args.length-1) + ")";
00217 }
00218
00224 public BeliefNode getVariable(GroundAtom gndAtom) {
00225 return coupling.getVariable(gndAtom);
00226 }
00227
00228 public int getVariableValue(BeliefNode var, PossibleWorld w) {
00229 return coupling.getVariableValue(var, w);
00230 }
00231
00237 public boolean isRegularVariable(BeliefNode var) {
00238 return coupling.hasCoupling(var);
00239 }
00240
00245 public Set<BeliefNode> getRegularVariables() {
00246 return coupling.getCoupledVariables();
00247 }
00248
00249 public VariableLogicCoupling getCoupling() {
00250 return coupling;
00251 }
00252 }