Search in sources :

Example 21 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project shifu by ShifuML.

the class JexlTest method testJavaExpressionNotNull.

@Test
public void testJavaExpressionNotNull() {
    JexlEngine jexl = new JexlEngine();
    String jexlExp = "bad_num != \"NULL\"";
    Expression e = jexl.createExpression(jexlExp);
    JexlContext jc = new MapContext();
    jc.set("bad_num", "2");
    // Now evaluate the expression, getting the result
    Boolean isEqual = (Boolean) e.evaluate(jc);
    Assert.assertTrue(isEqual);
    jc = new MapContext();
    jc.set("bad_num", "NULL");
    // Now evaluate the expression, getting the result
    isEqual = (Boolean) e.evaluate(jc);
    Assert.assertFalse(isEqual);
}
Also used : JexlEngine(org.apache.commons.jexl2.JexlEngine) Expression(org.apache.commons.jexl2.Expression) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext) Test(org.testng.annotations.Test)

Example 22 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project shifu by ShifuML.

the class DataNormalizeWorker method normalizeRecord.

/**
 * Normalize the training data record
 *
 * @param rfs
 *            - record fields
 * @return the data after normalization
 */
private List<Double> normalizeRecord(String[] rfs) {
    List<Double> retDouList = new ArrayList<Double>();
    if (rfs == null || rfs.length == 0) {
        return null;
    }
    String tag = CommonUtils.trimTag(rfs[this.targetColumnNum]);
    boolean isNotSampled = DataSampler.isNotSampled(modelConfig.getPosTags(), modelConfig.getNegTags(), modelConfig.getNormalizeSampleRate(), modelConfig.isNormalizeSampleNegOnly(), tag);
    if (isNotSampled) {
        return null;
    }
    JexlContext jc = new MapContext();
    Double cutoff = modelConfig.getNormalizeStdDevCutOff();
    for (int i = 0; i < rfs.length; i++) {
        ColumnConfig config = columnConfigList.get(i);
        if (weightExpr != null) {
            jc.set(config.getColumnName(), rfs[i]);
        }
        if (this.targetColumnNum == i) {
            if (modelConfig.getPosTags().contains(tag)) {
                retDouList.add(Double.valueOf(1));
            } else if (modelConfig.getNegTags().contains(tag)) {
                retDouList.add(Double.valueOf(0));
            } else {
                log.error("Invalid data! The target value is not listed - " + tag);
                // Return null to skip such record.
                return null;
            }
        } else if (!CommonUtils.isGoodCandidate(config, super.hasCandidates)) {
            retDouList.add(null);
        } else {
            String val = (rfs[i] == null) ? "" : rfs[i];
            retDouList.addAll(Normalizer.normalize(config, val, cutoff, modelConfig.getNormalizeType()));
        }
    }
    double weight = 1.0d;
    if (weightExpr != null) {
        Object result = weightExpr.evaluate(jc);
        if (result instanceof Integer) {
            weight = ((Integer) result).doubleValue();
        } else if (result instanceof Double) {
            weight = ((Double) result).doubleValue();
        } else if (result instanceof String) {
            // add to parse String data
            try {
                weight = Double.parseDouble((String) result);
            } catch (NumberFormatException e) {
                // Not a number, use default
                if (System.currentTimeMillis() % 100 == 0) {
                    log.warn("Weight column type is String and value cannot be parsed with {}, use default 1.0d.", result);
                }
                weight = 1.0d;
            }
        }
    }
    retDouList.add(weight);
    return retDouList;
}
Also used : ColumnConfig(ml.shifu.shifu.container.obj.ColumnConfig) ArrayList(java.util.ArrayList) MapContext(org.apache.commons.jexl2.MapContext) JexlContext(org.apache.commons.jexl2.JexlContext)

Example 23 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project jmxtrans by jmxtrans.

the class JexlNamingStrategy method formatName.

/**
 * Format the name for the given result.
 *
 * @param result - the result of a JMX query.
 * @return String - the formatted string resulting from the expression, or null if the formatting fails.
 */
