correct
[repair.git] / Repair / RepairCompiler / MCC / IR / DNFRule.java
1 package MCC.IR;
2 import java.util.*;
3
4 public class DNFRule {
5    Vector ruleconjunctions;
6
7     public DNFRule(Expr e) {
8         ruleconjunctions=new Vector();
9         ruleconjunctions.add(new RuleConjunction(new DNFExpr(false,e)));
10     }
11
12     public DNFRule(DNFExpr de) {
13         ruleconjunctions=new Vector();
14         ruleconjunctions.add(new RuleConjunction(de));
15     }
16
17     public DNFRule(RuleConjunction conj) {
18         ruleconjunctions=new Vector();
19         ruleconjunctions.add(conj);
20     }
21
22     public DNFRule(Vector conj) {
23         ruleconjunctions=conj;
24     }
25
26     DNFRule() {
27         ruleconjunctions=new Vector();
28     }
29
30     int size() {
31         return ruleconjunctions.size();
32     }
33
34     RuleConjunction get(int i) {
35         return (RuleConjunction)ruleconjunctions.get(i);
36     }
37
38     void add(RuleConjunction c) {
39         ruleconjunctions.add(c);
40     }
41
42     public DNFRule copy() {
43         Vector vector=new Vector();
44         for (int i=0;i<size();i++) {
45             vector.add(get(i).copy());
46         }
47         return new DNFRule(vector);
48     }
49
50     public DNFRule and(DNFRule c) {
51         DNFRule newdnf=new DNFRule();
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 DNFRule or(DNFRule c) {
61         DNFRule 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 DNFRule not() {
69         DNFRule copy=copy();
70         DNFRule notrule=null;
71         for (int i=0;i<size();i++) {
72             RuleConjunction conj=copy.get(i);
73             DNFRule newrule=null;
74             for (int j=0;j<conj.size();j++) {
75                 DNFExpr dp=conj.get(j);
76                 dp.negatePred();
77                 if (newrule==null)
78                    newrule=new DNFRule(dp);
79                 else
80                    newrule=newrule.or(new DNFRule(dp));
81             }
82             if (notrule==null)
83                notrule=newrule;
84             else
85                notrule=notrule.and(newrule);
86         }
87         return notrule;
88    }
89 }