Java Code Examples for org.dmg.pmml.PMMLObject

The following examples show how to use org.dmg.pmml.PMMLObject. 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
static
public <E extends PMMLObject> E load(Class<E> clazz, ClassLoader clazzLoader){
	ServiceLoader<E> serviceLoader;

	try {
		serviceLoader = ServiceLoader.load(clazz, clazzLoader);
	} catch(ServiceConfigurationError sce){
		throw new IllegalArgumentException(sce);
	}

	Iterator<E> it = serviceLoader.iterator();

	if(it.hasNext()){
		E object = it.next();

		if(it.hasNext()){
			throw new IllegalArgumentException();
		}

		return object;
	}

	throw new IllegalArgumentException();
}
 
Example 2
@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 3
@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 4
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 5
private void process(Node node){
	List<Node> path = new ArrayList<>();
	path.add(node);

	Deque<PMMLObject> parents = getParents();
	for(PMMLObject parent : parents){

		if(!(parent instanceof Node)){
			break;
		}

		path.add((Node)parent);
	}

	Collections.reverse(path);

	this.paths.put(node, path);
}
 
Example 6
static
private boolean shouldMeasure(Object object){

	if(object != null){

		if(object instanceof Enum){
			return false;
		} // End if

		if(object instanceof PMMLObject){
			return false;
		}

		return true;
	}

	return false;
}
 
Example 7
@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 8
static
public <E extends PMMLObject> void copyState(E from, E to){
	Class<?> fromClazz = from.getClass();
	Class<?> toClazz = to.getClass();

	// Allow copying to the same class or to a subclass, but not to a superclass
	if(!(fromClazz).isAssignableFrom(toClazz)){
		throw new IllegalArgumentException();
	}

	List<Field> fields = getFields(fromClazz);
	for(Field field : fields){
		Object value = getFieldValue(field, from);

		setFieldValue(field, to, value);
	}
}
 
Example 9
static
public <K extends PMMLObject, V> V getValue(K key, Cache<K, V> cache, Callable<? extends V> loader){

	try {
		return cache.get(key, loader);
	} catch(ExecutionException | UncheckedExecutionException e){
		Throwable cause = e.getCause();

		if(cause instanceof PMMLException){
			throw (PMMLException)cause;
		}

		throw new InvalidElementException(key)
			.initCause(cause);
	}
}
 
Example 10
@Override
public VisitorAction accept(Visitor visitor){
	VisitorAction status = visitor.visit(this);

	if(status == VisitorAction.CONTINUE){
		visitor.pushParent(this);

		status = PMMLObject.traverse(visitor, getMiningSchema(), getOutput(), getModelStats(), getModelExplanation(), getTargets(), getLocalTransformations(), getModelVerification());

		visitor.popParent();
	} // End if

	if(status == VisitorAction.TERMINATE){
		return VisitorAction.TERMINATE;
	}

	return VisitorAction.CONTINUE;
}
 
Example 11
@Override
public void pushParent(PMMLObject parent){
	super.pushParent(parent);

	if(parent instanceof MiningModel){
		MiningModel miningModel = (MiningModel)parent;

		processMiningModel(miningModel);
	} else

	if(parent instanceof Model){
		Model model = (Model)parent;

		processModel(model);
	}
}
 
Example 12
@Override
public VisitorAction visit(Node node){
	PMMLObject parent = getParent();

	if(parent instanceof TreeModel){
		TreeModel treeModel = (TreeModel)parent;

		MiningFunction miningFunction = treeModel.getMiningFunction();
		switch(miningFunction){
			case CLASSIFICATION:
				break;
			default:
				return VisitorAction.SKIP;
		}
	}

	node.setScore(parseTargetValue(node.getScore()));

	return super.visit(node);
}
 
Example 13
@Override
public VisitorAction visit(Value value){
	PMMLObject parent = getParent();

	Object simpleValue = value.getValue();
	if(simpleValue == null){
		throw new MissingAttributeException(value, PMMLAttributes.VALUE_VALUE);
	} // End if

	if(parent instanceof Field){
		Field<?> field = (Field<?>)parent;

		DataType dataType = field.getDataType();
		if(dataType != null){
			simpleValue = safeParseOrCast(dataType, simpleValue);

			value.setValue(simpleValue);
		}
	}

	return super.visit(value);
}
 
Example 14
static
public <K, E extends PMMLObject & Indexable<K>> Map<K, E> buildMap(List<E> elements, boolean nullable){
	Map<K, E> result = new LinkedHashMap<>();

	for(E element : elements){
		K key = ensureKey(element, nullable);

		if(result.containsKey(key)){
			throw new InvalidElementException(element);
		}

		result.put(key, element);
	}

	// Cannot use Guava's ImmutableMap, because it is null-hostile
	return Collections.unmodifiableMap(result);
}
 
Example 15
/**
 * <p>
 * Checks if this value is contained in the set of reference values.
 * </p>
 */
public boolean isIn(HasValueSet<?> hasValueSet){
	Array array = hasValueSet.getArray();
	if(array == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement((Class)hasValueSet.getClass()) + "/" + XPathUtil.formatElement(Array.class)), (PMMLObject)hasValueSet);
	} // End if

	if(array instanceof SetHolder){
		SetHolder setHolder = (SetHolder)array;

		return setHolder.contains(getDataType(), getValue());
	}

	List<?> values = ArrayUtil.getContent(array);

	return values.stream()
		.anyMatch(value -> equalsValue(value));
}
 
