55770f51614a349dcd7617fd2fe3712d1cbbe588
[repair.git] / Repair / RepairCompiler / MCC / IR / DNFConstraint.java
1 package MCC.IR;
2 import java.util.*;
3
4 public class DNFConstraint {
5    Vector conjunctions;
6
7     public DNFConstraint(Predicate p) {
8         conjunctions=new Vector();
9         conjunctions.add(new Conjunction(new DNFPredicate(false,p)));
10     }
11
12     public DNFConstraint(DNFPredicate dp) {
13         conjunctions=new Vector();
14         conjunctions.add(new Conjunction(dp));
15     }
16
17     public DNFConstraint(Conjunction conj) {
18         conjunctions=new Vector();
19         conjunctions.add(conj);
20     }
21
22     public DNFConstraint(Vector conj) {
23         conjunctions=conj;
24     }
25
26     DNFConstraint() {
27         conjunctions=new Vector();
28     }
29
30     int size() {
31         return conjunctions.size();
32     }
33
34     Conjunction get(int i) {
35         return (Conjunction)conjunctions.get(i);
36     }
37
38     void add(Conjunction c) {
39         conjunctions.add(c);
40     }
41
42     public DNFConstraint copy() {
43         Vector vector=new Vector();
44         for (int i=0;i<size();i++) {
45             vector.add(get(i).copy());
46         }
47         return new DNFConstraint(vector);
48     }
49
50     public DNFConstraint and(DNFConstraint c) {
51         DNFConstraint newdnf=new DNFConstraint();
52         for(int i=0;i<size();i++) {
53             for(int j=0;j<c.size();j++) {
54                 newdnf.add(get(i).append(c.get(j))); //Cross product
55             }
56         }
57         return newdnf;
58     }
59
60     public DNFConstraint or(DNFConstraint c) {
61         DNFConstraint copy=copy();
62         for(int i=0;i<c.size();i++) {
63             copy.add(c.get(i).copy()); //Add in other conjunctions
64         }
65         return copy;
66     }
67
68     public DNFConstraint not() {
69         DNFConstraint copy=copy();
70         DNFConstraint notconst=null;
71         for (int i=0;i<size();i++) {
72             Conjunction conj=copy.get(i);
73             DNFConstraint newconst=null;
74             for (int j=0;j<conj.size();j++) {
75                 DNFPredicate dp=conj.get(j);
76                 dp.negatePred();
77                 if (newconst==null)
78                    newconst=new DNFConstraint(dp);
79                 else
80                    newconst=newconst.or(new DNFConstraint(dp));
81             }
82             if (notconst==null)
83                notconst=newconst;
84             else
85                notconst=notconst.and(newconst);
86         }
87         return notconst;
88    }
89 }