use of org.apache.commons.jexl3.MapContext 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.jexl3.MapContext 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.jexl3.MapContext in project waltz by khartec.
the class PredicateEvaluatorTest method evaluate.
@Test
public void evaluate() {
ImmutableAssessmentContextValue assessmentValue = ImmutableAssessmentContextValue.builder().ratingCode("A").ratingName("aRating").ratingExternalId("A_RATING").ratingComment("a rating description").build();
ImmutableSurveyQuestionResponseContextValue surveyValue = ImmutableSurveyQuestionResponseContextValue.builder().value("yes").comment("a survey comment").build();
ImmutableContextVariable<Object> assessmentVar = ImmutableContextVariable.builder().name("assessmentVar").value(assessmentValue).entityRef(EntityReference.mkRef(EntityKind.APPLICATION, 1L)).build();
ImmutableContextVariable<Object> surveyVar = ImmutableContextVariable.builder().name("surveyVar").value(surveyValue).entityRef(EntityReference.mkRef(EntityKind.APPLICATION, 1L)).build();
Set<ContextVariable<?>> contextVariables = asSet(assessmentVar, surveyVar);
JexlBuilder builder = new JexlBuilder();
JexlEngine jexl = builder.create();
JexlExpression pred1Expr = jexl.createExpression("assessmentVar.ratingCode == 'A'");
JexlExpression pred2Expr = jexl.createExpression("assessmentVar.ratingCode == 'C' || surveyVar.value == 'yes'");
JexlExpression pred3Expr = jexl.createExpression("assessmentVar.ratingCode == 'C'");
JexlExpression invalidExpr = jexl.createExpression("foo?.bar");
MapContext ctx = createContext(contextVariables);
assertTrue(evaluatePredicate(pred1Expr, ctx), "pred1Expr should evaluate true, rating equals A");
assertTrue(evaluatePredicate(pred2Expr, ctx), "pred2Expr should evaluate true, survey value equals 'yes");
assertFalse(evaluatePredicate(pred3Expr, ctx), "pred3Expr should evaluate false, rating equals A");
assertFalse(evaluatePredicate(invalidExpr, ctx), "Invalid expressions should return false");
Object evaluate = invalidExpr.evaluate(ctx);
System.out.println(evaluate);
}
use of org.apache.commons.jexl3.MapContext in project waltz by khartec.
the class SideEffectTest method canMakeAssessmentRefs.
@Test
public void canMakeAssessmentRefs() {
JexlBuilder builder = new JexlBuilder();
TestSideEffectNamespace testNamespace = new TestSideEffectNamespace();
JexlEngine jexl = builder.namespaces(newHashMap("assessment", new AssessmentSideEffectNamespace(), "test", testNamespace)).create();
MapContext ctx = new MapContext();
String messageString = "I have updated something!";
ctx.set("message", messageString);
JexlExpression sideEffectExpression = jexl.createExpression("assessment:update('ARCH_REVIEW', 'YES')");
JexlExpression messageExpression = jexl.createExpression("test:sendMessage(message)");
messageExpression.evaluate(ctx);
assertEquals(messageString, testNamespace.lastMessage, "Should be able to use context as part of expression");
assertEquals(AssessmentSideEffectNamespace.update("ARCH_REVIEW", "YES"), sideEffectExpression.evaluate(ctx), "Should be able to parse survey templateExtId and ratingExtId from update()");
}
use of org.apache.commons.jexl3.MapContext 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;
}
Aggregations