Example 16
static
public <K, E extends PMMLObject & Indexable<K>> E findIndexable(List<E> elements, K key, boolean nullable){

	for(E element : elements){

		if(Objects.equals(ensureKey(element, nullable), key)){
			return element;
		}
	}

	return null;
}
 
Example 17
public Collection<Field<?>> getFields(PMMLObject... virtualParents){
	Deque<PMMLObject> parents = new ArrayDeque<>(getParents());

	for(PMMLObject virtualParent : virtualParents){
		parents.push(virtualParent);
	}

	return getFields(parents);
}
 
Example 18
private List<Field<?>> getScope(PMMLObject object){

		if(this.customScopes.size() > 0){
			return this.customScopes.getOrDefault(object, this.scopes.get(object));
		} else

		{
			return this.scopes.get(object);
		}
	}
 
Example 19
private void declareFields(PMMLObject object, Collection<? extends Field<?>> fields){
	List<Field<?>> scope = this.scopes.get(object);

	if(scope == null){
		scope = new ArrayList<>(fields.size());

		this.scopes.put(object, scope);
	}

	scope.addAll(fields);
}
 
Example 20
@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 21
@Override
public VisitorAction visit(PMMLObject object){

	if(object instanceof HasPredicate){
		HasPredicate<?> hasPredicate = (HasPredicate<?>)object;

		hasPredicate.setPredicate(filter(hasPredicate.getPredicate()));
	}

	return super.visit(object);
}
 
Example 22
@Override
public VisitorAction visit(PMMLObject object){

	if(object instanceof HasExpression){
		HasExpression<?> hasExpression = (HasExpression<?>)object;

		hasExpression.setExpression(filter(hasExpression.getExpression()));
	}

	return super.visit(object);
}
 
Example 23
static
public String formatElementOrAttribute(Class<? extends PMMLObject> elementClazz, Field field){
	XmlElement element = field.getAnnotation(XmlElement.class);
	XmlAttribute attribute = field.getAnnotation(XmlAttribute.class);

	if(element != null){
		Class<?> childElementClazz = field.getType();

		if((List.class).isAssignableFrom(childElementClazz)){
			ParameterizedType listType = (ParameterizedType)field.getGenericType();

			Type[] typeArguments = listType.getActualTypeArguments();
			if(typeArguments.length != 1){
				throw new IllegalArgumentException();
			}

			childElementClazz = (Class<?>)typeArguments[0];
		}

		try {
			return getElementName(elementClazz) + "/" + getElementName(childElementClazz);
		} catch(IllegalArgumentException iae){
			return getElementName(elementClazz) + "/" + element.name();
		}
	} else

	if(attribute != null){
		return getElementName(elementClazz) + "@" + attribute.name();
	}

	throw new IllegalArgumentException();
}
 
Example 24
static
public String formatAttribute(Class<? extends PMMLObject> elementClazz, Field field, Object value){
	XmlAttribute attribute = field.getAnnotation(XmlAttribute.class);

	if(attribute != null){
		return formatElementOrAttribute(elementClazz, field) + (value != null ? ("=" + String.valueOf(value)) : "");
	}

	throw new IllegalArgumentException();
}
 
Example 25
static
private Object ensureValue(HasValue<?> hasValue){
	Object value = hasValue.getValue();
	if(value == null){
		throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement((Class)hasValue.getClass()) + "@value"), (PMMLObject)hasValue);
	}

	return value;
}
 
Example 26
static
public <E extends PMMLObject & HasType<E> & HasExpression<E>> FieldValue evaluateTypedExpressionContainer(E hasTypedExpression, EvaluationContext context){
	FieldValue value = evaluateExpressionContainer(hasTypedExpression, context);

	if(FieldValueUtil.isMissing(value)){
		return FieldValues.MISSING_VALUE;
	}

	return value.cast(hasTypedExpression);
}
 
Example 27
static
public <E extends PMMLObject & HasExpression<E>> Expression ensureExpression(E hasExpression){
	Expression expression = hasExpression.getExpression();
	if(expression == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(hasExpression.getClass()) + "/<Expression>"), hasExpression);
	}

	return expression;
}
 
Example 28
static
public <E extends PMMLObject & HasTable<E>> InlineTable getInlineTable(E object){
	InlineTable inlineTable = object.getInlineTable();

	TableLocator tableLocator = object.getTableLocator();
	if(tableLocator != null){
		throw new UnsupportedElementException(tableLocator);
	}

	return inlineTable;
}
 
Example 29
public ElementFilter(XMLReader reader, Class<? extends PMMLObject> clazz){
	super(reader);

	XmlRootElement element = getElement(clazz);

	setNamespaceURI(element.namespace());
	setLocalName(element.name());
}
 
Example 30
static
private XmlRootElement getElement(Class<? extends PMMLObject> clazz){
	XmlRootElement result = clazz.getAnnotation(XmlRootElement.class);

	if(result == null){
		throw new IllegalArgumentException();
	}

	return result;
}