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

net.automatalib.util.automata.fsa.DFAs 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-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.fsa;

import java.util.Collection;

import net.automatalib.automata.fsa.DFA;
import net.automatalib.automata.fsa.MutableDFA;
import net.automatalib.automata.fsa.impl.compact.CompactDFA;
import net.automatalib.ts.acceptors.DeterministicAcceptorTS;
import net.automatalib.util.automata.copy.AutomatonCopyMethod;
import net.automatalib.util.automata.copy.AutomatonLowLevelCopy;
import net.automatalib.util.ts.acceptors.AcceptanceCombiner;
import net.automatalib.util.ts.acceptors.Acceptors;
import net.automatalib.util.ts.copy.TSCopy;
import net.automatalib.util.ts.traversal.TSTraversalMethod;
import net.automatalib.words.Alphabet;



/**
 * Operations on {@link DFA}s.
 * 

* Note that the methods provided by this class do not modify their input arguments. Such methods * are instead provided by the {@link MutableDFAs} class. * * @author Malte Isberner * */ public abstract class DFAs { /** * Most general way of combining two DFAs. The {@link AcceptanceCombiner} specified via the {@code combiner} parameter * specifies how acceptance values of the DFAs will be combined to an acceptance value in the result DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputs the input symbols to consider * @param out the mutable DFA for storing the result * @param combiner combination method for acceptance values * @return {@code out}, for convenience */ public static > A combine(DFA dfa1, DFA dfa2, Collection inputs, A out, AcceptanceCombiner combiner) { DeterministicAcceptorTS acc = Acceptors.combine(dfa1, dfa2, combiner); TSCopy.copy(TSTraversalMethod.DEPTH_FIRST, acc, -1, inputs, out); return out; } /** * Most general way of combining two DFAs. The behavior is the same as of the above * {@link #combine(DFA, DFA, Collection, MutableDFA, AcceptanceCombiner)}, but the result automaton * is automatically created as a {@link CompactDFA}. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputAlphabet the input alphabet * @param combiner combination method for acceptance values * @return a new DFA representing the combination of the specified DFA */ public static CompactDFA combine(DFA dfa1, DFA dfa2, Alphabet inputAlphabet, AcceptanceCombiner combiner) { return combine(dfa1, dfa2, inputAlphabet, new CompactDFA<>(inputAlphabet), combiner); } /** * Calculates the conjunction ("and") of two DFA, and stores the result in a given mutable DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputs the input symbols to consider * @param out a mutable DFA for storing the result * @return {@code out}, for convenience */ public static > A and(DFA dfa1, DFA dfa2, Collection inputs, A out) { return combine(dfa1, dfa2, inputs, out, AcceptanceCombiner.AND); } /** * Calculates the conjunction ("and") of two DFA, and returns the result as a new DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputAlphabet the input alphabet * @return a new DFA representing the conjunction of the specified DFA */ public static CompactDFA and(DFA dfa1, DFA dfa2, Alphabet inputAlphabet) { return and(dfa1, dfa2, inputAlphabet, new CompactDFA<>(inputAlphabet)); } /** * Calculates the disjunction ("or") of two DFA, and stores the result in a given mutable DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputs the input symbols to consider * @param out a mutable DFA for storing the result * @return {@code out}, for convenience */ public static > A or(DFA dfa1, DFA dfa2, Collection inputs, A out) { return combine(dfa1, dfa2, inputs, out, AcceptanceCombiner.OR); } /** * Calculates the disjunction ("or") of two DFA, and returns the result as a new DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputAlphabet the input alphabet * @return a new DFA representing the conjunction of the specified DFA */ public static CompactDFA or(DFA dfa1, DFA dfa2, Alphabet inputAlphabet) { return or(dfa1, dfa2, inputAlphabet, new CompactDFA<>(inputAlphabet)); } /** * Calculates the exclusive-or ("xor") of two DFA, and stores the result in a given mutable DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputs the input symbols to consider * @param out a mutable DFA for storing the result * @return {@code out}, for convenience */ public static > A xor(DFA dfa1, DFA dfa2, Collection inputs, A out) { return combine(dfa1, dfa2, inputs, out, AcceptanceCombiner.XOR); } /** * Calculates the exclusive-or ("xor") of two DFA, and returns the result as a new DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputAlphabet the input alphabet * @return a new DFA representing the conjunction of the specified DFA */ public static CompactDFA xor(DFA dfa1, DFA dfa2, Alphabet inputAlphabet) { return xor(dfa1, dfa2, inputAlphabet, new CompactDFA<>(inputAlphabet)); } /** * Calculates the equivalence ("<=>") of two DFA, and stores the result in a given mutable DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputs the input symbols to consider * @param out a mutable DFA for storing the result * @return {@code out}, for convenience */ public static > A equiv(DFA dfa1, DFA dfa2, Collection inputs, A out) { return combine(dfa1, dfa2, inputs, out, AcceptanceCombiner.EQUIV); } /** * Calculates the equivalence ("<=>") of two DFA, and returns the result as a new DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputAlphabet the input alphabet * @return a new DFA representing the conjunction of the specified DFA */ public static CompactDFA equiv(DFA dfa1, DFA dfa2, Alphabet inputAlphabet) { return equiv(dfa1, dfa2, inputAlphabet, new CompactDFA<>(inputAlphabet)); } /** * Calculates the implication ("=>") of two DFA, and stores the result in a given mutable DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputs the input symbols to consider * @param out a mutable DFA for storing the result * @return {@code out}, for convenience */ public static > A impl(DFA dfa1, DFA dfa2, Collection inputs, A out) { return combine(dfa1, dfa2, inputs, out, AcceptanceCombiner.IMPL); } /** * Calculates the implication ("=>") of two DFA, and returns the result as a new DFA. * * @param dfa1 the first DFA * @param dfa2 the second DFA * @param inputAlphabet the input alphabet * @return a new DFA representing the conjunction of the specified DFA */ public static CompactDFA impl(DFA dfa1, DFA dfa2, Alphabet inputAlphabet) { return impl(dfa1, dfa2, inputAlphabet, new CompactDFA<>(inputAlphabet)); } /** * Calculates the complement (negation) of a DFA, and stores the result in a given mutable DFA. *

* Note that unlike {@link MutableDFA#flipAcceptance()}, undefined transitions are treated as * leading to a rejecting sink state (and are thus turned into an accepting sink). * * @param dfa the DFA to complement * @param inputs the input symbols to consider * @param out a mutable DFA for storing the result * @return {@code out}, for convenience */ public static > A complement(DFA dfa, Collection inputs, A out) { AutomatonLowLevelCopy.copy(AutomatonCopyMethod.STATE_BY_STATE, dfa, inputs, out, b -> (b == null) ? true : !b, t -> null); MutableDFAs.complete(out, inputs, false, true); return out; } /** * Calculates the complement (negation) of a DFA, and returns the result as a new DFA. *

* Note that unlike {@link MutableDFA#flipAcceptance()}, undefined transitions are treated as * leading to a rejecting sink state (and are thus turned into an accepting sink). * * @param dfa the DFA to complement * @param inputAlphabet the input alphabet * @return a new DFA representing the complement of the specified DFA */ public static CompactDFA complement(DFA dfa, Alphabet inputAlphabet) { return complement(dfa, inputAlphabet, new CompactDFA<>(inputAlphabet)); } public static > A complete(DFA dfa, Collection inputs, A out) { AutomatonLowLevelCopy.copy(AutomatonCopyMethod.DFS, dfa, inputs, out); MutableDFAs.complete(out, inputs, true); return out; } public static CompactDFA complete(DFA dfa, Alphabet inputs) { return complete(dfa, inputs, new CompactDFA<>(inputs)); } private DFAs() { throw new IllegalStateException("Constructor should never be invoked"); } }