All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jpmml.evaluator.PMMLUtil Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
/*
 * Copyright (c) 2018 Villu Ruusmann
 *
 * This file is part of JPMML-Evaluator
 *
 * JPMML-Evaluator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JPMML-Evaluator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with JPMML-Evaluator.  If not, see .
 */
package org.jpmml.evaluator;

import java.util.List;
import java.util.Objects;

import com.google.common.base.Predicate;
import org.dmg.pmml.Model;
import org.dmg.pmml.PMML;
import org.jpmml.model.MissingElementException;
import org.jpmml.model.XPathUtil;

public class PMMLUtil {

	private PMMLUtil(){
	}

	static
	public Model findModel(PMML pmml, String modelName){
		Model model;

		if(modelName != null){
			Predicate predicate = new Predicate(){

				@Override
				public boolean apply(Model model){
					return Objects.equals(model.getModelName(), modelName);
				}
			};

			model = PMMLUtil.findModel(pmml, predicate, "@modelName=" + modelName);
		} else

		{
			Predicate predicate = new Predicate(){

				@Override
				public boolean apply(Model model){
					return model.isScorable();
				}
			};

			model = PMMLUtil.findModel(pmml, predicate, "@isScorable=true");
		}

		return model;
	}

	static
	public  M findModel(PMML pmml, Class clazz){
		Predicate predicate = new Predicate(){

			@Override
			public boolean apply(Model model){
				return clazz.isInstance(model) && model.isScorable();
			}
		};

		Model model = findModel(pmml, predicate, XPathUtil.formatElement(clazz) + "@isScorable=true");

		return clazz.cast(model);
	}

	static
	private Model findModel(PMML pmml, Predicate predicate, String predicateXPath){

		if(pmml.hasModels()){
			List models = pmml.getModels();

			for(Model model : models){

				if(predicate.apply(model)){
					return model;
				}
			}
		}

		throw new MissingElementException(MissingElementException.formatMessage(XPathUtil.formatElement(pmml.getClass()) + "/" + predicateXPath), pmml);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy