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

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

The 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.ArrayList;
import java.util.List;

import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableRangeSet;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.TreeRangeSet;
import org.dmg.pmml.DataField;
import org.dmg.pmml.DataType;
import org.dmg.pmml.Field;
import org.dmg.pmml.HasContinuousDomain;
import org.dmg.pmml.HasDiscreteDomain;
import org.dmg.pmml.Interval;
import org.dmg.pmml.MiningField;
import org.dmg.pmml.OpType;
import org.dmg.pmml.Target;
import org.dmg.pmml.Value;

public class FieldUtil {

	private FieldUtil(){
	}

	static
	public List getCategories(DataField dataField){
		return CacheUtil.getValue(dataField, FieldUtil.categoryCache);
	}

	static
	public  & HasDiscreteDomain> List getValidValues(F field){
		return CacheUtil.getValue(field, FieldUtil.validValueCache);
	}

	static
	public  & HasContinuousDomain> RangeSet getValidRanges(F field){
		return CacheUtil.getValue(field, FieldUtil.validRangeCache);
	}

	static
	public OpType getOpType(Field field, MiningField miningField){

		// "A MiningField overrides a (Data)Field"
		if(miningField != null){
			OpType opType = miningField.getOpType();

			if(opType != null){
				return opType;
			}
		}

		return field.requireOpType();
	}

	static
	public OpType getOpType(Field field, MiningField miningField, Target target){

		// "A MiningField overrides a (Data)Field, and a Target overrides a MiningField"
		if(miningField != null){
			OpType opType = miningField.getOpType();

			if(target != null){
				opType = target.getOpType(opType);
			} // End if

			if(opType != null){
				return opType;
			}
		}

		return field.requireOpType();
	}

	static
	public DataType getDataType(Field field){
		return field.requireDataType();
	}

	static
	private List parseCategories(DataField dataField){
		List result = new ArrayList<>();

		if(dataField.hasValues()){
			List pmmlValues = dataField.getValues();

			for(Value pmmlValue : pmmlValues){
				Object objectValue = pmmlValue.requireValue();

				Value.Property property = pmmlValue.getProperty();
				switch(property){
					case VALID:
						result.add(objectValue);
						break;
					default:
						break;
				}
			}
		}

		return result;
	}

	static
	private  & HasDiscreteDomain> List parseValidValues(F field){
		List result = new ArrayList<>();

		DataType dataType = field.requireDataType();

		if(field.hasValues()){
			List pmmlValues = field.getValues();

			for(Value pmmlValue : pmmlValues){
				Object objectValue = pmmlValue.requireValue();

				Value.Property property = pmmlValue.getProperty();
				switch(property){
					case VALID:
						result.add(TypeUtil.parseOrCast(dataType, objectValue));
						break;
					default:
						break;
				}
			}
		}

		return result;
	}

	static
	private  & HasContinuousDomain> RangeSet parseValidRanges(F field){
		RangeSet result = TreeRangeSet.create();

		if(field.hasIntervals()){
			List intervals = field.getIntervals();

			for(Interval interval : intervals){
				Range range = DiscretizationUtil.toRange(interval);

				result.add(range);
			}
		}

		return result;
	}

	private static final LoadingCache> categoryCache = CacheUtil.buildLoadingCache(new CacheLoader>(){

		@Override
		public List load(DataField dataField){
			return ImmutableList.copyOf(parseCategories(dataField));
		}
	});

	private static final LoadingCache, List> validValueCache = CacheUtil.buildLoadingCache(new CacheLoader, List>(){

		@Override
		public List load(Field field){
			return ImmutableList.copyOf(parseValidValues((Field & HasDiscreteDomain)field));
		}
	});

	private static final LoadingCache, RangeSet> validRangeCache = CacheUtil.buildLoadingCache(new CacheLoader, RangeSet>(){

		@Override
		public RangeSet load(Field field){
			return ImmutableRangeSet.copyOf(parseValidRanges((Field & HasContinuousDomain)field));
		}
	});
}