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) 2013-2019 TU Dortmund
 * This file is part of AutomataLib, http://www.automatalib.net/.
 *
 * 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 net.automatalib.util.automata.predicates;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Predicate;

import net.automatalib.automata.concepts.TransitionOutput;
import net.automatalib.commons.util.functions.FunctionsUtil;
import net.automatalib.ts.TransitionPredicate;

public final class TransitionPredicates {

    private TransitionPredicates() {
    }

    public static  TransitionPredicate safePred(TransitionPredicate pred,
                                                                  boolean nullValue) {
        if (pred != null) {
            return pred;
        }
        return (s, i, t) -> nullValue;
    }

    public static  TransitionPredicate compose(Predicate sourcePred,
                                                                 Predicate inputPred,
                                                                 Predicate transPred) {
        final Predicate safeSource = FunctionsUtil.safeToTrue(sourcePred);
        final Predicate safeInput = FunctionsUtil.safeToTrue(inputPred);
        final Predicate safeTrans = FunctionsUtil.safeToTrue(transPred);
        return (s, i, t) -> safeSource.test(s) && safeInput.test(i) && safeTrans.test(t);
    }

    public static  TransitionPredicate alwaysTrue() {
        return (s, i, t) -> true;
    }

    public static  TransitionPredicate alwaysFalse() {
        return (s, i, t) -> false;
    }

    public static  TransitionPredicate constantValue(boolean value) {
        return (s, i, t) -> value;
    }

    public static  TransitionPredicate sourceSatisfying(Predicate sourcePred) {
        return (s, i, t) -> sourcePred.test(s);
    }

    public static  TransitionPredicate inputSatisfying(Predicate inputPred) {
        return (s, i, t) -> inputPred.test(i);
    }

    public static  TransitionPredicate transitionSatisfying(Predicate transPred) {
        return (s, i, t) -> transPred.test(t);
    }

    public static  TransitionPredicate inputIs(Object input) {
        return (s, i, t) -> Objects.equals(i, input);
    }

    public static  TransitionPredicate inputIsNot(Object input) {
        return (s, i, t) -> !Objects.equals(i, input);
    }

    @SafeVarargs
    public static  TransitionPredicate inputIn(Object... inputs) {
        return inputIn(Arrays.asList(inputs));
    }

    public static  TransitionPredicate inputIn(Collection inputs) {
        return (s, i, t) -> inputs.contains(i);
    }

    @SafeVarargs
    public static  TransitionPredicate inputNotIn(Object... inputs) {
        return inputNotIn(Arrays.asList(inputs));
    }

    public static  TransitionPredicate inputNotIn(Collection inputs) {
        return (s, i, t) -> !inputs.contains(i);
    }

    public static  TransitionPredicate outputIs(TransitionOutput transOut,
                                                                  Object output) {
        return outputSatisfies(transOut, o -> Objects.equals(o, output));
    }

    public static  TransitionPredicate outputSatisfies(TransitionOutput transOut,
                                                                            Predicate outputPred) {
        return new OutputSatisfies<>(transOut, outputPred);
    }

    public static  TransitionPredicate outputIsNot(TransitionOutput transOut,
                                                                     Object output) {
        return outputViolates(transOut, o -> Objects.equals(o, output));
    }

    public static  TransitionPredicate outputViolates(TransitionOutput transOut,
                                                                           Predicate outputPred) {
        return new OutputSatisfies<>(transOut, outputPred, true);
    }

    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, outputs::contains);
    }

    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, outputs::contains);
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy