org.dmg.pmml.Constant Java Examples

The following examples show how to use org.dmg.pmml.Constant. 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: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateCaseWhenExpression(){
	String string = "CASE WHEN x1 < 0 THEN x1 WHEN x2 > 0 THEN x2 ELSE 0 END";

	FieldRef first = new FieldRef(FieldName.create("x1"));
	FieldRef second = new FieldRef(FieldName.create("x2"));

	Constant zero = PMMLUtil.createConstant(0, DataType.DOUBLE);

	Apply expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSTHAN)
			.addExpressions(first, zero)
		)
		.addExpressions(first)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATERTHAN)
				.addExpressions(second, zero)
			)
			.addExpressions(second)
			.addExpressions(zero)
		);

	checkExpression(expected, string);
}
 
Example #2
Source File: FormulaUtil.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private Map<String, String> parseRevalue(FunctionExpression.Argument replaceArgument){
	Map<String, String> result = new LinkedHashMap<>();

	FunctionExpression vectorExpression = toVectorExpression(replaceArgument);

	List<FunctionExpression.Argument> objectArguments = vectorExpression.getArguments();
	for(FunctionExpression.Argument objectArgument : objectArguments){
		String from = objectArgument.getTag();
		if(from == null){
			throw new IllegalArgumentException();
		}

		Constant constant = (Constant)objectArgument.getExpression();

		String to = (String)constant.getValue();
		if(to == null){
			throw new IllegalArgumentException();
		}

		result.put(from, to);
	}

	return result;
}
 
Example #3
Source File: FormulaUtil.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
static
private List<String> parseVector(FunctionExpression.Argument argument){
	List<String> result = new ArrayList<>();

	FunctionExpression vectorExpression = toVectorExpression(argument);

	List<FunctionExpression.Argument> objectArguments = vectorExpression.getArguments();
	for(FunctionExpression.Argument objectArgument : objectArguments){
		Constant constant = (Constant)objectArgument.getExpression();

		String string = ValueUtil.asString(constant.getValue());

		result.add(string);
	}

	return result;
}
 
Example #4
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 #5
Source File: TermFeature.java    From jpmml-sparkml with GNU Affero General Public License v3.0 5 votes vote down vote up
public Apply createApply(){
	DefineFunction defineFunction = getDefineFunction();
	Feature feature = getFeature();
	String value = getValue();

	Constant constant = PMMLUtil.createConstant(value, DataType.STRING);

	return PMMLUtil.createApply(defineFunction.getName(), feature.ref(), constant);
}
 
Example #6
Source File: Formula.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
public void addField(Field<?> field){
	RExpEncoder encoder = getEncoder();

	Feature feature = new ContinuousFeature(encoder, field);

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

		Expression expression = derivedField.getExpression();
		if(expression instanceof Apply){
			Apply apply = (Apply)expression;

			if(checkApply(apply, PMMLFunctions.POW, FieldRef.class, Constant.class)){
				List<Expression> expressions = apply.getExpressions();

				FieldRef fieldRef = (FieldRef)expressions.get(0);
				Constant constant = (Constant)expressions.get(1);

				try {
					String string = ValueUtil.asString(constant.getValue());

					int power = Integer.parseInt(string);

					feature = new PowerFeature(encoder, fieldRef.getField(), DataType.DOUBLE, power);
				} catch(NumberFormatException nfe){
					// Ignored
				}
			}
		}
	}

	putFeature(field.getName(), feature);

	this.fields.add(field);
}
 
Example #7
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateParenthesizedExpression(){
	String string = "TRUE | TRUE & FALSE";

	Constant trueConstant = PMMLUtil.createConstant("true", DataType.BOOLEAN);
	Constant falseConstant = PMMLUtil.createConstant("false", DataType.BOOLEAN);

	Expression expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(trueConstant)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.AND)
			.addExpressions(trueConstant, falseConstant)
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));

	string = "(TRUE | TRUE) & FALSE";

	expected = PMMLUtil.createApply(PMMLFunctions.AND)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.OR)
			.addExpressions(trueConstant, trueConstant)
		)
		.addExpressions(falseConstant);

	actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #8
Source File: ExpressionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public FieldValue evaluateConstant(Constant constant){
	boolean missing = constant.isMissing();
	if(missing){
		return FieldValues.MISSING_VALUE;
	}

	Object value = constant.getValue();

	DataType dataType = constant.getDataType();
	if(dataType != null){

		if(isEmptyContent(value)){

			switch(dataType){
				// "If the data type is string, then the empty content will be interpreted as an empty string"
				case STRING:
					return FieldValueUtil.create(TypeInfos.CATEGORICAL_STRING, "");
				// "If the data type is something other than string, then the empty content will be interpreted as a missing value of the specified data type"
				default:
					return FieldValues.MISSING_VALUE;
			}
		}
	} else

	{
		if(isEmptyContent(value)){
			return FieldValues.MISSING_VALUE;
		}

		dataType = TypeUtil.getConstantDataType(value);

	}

	OpType opType = TypeUtil.getOpType(dataType);

	return FieldValueUtil.create(dataType, opType, value);
}
 
