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

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

There is a newer version: 1.6.6
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.Map;

import org.dmg.pmml.FieldName;
import org.dmg.pmml.MiningFunction;
import org.dmg.pmml.Model;

/**
 * 

* Performs the evaluation of a {@link Model}. *

* *

Building and verifying an Evaluator instance

*
 * PMML pmml = ...;
 * EvaluatorBuilder evaluatorBuilder = new ModelEvaluatorBuilder(pmml);
 * Evaluator evaluator = evaluatorBuilder.build();
 * evaluator.verify();
 * 
* *

Preparing arguments

* Transforming an user-supplied map of arguments to a known-good PMML map of arguments: *
 * Map<String, ?> userArguments = ...;
 * Map<FieldName, FieldValue> arguments = new LinkedHashMap<>();
 * List<? extends InputField> inputFields = evaluator.getInputFields();
 * for(InputField inputField : inputFields){
 *   FieldName inputName = inputField.getName();
 *   Object rawValue = userArguments.get(inputName.getValue());
 *   FieldValue inputValue = inputField.prepare(rawValue);
 *   arguments.put(inputName, inputValue);
 * }
 * 
* *

Performing the evaluation

*
 * Map<FieldName, ?> results = evaluator.evaluate(arguments);
 * 
* *

Processing results

* Retrieving the values of {@link #getTargetFields() target fields} (ie. primary results): *
 * List<? extends TargetField> targetFields = evaluator.getTargetFields();
 * for(TargetField targetField : targetFields){
 *   FieldName targetName = targetField.getName();
 *   Object targetValue = results.get(targetName);
 * }
 * 
* * Decoding a {@link Computable complex value} to a Java primitive value: *
 * if(targetValue instanceof Computable){
 *   Computable computable = (Computable)targetValue;
 *   targetValue = computable.getResult();
 * }
 * 
* * Retrieving the values of {@link #getOutputFields() output fields} (ie. secondary results): *
 * List<? extends OutputField> outputFields = evaluator.getOutputFields();
 * for(OutputField outputField : outputFields){
 *   FieldName outputName = outputField.getName();
 *   Object outputValue = results.get(outputName);
 * }
 * 
* *

Handling exceptions

* A code block that does exception-prone work should be surrounded with two levels of try-catch statements. * The inner try statement should catch {@link EvaluationException} instances that indicate "local" problems, which are related to individual data records. * The outer try statement should catch {@link InvalidMarkupException} and {@link UnsupportedMarkupException} instances that indicate "global" problems, which are related to the class model object. *
 * try {
 *   List<Map<String, ?>> records = ...;
 *   for(Map<String, ?> record : records){
 *     try {
 *       // Do exception-prone work
 *     } catch(EvaluationException ee){
 *       // The work failed because of the data record.
 *       // Skip this data record and proceed as usual with the next one
 *     }
 *   }
 * } catch(InvalidMarkupException | UnsupportedMarkupException me){
 *   // The work failed because of the class model object.
 *   // This is a persistent problem that is very likely to affect all data records
 *   // Decommission the Evaluator instance
 * }
 * 
* * @see EvaluatorUtil * * @see HasGroupFields * @see HasOrderFields * * @see HasModel * @see HasPMML */ public interface Evaluator extends HasInputFields, HasResultFields { /** *

* Gets a short description of the {@link Model}. *

*/ String getSummary(); /** *

* Gets the type of the {@link Model}. *

*/ MiningFunction getMiningFunction(); /** *

* Verifies the model. *

* * @throws EvaluationException If the verification fails. * @throws InvalidMarkupException * @throws UnsupportedMarkupException */ Evaluator verify(); /** *

* Evaluates the model with the specified arguments. *

* * @param arguments Map of {@link #getInputFields() input field} values. * * @return Map of {@link #getTargetFields() target field} and {@link #getOutputFields() output field} values. * A target field could be mapped to a complex value or a simple value. * An output field is always mapped to a simple value. * Complex values are represented as instances of {@link Computable} that return simple values. * Simple values are represented using the Java equivalents of PMML data types (eg. String, Integer, Float, Double etc.). * A missing value is represented by null. * * @throws EvaluationException If the evaluation fails. * @throws InvalidMarkupException * @throws UnsupportedMarkupException * * @see Computable */ Map evaluate(Map arguments); /** *

* The name of the default target field. *

*/ FieldName DEFAULT_TARGET_NAME = null; }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy