/
BuiltInSymbolTable.java
119 lines (103 loc) · 3.63 KB
/
BuiltInSymbolTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Licensed to Odiago, Inc. under one or more contributor license
* agreements. See the NOTICE.txt file distributed with this work for
* additional information regarding copyright ownership. Odiago, Inc.
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.odiago.flumebase.exec;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.odiago.flumebase.exec.builtins.*;
import com.odiago.flumebase.lang.Function;
import com.odiago.flumebase.lang.Type;
/**
* A symbol table that is pre-populated with builtin functions
* and other symbols for the user to avail himself of.
*/
public class BuiltInSymbolTable extends SymbolTable {
private static final Logger LOG = LoggerFactory.getLogger(
BuiltInSymbolTable.class.getName());
private static Map<String, Symbol> BUILTINS;
static {
BUILTINS = new TreeMap<String, Symbol>();
// Add symbols for all built-in objects in the system.
loadBuiltinFunction(avg.class);
loadBuiltinFunction(bin2str.class);
loadBuiltinFunction(count.class);
loadBuiltinFunction(current_timestamp.class);
loadBuiltinFunction(event_timestamp.class);
loadBuiltinFunction(host.class);
loadBuiltinFunction(length.class);
loadBuiltinFunction(min.class);
loadBuiltinFunction(max.class);
loadBuiltinFunction(priority.class);
loadBuiltinFunction(priority_level.class);
loadBuiltinFunction(square.class);
loadBuiltinFunction(sum.class);
BUILTINS = Collections.unmodifiableMap(BUILTINS);
}
public BuiltInSymbolTable() {
}
@Override
public SymbolTable getParent() {
// This symbol table never has a parent.
return null;
}
@Override
public Symbol resolve(String symName) {
return resolveLocal(symName);
}
@Override
public Symbol resolveLocal(String symName) {
return BUILTINS.get(symName);
}
@Override
public void addSymbol(Symbol sym) {
throw new RuntimeException("Cannot add symbols to built in symbol table.");
}
@Override
public void remove(String symName) {
throw new RuntimeException("Cannot add symbols to built in symbol table.");
}
@Override
public Iterator<Symbol> iterator() {
return levelIterator();
}
@Override
public Iterator<Symbol> levelIterator() {
return BUILTINS.values().iterator();
}
/**
* Load instances of the built-in functions into the BuiltInSymbolTable.
*/
private static void loadBuiltinFunction(Class<? extends Function> cls) {
try {
Function fn = (Function) cls.newInstance();
Type retType = fn.getReturnType();
List<Type> argTypes = fn.getArgumentTypes();
String fnName = cls.getSimpleName();
LOG.debug("Loaded built-in function: " + fnName);
Symbol fnSymbol = new FnSymbol(fnName, fn, retType, argTypes);
BUILTINS.put(fnName, fnSymbol);
} catch (InstantiationException ie) {
LOG.error("Could not instantiate class: " + ie);
} catch (IllegalAccessException iae) {
LOG.error("IllegalAccessException creating class instance: " + iae);
}
}
}