Example #9
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void evaluateTextIndexNormalization(){
	FieldName name = FieldName.create("x");

	TextIndexNormalization stepOne = new TextIndexNormalization();

	List<List<String>> cells = Arrays.asList(
		Arrays.asList("interfaces?", "interface", "true"),
		Arrays.asList("is|are|seem(ed|s?)|were", "be", "true"),
		Arrays.asList("user friendl(y|iness)", "user_friendly", "true")
	);

	stepOne.setInlineTable(createInlineTable(cells, stepOne));

	TextIndexNormalization stepTwo = new TextIndexNormalization()
		.setInField("re")
		.setOutField("feature");

	cells = Arrays.asList(
		Arrays.asList("interface be (user_friendly|well designed|excellent)", "ui_good", "true")
	);

	stepTwo.setInlineTable(createInlineTable(cells, stepTwo));

	TextIndex textIndex = new TextIndex(name, new Constant("ui_good"))
		.setLocalTermWeights(TextIndex.LocalTermWeights.BINARY)
		.setCaseSensitive(false)
		.addTextIndexNormalizations(stepOne, stepTwo);

	assertEquals(1, evaluate(textIndex, name, "Testing the app for a few days convinced me the interfaces are excellent!"));
}
 
Example #10
Source File: CountVectorizer.java    From jpmml-sklearn with GNU Affero General Public License v3.0 4 votes vote down vote up
public Apply encodeApply(String function, Feature feature, int index, String term){
	Constant constant = PMMLUtil.createConstant(term, DataType.STRING);

	return PMMLUtil.createApply(function, feature.ref(), constant);
}
 
Example #11
Source File: ExpressionUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
static
FieldValue evaluateExpression(Expression expression, EvaluationContext context){

	if(expression instanceof Constant){
		return evaluateConstant((Constant)expression);
	} else

	if(expression instanceof FieldRef){
		return evaluateFieldRef((FieldRef)expression, context);
	} else

	if(expression instanceof NormContinuous){
		return evaluateNormContinuous((NormContinuous)expression, context);
	} else

	if(expression instanceof NormDiscrete){
		return evaluateNormDiscrete((NormDiscrete)expression, context);
	} else

	if(expression instanceof Discretize){
		return evaluateDiscretize((Discretize)expression, context);
	} else

	if(expression instanceof MapValues){
		return evaluateMapValues((MapValues)expression, context);
	} else

	if(expression instanceof TextIndex){
		return evaluateTextIndex((TextIndex)expression, context);
	} else

	if(expression instanceof Apply){
		return evaluateApply((Apply)expression, context);
	} else

	if(expression instanceof Aggregate){
		return evaluateAggregate((Aggregate)expression, context);
	} // End if

	if(expression instanceof JavaExpression){
		return evaluateJavaExpression((JavaExpression)expression, context);
	}

	throw new UnsupportedElementException(expression);
}
 
Example #12
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void evaluateConstantNaN(){
	Constant constant = new Constant("NaN");

	assertEquals(Double.NaN, evaluate(constant));

	constant.setDataType(DataType.FLOAT);

	assertEquals(Float.NaN, evaluate(constant));
}
 
Example #13
Source File: ExpressionUtilTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 2 votes vote down vote up
@Test
public void evaluateConstant(){
	Constant emptyString = new Constant()
		.setDataType(DataType.STRING);

	assertEquals("", evaluate(emptyString));

	emptyString.setMissing(true);

	assertEquals(null, evaluate(emptyString));

	Constant stringThree = new Constant("3")
		.setDataType(DataType.STRING);

	assertEquals("3", evaluate(stringThree));

	stringThree.setMissing(true);

	assertEquals(null, evaluate(stringThree));

	Constant integerThree = new Constant("3")
		.setDataType(DataType.INTEGER);

	assertEquals(3, evaluate(integerThree));

	integerThree.setMissing(true);

	assertEquals(null, evaluate(integerThree));

	Constant floatThree = new Constant("3")
		.setDataType(DataType.FLOAT);

	assertEquals(3f, evaluate(floatThree));

	floatThree.setMissing(true);

	assertEquals(null, evaluate(floatThree));

	Constant doubleThree = new Constant("3")
		.setDataType(DataType.DOUBLE);

	assertEquals(3d, evaluate(doubleThree));

	doubleThree.setMissing(true);

	assertEquals(null, evaluate(doubleThree));
}