01: package prefuse.data.expression;
02:
03: import java.util.Collections;
04: import java.util.HashSet;
05: import java.util.Set;
06:
07: /**
08: * Library class that computes some simple analyses of an expression. Each
09: * analysis is computed using a visitor instance.
10: *
11: * @author <a href="http://jheer.org">jeffrey heer</a>
12: */
13: public class ExpressionAnalyzer {
14:
15: /**
16: * Determine if an expression has a dependency on a data field.
17: * @param expr the expression to analyze
18: * @return true if the expression has at least one tuple data
19: * field dependency.
20: */
21: public static boolean hasDependency(Expression expr) {
22: ColumnCollector cc = new ColumnCollector(false);
23: expr.visit(cc);
24: return cc.getColumnCount() > 0;
25: }
26:
27: /**
28: * Get the set of data fields the expression is dependent upon.
29: * @param expr the expression to analyze
30: * @return a set of all data field names the expression references
31: */
32: public static Set getReferencedColumns(Expression expr) {
33: ColumnCollector cc = new ColumnCollector(true);
34: expr.visit(cc);
35: return cc.getColumnSet();
36: }
37:
38: /**
39: * ExpressionVisitor that collects all referenced columns / data fields
40: * in an Expression.
41: */
42: private static class ColumnCollector implements ExpressionVisitor {
43: private boolean store;
44: private Set m_cols;
45: private int m_count;
46:
47: public ColumnCollector(boolean store) {
48: this .store = store;
49: }
50:
51: public int getColumnCount() {
52: return m_count;
53: }
54:
55: public Set getColumnSet() {
56: if (m_cols == null) {
57: return Collections.EMPTY_SET;
58: } else {
59: return m_cols;
60: }
61: }
62:
63: public void visitExpression(Expression expr) {
64: if (expr instanceof ColumnExpression) {
65: ++m_count;
66: if (store) {
67: String field = ((ColumnExpression) expr)
68: .getColumnName();
69: if (m_cols == null)
70: m_cols = new HashSet();
71: m_cols.add(field);
72: }
73:
74: }
75: }
76:
77: public void down() {
78: // do nothing
79: }
80:
81: public void up() {
82: // do nothing
83: }
84: }
85:
86: } // end of class ExpressionAnalyzer
|