org.codehaus.janino.ExpressionEvaluator Java Examples

The following examples show how to use org.codehaus.janino.ExpressionEvaluator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TaxiQuerySolution.java    From flink-training-exercises with Apache License 2.0 6 votes vote down vote up
@Override
public void processBroadcastElement(String query,
									Context ctx,
									Collector<Tuple2<String, String>> out) throws Exception {

	out.collect(new Tuple2<>("QUERY", query));

	ExpressionEvaluator ee = cookBooleanExpression(query);
	ctx.getBroadcastState(queryDescriptor).put(QUERY_KEY, ee);

	ctx.applyToKeyedState(rideDescriptor, new KeyedStateFunction<Long, ValueState<TaxiRide>>() {
		@Override
		public void process(Long taxiId, ValueState<TaxiRide> taxiState) throws Exception {
			TaxiRide ride = taxiState.value();

			if (evaluateBooleanExpression(ee, ride, ctx.currentWatermark())) {
				out.collect(new Tuple2<>("PBE@" + timeFormatter.print(ctx.currentWatermark()), ride.toString()));
			}
		}
	});
}
 
Example #2
Source File: TestClassCompilationTypes.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private int janino() throws Exception{
  // Compile the expression once; relatively slow.
  org.codehaus.janino.ExpressionEvaluator ee = new org.codehaus.janino.ExpressionEvaluator("c > d ? c : d", // expression
      int.class, // expressionType
      new String[] { "c", "d" }, // parameterNames
      new Class[] { int.class, int.class } // parameterTypes
  );

  // Evaluate it with varying parameter values; very fast.
  return (Integer) ee.evaluate(new Object[] { // parameterValues
      new Integer(10), new Integer(11), });
}
 
Example #3
Source File: TestClassCompilationTypes.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
private int jdk() throws Exception{
  // Compile the expression once; relatively slow.
  ExpressionEvaluator ee = new ExpressionEvaluator("c > d ? c : d", // expression
      int.class, // expressionType
      new String[] { "c", "d" }, // parameterNames
      new Class[] { int.class, int.class } // parameterTypes
  );

  // Evaluate it with varying parameter values; very fast.
  return  (Integer) ee.evaluate(new Object[] { // parameterValues
      new Integer(10), new Integer(11), });
}
 
Example #4
Source File: TaxiQuerySolution.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
@Override
public void processElement(TaxiRide ride, ReadOnlyContext ctx, Collector<Tuple2<String, String>> out) throws Exception {

	// for every taxi, we want to store the most up-to-date information
	TaxiRide savedRide = taxiState.value();
	if (ride.compareTo(savedRide) > 0) {
		taxiState.update(ride);
	}

	ExpressionEvaluator ee = (ExpressionEvaluator) ctx.getBroadcastState(queryDescriptor).get(QUERY_KEY);
	if (evaluateBooleanExpression(ee, ride, ctx.currentWatermark())) {
		out.collect(new Tuple2<>("PE@" + timeFormatter.print(ctx.currentWatermark()), ride.toString()));
	}
}
 
Example #5
Source File: TaxiQuerySolution.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
private ExpressionEvaluator cookBooleanExpression(String expression) throws CompileException {
	ExpressionEvaluator ee = new ExpressionEvaluator();
	ee.setParameters(new String[] { "ride", "watermark" }, new Class[] { TaxiRide.class, long.class });
	ee.setExpressionType(boolean.class);
	ee.cook(expression);

	return ee;
}
 
Example #6
Source File: TaxiQuerySolution.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
private boolean evaluateBooleanExpression(ExpressionEvaluator ee, TaxiRide ride, long watermark) throws InvocationTargetException {
	boolean result= false;
	if (ee != null) {
		result = (boolean) ee.evaluate(new Object[] { ride, watermark });
	}
	return result;
}
 
Example #7
Source File: TaxiQueryExercise.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
private ExpressionEvaluator cookBooleanExpression(String expression) throws CompileException {
	ExpressionEvaluator ee = new ExpressionEvaluator();
	ee.setParameters(new String[] { "ride", "watermark" }, new Class[] { TaxiRide.class, long.class });
	ee.setExpressionType(boolean.class);
	ee.cook(expression);

	return ee;
}
 
Example #8
Source File: TaxiQueryExercise.java    From flink-training-exercises with Apache License 2.0 5 votes vote down vote up
private boolean evaluateBooleanExpression(ExpressionEvaluator ee, TaxiRide ride, long watermark) throws InvocationTargetException {
	boolean result= false;
	if (ee != null) {
		result = (boolean) ee.evaluate(new Object[] { ride, watermark });
	}
	return result;
}
 
