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

org.jpmml.evaluator.mining.SegmentationResult Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
/*
 * Copyright (c) 2024 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.mining;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

import com.google.common.collect.BiMap;
import com.google.common.collect.ForwardingMap;
import org.dmg.pmml.mining.Segment;
import org.jpmml.evaluator.HasEntityRegistry;
import org.jpmml.evaluator.TypeUtil;

abstract
public class SegmentationResult extends ForwardingMap implements HasEntityRegistry, HasSegmentResults {

	private Map results = null;


	SegmentationResult(Map results){
		setResults(results);
	}

	@Override
	@SuppressWarnings({"rawtypes", "unchecked"})
	public Map delegate(){
		Map results = getResults();

		return (Map)results;
	}

	public Map getResults(Iterable segmentIds){
		Iterator it = segmentIds.iterator();

		String segmentId = it.next();

		SegmentResult segmentResult = getSegmentResult(segmentId);
		if(segmentResult == null){
			return null;
		}

		while(it.hasNext()){
			SegmentationResult segmentationResult = TypeUtil.cast(SegmentationResult.class, segmentResult.getResults());

			segmentId = it.next();

			segmentResult = segmentationResult.getSegmentResult(segmentId);
			if(segmentResult == null){
				return null;
			}
		}

		return segmentResult.getResults();
	}

	SegmentResult getSegmentResult(String id){
		BiMap entityRegistry = getEntityRegistry();

		if(!entityRegistry.containsKey(id)){
			throw new IllegalArgumentException(id);
		}

		Collection segmentResults = getSegmentResults();
		if(segmentResults != null && !segmentResults.isEmpty()){

			for(SegmentResult segmentResult : segmentResults){

				if(Objects.equals(segmentResult.getEntityId(), id)){
					return segmentResult;
				}
			}
		}

		return null;
	}

	public Map getResults(){
		return this.results;
	}

	void setResults(Map results){
		this.results = Objects.requireNonNull(results);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy