00001 package edu.tum.cs.logic;
00002
00003 import java.util.Arrays;
00004 import java.util.Collection;
00005 import java.util.HashSet;
00006 import java.util.Vector;
00007
00008 import edu.tum.cs.srl.Database;
00009 import edu.tum.cs.util.StringTool;
00010
00011 public class Conjunction extends ComplexFormula {
00012
00013 public Conjunction(Collection<Formula> children) {
00014 super(children);
00015 }
00016
00017 public Conjunction(Formula... children) {
00018 super(children);
00019 }
00020
00021 public String toString() {
00022 return "(" + StringTool.join(" ^ ", children) + ")";
00023 }
00024
00025 @Override
00026 public boolean isTrue(IPossibleWorld w) {
00027 for (Formula child : children)
00028 if (!child.isTrue(w))
00029 return false;
00030 return true;
00031 }
00032
00033 @Override
00034 public Formula toCNF() {
00035
00036 Vector<Formula> clauses = new Vector<Formula>();
00037
00038 for (Formula child : this.children) {
00039 child = child.toCNF();
00040 if (child instanceof Conjunction) {
00041 Conjunction conj = (Conjunction) child;
00042 clauses.addAll(Arrays.asList(conj.children));
00043 } else if (child instanceof TrueFalse) {
00044 TrueFalse tf = (TrueFalse) child;
00045 if (!tf.isTrue())
00046 return tf;
00047 } else
00048 clauses.add(child);
00049 }
00050
00051 Vector<HashSet<String>> sclauses = new Vector<HashSet<String>>();
00052 for (Formula clause : clauses) {
00053 HashSet<String> s = new HashSet<String>();
00054 if (clause instanceof Disjunction) {
00055 for (Formula l : ((Disjunction) clause).children)
00056 s.add(l.toString());
00057 } else {
00058 s.add(clause.toString());
00059 }
00060 sclauses.add(s);
00061 }
00062 for (int i = 0; i < sclauses.size(); i++) {
00063 for (int j = i + 1; j < sclauses.size(); j++) {
00064 HashSet<String> s1 = sclauses.get(i);
00065 HashSet<String> s2 = sclauses.get(j);
00066 int iLarger = j;
00067 if (s1.size() > s2.size()) {
00068 HashSet<String> t = s1;
00069 s1 = s2;
00070 s2 = t;
00071 iLarger = i;
00072 }
00073
00074 boolean isSubset = true;
00075 for (String s : s1)
00076 if (!s2.contains(s)) {
00077 isSubset = false;
00078 break;
00079 }
00080 if (!isSubset)
00081 continue;
00082
00083 sclauses.remove(iLarger);
00084 clauses.remove(iLarger);
00085 if (iLarger == j)
00086 j--;
00087 else
00088 j = i;
00089 }
00090 }
00091
00092 return new Conjunction(clauses);
00093 }
00094
00095 @Override
00096 public Formula toNNF() {
00097 Vector<Formula> conjuncts = new Vector<Formula>();
00098 for(Formula child : this.children) {
00099 Formula newChild = child.toNNF();
00100 if(newChild instanceof Conjunction) {
00101 for(Formula nestedChild : ((Conjunction)newChild).children)
00102 conjuncts.add(nestedChild);
00103 }
00104 else
00105 conjuncts.add(newChild);
00106 }
00107 return new Conjunction(conjuncts);
00108 }
00109
00115 @Override
00116 public Formula simplify(Database evidence) {
00117 Vector<Formula> simplifiedChildren = new Vector<Formula>();
00118
00119 for (Formula child : this.children) {
00120 child = child.simplify(evidence);
00121
00122 if (child instanceof TrueFalse) {
00123 if (!((TrueFalse) child).isTrue())
00124 return TrueFalse.FALSE;
00125 else
00126 continue;
00127 } else
00128
00129 simplifiedChildren.add(child);
00130 }
00131
00132 if (!simplifiedChildren.isEmpty())
00133 return new Conjunction(simplifiedChildren);
00134 else
00135
00136 return TrueFalse.TRUE;
00137 }
00138 }