![JAR search and dependency download from the Maven repository](/logo.png)
net.automatalib.util.automata.Covers Maven / Gradle / Ivy
/* 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;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import net.automatalib.automata.DeterministicAutomaton;
import net.automatalib.commons.util.mappings.MutableMapping;
import net.automatalib.words.Word;
class Covers {
private static final class Record {
private final S state;
private final Word accessSequence;
private final Set coveredInputs;
public Record(S state, Word accessSequence) {
this(state, accessSequence, null);
}
public Record(S state, Word accessSequence, Set coveredInputs) {
this.state = state;
this.accessSequence = accessSequence;
this.coveredInputs = coveredInputs;
}
}
public static void cover(DeterministicAutomaton automaton,
Collection extends I> inputs, Collection super Word> states, Collection super Word> transitions) {
MutableMapping> reach = automaton.createStaticStateMapping();
Queue bfsQueue = new ArrayDeque();
S init = automaton.getInitialState();
reach.put(init, Word.epsilon());
bfsQueue.add(init);
if(states != null)
states.add(Word.epsilon());
S curr;
while((curr = bfsQueue.poll()) != null) {
Word as = reach.get(curr);
for(I in : inputs) {
S succ = automaton.getSuccessor(curr, in);
if(succ == null)
continue;
if(reach.get(succ) == null) {
Word succAs = as.append(in);
reach.put(succ, succAs);
if(states != null) {
states.add(succAs);
}
bfsQueue.add(succ);
}
else if(transitions != null)
transitions.add(as.append(in));
}
}
}
public static boolean incrementalStateCover(
DeterministicAutomaton automaton,
Collection extends I> inputs,
Collection extends Word> oldStates,
Collection super Word> newStates) {
MutableMapping> reach = automaton.createStaticStateMapping();
boolean augmented = false;
Queue> bfsQueue = new ArrayDeque<>();
for(Word oldStateAs : oldStates) {
S state = automaton.getState(oldStateAs);
if(state == null || reach.get(state) != null) {
continue; // strange, but we'll ignore it
}
Record rec = new Record<>(state, oldStateAs);
reach.put(state, rec);
bfsQueue.add(rec);
}
S init = automaton.getInitialState();
if(reach.get(init) == null) {
// apparently the initial state was not yet covered
Record rec = new Record<>(init, Word.epsilon());
reach.put(init, rec);
bfsQueue.add(rec);
newStates.add(Word.epsilon());
augmented = true;
}
Record curr;
while((curr = bfsQueue.poll()) != null) {
S state = curr.state;
Word as = curr.accessSequence;
for(I in : inputs) {
S succ = automaton.getSuccessor(state, in);
if(succ == null) {
continue;
}
if(reach.get(succ) == null) {
Word succAs = as.append(in);
Record succRec = new Record<>(succ, succAs);
reach.put(succ, succRec);
bfsQueue.add(succRec);
newStates.add(succAs);
augmented = true;
}
}
}
return augmented;
}
@SuppressWarnings("unchecked")
public static boolean incrementalCover(
DeterministicAutomaton automaton,
Collection extends I> inputs,
Collection extends Word> oldStateCover,
Collection extends Word> oldTransCover,
Collection super Word> newStateCover,
Collection super Word> newTransCover) {
MutableMapping> reach = automaton.createStaticStateMapping();
boolean augmented = false;
Queue> bfsQueue = new ArrayDeque<>();
// We enforce that the initial state *always* is covered by the empty word,
// regardless of whether other sequence in oldCover cover it
S init = automaton.getInitialState();
Record initRec = new Record(init, Word.epsilon(), new HashSet());
bfsQueue.add(initRec);
reach.put(init, initRec);
boolean hasEpsilon = false;
for(Word oldStateAs : oldStateCover) {
S state = automaton.getState(oldStateAs);
if(state == null || reach.get(state) != null) {
if(oldStateAs.isEmpty()) {
hasEpsilon = true;
}
continue; // strange, but we'll ignore it
}
Record rec = new Record<>(state, oldStateAs, new HashSet());
bfsQueue.add(rec);
reach.put(state, rec);
}
// Add transition cover information from *state covers*
for(Word oldStateAs : oldStateCover) {
if(oldStateAs.isEmpty()) {
continue;
}
Word asPrefix = oldStateAs.prefix(oldStateAs.length() - 1);
S pred = automaton.getState(asPrefix);
assert pred != null;
Record predRec = reach.get(pred);
if(predRec == null) {
throw new IllegalArgumentException("State cover was not prefix-closed: prefix of " + oldStateAs + " not in set");
}
I lastSym = oldStateAs.lastSymbol();
predRec.coveredInputs.add(lastSym);
}
// Till now, we haven't augmented any set. Now duplicate the transition cover
// to avoid aliasing problems
Word[] oldTransCoverArray = (oldTransCover.isEmpty()) ? null : oldTransCover.toArray(new Word[oldTransCover.size()]);
if(!hasEpsilon) {
if(newStateCover != null) {
newStateCover.add(Word.epsilon());
augmented = true;
}
}
// Add transition covers
if(oldTransCoverArray != null) {
for(Word oldTransAs : oldTransCoverArray) {
// Check if this transition now leads to a new state
S state = automaton.getState(oldTransAs);
if(state != null) {
Record rec = reach.get(state);
if(rec == null) {
// if so, add it to the state cover and to the queue
rec = new Record<>(state, oldTransAs, new HashSet());
bfsQueue.add(rec);
reach.put(state, rec);
if(newStateCover != null) {
newStateCover.add(oldTransAs);
augmented = true;
}
}
}
// In any case, mark the transition as covered
Word predAs = oldTransAs.prefix(oldTransAs.length() - 1);
S pred = automaton.getState(predAs);
if(pred == null) {
throw new IllegalArgumentException("Invalid transition: prefix of transition " + oldTransAs + " not covered by state cover");
}
I lastSym = oldTransAs.lastSymbol();
Record predRec = reach.get(pred);
predRec.coveredInputs.add(lastSym);
}
}
Record curr;
while((curr = bfsQueue.poll()) != null) {
for(I input : inputs) {
if(curr.coveredInputs.add(input)) {
S succ = automaton.getSuccessor(curr.state, input);
Word newAs = curr.accessSequence.append(input);
if(succ == null) {
// undefined transition, but still needs to be covered
if(newTransCover != null) {
newTransCover.add(newAs);
augmented = true;
}
}
else {
Record succRec = reach.get(succ);
if(succRec == null) {
// new state!
succRec = new Record<>(succ, newAs, new HashSet());
bfsQueue.add(succRec);
reach.put(succ, succRec);
if(newStateCover != null) {
newStateCover.add(newAs);
augmented = true;
}
}
else {
// new transition
if(newTransCover != null) {
newTransCover.add(newAs);
augmented = true;
}
}
}
}
}
}
return augmented;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy