org.apache.commons.jexl2.MapContext类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(299)

本文整理了Java中org.apache.commons.jexl2.MapContext类的一些代码示例,展示了MapContext类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MapContext类的具体详情如下:
包路径:org.apache.commons.jexl2.MapContext
类名称:MapContext

MapContext介绍

[英]Wraps a map in a context.

Each entry in the map is considered a variable name, value pair.
[中]在上下文中包装地图。
映射中的每个条目都被视为变量名、值对。

代码示例

代码示例来源:origin: azkaban/azkaban

Object result = null;
try {
 final Expression e = jexl.createExpression(innerExpression);
 result = e.evaluate(new MapContext());
} catch (final JexlException e) {
 throw new IllegalArgumentException("Expression " + value

代码示例来源:origin: traccar/traccar

private MapContext prepareContext(Position position) {
  MapContext result = new MapContext();
  if (mapDeviceAttributes) {
    Device device = Context.getIdentityManager().getById(position.getDeviceId());
    if (device != null) {
      for (Object key : device.getAttributes().keySet()) {
        result.set((String) key, device.getAttributes().get(key));
      }
    }
  }
  Set<Method> methods = new HashSet<>(Arrays.asList(position.getClass().getMethods()));
  methods.removeAll(Arrays.asList(Object.class.getMethods()));
  for (Method method : methods) {
    if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
      String name = Character.toLowerCase(method.getName().charAt(3)) + method.getName().substring(4);
      try {
        if (!method.getReturnType().equals(Map.class)) {
          result.set(name, method.invoke(position));
        } else {
          for (Object key : ((Map) method.invoke(position)).keySet()) {
            result.set((String) key, ((Map) method.invoke(position)).get(key));
          }
        }
      } catch (IllegalAccessException | InvocationTargetException error) {
        LOGGER.warn("Attribute reflection error", error);
      }
    }
  }
  return result;
}

代码示例来源:origin: stackoverflow.com

JexlEngine jexl = new JexlEngine();
 Expression expression = jexl.createExpression("value 1 = #value1 value 2 = #value2");
 int[] values = {1, 2};
 JexlContext context = new MapContext();
 for (int i = 0; i <  values.length; i++) {
   context.set("#value" + (i + 1), values[i]);
 }
 String result = (String)expression.evaluate(context);

代码示例来源:origin: stackoverflow.com

JexlEngine jexl = new JexlEngine();
 jexl.setSilent(true);
 jexl.setLenient(true);
 Expression expression = jexl.createExpression("(a || b && (c && d))");
 JexlContext jexlContext = new MapContext();
 //b and c and d should pass
 jexlContext.set("b",true);
 jexlContext.set("c",true);
 jexlContext.set("d",true);
 assertTrue((Boolean)expression.evaluate(jexlContext));
 jexlContext = new MapContext();
 //b and c and NOT d should be false
 jexlContext.set("b",true);
 jexlContext.set("c",true);
 //note this works without setting d to false on the context
 //because null evaluates to false
 assertFalse((Boolean)expression.evaluate(jexlContext));

代码示例来源:origin: org.opennms.features.measurements/org.opennms.features.measurements.impl

expressions.put(e.getLabel(), jexl.createExpression(e.getExpression()));
  } catch (JexlException ex) {
    throw new ExpressionException(ex, "Failed to parse expression label '{}'.", e.getLabel());
final JexlContext context = new MapContext(jexlValues);
jexl.getFunctions().put("jexl", jexlEvaluateFunctions);
      Object derived = expressionEntry.getValue().evaluate(context);
      double derivedAsDouble = Utils.toDouble(derived);

代码示例来源:origin: OpenNMS/newts

@Override
  public double apply(double... ds) {
    JexlContext jc = new MapContext();
    for(int i = 0; i < labels.length; i++) {
      jc.set(labels[i], ds[i]);
    }
    return ((Number)expr.evaluate(jc)).doubleValue();
    
  }
};

代码示例来源:origin: jmxtrans/jmxtrans

/**
 * 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;
}

代码示例来源:origin: stackoverflow.com

// Create or retrieve a JexlEngine
 JexlEngine jexl = new JexlEngine();
 // Create an expression object
 String valfromtextbox = "2(2*2)+5";
 Expression e = jexl.createExpression( valfromtextbox );
 // Create a context and add data
 JexlContext jc = new MapContext();
 // Now evaluate the expression, getting the result
 Object o = e.evaluate(jc);

代码示例来源:origin: stackoverflow.com

// Assuming we have a JexlEngine instance initialized in our class named 'jexl':
 // Create an expression object for our calculation
 String calculateTax = "((G1 + G2 + G3) * 0.1) + G4";
 Expression e = jexl.createExpression( calculateTax );
 // populate the context
 JexlContext context = new MapContext();
 context.set("G1", businessObject.getTotalSales());
 context.set("G2", taxManager.getTaxCredit(businessObject.getYear()));
 context.set("G3", businessObject.getIntercompanyPayments());
 context.set("G4", -taxManager.getAllowances());
 // ...
 // work it out
 Float result = (Float)e.evaluate(context);

代码示例来源:origin: org.apache.commons/commons-configuration2

/**
 * Creates a new {@code JexlContext} and initializes it with the variables
 * managed by this Lookup object.
 *
 * @return the newly created context
 */
private JexlContext createContext()
{
  final JexlContext ctx = new MapContext();
  initializeContext(ctx);
  return ctx;
}

