001/* Copyright (C) 2013 TU Dortmund
002 This file is part of LearnLib
003
004 LearnLib is free software; you can redistribute it and/or
005 modify it under the terms of the GNU Lesser General Public
006 License version 3.0 as published by the Free Software Foundation.
007
008 LearnLib is distributed in the hope that it will be useful,
009 but WITHOUT ANY WARRANTY; without even the implied warranty of
010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 Lesser General Public License for more details.
012
013 You should have received a copy of the GNU Lesser General Public
014 License along with LearnLib; if not, see
015 <http://www.gnu.de/documents/lgpl.en.html>.  */
016package de.learnlib.oracles;
017
018import de.learnlib.api.MembershipOracle;
019import de.learnlib.api.Query;
020import de.learnlib.statistics.HistogramDataSet;
021import de.learnlib.statistics.StatisticOracle;
022import java.util.Collection;
023
024/**
025 * Collects a histogram of passed query lengths.
026 *
027 * @param <I> input symbol class
028 * @param <O> output symbol class
029 *
030 * @author falkhowar
031 */
032public class HistogramOracle<I, O> implements StatisticOracle<I, O> {
033
034    /**
035     * dataset to be collected.
036     */
037    private final HistogramDataSet dataSet;
038
039    /**
040     * oracle used to answer queries.
041     */
042    private MembershipOracle<I, O> nextOracle;
043
044    /**
045     *
046     * @param next real oracle
047     * @param name name of the collected data set
048     */
049    public HistogramOracle(final MembershipOracle<I, O> next, final String name) {
050        this.nextOracle = next;
051        this.dataSet = new HistogramDataSet(name, "query length");
052    }
053
054    @Override
055    public final void processQueries(final Collection<? extends Query<I, O>> queries) {
056        for (Query<I, O> q : queries) {
057            this.dataSet.addDataPoint((long) q.getInput().size());
058        }
059        nextOracle.processQueries(queries);
060    }
061
062    /**
063     * @return the data set collected by this oracle.
064     */
065    @Override
066    public final HistogramDataSet getStatisticalData() {
067        return this.dataSet;
068    }
069
070    /**
071     * set used oracle.
072     *
073     * @param next oracle to be used
074     */
075    @Override
076    public final void setNext(final MembershipOracle<I, O> next) {
077        this.nextOracle = next;
078    }
079}