00001
00002
00003
00004
00005
00006
00007 package edu.tum.cs.srl.bayesnets.bln.coupling;
00008
00009 import java.util.HashMap;
00010 import java.util.Set;
00011 import java.util.Vector;
00012
00013 import edu.ksu.cis.bnj.ver3.core.BeliefNode;
00014 import edu.ksu.cis.bnj.ver3.core.Discrete;
00015 import edu.tum.cs.logic.GroundAtom;
00016 import edu.tum.cs.logic.GroundLiteral;
00017 import edu.tum.cs.logic.PossibleWorld;
00018 import edu.tum.cs.logic.WorldVariables;
00019 import edu.tum.cs.logic.WorldVariables.Block;
00020
00021 public class VariableLogicCoupling {
00022 protected WorldVariables worldVars;
00026 protected HashMap<BeliefNode, IVariableLogicCoupler> variable2groundAtomLookup;
00027 protected HashMap<GroundAtom, BeliefNode> groundAtom2variable;
00028
00029 public VariableLogicCoupling() {
00030 worldVars = new WorldVariables();
00031 variable2groundAtomLookup = new HashMap<BeliefNode, IVariableLogicCoupler>();
00032 groundAtom2variable = new HashMap<GroundAtom, BeliefNode>();
00033 }
00034
00035 public void addBooleanVariable(BeliefNode var, String predicateName, String[] params) {
00036 GroundAtom ga = new GroundAtom(predicateName, params);
00037 worldVars.add(ga);
00038 variable2groundAtomLookup.put(var, new BooleanVariable(ga));
00039 groundAtom2variable.put(ga, var);
00040 }
00041
00042 public void addBlockVariable(BeliefNode var, Discrete dom, String functionName, String[] params) {
00043 String[] atomParams = new String[params.length+1];
00044 for(int i = 0; i < params.length; i++)
00045 atomParams[i] = params[i];
00046 Vector<GroundAtom> block = new Vector<GroundAtom>(dom.getOrder());
00047 for(int i = 0; i < dom.getOrder(); i++) {
00048 atomParams[atomParams.length-1] = dom.getName(i);
00049 GroundAtom ga = new GroundAtom(functionName, atomParams.clone());
00050 block.add(ga);
00051 }
00052 Block b = worldVars.addBlock(block);
00053 for(GroundAtom ga : b)
00054 groundAtom2variable.put(ga, var);
00055 variable2groundAtomLookup.put(var, new BlockVariable(b));
00056 }
00057
00058 public GroundLiteral getGroundLiteral(BeliefNode var, int domIdx) {
00059 IVariableLogicCoupler vlc = variable2groundAtomLookup.get(var);
00060 if(vlc == null)
00061 throw new RuntimeException("Variable " + var + " has no logical coupling!");
00062 return vlc.getGroundLiteral(domIdx, worldVars);
00063 }
00064
00065 public WorldVariables getWorldVars() {
00066 return worldVars;
00067 }
00068
00074 public BeliefNode getVariable(GroundAtom gndAtom) {
00075 return groundAtom2variable.get(gndAtom);
00076 }
00077
00078 public int getVariableValue(BeliefNode var, PossibleWorld w) {
00079 return variable2groundAtomLookup.get(var).getValue(w);
00080 }
00081
00088 public void setVariableValue(BeliefNode var, int domIdx, PossibleWorld w) {
00089 variable2groundAtomLookup.get(var).setValue(w, domIdx);
00090 }
00091
00097 public boolean hasCoupling(BeliefNode var) {
00098 return variable2groundAtomLookup.containsKey(var);
00099 }
00100
00105 public Set<BeliefNode> getCoupledVariables() {
00106 return variable2groundAtomLookup.keySet();
00107 }
00108
00109 public Set<GroundAtom> getCoupledGroundAtoms() {
00110 return groundAtom2variable.keySet();
00111 }
00112
00118 public Iterable<String> getOriginalParams(BeliefNode var) {
00119 return variable2groundAtomLookup.get(var).getOriginalParams();
00120 }
00121 }