@Override
public String formatName(Result result) {
    String formatted;
    JexlContext context = new MapContext();
    this.populateContext(context, result);
    try {
        formatted = (String) this.parsedExpr.evaluate(context);
    } catch (JexlException jexlExc) {
        LOG.error("error applying JEXL expression to query results", jexlExc);
        formatted = null;
    }
    return formatted;
}
Also used : JexlException(org.apache.commons.jexl2.JexlException) JexlContext(org.apache.commons.jexl2.JexlContext) MapContext(org.apache.commons.jexl2.MapContext)

Example 24 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project nutch by apache.

the class JexlIndexingFilter method filter.

@Override
public NutchDocument filter(NutchDocument doc, Parse parse, Text url, CrawlDatum datum, Inlinks inlinks) throws IndexingException {
    // Create a context and add data
    JexlContext jcontext = new MapContext();
    jcontext.set("status", CrawlDatum.getStatusName(datum.getStatus()));
    jcontext.set("fetchTime", (long) (datum.getFetchTime()));
    jcontext.set("modifiedTime", (long) (datum.getModifiedTime()));
    jcontext.set("retries", datum.getRetriesSinceFetch());
    jcontext.set("interval", Integer.valueOf(datum.getFetchInterval()));
    jcontext.set("score", datum.getScore());
    jcontext.set("signature", StringUtil.toHexString(datum.getSignature()));
    jcontext.set("url", url.toString());
    jcontext.set("text", parse.getText());
    jcontext.set("title", parse.getData().getTitle());
    JexlContext httpStatusContext = new MapContext();
    httpStatusContext.set("majorCode", parse.getData().getStatus().getMajorCode());
    httpStatusContext.set("minorCode", parse.getData().getStatus().getMinorCode());
    httpStatusContext.set("message", parse.getData().getStatus().getMessage());
    jcontext.set("httpStatus", httpStatusContext);
    jcontext.set("documentMeta", metadataToContext(doc.getDocumentMeta()));
    jcontext.set("contentMeta", metadataToContext(parse.getData().getContentMeta()));
    jcontext.set("parseMeta", metadataToContext(parse.getData().getParseMeta()));
    JexlContext context = new MapContext();
    for (Entry<String, NutchField> entry : doc) {
        List<Object> values = entry.getValue().getValues();
        context.set(entry.getKey(), values.size() > 1 ? values : values.get(0));
    }
    jcontext.set("doc", context);
    try {
        if (Boolean.TRUE.equals(expr.execute(jcontext))) {
            return doc;
        }
    } catch (Exception e) {
        LOG.warn("Failed evaluating JEXL {}", expr.getSourceText(), e);
    }
    return null;
}
Also used : NutchField(org.apache.nutch.indexer.NutchField) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext) IndexingException(org.apache.nutch.indexer.IndexingException)

Example 25 with JexlContext

use of org.apache.commons.jexl2.JexlContext 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)

Aggregations

JexlContext (org.apache.commons.jexl2.JexlContext)31 Expression (org.apache.commons.jexl2.Expression)25 MapContext (org.apache.commons.jexl2.MapContext)23 JexlContext (org.apache.commons.jexl3.JexlContext)21 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 MapContext (org.apache.commons.jexl3.MapContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)5 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 JexlException (org.apache.commons.jexl2.JexlException)3 ObjectContext (org.apache.commons.jexl2.ObjectContext)3 IOException (java.io.IOException)2 ReadonlyContext (org.apache.commons.jexl2.ReadonlyContext)2 JexlScript (org.apache.commons.jexl3.JexlScript)2 NumberUtils (org.apache.commons.lang.math.NumberUtils)2 FloatWritable (org.apache.hadoop.io.FloatWritable)2 IntWritable (org.apache.hadoop.io.IntWritable)2 Text (org.apache.hadoop.io.Text)2 VersionMismatchException (org.apache.hadoop.io.VersionMismatchException)2