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

de.learnlib.algorithms.lstargeneric.ce.ObservationTableCEXHandlers Maven / Gradle / Ivy

/* Copyright (C) 2013 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.algorithms.lstargeneric.ce;

import java.util.Collections;
import java.util.List;

import net.automatalib.automata.concepts.SuffixOutput;
import net.automatalib.words.Word;
import de.learnlib.algorithms.lstargeneric.table.ObservationTable;
import de.learnlib.algorithms.lstargeneric.table.Row;
import de.learnlib.api.MembershipOracle;
import de.learnlib.api.Query;
import de.learnlib.counterexamples.GlobalSuffixFinder;
import de.learnlib.counterexamples.GlobalSuffixFinders;
import de.learnlib.counterexamples.LocalSuffixFinder;
import de.learnlib.counterexamples.LocalSuffixFinders;
import de.learnlib.oracles.DefaultQuery;

public class ObservationTableCEXHandlers {
	
	public static ObservationTableCEXHandler CLASSIC_LSTAR
		= new ObservationTableCEXHandler() {
			@Override
			public  List>> handleCounterexample(
					DefaultQuery ceQuery,
					ObservationTable table,
					SuffixOutput hypOutput,
					MembershipOracle oracle) {
				return handleClassicLStar(ceQuery, table, oracle);
			}
			@Override
			public boolean needsConsistencyCheck() {
				return true;
			}
		};
		
	public static ObservationTableCEXHandler SUFFIX1BY1
		= new ObservationTableCEXHandler() {
			@Override
			public 
			List>> handleCounterexample(
					DefaultQuery ceQuery,
					ObservationTable table,
					SuffixOutput hypOutput,
					MembershipOracle oracle) {
				return handleSuffix1by1(ceQuery, table, oracle);
			}
			@Override
			public boolean needsConsistencyCheck() {
				return false;
			}
		};
		
	public static ObservationTableCEXHandler MAHLER_PNUELI
		= fromGlobalSuffixFinder(GlobalSuffixFinders.MAHLER_PNUELI);
	
	public static ObservationTableCEXHandler SHAHBAZ
		= fromGlobalSuffixFinder(GlobalSuffixFinders.SHAHBAZ);
	
	public static ObservationTableCEXHandler FIND_LINEAR
		= fromLocalSuffixFinder(LocalSuffixFinders.FIND_LINEAR, false);
	
	public static ObservationTableCEXHandler FIND_LINEAR_ALLSUFFIXES
		= fromLocalSuffixFinder(LocalSuffixFinders.FIND_LINEAR, true);
	
	public static ObservationTableCEXHandler FIND_LINEAR_REVERSE
		= fromLocalSuffixFinder(LocalSuffixFinders.FIND_LINEAR_REVERSE, false);
	
	public static ObservationTableCEXHandler FIND_LINEAR_REVERSE_ALLSUFFIXES
		= fromLocalSuffixFinder(LocalSuffixFinders.FIND_LINEAR_REVERSE, true);
	
	public static ObservationTableCEXHandler RIVEST_SCHAPIRE
		= fromLocalSuffixFinder(LocalSuffixFinders.RIVEST_SCHAPIRE, false);
	
	public static ObservationTableCEXHandler RIVEST_SCHAPIRE_ALLSUFFIXES
		= fromLocalSuffixFinder(LocalSuffixFinders.RIVEST_SCHAPIRE, true);
		
	
	
	public static  
	ObservationTableCEXHandler fromGlobalSuffixFinder(final GlobalSuffixFinder globalFinder) {
		return new ObservationTableCEXHandler() {
			@Override
			public 
			List>> handleCounterexample(
					DefaultQuery ceQuery, ObservationTable table,
					SuffixOutput hypOutput, MembershipOracle oracle) {
				List> suffixes = globalFinder.findSuffixes(ceQuery, table, hypOutput, oracle);
				return handleGlobalSuffixes(table, suffixes, oracle);
			}
			@Override
			public boolean needsConsistencyCheck() {
				return false;
			}
		};
	}
	
	public static 
	ObservationTableCEXHandler fromLocalSuffixFinder(final LocalSuffixFinder localFinder, final boolean allSuffixes) {
		return new ObservationTableCEXHandler() {
			@Override
			public 
			List>> handleCounterexample(
					DefaultQuery ceQuery, ObservationTable table,
					SuffixOutput hypOutput, MembershipOracle oracle) {
				int suffixIdx = localFinder.findSuffixIndex(ceQuery, table, hypOutput, oracle);
				return handleLocalSuffix(ceQuery, table, suffixIdx, allSuffixes, oracle);
			}
			@Override
			public boolean needsConsistencyCheck() {
				return false;
			}
		};
	}
	
	
	
	public static 
	ObservationTableCEXHandler fromLocalSuffixFinder(final LocalSuffixFinder localFinder) {
		return fromLocalSuffixFinder(localFinder, false);
	}
	
	public static 
	List>> handleClassicLStar(DefaultQuery ceQuery,
			ObservationTable table, MembershipOracle oracle) {
		List>> unclosed = Collections.emptyList();
		
		List> suffixes = table.getSuffixes();
		
		Word ceWord = ceQuery.getInput();
		int ceLen = ceWord.length();
		
		for(int i = 1; i <= ceLen; i++) {
			Word suffix = ceWord.suffix(i);
			if(suffixes != null) {
				if(suffixes.contains(suffix))
					continue;
				suffixes = null;
			}
			
			unclosed = table.addSuffix(suffix, oracle);
			if(!unclosed.isEmpty())
				break;
		}
		
		return unclosed;
	}
	
	public static 
	List>> handleSuffix1by1(DefaultQuery ceQuery,
			ObservationTable table, MembershipOracle oracle) {
		List>> unclosed = Collections.emptyList();
		
		List> suffixes = table.getSuffixes();
		
		Word ceWord = ceQuery.getInput();
		int ceLen = ceWord.length();
		
		for(int i = 1; i <= ceLen; i++) {
			Word suffix = ceWord.suffix(i);
			if(suffixes != null) {
				if(suffixes.contains(suffix))
					continue;
				suffixes = null;
			}
			
			unclosed = table.addSuffix(suffix, oracle);
			if(!unclosed.isEmpty())
				break;
		}
		
		return unclosed;
	}
	
	public static 
	List>> handleGlobalSuffixes(ObservationTable table, List> suffixes,
			MembershipOracle oracle) {
		return table.addSuffixes(suffixes, oracle);
	}
	
	public static 
	List>> handleLocalSuffix(Query ceQuery, ObservationTable table,
			int suffixIndex, boolean allSuffixes, MembershipOracle oracle) {
		List> suffixes
			= GlobalSuffixFinders.suffixesForLocalOutput(ceQuery, suffixIndex, allSuffixes);
		return handleGlobalSuffixes(table, suffixes, oracle);
	}
	
	public static 
	List>> handleLocalSuffix(Query ceQuery, ObservationTable table,
			int suffixIndex, MembershipOracle oracle) {
		return handleLocalSuffix(ceQuery, table, suffixIndex, false, oracle);
	}
}