001/*
002 * Copyright (c) 2015-2020, Oracle and/or its affiliates. All rights reserved.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.tribuo.data.columnar.processors.response;
018
019import com.oracle.labs.mlrg.olcut.config.Config;
020import com.oracle.labs.mlrg.olcut.provenance.ConfiguredObjectProvenance;
021import com.oracle.labs.mlrg.olcut.provenance.impl.ConfiguredObjectProvenanceImpl;
022import org.tribuo.Output;
023import org.tribuo.OutputFactory;
024import org.tribuo.data.columnar.ResponseProcessor;
025
026import java.util.Optional;
027
028/**
029 * A response processor that returns the value in a given field.
030 */
031public class FieldResponseProcessor<T extends Output<T>> implements ResponseProcessor<T> {
032
033    @Config(mandatory = true,description="The field name to read.")
034    private String fieldName;
035
036    @Config(mandatory = true,description="Default value to return if one isn't found.")
037    private String defaultValue;
038
039    @Config(mandatory = true,description="The output factory to use.")
040    private OutputFactory<T> outputFactory;
041
042    /**
043     * For olcut.
044     */
045    private FieldResponseProcessor() {}
046
047    /**
048     * Constructs a response processor which passes the field value through the
049     * output factory.
050     * @param fieldName The field to read.
051     * @param defaultValue The default value to extract if it's not found.
052     * @param outputFactory The output factory to use.
053     */
054    public FieldResponseProcessor(String fieldName, String defaultValue, OutputFactory<T> outputFactory) {
055        this.fieldName = fieldName;
056        this.defaultValue = defaultValue;
057        this.outputFactory = outputFactory;
058    }
059
060    @Deprecated
061    @Override
062    public void setFieldName(String fieldName) {
063        this.fieldName = fieldName;
064    }
065
066    @Override
067    public OutputFactory<T> getOutputFactory() {
068        return outputFactory;
069    }
070
071    @Override
072    public String getFieldName() {
073        return fieldName;
074    }
075
076    @Override
077    public Optional<T> process(String value) {
078        String val = value == null ? defaultValue : value;
079        if (val != null) {
080            val = val.toUpperCase().trim();
081            if (val.isEmpty()) {
082                return Optional.empty();
083            } else{
084                return Optional.of(outputFactory.generateOutput(val));
085            }
086        } else {
087            return Optional.empty();
088        }
089    }
090
091    @Override
092    public String toString() {
093        return "FieldResponseProcessor(fieldName="+ fieldName +")";
094    }
095
096    @Override
097    public ConfiguredObjectProvenance getProvenance() {
098        return new ConfiguredObjectProvenanceImpl(this,"ResponseProcessor");
099    }
100}