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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations