00001 package edu.tum.cs.logic.sat;
00002
00003 import edu.tum.cs.logic.ComplexFormula;
00004 import edu.tum.cs.logic.Disjunction;
00005 import edu.tum.cs.logic.Formula;
00006 import edu.tum.cs.logic.GroundAtom;
00007 import edu.tum.cs.logic.GroundLiteral;
00008 import edu.tum.cs.logic.IPossibleWorld;
00009 import edu.tum.cs.srl.Database;
00010 import edu.tum.cs.util.StringTool;
00011
00012 public class Clause extends ComplexFormula {
00013
00014 public GroundLiteral[] lits;
00015
00016 public Clause(Formula f) throws Exception {
00017
00018 if(f instanceof GroundLiteral) {
00019 lits = new GroundLiteral[1];
00020 lits[0] = (GroundLiteral)f;
00021 }
00022 else if(f instanceof Disjunction) {
00023 Disjunction d = (Disjunction)f;
00024 lits = new GroundLiteral[d.children.length];
00025
00026 for(int i = 0; i < lits.length; i++) {
00027
00028 if(d.children[i] instanceof GroundLiteral)
00029 lits[i] = (GroundLiteral)d.children[i];
00030 else
00031 throw new Exception("Disjunction contains child of unacceptable type " + d.children[i].getClass().getSimpleName() + "; only GroundLiterals allowed.");
00032
00033 for(int j = 0; j < i; j++)
00034 if(lits[i].gndAtom == lits[j].gndAtom) {
00035 if(lits[i].isPositive != lits[j].isPositive)
00036 throw new TautologyException(d);
00037 throw new Exception("Tried to create SAT clause from disjunction with duplicate ground atoms: " + d);
00038 }
00039 }
00040 }
00041 else if(f instanceof GroundAtom) {
00042 lits = new GroundLiteral[1];
00043 lits[0] = new GroundLiteral(true, (GroundAtom)f);
00044 }
00045 else
00046 throw new Exception("Instance of type " + f.getClass().getSimpleName() + " cannot be treated as a clause");
00047 }
00048
00049 public static class TautologyException extends Exception {
00050 private static final long serialVersionUID = 1L;
00051
00052 public TautologyException(Disjunction d) {
00053 super("Tried to create SAT clause from tautology: " + d);
00054 }
00055 }
00056
00057 @Override
00058 public boolean isTrue(IPossibleWorld w) {
00059 for(GroundLiteral lit : lits)
00060 if(lit.isTrue(w))
00061 return true;
00062 return false;
00063 }
00064
00065 @Override
00066 public Formula toCNF() {
00067 return this;
00068 }
00069
00070 public String toString() {
00071 return StringTool.join(" v ", this.lits);
00072 }
00073
00074 @Override
00075 public Formula simplify(Database evidence) {
00076 throw new UnsupportedOperationException("Not supported yet.");
00077 }
00078
00079 @Override
00080 public Formula toNNF() {
00081 return this;
00082 }
00083 }