Search in sources :

Example 11 with JexlArithmetic

use of org.apache.commons.jexl2.JexlArithmetic in project commons-jexl by apache.

the class Operators method startsWith.

/**
 * The 'startsWith' operator implementation.
 * @param node     the node
 * @param operator the calling operator, $= or $!
 * @param left     the left operand
 * @param right    the right operand
 * @return true if left starts with right, false otherwise
 */
protected boolean startsWith(final JexlNode node, final String operator, final Object left, final Object right) {
    final JexlArithmetic arithmetic = interpreter.arithmetic;
    final JexlUberspect uberspect = interpreter.uberspect;
    try {
        // try operator overload
        final Object result = tryOverload(node, JexlOperator.STARTSWITH, left, right);
        if (result instanceof Boolean) {
            return (Boolean) result;
        }
        // use arithmetic / pattern matching ?
        final Boolean matched = arithmetic.startsWith(left, right);
        if (matched != null) {
            return matched;
        }
        // try a startsWith method (duck type)
        try {
            final Object[] argv = { right };
            JexlMethod vm = uberspect.getMethod(left, "startsWith", argv);
            if (returnsBoolean(vm)) {
                return (Boolean) vm.invoke(left, argv);
            }
            if (arithmetic.narrowArguments(argv)) {
                vm = uberspect.getMethod(left, "startsWith", argv);
                if (returnsBoolean(vm)) {
                    return (Boolean) vm.invoke(left, argv);
                }
            }
        } catch (final Exception e) {
            throw new JexlException(node, operator + " error", e);
        }
        // defaults to equal
        return arithmetic.equals(left, right);
    } catch (final ArithmeticException xrt) {
        throw new JexlException(node, operator + " error", xrt);
    }
}
Also used : JexlMethod(org.apache.commons.jexl3.introspection.JexlMethod) JexlException(org.apache.commons.jexl3.JexlException) JexlUberspect(org.apache.commons.jexl3.introspection.JexlUberspect) JexlArithmetic(org.apache.commons.jexl3.JexlArithmetic) JexlException(org.apache.commons.jexl3.JexlException)

Example 12 with JexlArithmetic

use of org.apache.commons.jexl2.JexlArithmetic in project commons-jexl by apache.

the class Asserter method assertExpression.

/**
 * Performs an assertion that the value of the given JEXL expression
 * evaluates to the given expected value.
 *
 * @param expression is the JEXL expression to evaluate
 * @param expected is the expected value of the expression
 * @throws Exception if the expression could not be evaluationed or an assertion
 * fails
 */
public void assertExpression(final String expression, final Object expected) throws Exception {
    final JexlScript exp = engine.createScript(expression);
    final Object value = exp.execute(context);
    if (expected instanceof BigDecimal) {
        final JexlArithmetic jexla = engine.getArithmetic();
        Assert.assertEquals("expression: " + expression, 0, ((BigDecimal) expected).compareTo(jexla.toBigDecimal(value)));
    }
    if (expected != null && value != null) {
        if (expected.getClass().isArray() && value.getClass().isArray()) {
            final int esz = Array.getLength(expected);
            final int vsz = Array.getLength(value);
            final String report = "expression: " + expression;
            Assert.assertEquals(report + ", array size", esz, vsz);
            for (int i = 0; i < vsz; ++i) {
                Assert.assertEquals(report + ", value@[]" + i, Array.get(expected, i), Array.get(value, i));
            }
        } else {
            Assert.assertEquals("expression: " + expression + ", " + expected.getClass().getSimpleName() + " ?= " + value.getClass().getSimpleName(), expected, value);
        }
    } else {
        Assert.assertEquals("expression: " + expression, expected, value);
    }
}
Also used : JexlScript(org.apache.commons.jexl3.JexlScript) JexlArithmetic(org.apache.commons.jexl3.JexlArithmetic) BigDecimal(java.math.BigDecimal)

Example 13 with JexlArithmetic

use of org.apache.commons.jexl2.JexlArithmetic in project datawave by NationalSecurityAgency.

the class EventErrorSummary method matches.

/**
 * Determine whether this event error summary matches a set of criteria
 *
 * @param jobName
 * @param dataType
 * @param uid
 * @param specifiedUUIDs
 * @param errorType
 * @param dateRange
 * @param jexlQuery
 * @return true if matches, false otherwise
 */
public boolean matches(String jobName, String dataType, String uid, Set<String> specifiedUUIDs, String errorType, Date[] dateRange, String jexlQuery, int maxProcessCount) {
    boolean matches = true;
    if (maxProcessCount > 0 && processedCount > maxProcessCount) {
        matches = false;
    } else if (jobName != null && !jobName.equals(this.jobName)) {
        matches = false;
    } else if (dataType != null && !dataType.equals(this.datatype)) {
        matches = false;
    } else if (uid != null && !uid.equals(this.uid)) {
        matches = false;
    } else if ((!specifiedUUIDs.isEmpty()) && !uuidMatchFound(specifiedUUIDs, uuids)) {
        matches = false;
    } else if (errorType != null && !matchesError(errorType)) {
        matches = false;
    } else if (dateRange != null) {
        if (dateRange[0] != null && errorDate.before(dateRange[0])) {
            matches = false;
        } else if (dateRange[1] != null && errorDate.after(dateRange[1])) {
            matches = false;
        }
    }
    if (matches && jexlQuery != null) {
        // Get a JexlEngine initialized with the correct JexlArithmetic for this Document
        JexlEngine engine = new JexlEngine();
        // Evaluate the JexlContext against the Script
        Script script = engine.createScript(jexlQuery);
        Object o = script.execute(this);
        // Jexl might return us a null depending on the AST
        if (o != null && Boolean.class.isAssignableFrom(o.getClass())) {
            matches = ((Boolean) o);
        } else if (o != null && Collection.class.isAssignableFrom(o.getClass())) {
            // if the function returns a collection of matches, return true/false
            // based on the number of matches
            matches = (!((Collection<?>) o).isEmpty());
        } else {
            matches = false;
        }
    }
    return matches;
}
Also used : Script(org.apache.commons.jexl2.Script) JexlEngine(org.apache.commons.jexl2.JexlEngine) Collection(java.util.Collection)

Aggregations

JexlArithmetic (org.apache.commons.jexl3.JexlArithmetic)11 JexlException (org.apache.commons.jexl3.JexlException)8 JexlMethod (org.apache.commons.jexl3.introspection.JexlMethod)8 JexlUberspect (org.apache.commons.jexl3.introspection.JexlUberspect)5 JexlOperator (org.apache.commons.jexl3.JexlOperator)2 JexlEvaluation (datawave.query.function.JexlEvaluation)1 EvaluationTrackingNestedIterator (datawave.query.iterator.profile.EvaluationTrackingNestedIterator)1 DefaultArithmetic (datawave.query.jexl.DefaultArithmetic)1 StatefulArithmetic (datawave.query.jexl.StatefulArithmetic)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 Method (java.lang.reflect.Method)1 BigDecimal (java.math.BigDecimal)1 MalformedURLException (java.net.MalformedURLException)1 Collection (java.util.Collection)1 Key (org.apache.accumulo.core.data.Key)1 Range (org.apache.accumulo.core.data.Range)1 IterationInterruptedException (org.apache.accumulo.core.iterators.IterationInterruptedException)1 TabletClosedException (org.apache.accumulo.tserver.tablet.TabletClosedException)1