Java Code Examples for org.jpmml.evaluator.Evaluator#evaluate()

The following examples show how to use org.jpmml.evaluator.Evaluator#evaluate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AssociationSchemaTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 6 votes vote down vote up
private void evaluate(List<String> items, List<String> recommendations, List<String> exclusiveRecommendations, List<String> ruleAssociations) throws Exception {
	Evaluator evaluator = createModelEvaluator();

	checkTargetFields(Collections.emptyList(), evaluator);

	Map<FieldName, ?> arguments = createItemArguments(items);

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

	assertEquals(recommendations, getOutput(results, "Recommendation"));
	assertEquals(exclusiveRecommendations, getOutput(results, "Exclusive_Recommendation"));
	assertEquals(ruleAssociations, getOutput(results, "Rule_Association"));

	assertEquals(Iterables.getFirst(recommendations, null), getOutput(results, "Top Recommendation"));
	assertEquals(Iterables.getFirst(exclusiveRecommendations, null), getOutput(results, "Top Exclusive_Recommendation"));
	assertEquals(Iterables.getFirst(ruleAssociations, null), getOutput(results, "Top Rule_Association"));
}
 
Example 2
Source File: PmmlProcessorConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
@ServiceActivator(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object evaluate(Message<?> input) {
	Model model = selectModel(input);
	Evaluator evaluator = modelEvaluatorFactory.newModelManager(pmml, model);

	Map<FieldName, FieldValue> arguments = new LinkedHashMap<>();

	List<FieldName> activeFields = evaluator.getActiveFields();
	for (FieldName activeField : activeFields) {
		// The raw (ie. user-supplied) value could be any Java primitive value
		Object rawValue = resolveActiveValue(input, activeField.getValue());

		// The raw value is passed through:
		// 1) outlier treatment,
		// 2) missing value treatment,
		// 3) invalid value treatment
		// and 4) type conversion
		FieldValue activeValue = evaluator.prepare(activeField, rawValue);

		arguments.put(activeField, activeValue);
	}

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

	MutableMessage<?> result = convertToMutable(input);

	for (Map.Entry<FieldName, ?> entry : results.entrySet()) {

                       String fieldName = null;
                       if (entry.getKey()==null)
                           fieldName = DEFAULT_OUTPUT_FIELD;
                       else 
		    fieldName = entry.getKey().getValue();

		Expression expression = properties.getOutputs().get(fieldName);
		if (expression == null) {
			expression = spelExpressionParser.parseExpression("payload." + fieldName);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("Setting result field named " + fieldName + " using SpEL[" + expression + " = "
					+ entry.getValue() + "]");
		}
		expression.setValue(evaluationContext, result, entry.getValue());
	}

	return result;

}
 
Example 3
Source File: SelectAllTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluate() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("sepal_length", 5.1d, "sepal_width", 3.5d, "petal_length", 1.4d, "petal_width", 0.2d);

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

	assertEquals(1, results.size());

	Collection<?> species = (Collection<?>)results.get(FieldName.create("species"));

	assertEquals(5, species.size());

	for(Object value : species){
		assertTrue((value instanceof Computable) & (value instanceof HasEntityId));
	}

	assertEquals(Arrays.asList("setosa", "setosa", "setosa", "setosa", "versicolor"), EvaluatorUtil.decode(species));
}
 
Example 4
Source File: FieldScopeTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 4 votes vote down vote up
@Test
public void evaluate() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("input", null);

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

	assertEquals(1000d, getTarget(results, "prediction"));

	arguments = createArguments("input", 1d);

	results = evaluator.evaluate(arguments);

	assertEquals(1d, getTarget(results, "prediction"));
}
 
Example 5
Source File: ModelChainTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
public Map<FieldName, ?> evaluateExample(double petalLength, double petalWidth) throws Exception {
	ConfigurationBuilder configurationBuilder = new ConfigurationBuilder()
		.setOutputFilter(OutputFilters.KEEP_FINAL_RESULTS);

	Configuration configuration = configurationBuilder.build();

	Evaluator evaluator = createModelEvaluator(configuration);

	Map<FieldName, ?> arguments = createArguments("petal_length", petalLength, "petal_width", petalWidth, "temperature", 0d, "cloudiness", 0d);

	return evaluator.evaluate(arguments);
}
 
Example 6
Source File: ComplexPartialScoreTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
private Double evaluateScore(Double income) throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("department", null, "age", null, "income", income);

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

	return (Double)getOutput(results, "Final Score");
}
 
Example 7
Source File: ReasonCodeTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
public Map<FieldName, ?> evaluateExample() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("department", "engineering", "age", 25, "income", 500d);

	return evaluator.evaluate(arguments);
}
 
Example 8
Source File: EmptyPPMatrixTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void evaluate() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments();

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

	assertEquals((Double)0d, getTarget(results, "high_time"));
}
 
Example 9
Source File: TieBreakTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void firstLevel() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("input", 1.5d);

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

	assertEquals("medium", getTarget(results, "output"));
}
 
Example 10
Source File: TieBreakTest.java    From jpmml-evaluator with GNU Affero General Public License v3.0 3 votes vote down vote up
@Test
public void secondLevel() throws Exception {
	Evaluator evaluator = createModelEvaluator();

	Map<FieldName, ?> arguments = createArguments("input", 3.5d);

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

	assertEquals("high", getTarget(results, "output"));
}
 
Example 11
Source File: ModelResource.java    From openscoring with GNU Affero General Public License v3.0 3 votes vote down vote up
static
protected EvaluationResponse evaluate(Evaluator evaluator, EvaluationRequest request){
	logger.info("Received {}", request);

	Map<String, ?> requestArguments = request.getArguments();

	EvaluationResponse response = new EvaluationResponse(request.getId());

	Map<FieldName, FieldValue> arguments = new LinkedHashMap<>();

	List<InputField> inputFields = evaluator.getInputFields();
	for(InputField inputField : inputFields){
		FieldName inputName = inputField.getName();

		String key = inputName.getValue();

		Object value = requestArguments.get(key);
		if(value == null && !requestArguments.containsKey(key)){
			logger.warn("Evaluation request {} does not specify an input field {}", request.getId(), key);
		}

		FieldValue inputValue = inputField.prepare(value);

		arguments.put(inputName, inputValue);
	}

	logger.debug("Evaluation request {} has prepared arguments: {}", request.getId(), arguments);

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

	logger.debug("Evaluation response {} has result: {}", response.getId(), results);

	response.setResults(EvaluatorUtil.decodeAll(results));

	logger.info("Returned {}", response);

	return response;
}