Search in sources :

Example 26 with MapContext

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;
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Example 27 with MapContext

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;
}
Also used : FloatWritable(org.apache.hadoop.io.FloatWritable) JexlContext(org.apache.commons.jexl3.JexlContext) Writable(org.apache.hadoop.io.Writable) FloatWritable(org.apache.hadoop.io.FloatWritable) IntWritable(org.apache.hadoop.io.IntWritable) Text(org.apache.hadoop.io.Text) MapContext(org.apache.commons.jexl3.MapContext) HashMap(java.util.HashMap) Map(java.util.Map) IntWritable(org.apache.hadoop.io.IntWritable) ProtocolStatus(org.apache.nutch.protocol.ProtocolStatus) IOException(java.io.IOException) VersionMismatchException(org.apache.hadoop.io.VersionMismatchException)

Example 28 with MapContext

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));
}
Also used : JexlEngine(org.apache.commons.jexl3.JexlEngine) JexlBuilder(org.apache.commons.jexl3.JexlBuilder) JexlExpression(org.apache.commons.jexl3.JexlExpression) MapContext(org.apache.commons.jexl3.MapContext) Test(org.junit.jupiter.api.Test)

Example 29 with MapContext

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;
}
Also used : Device(org.traccar.model.Device) MapContext(org.apache.commons.jexl2.MapContext) Method(java.lang.reflect.Method) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Example 30 with MapContext

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;
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) HashMap(java.util.HashMap) MapContext(org.apache.commons.jexl2.MapContext)

Aggregations

MapContext (org.apache.commons.jexl2.MapContext)32 MapContext (org.apache.commons.jexl3.MapContext)26 JexlContext (org.apache.commons.jexl2.JexlContext)23 Expression (org.apache.commons.jexl2.Expression)20 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 JexlContext (org.apache.commons.jexl3.JexlContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)7 Map (java.util.Map)7 HashSet (java.util.HashSet)4 JexlException (org.apache.commons.jexl2.JexlException)4 JexlBuilder (org.apache.commons.jexl3.JexlBuilder)4 JexlExpression (org.apache.commons.jexl3.JexlExpression)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JexlEngine (org.apache.commons.jexl3.JexlEngine)3 Test (org.junit.jupiter.api.Test)3 Device (org.traccar.model.Device)3 IOException (java.io.IOException)2