make some methods static in Reduction
[sbp.git] / src / edu / berkeley / sbp / Reduction.java
1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.*;
5 import edu.berkeley.sbp.util.*;
6 import edu.berkeley.sbp.Parser.Table.*;
7 import edu.berkeley.sbp.Sequence.Position;
8 import java.io.*;
9 import java.util.*;
10 import java.lang.reflect.*;
11
12 final class Reduction implements Comparable<Reduction> {
13
14     public Position position;
15     public GSS.Phase phase;
16     public Forest result;
17     public Node node;
18     boolean done = false;
19     
20     public Reduction(Node node, Position position, Forest result, GSS.Phase target) {
21         this.position = position;
22         this.result = result;
23         this.phase = target;
24         this.node = node;
25     }
26
27     public void perform() {
28         if (done) return;
29         done = true;
30         node.finish(position, result, phase);
31     }
32
33     public String toString() { return position+""; }
34
35     public int compareTo(Reduction r) {
36         int ret = compareTo0(r);
37         if (ret == 0) {
38             Walk.Cache cache = node.state().cache();
39             if (canKill(cache, position, r.position) && canKill(cache, r.position, position)) throw new Error();
40             if      (canKill(cache, position,   r.position)) ret =  1;
41             else if (canKill(cache, r.position, position)) ret = -1;
42             if      (canNeed(cache, position,   r.position)) ret =  1;
43             else if (canNeed(cache, r.position, position)) ret = -1;
44         }
45         return -1 * ret;
46     }
47
48     private static boolean isRightNullable(Walk.Cache c, Position p) {
49         if (p.isLast()) return true;
50         if (!c.possiblyEpsilon(p.element())) return false;
51         return isRightNullable(c, p.next());
52     }
53
54     public static boolean canKill(Walk.Cache cache, Position mep, Position himp) {
55         if (!isRightNullable(cache, mep))  return false;
56         if (!isRightNullable(cache, himp)) return false;
57         Sequence me  = mep.owner();
58         Sequence him = himp.owner();
59         for(Sequence killer : him.hates()) {
60             HashSet<Sequence> eq2 = new Walk.EquivalentTo(killer, cache).walk();
61             if (eq2.contains(me)) return true;
62         }
63         return false;
64     }
65
66     public static final int LESS_THAN = -1;
67     public static final int EQUAL_TO = 0;
68     public static final int GREATER_THAN = 1;
69
70     public int compareTo0(Reduction n) {
71         if (targetPhase()==null && n.targetPhase()==null) return EQUAL_TO;
72         if (targetPhase()==null) return LESS_THAN;
73         if (n.targetPhase()==null) return GREATER_THAN;
74         if (targetPhase().pos < n.targetPhase().pos) return LESS_THAN;
75         if (targetPhase().pos > n.targetPhase().pos) return GREATER_THAN;
76         return 0;
77     }
78     public int pos() { return targetPhase()==null ? 0 : targetPhase().pos; }
79     public GSS.Phase targetPhase() { return node.phase(); }
80
81     public static boolean canNeed(Walk.Cache cache, Position mep, Position himp) {
82         if (!isRightNullable(cache, mep))  return false;
83         if (!isRightNullable(cache, himp)) return false;
84         Sequence me  = mep.owner();
85         Sequence him = himp.owner();
86         for(Sequence needer : him.needs()) {
87             HashSet<Sequence> eq2 = new Walk.EquivalentTo(needer, cache).walk();
88             if (eq2.contains(me)) return true;
89         }
90         return false;
91     }
92 }