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.8
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.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

abstract
public class EvaluationContext {

	private Map fields = new LinkedHashMap<>();

	private List warnings = new ArrayList<>();


	abstract
	public Result resolveDerivedField(FieldName name);

	abstract
	public Result resolveFunction(String name);

	/**
	 * @see #getFieldEntry(FieldName)
	 */
	public FieldValue getField(FieldName name){
		Map.Entry entry = getFieldEntry(name);
		if(entry != null){
			return entry.getValue();
		}

		return null;
	}

	public Map.Entry getFieldEntry(FieldName name){
		Map fields = getFields();

		if(fields.containsKey(name)){
			Map.Entry entry = new AbstractMap.SimpleEntry<>(name, fields.get(name));

			return entry;
		}

		return null;
	}

	public void declare(FieldName name, Object value){

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

			return;
		}

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

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

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

		fields.put(name, value);
	}

	public void declareAll(Map fields){
		declareAll(fields.keySet(), fields);
	}

	public void declareAll(Collection names, Map fields){

		for(FieldName name : names){
			declare(name, fields.get(name));
		}
	}

	public FieldValue createFieldValue(FieldName name, Object value){
		return FieldValueUtil.create(value);
	}

	public void addWarning(String warning){
		List warnings = getWarnings();

		warnings.add(warning);
	}

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

	public List getWarnings(){
		return this.warnings;
	}

	 Result createResult(E element){

		if(element != null){
			return new Result<>(element);
		}

		return null;
	}

	public class Result {

		private E element = null;


		Result(E element){
			setElement(element);
		}

		public EvaluationContext getContext(){
			return EvaluationContext.this;
		}

		public E getElement(){
			return this.element;
		}

		private void setElement(E element){
			this.element = element;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy