00001 package edu.tum.cs.logic;
00002
00003 import java.util.Collection;
00004 import java.util.HashMap;
00005 import java.util.Map;
00006 import java.util.Set;
00007 import java.util.Vector;
00008
00009 import edu.tum.cs.logic.parser.FormulaParser;
00010 import edu.tum.cs.logic.parser.ParseException;
00011 import edu.tum.cs.srl.Database;
00012
00013 public abstract class Formula {
00020 public abstract void getVariables(Database db, Map<String,String> ret) throws Exception;
00029 public abstract Formula ground(Map<String, String> binding, WorldVariables worldVars, Database db) throws Exception;
00034 public abstract void getGroundAtoms(Set<GroundAtom> ret);
00035 public abstract boolean isTrue(IPossibleWorld w);
00036
00045 public Vector<Formula> getAllGroundings(Database db, WorldVariables worldVars, boolean simplify) throws Exception {
00046 Vector<Formula> ret = new Vector<Formula>();
00047 addAllGroundingsTo(ret, db, worldVars, simplify);
00048 return ret;
00049 }
00050
00059 public void addAllGroundingsTo(Collection<Formula> collection, Database db, WorldVariables worldVars, boolean simplify) throws Exception {
00060 HashMap<String, String> vars = new HashMap<String, String>();
00061 getVariables(db, vars);
00062 String[] varNames = vars.keySet().toArray(new String[vars.size()]);
00063 generateGroundings(collection, db, new HashMap<String, String>(), varNames, 0, vars, worldVars, simplify);
00064 }
00065
00078 protected void generateGroundings(Collection<Formula> ret, Database db, Map<String, String> binding, String[] varNames, int i, Map<String, String> var2domName, WorldVariables worldVars, boolean simplify) throws Exception {
00079
00080 if(i == varNames.length) {
00081 Formula f = (this.ground(binding, worldVars, db));
00082 if(simplify)
00083 f = f.simplify(db);
00084 if(f instanceof TrueFalse) {
00085
00086
00087 }
00088 else
00089 ret.add(f);
00090 return;
00091 }
00092
00093 String varName = varNames[i];
00094 String domName = var2domName.get(varName);
00095 Iterable<String> domain = db.getDomain(domName);
00096 if(domain == null)
00097 throw new Exception("Domain named '" + domName + "' (of variable " + varName + " in formula " + this.toString() + ") not found in the database!");
00098 for(String element : domain) {
00099 binding.put(varName, element);
00100 generateGroundings(ret, db, binding, varNames, i+1, var2domName, worldVars, simplify);
00101 }
00102 }
00103
00104 public abstract Formula toCNF();
00105
00106 public abstract Formula toNNF();
00107
00113 public abstract Formula simplify(Database evidence);
00114
00115 public static Formula fromString(String f) throws ParseException {
00116 return FormulaParser.parse(f);
00117 }
00118
00119 public static void main(String[] args) throws ParseException {
00120 String s = "a(x) <=> b(x)";
00121
00122
00123 s = "(a(x) ^ b(x) ^ !c(x) ^ !d(x)) v (a(x) ^ !b(x) ^ c(x) ^ !d(x)) v (!a(x) ^ b(x) ^ c(x) ^ !d(x)) v (a(x) ^ !b(x) ^ !c(x) ^ d(x)) v (!a(x) ^ b(x) ^ !c(x) ^ d(x)) v (!a(x) ^ !b(x) ^ c(x) ^ d(x))";
00124 Formula f = fromString(s);
00125 System.out.println("CNF: " + f.toCNF().toString());
00126 }
00127 }