001: package prefuse.data.expression;
002:
003: import java.util.HashMap;
004:
005: import prefuse.visual.expression.GroupSizeFunction;
006: import prefuse.visual.expression.HoverPredicate;
007: import prefuse.visual.expression.InGroupPredicate;
008: import prefuse.visual.expression.QueryExpression;
009: import prefuse.visual.expression.SearchPredicate;
010: import prefuse.visual.expression.ValidatedPredicate;
011: import prefuse.visual.expression.VisiblePredicate;
012:
013: /**
014: * Function table that allows lookup of registered FunctionExpressions
015: * by their function name.
016: *
017: * @author <a href="http://jheer.org">jeffrey heer</a>
018: */
019: public class FunctionTable {
020:
021: private FunctionTable() {
022: // prevent instantiation
023: }
024:
025: private static HashMap s_functionTable;
026: static {
027: s_functionTable = new HashMap();
028: // tuple functions
029: addFunction("ROW", RowFunction.class);
030: addFunction("ISNODE", IsNodeFunction.class);
031: addFunction("ISEDGE", IsEdgeFunction.class);
032: addFunction("DEGREE", DegreeFunction.class);
033: addFunction("INDEGREE", InDegreeFunction.class);
034: addFunction("OUTDEGREE", OutDegreeFunction.class);
035: addFunction("CHILDCOUNT", ChildCountFunction.class);
036: addFunction("TREEDEPTH", TreeDepthFunction.class);
037:
038: // numeric functions
039: addFunction("ABS", AbsFunction.class);
040: addFunction("ACOS", AcosFunction.class);
041: addFunction("ASIN", AsinFunction.class);
042: addFunction("ATAN", AtanFunction.class);
043: addFunction("ATAN2", Atan2Function.class);
044: addFunction("CEIL", CeilFunction.class);
045: addFunction("CEILING", CeilFunction.class);
046: addFunction("COS", CosFunction.class);
047: addFunction("COT", CotFunction.class);
048: addFunction("DEGREES", DegreesFunction.class);
049: addFunction("E", EFunction.class);
050: addFunction("EXP", ExpFunction.class);
051: addFunction("FLOOR", FloorFunction.class);
052: addFunction("LOG", LogFunction.class);
053: addFunction("LOG2", Log2Function.class);
054: addFunction("LOG10", Log10Function.class);
055: addFunction("MAX", MaxFunction.class);
056: addFunction("MIN", MaxFunction.class);
057: addFunction("MOD", MaxFunction.class);
058: addFunction("PI", PiFunction.class);
059: addFunction("POW", PowFunction.class);
060: addFunction("POWER", PowFunction.class);
061: addFunction("RADIANS", RadiansFunction.class);
062: addFunction("RAND", RandFunction.class);
063: addFunction("ROUND", RoundFunction.class);
064: addFunction("SIGN", SignFunction.class);
065: addFunction("SIN", SinFunction.class);
066: addFunction("SQRT", SqrtFunction.class);
067: addFunction("SUM", SumFunction.class);
068: addFunction("TAN", TanFunction.class);
069:
070: addFunction("SAFELOG10", SafeLog10Function.class);
071: addFunction("SAFESQRT", SafeSqrtFunction.class);
072:
073: // string functions
074: addFunction("CAP", CapFunction.class);
075: addFunction("CONCAT", ConcatFunction.class);
076: addFunction("CONCAT_WS", ConcatWsFunction.class);
077: addFunction("FORMAT", FormatFunction.class);
078: addFunction("INSERT", RPadFunction.class);
079: addFunction("LENGTH", LengthFunction.class);
080: addFunction("LOWER", LowerFunction.class);
081: addFunction("LCASE", LowerFunction.class);
082: addFunction("LEFT", LeftFunction.class);
083: addFunction("LPAD", LPadFunction.class);
084: addFunction("MID", SubstringFunction.class);
085: addFunction("POSITION", PositionFunction.class);
086: addFunction("REVERSE", ReverseFunction.class);
087: addFunction("REPEAT", RepeatFunction.class);
088: addFunction("REPLACE", ReplaceFunction.class);
089: addFunction("RIGHT", RightFunction.class);
090: addFunction("RPAD", RPadFunction.class);
091: addFunction("SPACE", SpaceFunction.class);
092: addFunction("SUBSTRING", SubstringFunction.class);
093: addFunction("UPPER", UpperFunction.class);
094: addFunction("UCASE", UpperFunction.class);
095:
096: // color functions
097: addFunction("RGB", RGBFunction.class);
098: addFunction("RGBA", RGBAFunction.class);
099: addFunction("GRAY", GrayFunction.class);
100: addFunction("HEX", HexFunction.class);
101: addFunction("HSB", HSBFunction.class);
102: addFunction("HSBA", HSBAFunction.class);
103: addFunction("COLORINTERP", ColorInterpFunction.class);
104:
105: // visualization functions
106: addFunction("GROUPSIZE", GroupSizeFunction.class);
107: addFunction("HOVER", HoverPredicate.class);
108: addFunction("INGROUP", InGroupPredicate.class);
109: addFunction("MATCH", SearchPredicate.class);
110: addFunction("QUERY", QueryExpression.class);
111: addFunction("VISIBLE", VisiblePredicate.class);
112: addFunction("VALIDATED", ValidatedPredicate.class);
113: }
114:
115: /**
116: * Indicates if a function of the given name is included in the function
117: * table.
118: * @param name the function name
119: * @return true if the function is in the table, false otherwise
120: */
121: public static boolean hasFunction(String name) {
122: return s_functionTable.containsKey(name);
123: }
124:
125: /**
126: * Add a function to the function table. It will then become available
127: * for use with compiled statements of the prefuse expression language.
128: * @param name the name of the function. This name must not already
129: * be registered in the table, i.e. there is no function overloading.
130: * @param type the Class instance of the function itself
131: */
132: public static void addFunction(String name, Class type) {
133: if (!Function.class.isAssignableFrom(type)) {
134: throw new IllegalArgumentException(
135: "Type argument must be a subclass of FunctionExpression.");
136: }
137: if (hasFunction(name)) {
138: throw new IllegalArgumentException(
139: "Function with that name already exists");
140: }
141: String lo = name.toLowerCase();
142: String hi = name.toUpperCase();
143: if (!name.equals(lo) && !name.equals(hi))
144: throw new IllegalArgumentException(
145: "Name can't have mixed case, try \"" + hi
146: + "\" instead.");
147: s_functionTable.put(lo, type);
148: s_functionTable.put(hi, type);
149: }
150:
151: /**
152: * Get a new Function instance for the function with the given name.
153: * @param name the name of the function to create
154: * @return the instantiated Function
155: */
156: public static Function createFunction(String name) {
157: Class type = (Class) s_functionTable.get(name);
158: if (type == null) {
159: throw new IllegalArgumentException(
160: "Unrecognized function name");
161: }
162: try {
163: return (Function) type.newInstance();
164: } catch (InstantiationException e) {
165: throw new RuntimeException(e);
166: } catch (IllegalAccessException e) {
167: throw new RuntimeException(e);
168: }
169: }
170:
171: } // end of class FunctionTable
|