org.jpmml.model.ReflectionUtil Java Examples

The following examples show how to use org.jpmml.model.ReflectionUtil. 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-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translate(){
	String string = "(1.0 + log(A / B)) ^ 2";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.POW)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.ADD)
			.addExpressions(PMMLUtil.createConstant("1.0", DataType.DOUBLE))
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.LN)
				.addExpressions(PMMLUtil.createApply(PMMLFunctions.DIVIDE)
					.addExpressions(new FieldRef(FieldName.create("A")), new FieldRef(FieldName.create("B")))
				)
			)
		)
		.addExpressions(PMMLUtil.createConstant("2", DataType.INTEGER));

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #2
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateLogicalExpression(){
	String string = "a >= 0.0 & b >= 0.0 | c <= 0.0";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.AND)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL)
				.addExpressions(new FieldRef(FieldName.create("a")), PMMLUtil.createConstant("0.0", DataType.DOUBLE))
			)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATEROREQUAL)
				.addExpressions(new FieldRef(FieldName.create("b")), PMMLUtil.createConstant("0.0", DataType.DOUBLE))
			)
		)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSOREQUAL)
			.addExpressions(new FieldRef(FieldName.create("c")), PMMLUtil.createConstant("0.0", DataType.DOUBLE))
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #3
Source File: FieldNameFilterer.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public VisitorAction visit(PMMLObject object){
	List<Field> fields = ReflectionUtil.getFields(object.getClass());

	for(Field field : fields){

		if((FieldName.class).equals(field.getType())){
			FieldName name = (FieldName)ReflectionUtil.getFieldValue(field, object);

			name = filter(name);

			ReflectionUtil.setFieldValue(field, object, name);
		}
	}

	return super.visit(object);
}
 
Example #4
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateRelationalExpression(){
	String string = "if(x < 0) \"negative\" else if(x > 0) \"positive\" else \"zero\"";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.LESSTHAN)
			.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("0", DataType.INTEGER))
		)
		.addExpressions(PMMLUtil.createConstant("negative", DataType.STRING))
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.IF)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.GREATERTHAN)
				.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("0", DataType.INTEGER))
			)
			.addExpressions(PMMLUtil.createConstant("positive", DataType.STRING))
			.addExpressions(PMMLUtil.createConstant("zero", DataType.STRING))
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #5
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateArithmeticExpressionChain(){
	String string = "A + B - X + C";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.ADD)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.SUBTRACT)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.ADD)
				.addExpressions(new FieldRef(FieldName.create("A")), new FieldRef(FieldName.create("B")))
			)
			.addExpressions(new FieldRef(FieldName.create("X")))
		)
		.addExpressions(new FieldRef(FieldName.create("C")));

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #6
Source File: Interner.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public VisitorAction visit(PMMLObject object){
	Class<? extends V> type = getType();

	List<Field> fields = ReflectionUtil.getFields(object.getClass());
	for(Field field : fields){
		Object value = ReflectionUtil.getFieldValue(field, object);

		if(type.isInstance(value)){
			V internedValue = intern(type.cast(value));

			ReflectionUtil.setFieldValue(field, object, internedValue);
		}
	}

	return super.visit(object);
}
 
Example #7
Source File: ArrayListTrimmer.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public VisitorAction visit(PMMLObject object){
	List<Field> fields = ReflectionUtil.getFields(object.getClass());

	for(Field field : fields){
		Object value = ReflectionUtil.getFieldValue(field, object);

		if(value instanceof ArrayList){
			ArrayList<?> list = (ArrayList<?>)value;

			list.trimToSize();
		}
	}

	return super.visit(object);
}
 
