org.dmg.pmml.PMMLObject Java Examples

The following examples show how to use org.dmg.pmml.PMMLObject. 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: ServiceLoaderUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #3
Source File: JavaModel.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #4
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
@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 #5
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(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 #6
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 #7
Source File: MemoryMeasurer.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #8
Source File: TreePathFinder.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #9
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 #10
Source File: ReflectionUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 #11
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 #12
Source File: CacheUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
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 #13
Source File: IndexableUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
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 #14
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 #15
Source File: FieldValue.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * <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
Source File: NodeScoreParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PMMLObject popParent(){
	PMMLObject parent = super.popParent();

	if(parent instanceof TreeModel){
		this.mathContext = null;
	}

	return parent;
}
 
Example #17
Source File: PredicateUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public <E extends PMMLObject & HasPredicate<E>> Predicate ensurePredicate(E hasPredicate){
	Predicate predicate = hasPredicate.getPredicate();
	if(predicate == null){
		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(hasPredicate.getClass()) + "/<Predicate>"), hasPredicate);
	}

	return predicate;
}
 
Example #18
Source File: NodeScoreParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void pushParent(PMMLObject parent){
	super.pushParent(parent);

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

		this.mathContext = treeModel.getMathContext();
	}
}
 
Example #19
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 #20
Source File: ValueParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private <E extends PMMLObject & HasValue<E>> void parseValue(FieldName name, E hasValue){
	Object value = hasValue.getValue();
	if(value == null){
		throw new MissingAttributeException(MissingAttributeException.formatMessage(XPathUtil.formatElement(hasValue.getClass()) + "@value"), hasValue);
	}

	DataType dataType = resolveDataType(name);
	if(dataType != null){
		value = parseOrCast(dataType, value);

		hasValue.setValue(value);
	}
}
 
Example #21
Source File: ElementFilter.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ElementFilter(XMLReader reader, Class<? extends PMMLObject> clazz){
	super(reader);

	XmlRootElement element = getElement(clazz);

	setNamespaceURI(element.namespace());
	setLocalName(element.name());
}
 
Example #22
Source File: TargetCategoryParser.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public PMMLObject popParent(){
	PMMLObject parent = super.popParent();

	if(parent instanceof Model){
		this.targetDataTypes.pop();

		this.dataType = null;
	}

	return parent;
}
 
Example #23
Source File: PMMLException.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
public PMMLException ensureContext(PMMLObject parentContext){
	PMMLObject context = getContext();

	if(context == null){
		setContext(parentContext);
	}

	return this;
}
 
Example #24
Source File: InvalidMarkupInspector.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setObject(PMMLObject object){

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

			this.object = object;
		}
 
Example #25
Source File: IndexableUtil.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
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 #26
Source File: JacksonUtil.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
public void write(PMMLObject object, OutputStream os) throws IOException {
	ObjectMapper objectMapper = createObjectMapper(null);

	ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();

	objectWriter.writeValue(os, object);
}
 
Example #27
Source File: VersionInspectorTest.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private void assertVersionRange(PMMLObject object, Version minimum, Version maximum){
	VersionInspector inspector = new VersionInspector();
	inspector.applyTo(object);

	assertEquals(Arrays.asList(minimum, maximum), Arrays.asList(inspector.getMinimum(), inspector.getMaximum()));
}
 
Example #28
Source File: SimpleNode.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public VisitorAction accept(Visitor visitor){
	VisitorAction status = visitor.visit(this);

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

		if(status == VisitorAction.CONTINUE && hasExtensions()){
			status = PMMLObject.traverse(visitor, getExtensions());
		} // End if

		if(status == VisitorAction.CONTINUE){
			status = PMMLObject.traverse(visitor, getPredicate(), getPartition());
		} // End if

		if(status == VisitorAction.CONTINUE && hasScoreDistributions()){
			status = PMMLObject.traverse(visitor, getScoreDistributions());
		} // End if

		if(status == VisitorAction.CONTINUE && hasNodes()){
			status = PMMLObject.traverse(visitor, getNodes());
		} // End if

		if(status == VisitorAction.CONTINUE){
			status = PMMLObject.traverse(visitor, getEmbeddedModel());
		}

		visitor.popParent();
	} // End if

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

	return VisitorAction.CONTINUE;
}
 
Example #29
Source File: ElementFilter.java    From jpmml-model with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static
private XmlRootElement getElement(Class<? extends PMMLObject> clazz){
	XmlRootElement result = clazz.getAnnotation(XmlRootElement.class);

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

	return result;
}
 
Example #30
Source File: UnsupportedElementException.java    From jpmml-evaluator with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private String formatElement(PMMLObject object){
	Class<? extends PMMLObject> clazz = object.getClass();

	String result = XPathUtil.formatElement(clazz);

	String name = clazz.getName();
	if(!name.startsWith("org.dmg.pmml.")){
		result += (" (Java class " + name + ")");
	}

	return result;
}