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

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

There is a newer version: 1.6.11
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.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dmg.pmml.DefineFunction;
import org.dmg.pmml.FieldName;

abstract
public class EvaluationContext {

	private Map fields = new HashMap<>();

	private List warnings = null;


	EvaluationContext(){
	}

	abstract
	protected FieldValue createFieldValue(FieldName name, Object value);

	void reset(){
		this.fields.clear();

		if(this.warnings != null){
			this.warnings.clear();
		}
	}

	public FieldValue evaluate(FieldName name){
		Map fields = getFields();

		if(fields.size() > 0){
			FieldValue value = fields.get(name);

			if((value != null) || (value == null && fields.containsKey(name))){
				return value;
			}
		}

		return resolve(name);
	}

	protected FieldValue resolve(FieldName name){
		throw new MissingFieldException(name);
	}

	public FieldValue getField(FieldName name){
		Map fields = getFields();

		return fields.get(name);
	}

	public FieldValue declare(FieldName name, Object value){

		if(value instanceof FieldValue){
			return declare(name, (FieldValue)value);
		}

		return declare(name, createFieldValue(name, value));
	}

	public FieldValue declare(FieldName name, FieldValue value){
		Map fields = getFields();

		boolean declared = fields.containsKey(name);
		if(declared){
			throw new DuplicateValueException(name);
		}

		fields.put(name, value);

		return value;
	}

	protected void declareAll(Map values){
		Collection> entries = values.entrySet();

		for(Map.Entry entry : entries){
			declare(entry.getKey(), entry.getValue());
		}
	}

	protected DefineFunction getDefineFunction(String name){
		throw new UnsupportedOperationException();
	}

	public void addWarning(String warning){

		if(this.warnings == null){
			this.warnings = new ArrayList<>();
		}

		this.warnings.add(warning);
	}

	public Map getFields(){
		return this.fields;
	}

	public List getWarnings(){

		if(this.warnings == null){
			return Collections.emptyList();
		}

		return this.warnings;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy