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

de.learnlib.mealy.SymbolOracleWrapper 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.mealy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

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

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

import net.automatalib.words.Word;


/**
 * Word-to-Symbol-Oracle adapter.
 * 
 * Wraps an oracle which uses {@link Word}s as its output to an oracle which only
 * yields the last symbol of each output.
 * 
 * @author Malte Isberner 
 *
 * @param  input symbol class
 * @param  output symbol class
 */
@ParametersAreNonnullByDefault
final class SymbolOracleWrapper implements MembershipOracle {
	
	@ParametersAreNonnullByDefault
	private static final class LastSymbolQuery extends Query> {
		
		private final Query originalQuery;
		
		public LastSymbolQuery(Query originalQuery) {
			this.originalQuery = originalQuery;
		}

		@Override
		@Nonnull
		public Word getPrefix() {
			return originalQuery.getPrefix();
		}

		@Override
		@Nonnull
		public Word getSuffix() {
			return originalQuery.getSuffix();
		}

		@Override
		public void answer(Word output) {
			if(output == null) {
				throw new IllegalArgumentException("Query answer words must not be null");
			}
			originalQuery.answer(output.isEmpty() ? null : output.lastSymbol());
		}

		@Override
		public String toString() {
			return originalQuery.toString();
		}

	}
	
	private final MembershipOracle> wordOracle;

	/**
	 * Constructor.
	 * @param wordOracle the {@link MembershipOracle} returning output words.
	 */
	public SymbolOracleWrapper(MembershipOracle> wordOracle) {
		this.wordOracle = wordOracle;
	}

	/*
	 * (non-Javadoc)
	 * @see de.learnlib.api.MembershipOracle#processQueries(java.util.Collection)
	 */
	@Override
	public void processQueries(Collection> queries) {
		List> lsQueries = new ArrayList>(queries.size());
		for(Query qry : queries) {
			lsQueries.add(new LastSymbolQuery(qry));
		}
		
		wordOracle.processQueries(lsQueries);
	}

}