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

de.learnlib.mealy.SymbolOracleWrapper Maven / Gradle / Ivy

The newest version!
/* Copyright (C) 2013-2014 TU Dortmund
 * This file is part of LearnLib, http://www.learnlib.de/.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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 type
 * @param  output symbol type
 */
@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);
	}

}