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

org.jpmml.converter.InteractionFeature Maven / Gradle / Ivy

There is a newer version: 1.5.10
Show newest version
/*
 * Copyright (c) 2016 Villu Ruusmann
 *
 * This file is part of JPMML-Converter
 *
 * JPMML-Converter 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-Converter 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-Converter.  If not, see .
 */
package org.jpmml.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

import org.dmg.pmml.Apply;
import org.dmg.pmml.DataType;
import org.dmg.pmml.Field;
import org.dmg.pmml.FieldName;
import org.jpmml.model.ToStringHelper;

public class InteractionFeature extends Feature {

	private List features = null;


	public InteractionFeature(PMMLEncoder encoder, Field field, List features){
		this(encoder, field.getName(), field.getDataType(), features);
	}

	public InteractionFeature(PMMLEncoder encoder, FieldName name, DataType dataType, List features){
		super(encoder, name, dataType);

		setFeatures(features);
	}

	@Override
	public ContinuousFeature toContinuousFeature(){
		Supplier applySupplier = () -> {
			List features = getFeatures();

			Apply apply = PMMLUtil.createApply("*", ((features.get(0)).toContinuousFeature()).ref(), ((features.get(1)).toContinuousFeature()).ref());

			for(int i = 2; i < features.size(); i++){
				apply = PMMLUtil.createApply("*", apply, ((features.get(i)).toContinuousFeature()).ref());
			}

			return apply;
		};

		return toContinuousFeature(getName(), DataType.DOUBLE, applySupplier);
	}

	@Override
	public int hashCode(){
		return (31 * super.hashCode()) + Objects.hashCode(this.getFeatures());
	}

	@Override
	public boolean equals(Object object){

		if(object instanceof InteractionFeature){
			InteractionFeature that = (InteractionFeature)object;

			return super.equals(object) && Objects.equals(this.getFeatures(), that.getFeatures());
		}

		return false;
	}

	@Override
	protected ToStringHelper toStringHelper(){
		return super.toStringHelper()
			.add("features", getFeatures());
	}

	public List getInputFeatures(){
		List result = new ArrayList<>();

		List features = getFeatures();
		for(Feature feature : features){

			if(feature instanceof InteractionFeature){
				InteractionFeature interactionFeature = (InteractionFeature)feature;

				result.addAll(interactionFeature.getInputFeatures());
			} else

			{
				result.add(feature);
			}
		}

		return result;
	}

	public List getFeatures(){
		return this.features;
	}

	private void setFeatures(List features){
		features = Objects.requireNonNull(features);

		if(features.size() < 2){
			throw new IllegalArgumentException();
		}

		this.features = features;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy