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.dfa;
018
019import java.util.Collection;
020
021import net.automatalib.automata.fsa.DFA;
022import net.automatalib.incremental.dfa.AbstractIncrementalDFABuilder;
023import net.automatalib.incremental.dfa.Acceptance;
024import net.automatalib.incremental.dfa.IncrementalDFABuilder;
025import net.automatalib.words.Word;
026import de.learnlib.api.EquivalenceOracle;
027import de.learnlib.api.EquivalenceOracle.DFAEquivalenceOracle;
028import de.learnlib.oracles.DefaultQuery;
029
030/**
031 * An {@link EquivalenceOracle} that tests an hypothesis for consistency with the
032 * contents of a {@link DFACacheOracle}.
033 * 
034 * @author Malte Isberner <malte.isberner@gmail.com>
035 *
036 * @param <I> input symbol class
037 */
038public final class DFACacheConsistencyTest<I> implements
039                DFAEquivalenceOracle<I> {
040        
041        private final AbstractIncrementalDFABuilder<I> incDfa;
042        
043        /**
044         * Constructor.
045         * @param incDfa the {@link IncrementalDFABuilder} data structure of the cache
046         */
047        public DFACacheConsistencyTest(AbstractIncrementalDFABuilder<I> incDfa) {
048                this.incDfa = incDfa;
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, Boolean> findCounterExample(DFA<?, I> hypothesis,
057                        Collection<? extends I> inputs) {
058                Word<I> w = incDfa.findSeparatingWord(hypothesis, inputs, false);
059                if(w == null)
060                        return null;
061                Acceptance acc = incDfa.lookup(w);
062                assert (acc != Acceptance.DONT_KNOW);
063                
064                Boolean out = (acc == Acceptance.TRUE) ? true : false;
065                DefaultQuery<I,Boolean> result = new DefaultQuery<>(w);
066                result.answer(out);
067                return result;
068        }
069
070}
071