All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.learnlib.eqtests.basic.WMethodEQOracle Maven / Gradle / Ivy

There is a newer version: 0.12.0
Show newest version
/* Copyright (C) 2014 TU Dortmund
 * This file is part of LearnLib, http://www.learnlib.de/.
 * 
 * LearnLib is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 3.0 as published by the Free Software Foundation.
 * 
 * LearnLib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with LearnLib; if not, see
 * .
 */
package de.learnlib.eqtests.basic;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import net.automatalib.automata.UniversalDeterministicAutomaton;
import net.automatalib.automata.concepts.Output;
import net.automatalib.automata.fsa.DFA;
import net.automatalib.automata.transout.MealyMachine;
import net.automatalib.commons.util.collections.CollectionsUtil;
import net.automatalib.util.automata.Automata;
import net.automatalib.words.Word;
import net.automatalib.words.WordBuilder;
import de.learnlib.api.EquivalenceOracle;
import de.learnlib.api.MembershipOracle;
import de.learnlib.oracles.DefaultQuery;

/**
 * Implements an equivalence test by applying the W-method test on the given
 * hypothesis automaton, as described in "Testing software design modeled by finite state machines"
 * by T.S. Chow.
 * 
 * @author Malte Isberner
 *
 * @param  automaton type
 * @param  input symbol type
 * @param  output domain type
 */
public class WMethodEQOracle & Output, I, D>
	implements EquivalenceOracle {
	
	public static class DFAWMethodEQOracle extends WMethodEQOracle,I,Boolean>
			implements DFAEquivalenceOracle {
		public DFAWMethodEQOracle(int maxDepth,
				MembershipOracle sulOracle) {
			super(maxDepth, sulOracle);
		}
	}
	
	public static class MealyWMethodEQOracle extends WMethodEQOracle,I,Word>
			implements MealyEquivalenceOracle {
		public MealyWMethodEQOracle(int maxDepth,
				MembershipOracle> sulOracle) {
			super(maxDepth, sulOracle);
		}
	}
	
	private int maxDepth;
	private final MembershipOracle sulOracle;
	
	/**
	 * Constructor.
	 * @param maxDepth the maximum length of the "middle" part of the test cases
	 * @param sulOracle interface to the system under learning
	 */
	public WMethodEQOracle(int maxDepth, MembershipOracle sulOracle) {
		this.maxDepth = maxDepth;
		this.sulOracle = sulOracle;
	}
	
	public void setMaxDepth(int maxDepth) {
		this.maxDepth = maxDepth;
	}

	/*
	 * (non-Javadoc)
	 * @see de.learnlib.api.EquivalenceOracle#findCounterExample(java.lang.Object, java.util.Collection)
	 */
	@Override
	public DefaultQuery findCounterExample(A hypothesis,
			Collection inputs) {
		
		List> transCover = Automata.transitionCover(hypothesis, inputs);
		List> charSuffixes = Automata.characterizingSet(hypothesis, inputs);
		
		// Special case: List of characterizing suffixes may be empty,
		// but in this case we still need to test!
		if(charSuffixes.isEmpty())
			charSuffixes = Collections.singletonList(Word.epsilon());
		
		
		WordBuilder wb = new WordBuilder<>();
		
		for(List middle : CollectionsUtil.allTuples(inputs, 1, maxDepth)) {
			for(Word trans : transCover) {
				for(Word suffix : charSuffixes) {
					wb.append(trans).append(middle).append(suffix);
					Word queryWord = wb.toWord();
					wb.clear();
					DefaultQuery query = new DefaultQuery<>(queryWord);
					D hypOutput = hypothesis.computeOutput(queryWord);
					sulOracle.processQueries(Collections.singleton(query));
					if(!Objects.equals(hypOutput, query.getOutput()))
						return query;
				}
			}
		}
		
		return null;
	}
	
}