00001 package edu.tum.cs.logic;
00002
00003 import java.util.ArrayList;
00004 import java.util.Arrays;
00005 import java.util.Collection;
00006 import java.util.Collections;
00007 import java.util.HashMap;
00008 import java.util.Map;
00009 import java.util.Vector;
00010
00011 import edu.tum.cs.srl.Database;
00012 import edu.tum.cs.util.StringTool;
00013
00014 public class Exist extends UngroundedFormula {
00015 Formula f;
00016 Collection<String> vars;
00020 Map<String, String> var2domName;
00021
00022 public Exist(Collection<String> vars, Formula f) {
00023 this.vars = vars;
00024 this.f = f;
00025 this.var2domName = new HashMap<String, String>();
00026 }
00027
00028 public Exist(String[] vars, Formula f) {
00029 this(Arrays.asList(vars), f);
00030 }
00031
00032 public String toString() {
00033 return "EXIST " + StringTool.join(",", vars) + " (" + f.toString() + ")";
00034 }
00035
00036 @Override
00037 public void getVariables(Database db, Map<String, String> ret) throws Exception {
00038 f.getVariables(db, ret);
00039 for(String var : vars) {
00040 var2domName.put(var, ret.remove(var));
00041 }
00042 }
00043
00044 @Override
00045 public Formula ground(Map<String, String> binding, WorldVariables worldVars, Database db) throws Exception {
00046
00047 if(var2domName.size() < vars.size()) {
00048 this.getVariables(db, new HashMap<String, String>());
00049 }
00050
00051 Vector<Formula> disjuncts = new Vector<Formula>();
00052 f.generateGroundings(disjuncts, db, binding, vars.toArray(new String[vars.size()]), 0, var2domName, worldVars, false);
00053 return new Disjunction(disjuncts);
00054 }
00055
00056 @Override
00057 public Formula toCNF() {
00058 throw new RuntimeException("Cannot convert ungrounded formula to CNF.");
00059 }
00060
00061 @Override
00062 public Formula toNNF() {
00063 throw new RuntimeException("Cannot convert ungrounded formula to NNF.");
00064 }
00065 }