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

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

There is a newer version: 1.7.2
Show newest version
/*
 * Copyright (c) 2013 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.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import org.dmg.pmml.DataType;
import org.dmg.pmml.FieldName;
import org.dmg.pmml.OpType;

public class EvaluatorUtil {

	private EvaluatorUtil(){
	}

	/**
	 * @see Computable
	 */
	static
	public Object decode(Object object){

		if(object instanceof Computable){
			Computable computable = (Computable)object;

			return computable.getResult();
		} // End if

		if(object instanceof Collection){
			Collection rawValues = (Collection)object;

			Collection decodedValues = createCollection(rawValues);

			for(Object rawValue : rawValues){
				decodedValues.add(decode(rawValue));
			}

			return decodedValues;
		}

		return object;
	}

	/**
	 * Decouples a {@link Map} instance from the current runtime environment by decoding both its keys and values.
	 */
	static
	public Map decode(Map map){
		Map result = new LinkedHashMap<>();

		Collection> entries = map.entrySet();
		for(Map.Entry entry : entries){
			FieldName key = entry.getKey();
			Object value = entry.getValue();

			try {
				result.put(key != null ? key.getValue() : null, decode(value));
			} catch(UnsupportedOperationException uoe){
				// Ignored
			}
		}

		return result;
	}

	static
	public FieldValue prepare(Evaluator evaluator, FieldName name, Object value){

		if(value instanceof Collection){
			Collection rawValues = (Collection)value;

			Collection preparedValues = createCollection(rawValues);

			DataType dataType = null;

			OpType opType = null;

			for(Object rawValue : rawValues){
				FieldValue preparedValue = evaluator.prepare(name, rawValue);
				if(preparedValue != null){

					if(dataType == null){
						dataType = preparedValue.getDataType();
					} // End if

					if(opType == null){
						opType = preparedValue.getOpType();
					}
				}

				preparedValues.add(FieldValueUtil.getValue(preparedValue));
			}

			return FieldValueUtil.create(dataType, opType, preparedValues);
		}

		return evaluator.prepare(name, value);
	}

	static
	public  List> groupRows(K groupKey, List> table){
		Map> groupedRows = new LinkedHashMap<>();

		for(int i = 0; i < table.size(); i++){
			Map row = table.get(i);

			Object groupValue = row.get(groupKey);

			ListMultimap groupedRow = groupedRows.get(groupValue);
			if(groupedRow == null){
				groupedRow = ArrayListMultimap.create();

				groupedRows.put(groupValue, groupedRow);
			}

			Collection> entries = row.entrySet();
			for(Map.Entry entry : entries){
				K key = entry.getKey();
				Object value = entry.getValue();

				groupedRow.put(key, value);
			}
		}

		List> resultTable = new ArrayList<>();

		Collection>> entries = groupedRows.entrySet();
		for(Map.Entry> entry : entries){
			Map resultRow = new LinkedHashMap<>();
			resultRow.putAll((entry.getValue()).asMap());

			// The value of the "group by" column is a single Object, not a Collection (ie. java.util.List) of Objects
			resultRow.put(groupKey, entry.getKey());

			resultTable.add(resultRow);
		}

		return resultTable;
	}

	static
	private Collection createCollection(Collection template){

		// Try to preserve the original contract
		if(template instanceof Set){
			return new LinkedHashSet<>();
		}

		return new ArrayList<>();
	}
}