KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > PredicateChain


1 /**
2  *
3  */

4 package prefuse.util;
5
6 import prefuse.data.Tuple;
7 import prefuse.data.expression.Expression;
8 import prefuse.data.expression.IfExpression;
9 import prefuse.data.expression.ObjectLiteral;
10 import prefuse.data.expression.Predicate;
11
12 /**
13  * A chain of Predicates and associated values, maintain a large
14  * if-statement structure for looking up values based on a Predicate
15  * condition.
16  * @author <a HREF="http://jheer.org">jeffrey heer</a>
17  */

18 public class PredicateChain {
19     
20     private Expression m_head = new ObjectLiteral(null);
21     private IfExpression m_tail = null;
22     
23     /**
24      * Return the backing predicate chain as an Expression instance.
25      * @return the predicate chain, either an IfExpression or
26      * the single terminal default Expression instance.
27      */

28     public Expression getExpression() {
29         return m_head;
30     }
31     
32     /**
33      * Evaluate the predicate chain for the given Tuple.
34      * @param t the Tuple
35      * @return the object associated with the first Predicate
36      * that successfully matches the Tuple.
37      */

38     public Object JavaDoc get(Tuple t) {
39         return m_head.get(t);
40     }
41     
42     /**
43      * Add a new rule to the end of the chain, associating a Predicate
44      * condition with an Object value.
45      * @param p the Predicate condition
46      * @param val the associated Object value
47      */

48     public void add(Predicate p, Object JavaDoc val) {
49         if ( m_tail == null ) {
50             m_tail = new IfExpression(p, new ObjectLiteral(val), m_head);
51             m_head = m_tail;
52         } else {
53             IfExpression ie = new IfExpression(p, new ObjectLiteral(val),
54                                                m_tail.getElseExpression());
55             m_tail.setElseExpression(ie);
56             m_tail = ie;
57         }
58     }
59     
60     /**
61      * Remove rules using the given predicate from this predicate chain.
62      * This method will not remove rules in which this predicate is used
63      * within a composite of clauses, such as an AND or OR. It only removes
64      * rules using this predicate as the top-level trigger.
65      * @param p the predicate to remove from the chain
66      * @return true if a rule was successfully removed, false otherwise
67      */

68     public boolean remove(Predicate p) {
69         if ( p == null ) return false;
70         
71         IfExpression prev = null;
72         Expression expr = m_head;
73         while ( expr instanceof IfExpression ) {
74             IfExpression ifex = (IfExpression)expr;
75             Predicate test = (Predicate)ifex.getTestPredicate();
76             if ( p.equals(test) ) {
77                 Expression elseex = ifex.getElseExpression();
78                 ifex.setElseExpression(new ObjectLiteral(null));
79                 if ( prev != null ) {
80                     prev.setElseExpression(elseex);
81                     if ( ifex == m_tail )
82                         m_tail = prev;
83                 } else {
84                     m_head = elseex;
85                     if ( ifex == m_tail )
86                         m_tail = null;
87                 }
88                 return true;
89             } else {
90                 prev = ifex;
91                 expr = ifex.getElseExpression();
92             }
93         }
94         return false;
95     }
96     
97     /**
98      * Remove all rules from the predicate chain.
99      */

100     public void clear() {
101         m_head = new ObjectLiteral(null);
102         m_tail = null;
103     }
104     
105 } // end of class PredicateChain
106
Popular Tags