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

net.automatalib.util.automata.predicates.TransitionPredicates Maven / Gradle / Ivy

Go to download

This artifact provides various common utility operations for analyzing and manipulating automata and graphs, such as traversal, minimization and copying.

There is a newer version: 0.11.0
Show newest version
/* Copyright (C) 2014 TU Dortmund
 * This file is part of AutomataLib, http://www.automatalib.net/.
 * 
 * AutomataLib 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.
 * 
 * AutomataLib 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 AutomataLib; if not, see
 * http://www.gnu.de/documents/lgpl.en.html.
 */
package net.automatalib.util.automata.predicates;

import java.util.Arrays;
import java.util.Collection;

import javax.annotation.Nonnull;

import net.automatalib.automata.concepts.TransitionOutput;
import net.automatalib.ts.TransitionPredicate;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

public abstract class TransitionPredicates {

	private TransitionPredicates() {}
	
	public static 
	Predicate toUnaryPredicate(final TransitionPredicate transPred,
			final S source, final I input) {
		return new Predicate() {
			@Override
			public boolean apply(@Nonnull T trans) {
				return transPred.apply(source, input, trans);
			}
		};
	}
	
	public static 
	TransitionPredicate safePred(TransitionPredicate pred, boolean nullValue) {
		if(pred != null) {
			return pred;
		}
		return constantValue(nullValue);
	}
	
	public static 
	TransitionPredicate compose(Predicate sourcePred, Predicate inputPred, Predicate transPred) {
		if(sourcePred == null) {
			sourcePred = Predicates.alwaysTrue();
		}
		if(inputPred == null) {
			inputPred = Predicates.alwaysTrue();
		}
		if(transPred == null) {
			transPred = Predicates.alwaysTrue();
		}
		return new CompositeTransitionPredicate<>(sourcePred, inputPred, transPred); 
	}
	
	public static 
	TransitionPredicate constantValue(boolean value) {
		if(value) {
			return alwaysTrue();
		}
		return alwaysFalse();
	}
	
	@SuppressWarnings("unchecked")
	public static 
	TransitionPredicate alwaysTrue() {
		return (TransitionPredicate)ConstantTransitionPredicate.TRUE;
	}
	
	@SuppressWarnings("unchecked")
	public static 
	TransitionPredicate alwaysFalse() {
		return (TransitionPredicate)ConstantTransitionPredicate.FALSE;
	}
	
	public static 
	TransitionPredicate sourceSatisfying(Predicate sourcePred) {
		return compose(sourcePred, null, null);
	}
	
	public static 
	TransitionPredicate inputSatisfying(Predicate inputPred) {
		return compose(null, inputPred, null);
	}
	
	public static 
	TransitionPredicate transitionSatisfying(Predicate transPred) {
		return compose(null, null, transPred);
	}
	
	public static 
	TransitionPredicate inputIs(Object input) {
		return inputSatisfying(Predicates.equalTo(input));
	}
	
	public static 
	TransitionPredicate inputIsNot(Object input) {
		return inputSatisfying(Predicates.not(Predicates.equalTo(input)));
	}
	
	@SafeVarargs
	public static 
	TransitionPredicate inputIn(Object ...inputs) {
		return inputIn(Arrays.asList(inputs));
	}
	
	public static 
	TransitionPredicate inputIn(Collection inputs) {
		return inputSatisfying(Predicates.in(inputs));
	}
	
	@SafeVarargs
	public static 
	TransitionPredicate inputNotIn(Object... inputs) {
		return inputNotIn(Arrays.asList(inputs));
	}
	
	public static 
	TransitionPredicate inputNotIn(Collection inputs) {
		return inputSatisfying(Predicates.not(Predicates.in(inputs)));
	}
	
	
	
	public static 
	TransitionPredicate outputSatisfies(TransitionOutput transOut,
			Predicate outputPred) {
		return new OutputSatisfies<>(transOut, outputPred);
	}
	
	public static 
	TransitionPredicate outputViolates(TransitionOutput transOut,
			Predicate outputPred) {
		return new OutputSatisfies<>(transOut, outputPred, true);
	}
	
	public static 
	TransitionPredicate outputIs(TransitionOutput transOut,
			Object output) {
		return outputSatisfies(transOut, Predicates.equalTo(output));
	}
	
	public static 
	TransitionPredicate outputIsNot(TransitionOutput transOut,
			Object output) {
		return outputViolates(transOut, Predicates.equalTo(output));
	}
	
	public static 
	TransitionPredicate outputIn(TransitionOutput transOut,
			Object... outputs) {
		return outputIn(transOut, Arrays.asList(outputs));
	}
	
	public static 
	TransitionPredicate outputIn(TransitionOutput transOut,
			Collection outputs) {
		return outputSatisfies(transOut, Predicates.in(outputs));
	}
	
	
	public static 
	TransitionPredicate outputNotIn(TransitionOutput transOut,
			Object... outputs) {
		return outputNotIn(transOut, Arrays.asList(outputs));
	}
	
	public static 
	TransitionPredicate outputNotIn(TransitionOutput transOut,
			Collection outputs) {
		return outputViolates(transOut, Predicates.in(outputs));
	}
	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy