use of org.apache.commons.jexl3.MapContext in project nutch by apache.
the class JexlExchange method match.
/**
* Determines if the document must go to the related index writers.
*
* @param doc The given document.
* @return True if the given document match with this exchange. False in other case.
*/
@Override
public boolean match(NutchDocument doc) {
// Create a context and add data
JexlContext jexlContext = new MapContext();
jexlContext.set("doc", doc);
try {
if (Boolean.TRUE.equals(expression.execute(jexlContext))) {
return true;
}
} catch (Exception ignored) {
}
return false;
}
use of org.apache.commons.jexl3.MapContext in project nutch by apache.
the class CrawlDatum method execute.
public boolean execute(JexlScript expr, String url) {
if (expr != null && url != null) {
// Create a context and add data
JexlContext jcontext = new MapContext();
// https://issues.apache.org/jira/browse/NUTCH-2229
jcontext.set("url", url);
jcontext.set("status", getStatusName(getStatus()));
jcontext.set("fetchTime", (long) (getFetchTime()));
jcontext.set("modifiedTime", (long) (getModifiedTime()));
jcontext.set("retries", getRetriesSinceFetch());
jcontext.set("interval", Integer.valueOf(getFetchInterval()));
jcontext.set("score", getScore());
jcontext.set("signature", StringUtil.toHexString(getSignature()));
// Set metadata variables
for (Map.Entry<Writable, Writable> entry : getMetaData().entrySet()) {
Object value = entry.getValue();
Text tkey = (Text) entry.getKey();
if (value instanceof FloatWritable) {
FloatWritable fvalue = (FloatWritable) value;
jcontext.set(tkey.toString(), fvalue.get());
}
if (value instanceof IntWritable) {
IntWritable ivalue = (IntWritable) value;
jcontext.set(tkey.toString(), ivalue.get());
}
if (value instanceof Text) {
Text tvalue = (Text) value;
jcontext.set(tkey.toString().replace("-", "_"), tvalue.toString());
}
if (value instanceof ProtocolStatus) {
ProtocolStatus pvalue = (ProtocolStatus) value;
jcontext.set(tkey.toString().replace("-", "_"), pvalue.toString());
}
}
try {
if (Boolean.TRUE.equals(expr.execute(jcontext))) {
return true;
}
} catch (Exception e) {
//
}
}
return false;
}
use of org.apache.commons.jexl3.MapContext in project waltz by khartec.
the class ReferenceBuilderNamespaceTest method evalTest.
@Test
public void evalTest() {
JexlBuilder builder = new JexlBuilder();
JexlEngine jexl = builder.create();
JexlExpression expr = jexl.createExpression("x == 'IN_SCOPE' && y == 10");
MapContext ctx1 = new MapContext();
ctx1.set("x", "IN_SCOPE");
ctx1.set("y", 10);
MapContext ctx2 = new MapContext();
ctx2.set("x", "IN_SCOPE");
ctx2.set("y", 12);
System.out.printf("Result1: [%s]\n", expr.evaluate(ctx1));
System.out.printf("Result2: [%s]\n", expr.evaluate(ctx2));
}
use of org.apache.commons.jexl3.MapContext in project traccar by tananaev.
the class ComputedAttributesHandler method prepareContext.
private MapContext prepareContext(Position position) {
MapContext result = new MapContext();
if (includeDeviceAttributes) {
Device device = identityManager.getById(position.getDeviceId());
if (device != null) {
for (Object key : device.getAttributes().keySet()) {
result.set((String) key, device.getAttributes().get(key));
}
}
}
Set<Method> methods = new HashSet<>(Arrays.asList(position.getClass().getMethods()));
methods.removeAll(Arrays.asList(Object.class.getMethods()));
for (Method method : methods) {
if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
String name = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4);
try {
if (!method.getReturnType().equals(Map.class)) {
result.set(name, method.invoke(position));
} else {
for (Object key : ((Map) method.invoke(position)).keySet()) {
result.set((String) key, ((Map) method.invoke(position)).get(key));
}
}
} catch (IllegalAccessException | InvocationTargetException error) {
LOGGER.warn("Attribute reflection error", error);
}
}
}
return result;
}
use of org.apache.commons.jexl3.MapContext in project opennms by OpenNMS.
the class ExpressionConfigWrapper method evaluate.
@Override
public double evaluate(Map<String, Double> values) throws ThresholdExpressionException {
// Add all of the variable values to the script context
Map<String, Object> context = new HashMap<String, Object>();
context.putAll(values);
// To workaround NMS-5019
context.put("datasources", new HashMap<String, Double>(values));
context.put("math", new MathBinding());
double result = Double.NaN;
try {
// Fetch an instance of the JEXL script engine to evaluate the script expression
Object resultObject = new JexlEngine().createExpression(m_expression.getExpression()).evaluate(new MapContext(context));
result = Double.parseDouble(resultObject.toString());
} catch (Throwable e) {
throw new ThresholdExpressionException("Error while evaluating expression " + m_expression.getExpression() + ": " + e.getMessage(), e);
}
return result;
}
Aggregations