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.Iterator;
00007 import java.util.Set;
00008 import java.util.Vector;
00009
00010 import edu.tum.cs.srl.Database;
00011 import edu.tum.cs.util.StringTool;
00012
00013 public class Disjunction extends ComplexFormula {
00014
00015 public Disjunction(Collection<Formula> children) {
00016 super(children);
00017 }
00018
00019 public Disjunction(Formula... children) {
00020 super(children);
00021 }
00022
00023 public String toString() {
00024 return "(" + StringTool.join(" v ", children) + ")";
00025 }
00026
00027 @Override
00028 public boolean isTrue(IPossibleWorld w) {
00029 for (Formula child : children)
00030 if (child.isTrue(w))
00031 return true;
00032 return false;
00033 }
00034
00035 @Override
00036 public Formula toCNF() {
00037
00038 Set<Formula> clause = new HashSet<Formula>();
00039 Set<String> strClause = new HashSet<String>();
00040 Collection<Conjunction> conjunctions = new Vector<Conjunction>();
00041
00042
00043 for (Formula child : children) {
00044 child = child.toCNF();
00045 if (child instanceof Conjunction) {
00046 conjunctions.add((Conjunction) child);
00047 } else if (child instanceof Disjunction) {
00048 for (Formula c : ((Disjunction) child).children)
00049 if (!strClause.contains(child.toString())) {
00050 clause.add(c);
00051 strClause.add(c.toString());
00052 }
00053
00054 } else if (child instanceof TrueFalse) {
00055 if (((TrueFalse) child).isTrue())
00056 return child;
00057 } else {
00058
00059 if (!strClause.contains(child.toString())) {
00060 clause.add(child);
00061 strClause.add(child.toString());
00062 }
00063 }
00064 }
00065 if (conjunctions.isEmpty())
00066 return clause.size() == 1 ? clause.iterator().next() : new Disjunction(clause);
00067 else {
00068
00069
00070 Iterator<Conjunction> i = conjunctions.iterator();
00071 Formula[] conjuncts = i.next().children;
00072 while (i.hasNext())
00073 clause.add(i.next());
00074 Formula RD = new Disjunction(clause);
00075 Vector<Formula> elems = new Vector<Formula>();
00076 for (Formula Ci : conjuncts)
00077 elems.add(new Disjunction(Ci, RD));
00078 return new Conjunction(elems).toCNF();
00079 }
00080 }
00081
00082 @Override
00083 public Formula toNNF() {
00084 Vector<Formula> disjuncts = new Vector<Formula>();
00085 for(Formula child : this.children) {
00086 Formula newChild = child.toNNF();
00087 if(newChild instanceof Disjunction) {
00088 for(Formula nestedChild : ((Disjunction)newChild).children)
00089 disjuncts.add(nestedChild);
00090 }
00091 else
00092 disjuncts.add(newChild);
00093 }
00094 return new Disjunction(disjuncts);
00095 }
00096
00102 @Override
00103 public Formula simplify(Database evidence) {
00104 Vector<Formula> simplifiedChildren = new Vector<Formula>();
00105
00106 for (Formula child : this.children) {
00107 child = child.simplify(evidence);
00108
00109 if (child instanceof TrueFalse) {
00110 if (((TrueFalse) child).isTrue())
00111 return TrueFalse.TRUE;
00112 else
00113 continue;
00114 } else
00115
00116 simplifiedChildren.add(child);
00117 }
00118
00119 if (!simplifiedChildren.isEmpty())
00120 return new Disjunction(simplifiedChildren);
00121 else
00122
00123 return TrueFalse.FALSE;
00124 }
00125 }