001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of LearnLib, http://www.learnlib.de/.
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>.
016 */
017package de.learnlib.cache.mealy;
018
019import java.util.Collection;
020
021import net.automatalib.automata.transout.MealyMachine;
022import net.automatalib.incremental.mealy.IncrementalMealyBuilder;
023import net.automatalib.words.Word;
024import net.automatalib.words.WordBuilder;
025import de.learnlib.api.EquivalenceOracle;
026import de.learnlib.oracles.DefaultQuery;
027
028/**
029 * An {@link EquivalenceOracle} that tests an hypothesis for consistency with the
030 * contents of a {@link MealyCacheOracle}.
031 * 
032 * @author Malte Isberner <malte.isberner@gmail.com>
033 *
034 * @param <I> input symbol class
035 * @param <O> output symbol class
036 */
037public class MealyCacheConsistencyTest<I, O> implements
038                EquivalenceOracle<MealyMachine<?,I,?,O>, I, Word<O>> {
039        
040        private final IncrementalMealyBuilder<I, O> incMealy;
041        
042        /**
043         * Constructor.
044         * @param incMealy the {@link IncrementalMealyBuilder} data structure underlying the
045         * cache.
046         */
047        public MealyCacheConsistencyTest(IncrementalMealyBuilder<I, O> incMealy) {
048                this.incMealy = incMealy;
049        }
050
051        /*
052         * (non-Javadoc)
053         * @see de.learnlib.api.EquivalenceOracle#findCounterExample(java.lang.Object, java.util.Collection)
054         */
055        @Override
056        public DefaultQuery<I, Word<O>> findCounterExample(
057                        MealyMachine<?, I, ?, O> hypothesis, Collection<? extends I> inputs) {
058                Word<I> w = incMealy.findSeparatingWord(hypothesis, inputs, false);
059                if(w == null)
060                        return null;
061                WordBuilder<O> wb = new WordBuilder<O>(w.length());
062                incMealy.lookup(w, wb);
063                DefaultQuery<I,Word<O>> result = new DefaultQuery<>(w);
064                result.answer(wb.toWord());
065                return result;
066        }
067
068}