-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathExp4JTemporalExpressionEvaluator.java
353 lines (306 loc) · 14.9 KB
/
Exp4JTemporalExpressionEvaluator.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.brein.time.expressions;
import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
import net.objecthunter.exp4j.function.Function;
import net.objecthunter.exp4j.tokenizer.UnknownFunctionOrVariableException;
import org.apache.log4j.Logger;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Exp4JTemporalExpressionEvaluator implements ITemporalExpressionEvaluator {
public static final String GENERAL_VARIABLES = "generalVariables";
public static final String ADDITIONAL_FUNCTIONS = "additionalFunctions";
public static final String LOWEST_TIME_GRANULARITY = "smallestTimeUnit";
public static final TimeUnit DEFAULT_LOWEST_TIME_GRANULARITY = TimeUnit.MILLISECONDS;
private static final Logger LOGGER = Logger.getLogger(Exp4JTemporalExpressionEvaluator.class);
private static final Map<String, TimeUnit> UNITS = Stream.of(
new SimpleEntry<>("ns", TimeUnit.NANOSECONDS),
new SimpleEntry<>("μs", TimeUnit.MICROSECONDS),
new SimpleEntry<>("ms", TimeUnit.MILLISECONDS),
new SimpleEntry<>("sec", TimeUnit.SECONDS),
new SimpleEntry<>("min", TimeUnit.MINUTES),
new SimpleEntry<>("h", TimeUnit.HOURS),
new SimpleEntry<>("d", TimeUnit.DAYS)
).collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
private final Map<String, Expression> expressions = new ConcurrentHashMap<>();
private Map<String, Double> generalVariables;
private List<Function> additionalFunctions;
private TimeUnit lowestTimeGranularity;
public void init() {
init(Collections.emptyMap());
}
@Override
public void init(final Map<String, Object> settings) {
// this has to be done first, so that all the other methods work properly
this.lowestTimeGranularity = readLowestTimeGranularity(settings);
/*
* The settings may contain a map of general-variables, which bind a numeric value
* to a named variable.
*/
this.generalVariables = new HashMap<>();
this.generalVariables.putAll(createDefaultVariables());
this.generalVariables.putAll(readGeneralVariables(settings));
/*
* Now add all the additional functions, which may have been defined via settings
* or are additional functions, which are provided in the temporal context.
*/
this.additionalFunctions = new ArrayList<>();
this.additionalFunctions.addAll(createDefaultAdditionalFunctions());
this.additionalFunctions.addAll(readAdditionalFunctions(settings));
}
@Override
public String addFormula(final String identifier,
final String formula,
final Set<String> variables) throws IllegalArgumentException {
final Set<String> normalizedVariables = variables == null ? Collections.emptySet() : variables;
try {
this.expressions.put(identifier, new ExpressionBuilder(formula)
.functions(this.additionalFunctions)
.variables(this.generalVariables.keySet())
.variables(normalizedVariables)
.build());
} catch (final UnknownFunctionOrVariableException e) {
throw new IllegalArgumentException("Unable to parse the formula: " + formula, e);
}
return identifier;
}
@Override
public void removeFormula(final String identifier) {
this.expressions.remove(identifier);
}
@Override
public long evaluate(final String identifier,
final Map<String, Long> variableBindings) throws IllegalArgumentException {
final Expression expression = this.expressions.get(identifier);
if (expression == null) {
throw new IllegalArgumentException("The identifier '" + identifier + "'" +
" does not have any valid formula attached.");
}
// add the variables (first general, afterwards specific) and evaluate
final Double result = expression
.setVariables(this.generalVariables)
.setVariables(variableBindings.entrySet().stream()
.map(e -> new SimpleEntry<>(e.getKey(), e.getValue().doubleValue()))
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)))
.evaluate();
// return the long-value
if (result.isNaN() || result.isInfinite()) {
LOGGER.warn("Received invalid long-result, returning 0 instead.");
return 0L;
} else {
return result.longValue();
}
}
@Override
public TimeUnit getEvaluationUnit() {
return this.lowestTimeGranularity;
}
/**
* There is no need to actually close this implementation of the {@code ITemporalExpressionEvaluator}, since
* it does not bind any resources. Nevertheless, to fulfill the interface the method is implemented.
*
* @see ITemporalExpressionEvaluator
*/
@Override
public void close() {
this.expressions.clear();
this.generalVariables = null;
this.additionalFunctions = null;
}
protected TimeUnit readLowestTimeGranularity(final Map<String, Object> settings) {
final Object lowestGranularityObj = settings.get(LOWEST_TIME_GRANULARITY);
if (lowestGranularityObj == null) {
return DEFAULT_LOWEST_TIME_GRANULARITY;
} else if (TimeUnit.class.isInstance(lowestGranularityObj)) {
return TimeUnit.class.cast(lowestGranularityObj);
} else if (String.class.isInstance(lowestGranularityObj)) {
try {
return TimeUnit.valueOf(String.class.cast(lowestGranularityObj).toUpperCase());
} catch (final IllegalArgumentException e) {
LOGGER.error(String.format("Unable to parse the specified '%s' value '%s', using default '%s'.",
LOWEST_TIME_GRANULARITY, lowestGranularityObj, DEFAULT_LOWEST_TIME_GRANULARITY), e);
return DEFAULT_LOWEST_TIME_GRANULARITY;
}
} else {
LOGGER.warn(String.format("Unable to parse the specified '%s' value '%s', using default '%s'.",
LOWEST_TIME_GRANULARITY, lowestGranularityObj, DEFAULT_LOWEST_TIME_GRANULARITY));
return DEFAULT_LOWEST_TIME_GRANULARITY;
}
}
protected Map<String, Double> createDefaultVariables() {
return UNITS.entrySet().stream()
.filter(e -> this.lowestTimeGranularity.compareTo(e.getValue()) <= 0)
.map(e -> {
final Long value = this.lowestTimeGranularity.convert(1, e.getValue());
return new SimpleEntry<>(e.getKey(), value.doubleValue());
})
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
}
protected Map<String, Double> readGeneralVariables(final Map<String, Object> settings) {
if (settings == null) {
return Collections.emptyMap();
}
final Object generalVariablesObj = settings.get(GENERAL_VARIABLES);
if (generalVariablesObj == null) {
return Collections.emptyMap();
} else if (!(generalVariablesObj instanceof Map)) {
return Collections.emptyMap();
}
/*
* Otherwise let's make sure the elements in the map are valid and
* create a map that contains double values.
*/
@SuppressWarnings("unchecked")
final Map<Object, Object> map = Map.class.cast(generalVariablesObj);
return map.entrySet().stream()
.map(e -> {
final Object nameObj = e.getKey();
final Object valueObj = e.getValue();
if (String.class.isInstance(nameObj) && Number.class.isInstance(valueObj)) {
return new SimpleEntry<>(String.class.cast(nameObj),
Number.class.cast(valueObj).doubleValue());
} else {
LOGGER.warn(String.format("Found invalid '%s' settings " + "(key: %s, value: %s)",
GENERAL_VARIABLES, nameObj, valueObj));
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));
}
protected List<Function> createDefaultAdditionalFunctions() {
final List<Function> defaultAdditionalFunctions = new ArrayList<>();
defaultAdditionalFunctions.add(new Function("now", 0) {
@Override
public double apply(final double... doubles) {
final TimeUnit unit = Exp4JTemporalExpressionEvaluator.this.lowestTimeGranularity;
return unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
});
defaultAdditionalFunctions.addAll(UNITS.entrySet().stream()
.filter(e -> this.lowestTimeGranularity.compareTo(e.getValue()) <= 0)
.map(e -> new Function(createToFunctionName(e.getValue()), 1) {
@Override
public double apply(final double... args) {
final Double value = args[0];
final long mappedValue = value.longValue();
final TimeUnit unit = Exp4JTemporalExpressionEvaluator.this.lowestTimeGranularity;
return e.getValue().convert(mappedValue, unit);
}
})
.collect(Collectors.toList()));
return defaultAdditionalFunctions;
}
protected String createToFunctionName(final TimeUnit unit) {
final String lowerCase = unit.name().toLowerCase();
return "to" + lowerCase.substring(0, 1).toUpperCase() + lowerCase.substring(1);
}
protected List<Function> readAdditionalFunctions(final Map<String, Object> settings) {
if (settings == null) {
return Collections.emptyList();
}
final Object additionalFunctionsObj = settings.get(ADDITIONAL_FUNCTIONS);
if (additionalFunctionsObj == null) {
return Collections.emptyList();
} else if (!(additionalFunctionsObj instanceof Map)) {
return Collections.emptyList();
}
/*
* Otherwise let's make sure the elements in the map are valid and
* create a map that contains functions as values.
*/
@SuppressWarnings("unchecked")
final Map<Object, Object> map = Map.class.cast(additionalFunctionsObj);
return map.entrySet().stream()
.map(e -> {
final Object nameObj = e.getKey();
final Object valueObj = e.getValue();
if (!String.class.isInstance(nameObj)) {
LOGGER.warn(String.format("Found invalid %s (key: %s, value: %s)",
ADDITIONAL_FUNCTIONS, nameObj, valueObj));
return null;
}
final String name = String.class.cast(nameObj);
if (Function.class.isInstance(valueObj)) {
final Function func = Function.class.cast(valueObj);
if (!name.equals(func.getName())) {
LOGGER.warn(String.format("Mismatch in function name (%s vs. %s)", name, func.getName()));
}
return func;
} else if (java.util.function.Supplier.class.isInstance(valueObj)) {
return new Function(name, 0) {
@Override
@SuppressWarnings("unchecked")
public double apply(final double... args) {
final Object result = java.util.function.Supplier.class.cast(valueObj).get();
return toDouble(result);
}
};
} else if (java.util.function.Function.class.isInstance(valueObj)) {
return new Function(name, 1) {
@Override
@SuppressWarnings("unchecked")
public double apply(final double... args) {
final Object result = java.util.function.Function.class.cast(valueObj)
.apply(toDouble(args[0]));
return toDouble(result);
}
};
} else if (java.util.function.BiFunction.class.isInstance(valueObj)) {
return new Function(name, 2) {
@Override
@SuppressWarnings("unchecked")
public double apply(final double... args) {
final Object result = java.util.function.BiFunction.class.cast(valueObj)
.apply(toDouble(args[0]), toDouble(args[1]));
return toDouble(result);
}
};
} else {
LOGGER.warn(String.format("Found invalid additionalFunction (key: %s, value: %s)",
name, valueObj));
return null;
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
/**
* Simple helper method to transform an {@code Object} into a {@code double}. The method returns {@code NaN}, if
* the value cannot be transformed.
*
* @param val the value to transform
*
* @return the created double
*/
protected double toDouble(final Object val) {
if (val == null) {
return Double.NaN;
} else if (Double.class.isInstance(val)) {
return Double.class.cast(val);
} else if (Number.class.isInstance(val)) {
return Number.class.cast(val).doubleValue();
} else if (double.class.isInstance(val)) {
return double.class.cast(val);
} else if (long.class.isInstance(val)) {
return long.class.cast(val);
} else if (int.class.isInstance(val)) {
return int.class.cast(val);
} else if (byte.class.isInstance(val)) {
return byte.class.cast(val);
} else if (float.class.isInstance(val)) {
return float.class.cast(val);
} else {
return Double.NaN;
}
}
}