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.extractors;
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.Example;
023import org.tribuo.data.columnar.ColumnarIterator;
024import org.tribuo.data.columnar.FieldExtractor;
025
026import java.util.Optional;
027
028/**
029 * An Extractor with special casing for loading the index from a Row.
030 * The index is written out as a Long.
031 * <p>
032 * This is the row wise count, i.e., the number of examples that the data
033 * source has processed, rather than anything extracted from the data.
034 */
035public class IndexExtractor implements FieldExtractor<Long> {
036
037    @Config(description = "The metadata key to emit, defaults to Example.NAME")
038    private String metadataName = Example.NAME;
039
040    /**
041     * Extracts the index, writing to the supplied metadata field name.
042     * @param metadataName The metadata field to write to.
043     */
044    public IndexExtractor(String metadataName) {
045        this.metadataName = metadataName;
046    }
047
048    /**
049     * Extracts the index writing to the default metadata field name {@link Example#NAME}.
050     */
051    public IndexExtractor() {}
052
053    @Override
054    public String getMetadataName() {
055        return metadataName;
056    }
057
058    @Override
059    public Class<Long> getValueType() {
060        return Long.class;
061    }
062
063    @Override
064    public Optional<Long> extract(ColumnarIterator.Row row) {
065        return row.getIndex() == -1 ? Optional.empty() : Optional.of(row.getIndex());
066    }
067
068    @Override
069    public ConfiguredObjectProvenance getProvenance() {
070        return new ConfiguredObjectProvenanceImpl(this, "IndexExtractor");
071    }
072}