View Javadoc
1   package org.wikimedia.search.extra.regex.expression;
2   
3   import com.google.common.collect.ImmutableSet;
4   
5   /**
6    * Immutable representation of some logic. Expressions can be simplified and
7    * transformed. Simplifying expressions eliminates extraneous terms and factors
8    * out common terms.  Transformation allows client code to convert the expression
9    * to some other (maybe evaluable) form.
10   *
11   * @param <T> type stored in leaves
12   */
13  public interface Expression<T> {
14      /**
15       * Returns a simplified copy of this expression. If the simplification
16       * didn't change anything then returns this.
17       */
18      Expression<T> simplify();
19  
20      /**
21       * Is this node in the expression always true? Note that this only
22       * represents _this_ node of the expression. To know if the entire
23       * expression is always true of false first {@link Expression#simplify()}
24       * it.
25       */
26      boolean alwaysTrue();
27  
28      /**
29       * Is this node in the expression always false? Note that this only
30       * represents _this_ node of the expression. To know if the entire
31       * expression is always true of false first {@link Expression#simplify()}
32       * it.
33       */
34      boolean alwaysFalse();
35  
36      /**
37       * Is this expression made of many subexpressions?
38       */
39      boolean isComposite();
40  
41      /**
42       * Compute recursively the number of boolean clauses in this branch.
43       * @return the number of boolean clauses, Integer.MAX_INT may be returned if the number of clauses is greater
44       */
45      int countClauses();
46  
47      /**
48       * Transform this expression into another form.
49       *
50       * @param <J> result of the transformation.
51       */
52      <J> J transform(Transformer<T, J> transformer);
53  
54      /**
55       * Transformer for expression components.
56       *
57       * @param <T> type stored in leaves
58       * @param <J> result of the transformation.
59       */
60      interface Transformer<T, J> {
61          /**
62           * Transform an expression that is always true.
63           */
64          J alwaysTrue();
65  
66          /**
67           * Transform an expression that is always false.
68           */
69          J alwaysFalse();
70  
71          /**
72           * Transform a leaf expression.
73           *
74           * @param t data stored in the leaf
75           * @return result of the transform
76           */
77          J leaf(T t);
78  
79          /**
80           * Transform an and expression.
81           *
82           * @param js transformed sub-expressions
83           * @return result of the transform
84           */
85          J and(ImmutableSet<J> js);
86  
87          /**
88           * Transform an or expression.
89           *
90           * @param js transformed sub-expressions
91           * @return result of the transform
92           */
93          J or(ImmutableSet<J> js);
94      }
95  }