Example #8
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void translateInterval(){
	Interval expected = new Interval(Interval.Closure.OPEN_CLOSED)
		.setLeftMargin(new Double("-10.0E0"))
		.setRightMargin(new Double("+10.0E0"));

	Interval actual = ExpressionTranslator.translateInterval("(-10.0E+0, +10.0E-0]");

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

	try {
		ExpressionTranslator.translateInterval("(0, NaN)");

		fail();
	} catch(IllegalArgumentException iae){
		// Ignored
	}

	expected = new Interval(Interval.Closure.CLOSED_CLOSED)
		.setLeftMargin(null)
		.setRightMargin(null);

	actual = ExpressionTranslator.translateInterval("[-Inf, +Inf]");

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #9
Source File: ArrayListTransformer.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public VisitorAction visit(PMMLObject object){
	List<Field> fields = ReflectionUtil.getFields(object.getClass());

	for(Field field : fields){
		Object value = ReflectionUtil.getFieldValue(field, object);

		if((value instanceof ArrayList) && (value.getClass()).equals(ArrayList.class)){
			ArrayList<?> list = (ArrayList<?>)value;

			List<?> transformedList = transform(list);
			if(list != transformedList){
				ReflectionUtil.setFieldValue(field, object, transformedList);
			}
		}
	}

	return super.visit(object);
}
 
Example #10
Source File: VersionInspector.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void inspect(Field field, Object value){
	Class<?> type = field.getType();

	if(type.isPrimitive()){

		if(ReflectionUtil.isDefaultValue(value)){
			return;
		}
	} else

	{
		if(isNull(value)){
			Optional optional = field.getAnnotation(Optional.class);
			if(optional != null){
				updateMinimum(optional.value());
			}

			Required required = field.getAnnotation(Required.class);
			if(required != null){
				updateMaximum(previous(required.value()));
			}

			return;
		}
	}

	inspect(field);
}
 
Example #11
Source File: InvalidMarkupInspector.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public VisitorAction visit(PMMLObject object){
	List<Field> fields = ReflectionUtil.getFields(object.getClass());

	for(Field field : fields){
		Object value = ReflectionUtil.getFieldValue(field, object);

		if(value instanceof List){
			List<?> collection = (List<?>)value;

			// The getter method may have initialized the field with an empty ArrayList instance
			if(collection.isEmpty()){
				value = null;
			}
		} // End if

		// The field is set
		if(value != null){
			continue;
		}

		XmlElement element = field.getAnnotation(XmlElement.class);
		if(element != null && element.required()){
			report(new MissingElementException(object, field));
		}

		XmlAttribute attribute = field.getAnnotation(XmlAttribute.class);
		if(attribute != null && attribute.required()){
			report(new MissingAttributeException(object, field));
		}
	}

	return super.visit(object);
}
 
Example #12
Source File: VersionInspector.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public VisitorAction visit(PMMLObject object){

	for(Class<?> clazz = object.getClass(); clazz != null; clazz = clazz.getSuperclass()){
		inspect(clazz);
	}

	List<Field> fields = ReflectionUtil.getFields(object.getClass());
	for(Field field : fields){
		Object value = ReflectionUtil.getFieldValue(field, object);

		inspect(field, value);

		// The field is set to an enum constant
		if(value instanceof Enum){
			Enum<?> enumValue = (Enum<?>)value;

			Field enumField;

			try {
				Class<?> enumClazz = enumValue.getClass();

				enumField = enumClazz.getField(enumValue.name());
			} catch(NoSuchFieldException nsfe){
				throw new RuntimeException(nsfe);
			}

			inspect(enumField);
		}
	}

	return super.visit(object);
}
 
Example #13
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 #14
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateExponentiationExpression(){
	String string = "-2^-3";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
		.addExpressions(PMMLUtil.createConstant(-1))
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.POW)
			.addExpressions(PMMLUtil.createConstant("2", DataType.INTEGER), PMMLUtil.createConstant("-3", DataType.INTEGER))
		);

	Expression actual = ExpressionTranslator.translateExpression(string);

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

	string = "-2^-2*1.5";

	expected = PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.MULTIPLY)
			.addExpressions(PMMLUtil.createConstant(-1))
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.POW)
				.addExpressions(PMMLUtil.createConstant("2", DataType.INTEGER), PMMLUtil.createConstant("-2", DataType.INTEGER))
			)
		)
		.addExpressions(PMMLUtil.createConstant("1.5", DataType.DOUBLE));

	actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #15
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateLogicalExpressionChain(){
	String string = "(x == 0) | ((x == 1) | (x == 2)) | x == 3";

	Apply left = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("0", DataType.INTEGER));

	Apply middleLeft = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("1", DataType.INTEGER));

	Apply middleRight = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("2", DataType.INTEGER));

	Apply right = PMMLUtil.createApply(PMMLFunctions.EQUAL)
		.addExpressions(new FieldRef(FieldName.create("x")), PMMLUtil.createConstant("3", DataType.INTEGER));

	Expression expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.OR)
			.addExpressions(left)
			.addExpressions(PMMLUtil.createApply(PMMLFunctions.OR)
				.addExpressions(middleLeft, middleRight)
			)
		)
		.addExpressions(right);

	Expression actual = ExpressionTranslator.translateExpression(string, false);

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

	expected = PMMLUtil.createApply(PMMLFunctions.OR)
		.addExpressions(left, middleLeft, middleRight, right);

	actual = ExpressionTranslator.translateExpression(string, true);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #16
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test
public void translateIfExpression(){
	String string = "if(is.na(x)) TRUE else FALSE";

	Expression expected = PMMLUtil.createApply(PMMLFunctions.IF)
		.addExpressions(PMMLUtil.createApply(PMMLFunctions.ISMISSING)
			.addExpressions(new FieldRef(FieldName.create("x")))
		)
		.addExpressions(PMMLUtil.createConstant("true", DataType.BOOLEAN), PMMLUtil.createConstant("false", DataType.BOOLEAN));

	Expression actual = ExpressionTranslator.translateExpression(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #17
Source File: ExpressionTranslatorTest.java    From jpmml-sparkml with GNU Affero General Public License v3.0 4 votes vote down vote up
static
private void checkExpression(org.dmg.pmml.Expression expected, String string){
	org.dmg.pmml.Expression actual = translate(string);

	assertTrue(ReflectionUtil.equals(expected, actual));
}
 
Example #18
Source File: CustomPMML.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CustomPMML(PMML pmml){
	super();

	ReflectionUtil.copyState(pmml, this);
}
 
Example #19
Source File: CustomSimplePredicate.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public CustomSimplePredicate(SimplePredicate simplePredicate){
	super();

	ReflectionUtil.copyState(simplePredicate, this);
}
 
Example #20
Source File: RichOutputField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public RichOutputField(OutputField outputField){
	ReflectionUtil.copyState(outputField, this);
}
 
Example #21
Source File: RichDataField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public RichDataField(DataField dataField){
	ReflectionUtil.copyState(dataField, this);
}
 
Example #22
Source File: RichDerivedField.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public RichDerivedField(DerivedField derivedField){
	ReflectionUtil.copyState(derivedField, this);
}
 
Example #23
Source File: RichBaseCumHazardTables.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public RichBaseCumHazardTables(DataType dataType, BaseCumHazardTables baseCumHazardTables){
	setDataType(dataType);

	ReflectionUtil.copyState(baseCumHazardTables, this);
}
 
Example #24
Source File: RichBayesInput.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
public RichBayesInput(DataType dataType, BayesInput bayesInput){
	setDataType(dataType);

	ReflectionUtil.copyState(bayesInput, this);
}
 
Example #25
Source File: InvalidMarkupInspectorTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void inspect() throws Exception {
	DataDictionary dataDictionary = new DataDictionary()
		.setNumberOfFields(1);

	Field field = ReflectionUtil.getField(DataDictionary.class, "dataFields");

	assertNull(ReflectionUtil.getFieldValue(field, dataDictionary));

	List<DataField> dataFields = dataDictionary.getDataFields();
	assertEquals(0, dataFields.size());

	assertNotNull(ReflectionUtil.getFieldValue(field, dataDictionary));

	PMML pmml = new PMML(null, null, dataDictionary);

	InvalidMarkupInspector inspector = new InvalidMarkupInspector();

	try {
		inspector.applyTo(pmml);

		fail();
	} catch(InvalidMarkupException ime){
		List<InvalidMarkupException> exceptions = inspector.getExceptions();

		String[] features = {"PMML@version", "PMML/Header", "DataDictionary", "DataDictionary/DataField"};

		assertEquals(features.length, exceptions.size());
		assertEquals(0, exceptions.indexOf(ime));

		for(int i = 0; i < exceptions.size(); i++){
			InvalidMarkupException exception = exceptions.get(i);

			String message = exception.getMessage();

			assertTrue(message.contains(features[i]));
		}
	}
}
 
Example #26
Source File: ExpressionTranslatorTest.java    From jpmml-r with GNU Affero General Public License v3.0 2 votes vote down vote up
@Test
public void translateFunctionExpression(){
	String string = "parent(first = child(A, log(A)), child(1 + B, right = 0), \"third\" = child(left = 0, c(A, B, C)))";

	FunctionExpression functionExpression = (FunctionExpression)ExpressionTranslator.translateExpression(string);

	checkFunctionExpression(functionExpression, "parent", "first", null, "third");

	FunctionExpression.Argument first = functionExpression.getArgument("first");
	FunctionExpression.Argument second;

	try {
		second = functionExpression.getArgument("second");

		fail();
	} catch(IllegalArgumentException iae){
		second = functionExpression.getArgument(1);
	}

	FunctionExpression.Argument third = functionExpression.getArgument("third");

	assertEquals("first = child(A, log(A))", first.format());
	assertEquals("child(A, log(A))", first.formatExpression());

	List<Expression> expressions = checkFunctionExpression((FunctionExpression)first.getExpression(), "child", null, null);

	assertTrue(ReflectionUtil.equals(new FieldRef(FieldName.create("A")), expressions.get(0)));
	assertTrue(ReflectionUtil.equals(PMMLUtil.createApply(PMMLFunctions.LN, new FieldRef(FieldName.create("A"))), expressions.get(1)));

	assertEquals("child(1 + B, right = 0)", second.format());
	assertEquals("child(1 + B, right = 0)", second.formatExpression());

	expressions = checkFunctionExpression((FunctionExpression)second.getExpression(), "child", null, "right");

	assertTrue(ReflectionUtil.equals(PMMLUtil.createApply(PMMLFunctions.ADD, PMMLUtil.createConstant("1", DataType.INTEGER), new FieldRef(FieldName.create("B"))), expressions.get(0)));
	assertTrue(ReflectionUtil.equals(PMMLUtil.createConstant("0", DataType.INTEGER), expressions.get(1)));

	assertEquals("\"third\" = child(left = 0, c(A, B, C))", third.format());
	assertEquals("child(left = 0, c(A, B, C))", third.formatExpression());

	expressions = checkFunctionExpression((FunctionExpression)third.getExpression(), "child", "left", null);

	assertTrue(ReflectionUtil.equals(PMMLUtil.createConstant("0", DataType.INTEGER), expressions.get(0)));

	checkFunctionExpression((FunctionExpression)expressions.get(1), "c", null, null, null);
}