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

org.jpmml.translator.tree.DiscreteValueFinder Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import org.dmg.pmml.ComplexArray;
import org.dmg.pmml.SimplePredicate;
import org.dmg.pmml.SimpleSetPredicate;
import org.dmg.pmml.VisitorAction;
import org.jpmml.model.visitors.AbstractVisitor;

public class DiscreteValueFinder extends AbstractVisitor {

	private Map> fieldValues = new LinkedHashMap<>();


	@Override
	public VisitorAction visit(SimplePredicate simplePredicate){
		String fieldName = simplePredicate.requireField();
		SimplePredicate.Operator operator = simplePredicate.requireOperator();

		switch(operator){
			case EQUAL:
			case NOT_EQUAL:
				Object value = simplePredicate.requireValue();

				addValue(fieldName, value);
				break;
			default:
				break;
		}

		return super.visit(simplePredicate);
	}

	@Override
	public VisitorAction visit(SimpleSetPredicate simpleSetPredicate){
		String fieldName = simpleSetPredicate.requireField();
		SimpleSetPredicate.BooleanOperator booleanOperator = simpleSetPredicate.requireBooleanOperator();
		ComplexArray array = (ComplexArray)simpleSetPredicate.requireArray();

		Collection values = array.getValue();

		switch(booleanOperator){
			case IS_IN:
			case IS_NOT_IN:
				for(Object value : values){
					addValue(fieldName, value);
				}
				break;
			default:
				break;
		}

		return super.visit(simpleSetPredicate);
	}

	public void addValue(String fieldName, Object value){
		Map> fieldValues = getFieldValues();

		Set values = fieldValues.get(fieldName);
		if(values == null){
			values = new LinkedHashSet<>();

			fieldValues.put(fieldName, values);
		}

		values.add(value);
	}

	public Map> getFieldValues(){
		return this.fieldValues;
	}
}