代码示例来源:origin: stackoverflow.com

JexlEngine jexl = new JexlEngine();
Expression func = jexl.createExpression("x1*x2-x3");
MapContext mc = new MapContext();
mc.set("x1", 5);
mc.set("x2", 3);
mc.set("x3", 2);
System.out.println(func.evaluate(mc));

代码示例来源:origin: gooddata/GoodData-CL

int idx = 0;
String key = "";
JexlContext jc = new MapContext();
for (int i = 0; i < columns.size(); i++) {
  SourceColumn c = columns.get(i);
        key += row[idx] + "|";
      jc.set(c.getName(), (row[idx] != null) ? (row[idx]) : (""));
      idx++;
    } else {
  jc.set("IDENTITY", identity);
jc.set("GdcDateArithmetics", da);
      Object result = expressions[i].evaluate(jc);
      String value = (result != null) ? (result.toString()) : ("");
      nrow.add(value);

代码示例来源:origin: stackoverflow.com

List<Shapes> allShapes = shapesDao.getAllShapes();
   for (Shapes shapeobj : allShapes)
   {
     if(shapeobj.getShapeId() == id)
     {
       formula = shapeobj.getShapeFormula();
     }
   }
   final JexlEngine jexl = new 
 JexlBuilder().cache(512).strict(true).silent(false).create();
     String jexlExp = formula;
     JexlExpression  e = jexl.createExpression(formula);
     JexlContext context = new MapContext();
     for(int i = 0; i<params; i++)
     {
       for(int j=0;j<completed.length;j++)
       {
         System.out.println("-------------------------");
         System.out.println(completed[j]);
         System.out.println("-------------------------");
         context.set(completed[j], request.getParameter(completed[j]));
       }
     }
     Object o = e.evaluate(context);

代码示例来源:origin: stackoverflow.com

JexlEngine jexl = new JexlEngine(null, new NoStringCoercionArithmetic(), null, null);
jexl.setLenient(false);
jexl.setStrict(true); 
JexlContext jc = new MapContext();
Expression exp = jexl.createExpression("\"1\"+\"1\"");
System.out.println(exp.evaluate(jc)); // expected result "11"

代码示例来源:origin: org.apache.xbean/xbean-blueprint

public JexlExpressionParser(final Map<String, Object> vars) {
  if (vars == null) {
    throw new IllegalArgumentException("vars");
  }
  engine = new JexlEngine();
  jexl = new UnifiedJEXL(engine);
  context = new MapContext(vars);
  log.trace("Using variables: {}", vars);
}

代码示例来源:origin: stackoverflow.com

public class Formatter {
  public static String format(String format, Object ... inputs) {
    JexlContext context = new MapContext();
    for (int i=0;i<inputs.length;i++) {
      context.set("_" + (i+1), inputs[i] );
    }
    JexlEngine jexl = new JexlEngine();
    UnifiedJEXL ujexl = new UnifiedJEXL(jexl);
    UnifiedJEXL.Expression expr = ujexl.parse(format);
    return expr.evaluate(context).toString();
  }
}

代码示例来源:origin: stackoverflow.com

System.out.println("java.security.policy=" + System.getProperty("java.security.policy"));
System.setSecurityManager(new SecurityManager());
try {
  Permissions perms = new Permissions();
  perms.add(new RuntimePermission("accessDeclaredMembers"));
  ProtectionDomain domain = new ProtectionDomain(new CodeSource( null, (Certificate[]) null ), perms );
  AccessControlContext restrictedAccessControlContext = new AccessControlContext(new ProtectionDomain[] { domain } );

  JexlEngine jexlEngine = new JexlEngine();
  final Script finalExpression = jexlEngine.createScript(
      "i = 0; intClazz = i.class; "
      + "clazz = intClazz.forName(\"java.lang.System\"); "
      + "m = clazz.methods; m[0].invoke(null, 1); c");

  AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
    @Override
    public Object run() throws Exception {
      return finalExpression.execute(new MapContext());
    }
  }, restrictedAccessControlContext);
}
catch (Throwable ex) {
  ex.printStackTrace();
}

代码示例来源:origin: org.apache.jmeter/ApacheJMeter_functions

JexlContext jc = new MapContext();
jc.set("log", log); //$NON-NLS-1$
jc.set("ctx", jmctx); //$NON-NLS-1$
jc.set("vars", vars); //$NON-NLS-1$
jc.set("props", JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
Script e = getJexlEngine().createScript( exp );
Object o = e.execute(jc);
if (o != null)

代码示例来源:origin: neuland/jade4j

public Object evaluateExpression(String expression, JadeModel model) throws ExpressionException {
  try {
    expression = removeVar(expression);
    if (isplusplus.matcher(expression).find()) {
      expression = convertPlusPlusExpression(expression);
    }
    if (isminusminus.matcher(expression).find()) {
      expression = convertMinusMinusExpression(expression);
    }
    Script e = jexl.createScript(expression);
    Object evaluate = e.execute(new MapContext(model));
    return evaluate;
  } catch (Exception e) {
    throw new ExpressionException(expression, e);
  }
}

代码示例来源:origin: azkaban/azkaban

private void setCheckers(final Map<String, ConditionChecker> checkers) {
 this.checkers = checkers;
 for (final ConditionChecker checker : checkers.values()) {
  this.context.set(checker.getId(), checker);
 }
 updateNextCheckTime();
}

相关文章

微信公众号

最新文章

更多