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

de.learnlib.oracles.MQUtil Maven / Gradle / Ivy

There is a newer version: 0.12.0
Show newest version
/* Copyright (C) 2013-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.oracles;

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

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import de.learnlib.api.MembershipOracle;
import de.learnlib.api.Query;
import de.learnlib.api.QueryAnswerer;

import net.automatalib.automata.concepts.SuffixOutput;
import net.automatalib.words.Word;

@ParametersAreNonnullByDefault
public abstract class MQUtil {
	
	@Nullable
	public static  D output(MembershipOracle oracle, Word queryWord) {
		return query(oracle, queryWord).getOutput();
	}
	
	@Nullable
	public static  D output(MembershipOracle oracle, Word prefix, Word suffix) {
		return query(oracle, prefix, suffix).getOutput();
	}
	
	@Nonnull
	public static  DefaultQuery query(MembershipOracle oracle, Word prefix, Word suffix) {
		DefaultQuery qry = new DefaultQuery<>(prefix, suffix);
		oracle.processQueries(Collections.singleton(qry));
		return qry;
	}
	
	@Nonnull
	public static  DefaultQuery query(MembershipOracle oracle, Word queryWord) {
		return query(oracle, Word.epsilon(), queryWord);
	}
	
	public static  void answerQueries(QueryAnswerer answerer, Collection> queries) {
		for(Query query : queries) {
			Word prefix = query.getPrefix();
			Word suffix = query.getSuffix();
			D answer = answerer.answerQuery(prefix, suffix);
			query.answer(answer);
		}
	}
	
	public static  void answerQueriesParallel(QueryAnswerer answerer, Collection> queries) {
		queries.parallelStream().forEach(q -> {
			Word prefix = q.getPrefix();
			Word suffix = q.getSuffix();
			D answer = answerer.answerQuery(prefix, suffix);
			q.answer(answer);
		});
	}
	
	public static  boolean isCounterexample(DefaultQuery query, SuffixOutput hyp) {
		D qryOut = query.getOutput();
		D hypOut = hyp.computeSuffixOutput(query.getPrefix(), query.getSuffix());
		return !Objects.equals(qryOut, hypOut);
	}
}