00001 package edu.tum.cs.probcog;
00002
00003 import java.util.Collection;
00004 import java.util.HashMap;
00005 import java.util.Map;
00006 import java.util.Vector;
00007 import java.util.Map.Entry;
00008
00009 import edu.tum.cs.inference.IParameterHandler;
00010 import edu.tum.cs.inference.ParameterHandler;
00011 import edu.tum.cs.srl.Signature;
00012
00013 public abstract class Model implements IParameterHandler {
00017 protected Map<String,Object> defaultParameters;
00021 public HashMap<String,String> constantMapFromProbCog;
00025 public HashMap<String,String> constantMapToProbCog;
00029 protected String name;
00030 protected ParameterHandler paramHandler;
00031 protected HashMap<String, Object> actualParams;
00032 protected boolean haveSession = false;
00033
00034 public Model(String name) throws Exception {
00035 defaultParameters = new HashMap<String,Object>();
00036 this.name = name;
00037 constantMapFromProbCog = null;
00038 paramHandler = new ParameterHandler(this);
00039 }
00040
00041 protected abstract void _setEvidence(Iterable<String[]> evidence) throws Exception;
00042 public abstract void instantiate() throws Exception;
00043
00050 protected abstract java.util.Vector<InferenceResult> _infer(Iterable<String> queries) throws Exception;
00051 public abstract Vector<String[]> getDomains();
00052
00053 public abstract Vector<String[]> getPredicates();
00054
00055 protected static Vector<String[]> getPredicatesFromSignatures(Collection<Signature> sigs) {
00056 Vector<String[]> ret = new Vector<String[]>();
00057 for(Signature sig : sigs) {
00058 int numArgTypes = sig.argTypes.length;
00059 if(!sig.isBoolean())
00060 numArgTypes++;
00061 String[] a = new String[1+numArgTypes];
00062 a[0] = sig.functionName;
00063 for(int i = 1; i < a.length; i++) {
00064 if(i-1 < sig.argTypes.length)
00065 a[i] = sig.argTypes[i-1];
00066 else
00067 a[i] = sig.returnType;
00068 }
00069 ret.add(a);
00070 }
00071 return ret;
00072 }
00073
00074 public void beginSession(Map<String, Object> params) throws Exception {
00075 actualParams = new HashMap<String, Object>(defaultParameters);
00076 if(params != null)
00077 actualParams.putAll(params);
00078 paramHandler.handle(actualParams, false);
00079 }
00080
00081 public void beginSession() throws Exception {
00082 beginSession(null);
00083 }
00084
00085 public void setEvidence(Iterable<String[]> evidence) throws Exception {
00086 if(haveSession == false)
00087 beginSession();
00088
00089 Vector<String[]> newEvidence = new Vector<String[]>();
00090 for(String[] s : evidence) {
00091 boolean keep = true;
00092 for(int i = 1; i < s.length; i++) {
00093 s[i] = this.mapConstantToProbCog(s[i]);
00094 if(s[i] == null) {
00095 keep = false;
00096 break;
00097 }
00098 }
00099 if(keep)
00100 newEvidence.add(s);
00101 }
00102
00103 _setEvidence(newEvidence);
00104 }
00105
00106 public java.util.Vector<InferenceResult> infer(Iterable<String> queries) throws Exception {
00107
00108 Vector<InferenceResult> actualResults = _infer(queries);
00109
00110 Vector<InferenceResult> mappedResults = new Vector<InferenceResult>();
00111 for(InferenceResult r : actualResults) {
00112 if(!r.mapConstants(this))
00113 continue;
00114 mappedResults.add(r);
00115 }
00116 return mappedResults;
00117 }
00118
00124 public void setDefaultParameter(String key, Object value) {
00125 defaultParameters.put(key, value);
00126 }
00127
00128 public void setDefaultParameters(Map<String,Object> params) {
00129 this.defaultParameters = params;
00130 }
00131
00136 public void setConstantMap(HashMap<String,String> constantMap) {
00137 this.constantMapFromProbCog = constantMap;
00138
00139 constantMapToProbCog = new HashMap<String,String>();
00140 for(Entry<String, String> e : constantMapFromProbCog.entrySet())
00141 constantMapToProbCog.put(e.getValue(), e.getKey());
00142 }
00143
00149 public String mapConstantFromProbCog(String c) {
00150 if(constantMapFromProbCog == null)
00151 return c;
00152 String c2 = constantMapFromProbCog.get(c);
00153 if(c2 == null)
00154 return c;
00155 if(c2.length() == 0)
00156 return null;
00157 return c2;
00158 }
00159
00165 public String mapConstantToProbCog(String c) {
00166 if(constantMapToProbCog == null)
00167 return c;
00168 String c2 = constantMapToProbCog.get(c);
00169 if(c2 == null)
00170 return c;
00171 if(c2.length() == 0)
00172 return null;
00173 return c2;
00174 }
00175
00181 public String getConstantType(String constant) {
00182 constant = mapConstantToProbCog(constant);
00183 if(constant == null)
00184 return null;
00185 return _getConstantType(constant);
00186 }
00187
00188 protected abstract String _getConstantType(String constant);
00189
00190 public String getName() {
00191 return name;
00192 }
00193
00194 public ParameterHandler getParameterHandler() {
00195 return paramHandler;
00196 }
00197 }