org.jpmml.evaluator.ExpressionUtil Java Examples

The following examples show how to use org.jpmml.evaluator.ExpressionUtil. 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: ValueParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public VisitorAction visit(Constant constant){
	boolean missing = constant.isMissing();

	if(!missing){
		Object value = constant.getValue();

		if(!ExpressionUtil.isEmptyContent(value)){
			DataType dataType = constant.getDataType();
			if(dataType == null){
				dataType = TypeUtil.getConstantDataType(value);
			}

			value = parseOrCast(dataType, value);

			constant.setValue(value);
		}
	}

	return super.visit(constant);
}
 
Example #2
Source File: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public void checkValue(Object expectedValue, String sqlExpression){
	Expression expression = translateInternal("SELECT (" + sqlExpression + ") FROM __THIS__");

	Object sparkValue = expression.eval(InternalRow.empty());

	if(expectedValue instanceof String){
		assertEquals(expectedValue, sparkValue.toString());
	} else

	if(expectedValue instanceof Integer){
		assertEquals(expectedValue, ((Number)sparkValue).intValue());
	} else

	if(expectedValue instanceof Float){
		assertEquals(expectedValue, ((Number)sparkValue).floatValue());
	} else

	if(expectedValue instanceof Double){
		assertEquals(expectedValue, ((Number)sparkValue).doubleValue());
	} else

	{
		assertEquals(expectedValue, sparkValue);
	}

	org.dmg.pmml.Expression pmmlExpression = ExpressionTranslator.translate(expression);

	EvaluationContext context = new VirtualEvaluationContext();
	context.declareAll(Collections.emptyMap());

	FieldValue value = ExpressionUtil.evaluate(pmmlExpression, context);

	Object pmmlValue = FieldValueUtil.getValue(value);
	assertEquals(expectedValue, pmmlValue);
}
 
Example #3
Source File: FunctionTransformerTest.java    From jpmml-sklearn with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private Object evaluate(String function, Object value){
	UFunc ufunc = new UFunc("numpy.core", "_ufunc_reconstruct");
	ufunc.__init__(new String[]{"numpy", function});

	FieldName name = FieldName.create("x");

	DataType dataType;

	if(value instanceof Integer){
		dataType = DataType.INTEGER;
	} else

	if(value instanceof Float){
		dataType = DataType.FLOAT;
	} else

	{
		dataType = DataType.DOUBLE;
	}

	EvaluationContext context = new VirtualEvaluationContext();
	context.declare(name, FieldValueUtil.create(dataType, OpType.CONTINUOUS, value));

	Expression expression = UFuncUtil.encodeUFunc(ufunc, Collections.singletonList(new FieldRef(name)));

	FieldValue result = ExpressionUtil.evaluate(expression, context);

	return FieldValueUtil.getValue(result);
}
 
Example #4
Source File: NeuralNetworkEvaluator.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private Expression getOutputExpression(NeuralOutput neuralOutput){
	DerivedField derivedField = neuralOutput.getDerivedField();
	if(derivedField == null){
		throw new MissingElementException(neuralOutput, PMMLElements.NEURALOUTPUT_DERIVEDFIELD);
	}

	Expression expression = ExpressionUtil.ensureExpression(derivedField);

	if(expression instanceof FieldRef){
		FieldRef fieldRef = (FieldRef)expression;

		FieldName name = fieldRef.getField();
		if(name == null){
			throw new MissingAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD);
		}

		Field<?> field = resolveField(name);
		if(field == null){
			throw new MissingFieldException(name, fieldRef);
		} // End if

		if(field instanceof DataField){
			return expression;
		} else

		if(field instanceof DerivedField){
			DerivedField targetDerivedField = (DerivedField)field;

			Expression targetExpression = ExpressionUtil.ensureExpression(targetDerivedField);

			return targetExpression;
		} else

		{
			throw new InvalidAttributeException(fieldRef, org.dmg.pmml.PMMLAttributes.FIELDREF_FIELD, name);
		}
	}

	return expression;
}