00001 package edu.tum.cs.logic;
00002
00003 import java.util.Collection;
00004 import java.util.Map;
00005
00006 import edu.tum.cs.srl.Database;
00007 import edu.tum.cs.srl.Signature;
00008 import edu.tum.cs.util.StringTool;
00009
00010 public class Atom extends UngroundedFormula {
00011
00012 public Collection<String> params;
00013 public String predName;
00014
00015 public Atom(String predName, Collection<String> params) {
00016 this.predName = predName;
00017 this.params = params;
00018 }
00019
00020 @Override
00021 public String toString() {
00022 return predName + "(" + StringTool.join(",", params) + ")";
00023 }
00024
00025 @Override
00026 public void getVariables(Database db, Map<String, String> ret) throws Exception {
00027 Signature sig = db.getSignature(predName);
00028 if(sig == null)
00029 throw new Exception("Unknown predicate '" + predName + "'");
00030 int i = 0;
00031 for(String param : params) {
00032 if(isVariable(param)) {
00033 String type;
00034 if(i < sig.argTypes.length)
00035 type = sig.argTypes[i];
00036 else
00037 type = sig.returnType;
00038 String oldval = ret.put(param, type);
00039 if(oldval != null && !type.equals(oldval))
00040 throw new Exception("The variable " + param + " is bound to more than one domain: " + oldval + " and " + type);
00041 }
00042 ++i;
00043 }
00044 }
00045
00046 public static boolean isVariable(String paramName) {
00047 return Character.isLowerCase(paramName.charAt(0));
00048 }
00049
00050 @Override
00051 public Formula ground(Map<String, String> binding, WorldVariables vars, Database db) throws Exception {
00052 StringBuffer sb = new StringBuffer(predName + "(");
00053 int i = 0;
00054 for(String param : params) {
00055 if(i++ > 0)
00056 sb.append(',');
00057 String value = binding.get(param);
00058 if(value == null) {
00059 if(isVariable(param))
00060 throw new Exception("Cannot ground " + toString() + " with binding " + binding + " - variable " + param + " unbound.");
00061 value = param;
00062 }
00063 sb.append(value);
00064 }
00065 sb.append(')');
00066 String strGA = sb.toString();
00067 GroundAtom ga = vars.get(strGA);
00068 if(ga == null)
00069 throw new Exception("Could not find ground atom '" + strGA + "' in set of world variables " + vars);
00070 return ga;
00071 }
00072
00073 @Override
00074 public Formula toCNF() {
00075 return this;
00076 }
00077
00078 @Override
00079 public Formula toNNF() {
00080 return this;
00081 }
00082 }