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.anomaly;
018
019import com.oracle.labs.mlrg.olcut.util.Pair;
020import org.tribuo.ImmutableOutputInfo;
021import org.tribuo.anomaly.Event.EventType;
022
023import java.util.ArrayList;
024import java.util.Iterator;
025import java.util.List;
026import java.util.logging.Level;
027import java.util.logging.Logger;
028
029/**
030 * An {@link ImmutableOutputInfo} object for {@link Event}s.
031 * <p>
032 * The ids are predefined for {@link Event} in the Event class itself.
033 */
034public final class ImmutableAnomalyInfo extends AnomalyInfo implements ImmutableOutputInfo<Event> {
035    private static final long serialVersionUID = 1L;
036
037    private static final Logger logger = Logger.getLogger(ImmutableAnomalyInfo.class.getName());
038
039    ImmutableAnomalyInfo(AnomalyInfo info) {
040        super(info);
041    }
042
043    @Override
044    public int getID(Event output) {
045        return output.getType().getID();
046    }
047
048    @Override
049    public Event getOutput(int id) {
050        if (id == EventType.ANOMALOUS.getID()) {
051            return AnomalyFactory.ANOMALOUS_EVENT;
052        } else if (id == EventType.EXPECTED.getID()) {
053            return AnomalyFactory.EXPECTED_EVENT;
054        } else {
055            logger.log(Level.INFO,"No entry found for id " + id);
056            return null;
057        }
058    }
059
060    @Override
061    public long getTotalObservations() {
062        return anomalyCount + expectedCount;
063    }
064
065    @Override
066    public ImmutableAnomalyInfo copy() {
067        return new ImmutableAnomalyInfo(this);
068    }
069
070    @Override
071    public Iterator<Pair<Integer, Event>> iterator() {
072        List<Pair<Integer,Event>> list = new ArrayList<>();
073
074        list.add(new Pair<>(AnomalyFactory.ANOMALOUS_EVENT.getType().getID(),AnomalyFactory.ANOMALOUS_EVENT));
075        list.add(new Pair<>(AnomalyFactory.EXPECTED_EVENT.getType().getID(),AnomalyFactory.EXPECTED_EVENT));
076
077        return list.iterator();
078    }
079}