Example #9
Source File: JavaFilter.java    From hop with Apache License 2.0 4 votes vote down vote up
private boolean calcFields( IRowMeta rowMeta, Object[] r ) throws HopValueException {
  try {
    // Initialize evaluators etc. Only do it once.
    //
    if ( data.expressionEvaluator == null ) {
      String realCondition = environmentSubstitute( meta.getCondition() );
      data.argumentIndexes = new ArrayList<Integer>();

      List<String> parameterNames = new ArrayList<>();
      List<Class<?>> parameterTypes = new ArrayList<Class<?>>();

      for ( int i = 0; i < data.outputRowMeta.size(); i++ ) {

        IValueMeta valueMeta = data.outputRowMeta.getValueMeta( i );

        // See if the value is being used in a formula...
        //
        if ( realCondition.contains( valueMeta.getName() ) ) {
          // If so, add it to the indexes...
          data.argumentIndexes.add( i );

          Class<?> parameterType;
          switch ( valueMeta.getType() ) {
            case IValueMeta.TYPE_STRING:
              parameterType = String.class;
              break;
            case IValueMeta.TYPE_NUMBER:
              parameterType = Double.class;
              break;
            case IValueMeta.TYPE_INTEGER:
              parameterType = Long.class;
              break;
            case IValueMeta.TYPE_DATE:
              parameterType = Date.class;
              break;
            case IValueMeta.TYPE_BIGNUMBER:
              parameterType = BigDecimal.class;
              break;
            case IValueMeta.TYPE_BOOLEAN:
              parameterType = Boolean.class;
              break;
            case IValueMeta.TYPE_BINARY:
              parameterType = byte[].class;
              break;
            default:
              parameterType = String.class;
              break;
          }
          parameterTypes.add( parameterType );
          parameterNames.add( valueMeta.getName() );
        }
      }

      // Create the expression evaluator: is relatively slow so we do it only for the first row...
      //
      data.expressionEvaluator = new ExpressionEvaluator();
      data.expressionEvaluator.setParameters(
        parameterNames.toArray( new String[ parameterNames.size() ] ), parameterTypes
          .toArray( new Class<?>[ parameterTypes.size() ] ) );
      data.expressionEvaluator.setReturnType( Object.class );
      data.expressionEvaluator.setThrownExceptions( new Class<?>[] { Exception.class } );
      data.expressionEvaluator.cook( realCondition );

      // Also create the argument data structure once...
      //
      data.argumentData = new Object[ data.argumentIndexes.size() ];
    }

    // This method can only accept the specified number of values...
    //
    for ( int x = 0; x < data.argumentIndexes.size(); x++ ) {
      int index = data.argumentIndexes.get( x );
      IValueMeta outputValueMeta = data.outputRowMeta.getValueMeta( index );
      data.argumentData[ x ] = outputValueMeta.convertToNormalStorageType( r[ index ] );
    }

    Object formulaResult = data.expressionEvaluator.evaluate( data.argumentData );

    if ( formulaResult instanceof Boolean ) {
      return (Boolean) formulaResult;
    } else {
      throw new HopException( "The result of the filter expression must be a boolean and we got back : "
        + formulaResult.getClass().getName() );
    }
  } catch ( Exception e ) {
    throw new HopValueException( e );
  }
}
 
Example #10
Source File: JavaFilter.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private boolean calcFields( RowMetaInterface rowMeta, Object[] r ) throws KettleValueException {
  try {
    // Initialize evaluators etc. Only do it once.
    //
    if ( data.expressionEvaluator == null ) {
      String realCondition = environmentSubstitute( meta.getCondition() );
      data.argumentIndexes = new ArrayList<Integer>();

      List<String> parameterNames = new ArrayList<String>();
      List<Class<?>> parameterTypes = new ArrayList<Class<?>>();

      for ( int i = 0; i < data.outputRowMeta.size(); i++ ) {

        ValueMetaInterface valueMeta = data.outputRowMeta.getValueMeta( i );

        // See if the value is being used in a formula...
        //
        if ( realCondition.contains( valueMeta.getName() ) ) {
          // If so, add it to the indexes...
          data.argumentIndexes.add( i );

          Class<?> parameterType;
          switch ( valueMeta.getType() ) {
            case ValueMetaInterface.TYPE_STRING:
              parameterType = String.class;
              break;
            case ValueMetaInterface.TYPE_NUMBER:
              parameterType = Double.class;
              break;
            case ValueMetaInterface.TYPE_INTEGER:
              parameterType = Long.class;
              break;
            case ValueMetaInterface.TYPE_DATE:
              parameterType = Date.class;
              break;
            case ValueMetaInterface.TYPE_BIGNUMBER:
              parameterType = BigDecimal.class;
              break;
            case ValueMetaInterface.TYPE_BOOLEAN:
              parameterType = Boolean.class;
              break;
            case ValueMetaInterface.TYPE_BINARY:
              parameterType = byte[].class;
              break;
            default:
              parameterType = String.class;
              break;
          }
          parameterTypes.add( parameterType );
          parameterNames.add( valueMeta.getName() );
        }
      }

      // Create the expression evaluator: is relatively slow so we do it only for the first row...
      //
      data.expressionEvaluator = new ExpressionEvaluator();
      data.expressionEvaluator.setParameters(
        parameterNames.toArray( new String[parameterNames.size()] ), parameterTypes
          .toArray( new Class<?>[parameterTypes.size()] ) );
      data.expressionEvaluator.setReturnType( Object.class );
      data.expressionEvaluator.setThrownExceptions( new Class<?>[] { Exception.class } );
      data.expressionEvaluator.cook( realCondition );

      // Also create the argument data structure once...
      //
      data.argumentData = new Object[data.argumentIndexes.size()];
    }

    // This method can only accept the specified number of values...
    //
    for ( int x = 0; x < data.argumentIndexes.size(); x++ ) {
      int index = data.argumentIndexes.get( x );
      ValueMetaInterface outputValueMeta = data.outputRowMeta.getValueMeta( index );
      data.argumentData[x] = outputValueMeta.convertToNormalStorageType( r[index] );
    }

    Object formulaResult = data.expressionEvaluator.evaluate( data.argumentData );

    if ( formulaResult instanceof Boolean ) {
      return (Boolean) formulaResult;
    } else {
      throw new KettleException( "The result of the filter expression must be a boolean and we got back : "
        + formulaResult.getClass().getName() );
    }
  } catch ( Exception e ) {
    throw new KettleValueException( e );
  }
}