本文整理汇总了Java中org.apache.commons.jexl2.MapContext类的典型用法代码示例。如果您正苦于以下问题:Java MapContext类的具体用法?Java MapContext怎么用?Java MapContext使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MapContext类属于org.apache.commons.jexl2包,在下文中一共展示了MapContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: matches
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
private boolean matches(Key k) {
if (log.isDebugEnabled()) {
log.debug("You've reached the match function!");
}
JexlContext ctx = new MapContext();
// Add the field value from the key to the context
// String fieldValue = k.getColumnQualifier().toString().split("\0")[0];
// String fieldValue = getFieldValueFromKey(k);
keyParser.parse(k);
String fieldValue = keyParser.getFieldValue();
ctx.set(fNameString, fieldValue);
Object o = expr.evaluate(ctx);
if (o instanceof Boolean && (((Boolean) o) == true)) {
if (log.isDebugEnabled()) {
log.debug("matches:: fName: " + fName + " , fValue: " + fieldValue + " , operator: " + fOperator + " , key: " + k);
}
return true;
} else {
if (log.isDebugEnabled()) {
log.debug("NO MATCH:: fName: " + fName + " , fValue: " + fieldValue + " , operator: " + fOperator + " , key: " + k);
}
return false;
}
}
示例2: Variables
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
/**
* Get the single instance of the variables class
*
* @return The variable class instance
*
* static public Variables getInstance(){ if (Variables.instance == null) Variables.instance = new
* Variables(); return Variables.instance; }
*
* /** Constructor
*/
public Variables() {
this.variables = new HashMap<String, VariableValue>();
try {
doLog = new TankConfig().getAgentConfig().getLogVariables();
} catch (Exception e) {
// eat this one
}
context = new MapContext();
new JexlStringFunctions().visit(context);
new JexlIOFunctions().visit(context);
new JexlDateFunctions().visit(context);
new JexlMonetaryFunctions().visit(context);
new JexlNumericFunctions().visit(context);
new JexlTaxFunctions().visit(context);
// get from config??
}
示例3: addAttrsToContext
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
public JexlContext addAttrsToContext(
final Collection<? extends AbstractAttr> attributes,
final JexlContext jexlContext) {
JexlContext context = jexlContext == null
? new MapContext() : jexlContext;
for (AbstractAttr attribute : attributes) {
List<String> attributeValues = attribute.getValuesAsStrings();
String expressionValue = attributeValues.isEmpty()
? "" : attributeValues.get(0);
LOG.debug("Add attribute {} with value {}",
new Object[]{attribute.getSchema().getName(),
expressionValue});
context.set(attribute.getSchema().getName(), expressionValue);
}
return context;
}
示例4: addDerAttrsToContext
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
public JexlContext addDerAttrsToContext(
final Collection<? extends AbstractDerAttr> derAttributes,
final Collection<? extends AbstractAttr> attributes,
final JexlContext jexlContext) {
JexlContext context = jexlContext == null
? new MapContext() : jexlContext;
for (AbstractDerAttr attribute : derAttributes) {
String expressionValue = attribute.getValue(attributes);
if (expressionValue == null) {
expressionValue = "";
}
LOG.debug("Add derived attribute {} with value {}",
new Object[]{attribute.getDerivedSchema().getName(),
expressionValue});
context.set(
attribute.getDerivedSchema().getName(), expressionValue);
}
return context;
}
示例5: isAboveThreshold
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
/**
*
*/
@Override
public boolean isAboveThreshold(MetricTimeSeries timeSeries)
{
Set<Long> timeWindowSet = timeSeries.getTimeWindowSet();
JexlEngine jexl = new JexlEngine();
JexlContext context = new MapContext();
Expression e = jexl.createExpression(this.thresholdExpr);
for(String metricName : metricNames){
long sum = 0;
for (Long timeWindow : timeWindowSet) {
sum += timeSeries.get(timeWindow, metricName).longValue();
}
context.set(metricName, sum);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(metricName + " = " + sum);
}
}
return ((Boolean)e.evaluate(context)).booleanValue();
}
示例6: testPower
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
@Test
public void testPower() {
JexlEngine engine = ExpressionEvalFactory.getInstance();
Expression expr = engine.createExpression("X^2");
JexlContext context = new MapContext();
// Don't need this really
context.set("A", 20);
context.set("B", 0);
ArrayList<Double> actualList = new ArrayList<>();
for (int i = -20; i < 20; i++) {
context.set("X", i);
Double result = (Double) expr.evaluate(context);
actualList.add(result);
System.out.println(result + ",");
}
Double[] actual = actualList.toArray(new Double[0]);
Double[] expected = {400.0, 361.0, 324.0, 289.0, 256.0, 225.0, 196.0,
169.0, 144.0, 121.0, 100.0, 81.0, 64.0, 49.0, 36.0, 25.0, 16.0,
9.0, 4.0, 1.0, 0.0, 1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0,
81.0, 100.0, 121.0, 144.0, 169.0, 196.0, 225.0, 256.0, 289.0,
324.0, 361.0};
Assert.assertArrayEquals("results wrong", expected, actual);
}
示例7: evaluate
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Override
public Object evaluate(final String expression, final Map<String, ?> values) {
ArgUtils.notEmpty(expression, "expression");
ArgUtils.notNull(values, "values");
if(logger.isDebugEnabled()) {
logger.debug("Evaluating JEXL expression: {}", expression);
}
try {
Expression expr = expressionCache.get(expression);
if (expr == null) {
expr = jexlEngine.createExpression(expression);
expressionCache.put(expression, expr);
}
return expr.evaluate(new MapContext((Map<String, Object>) values));
} catch(Exception ex) {
throw new ExpressionEvaluationException(String.format("Evaluating [%s] script with JEXL failed.", expression), ex);
}
}
示例8: evaluate
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
@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);
context.put("datasources", new HashMap<String, Double>(values)); // To workaround NMS-5019
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;
}
示例9: evaluate
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
@Override
public Object evaluate(final String expression, final Map<String, Object> values) {
Objects.requireNonNull(expression, "expression shoud not be null.");
Objects.requireNonNull(values, "values shoud not be null.");
if(logger.isDebugEnabled()) {
logger.debug("Evaluating JEXL expression: {}", expression);
}
try {
Expression expr = expressionCache.get(expression);
if (expr == null) {
expr = jexlEngine.createExpression(expression);
expressionCache.put(expression, expr);
}
return expr.evaluate(new MapContext((Map<String, Object>) values));
} catch(Exception ex) {
throw new ExpressionEvaluationException(String.format("Evaluating [%s] script with JEXL failed.", expression), ex,
expression, values);
}
}
示例10: _resolveAllFromSameSentence
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
private static void _resolveAllFromSameSentence(Document d, String filter, String comment) {
JexlEngine jexl = new JexlEngine();
jexl.setSilent(JEXL_SILENT);
jexl.setLenient(JEXL_LENIENT);
jexl.setDebug(JEXL_DBG);
Expression expression = jexl.createExpression(filter);
JexlContext jexlContext = new MapContext();
jexlContext.set("Filter", Filter.class);
for (Mention s: d.mentions) {
jexlContext.set("s", s);
List<Mention> tt = MentionBrowser.getAllFromSameSentence(s, expression, jexlContext);
for (Mention t : tt) {
_resolve(d, s, t, comment);
}
}
}
示例11: matches
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
private boolean matches(Key k) {
if (log.isDebugEnabled()) {
log.debug("You've reached the match function!");
}
JexlContext ctx = new MapContext();
// Add the field value from the key to the context
// String fieldValue = k.getColumnQualifier().toString().split("\0")[0];
// String fieldValue = getFieldValueFromKey(k);
keyParser.parse(k);
String fieldValue = keyParser.getFieldValue();
ctx.set(fNameString, fieldValue);
Object o = expr.evaluate(ctx);
if (o instanceof Boolean && (((Boolean) o) == true)) {
if (log.isDebugEnabled()) {
log.debug("matches:: fName: " + fName + " , fValue: " + fieldValue + " , operator: " + fOperator + " , key: " + k);
}
return true;
} else {
if (log.isDebugEnabled()) {
log.debug("NO MATCH:: fName: " + fName + " , fValue: " + fieldValue + " , operator: " + fOperator + " , key: " + k);
}
return false;
}
}
示例12: checkFunctions
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
/**
* TODO FIXME Must be better way than this...
* @param expr
* @throws Exception
*/
private void checkFunctions(String expr) throws Exception {
// We do not support the . operator for now because
// otherwise http://jira.diamond.ac.uk/browse/SCI-1731
//if (expr.indexOf('.')>-1) {
// throw new Exception("The dot operator '.' is not supported.");
//}
// We now evaluate the expression to try and trap invalid functions.
try {
final Script script = jexl.createScript(expr);
Set<List<String>> names = script.getVariables();
Collection<String> vars = unpack(names);
final Map<String,Object> dummy = new HashMap<String,Object>(vars.size());
for (String name : vars) dummy.put(name, 1);
MapContext dCnxt = new MapContext(dummy);
expression.evaluate(dCnxt);
} catch (JexlException ne) {
if (ne.getMessage().toLowerCase().contains("no such function namespace")) {
final String msg = ne.toString();
final String[] segs = msg.split(":");
throw new Exception(segs[3], ne);
}
} catch (Exception ignored) {
// We allow the expression but it might fail later
}
}
示例13: evaluate
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public Object evaluate(final String expression, final Map<String, ?> values) throws ExpressionEvaluationException {
LOG.debug("Evaluating JEXL expression: {1}", expression);
try {
Expression expr = expressionCache.get(expression);
if (expr == null) {
expr = jexl.createExpression(expression);
expressionCache.put(expression, expr);
}
return expr.evaluate(new MapContext((Map<String, Object>) values));
} catch (final Exception ex) {
throw new ExpressionEvaluationException("Evaluating JEXL expression failed: " + expression, ex);
}
}
示例14: evaluateMandatoryCondition
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
private boolean evaluateMandatoryCondition(
final String mandatoryCondition,
final List<? extends AbstractAttr> attributes) {
JexlContext jexlContext = new MapContext();
jexlUtil.addAttrsToContext(attributes, jexlContext);
return Boolean.parseBoolean(
jexlUtil.evaluate(
mandatoryCondition, jexlContext));
}
示例15: populateJexlContext
import org.apache.commons.jexl2.MapContext; //导入依赖的package包/类
@Override
protected ReadonlyContext populateJexlContext(String model, StringContext context) {
JexlContext jexlContext = new MapContext();
jexlContext.set("text", model);
return new ReadonlyContext(jexlContext);
}