Java Code Examples for org.jpmml.model.ReflectionUtil

The following examples show how to use org.jpmml.model.ReflectionUtil. These examples are extracted from open source projects. 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
@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
@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
@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 4
@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 5
@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 6
@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 7
@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 Project: jpmml-model   Source File: Interner.java    License: 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 9
@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 10
@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 11
@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 12
@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 13
@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
@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 15
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 16
@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 17
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
public CustomPMML(PMML pmml){
	super();

	ReflectionUtil.copyState(pmml, this);
}
 
Example 19
public CustomSimplePredicate(SimplePredicate simplePredicate){
	super();

	ReflectionUtil.copyState(simplePredicate, this);
}
 
Example 20
public RichOutputField(OutputField outputField){
	ReflectionUtil.copyState(outputField, this);
}
 
Example 21
public RichDataField(DataField dataField){
	ReflectionUtil.copyState(dataField, this);
}
 
Example 22
public RichDerivedField(DerivedField derivedField){
	ReflectionUtil.copyState(derivedField, this);
}
 
Example 23
public RichBaseCumHazardTables(DataType dataType, BaseCumHazardTables baseCumHazardTables){
	setDataType(dataType);

	ReflectionUtil.copyState(baseCumHazardTables, this);
}
 
Example 24
public RichBayesInput(DataType dataType, BayesInput bayesInput){
	setDataType(dataType);

	ReflectionUtil.copyState(bayesInput, this);
}
 
Example 25
@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 = {"[email protected]", "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
@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);
}