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

net.automatalib.util.automata.equivalence.CharacterizingSets 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 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.equivalence;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;

import net.automatalib.automata.UniversalDeterministicAutomaton;
import net.automatalib.util.automata.Automata;
import net.automatalib.words.Word;


/**
 * Operations for calculating characterizing sets.
 * 

* A characterizing set for a whole automaton is a set W of words such that for every two states * s1 and s2, there exists a word w ∈ W such that * w exposes a difference between s1 and s2 (i.e., * either covers a transition with differing property (or not defined in only one case), * or reaching a successor state with differing properties), or there exists no such word at all. *

* A characterizing set for a single state s is a set W of words such that * for every state t, there exists a word w ∈ W such that w exposes * a difference between s and t, or there exists no such word at all. * * @author Malte Isberner * */ public class CharacterizingSets { private static List buildTrace(UniversalDeterministicAutomaton automaton, S state, Word suffix) { List trace = new ArrayList(2*suffix.length()); S curr = state; for(I sym : suffix) { T trans = automaton.getTransition(curr, sym); if(trans == null) break; Object prop = automaton.getTransitionProperty(trans); trace.add(prop); curr = automaton.getSuccessor(trans); prop = automaton.getStateProperty(curr); trace.add(prop); } return trace; } private static boolean checkTrace(UniversalDeterministicAutomaton automaton, S state, Word suffix, List trace) { Iterator it = trace.iterator(); S curr = state; for(I sym : suffix) { T trans = automaton.getTransition(curr, sym); if(!it.hasNext()) return (trans == null); Object prop = automaton.getTransitionProperty(trans); if(!Objects.equals(prop, it.next())) return false; curr = automaton.getSuccessor(trans); prop = automaton.getStateProperty(curr); if(!Objects.equals(prop, it.next())) return false; } return true; } private static void cluster(UniversalDeterministicAutomaton automaton, Word suffix, Iterator stateIt, Map,List> bucketMap) { while(stateIt.hasNext()) { S state = stateIt.next(); List trace = buildTrace(automaton, state, suffix); List bucket = bucketMap.get(trace); if(bucket == null) { bucket = new ArrayList(); bucketMap.put(trace, bucket); } bucket.add(state); } } /** * Computes a characterizing set for a specified state in the given automaton. * @param automaton the automaton containing the state * @param inputs the input alphabets to consider * @param state the state for which to determine the characterizing set * @param result the collection in which to store the characterizing words */ public static void findCharacterizingSet(UniversalDeterministicAutomaton automaton, Collection inputs, S state, Collection> result) { Object prop = automaton.getStateProperty(state); List currentBlock = new ArrayList(); boolean multipleStateProps = false; for(S s : automaton) { if(Objects.equals(s, state)) continue; Object sProp = automaton.getStateProperty(s); if(!Objects.equals(sProp, prop)) multipleStateProps = true; else currentBlock.add(s); } if(multipleStateProps) result.add(Word.epsilon()); while(!currentBlock.isEmpty()) { List nextBlock = new ArrayList(); Iterator it = currentBlock.iterator(); Word suffix = null; while(it.hasNext() && suffix == null) { S s = it.next(); suffix = Automata.findSeparatingWord(automaton, state, s, inputs); } if(suffix == null) return; result.add(suffix); List trace = buildTrace(automaton, state, suffix); while(it.hasNext()) { S s = it.next(); if(checkTrace(automaton, s, suffix, trace)) nextBlock.add(s); } currentBlock = nextBlock; } } /** * Computes a characterizing set for the given automaton. * @param automaton the automaton for which to determine the characterizing set. * @param inputs the input alphabets to consider * @param result the collection in which to store the characterizing words */ public static void findCharacterizingSet(UniversalDeterministicAutomaton automaton, Collection inputs, Collection> result) { Map> initBlockMap = new HashMap>(); Queue> blocks = new ArrayDeque>(); for(S state : automaton) { Object prop = automaton.getStateProperty(state); List initBlock = initBlockMap.get(prop); if(initBlock == null) { initBlock = new ArrayList(); blocks.offer(initBlock); initBlockMap.put(prop, initBlock); } initBlock.add(state); } if(blocks.size() > 1) result.add(Word.epsilon()); List currBlock; while((currBlock = blocks.poll()) != null) { Iterator it = currBlock.iterator(); S ref = it.next(); Word suffix = null; S state = null; while(it.hasNext() && suffix == null) { state = it.next(); suffix = Automata.findSeparatingWord(automaton, ref, state, inputs); } if(suffix == null) continue; result.add(suffix); int otherBlocks = blocks.size(); Map,List> buckets = new HashMap,List>(); List firstBucket = new ArrayList(); List secondBucket = new ArrayList(); firstBucket.add(ref); buckets.put(buildTrace(automaton, ref, suffix), firstBucket); secondBucket.add(state); buckets.put(buildTrace(automaton, state, suffix), secondBucket); cluster(automaton, suffix, it, buckets); blocks.addAll(buckets.values()); while(otherBlocks-- > 0) { List block = blocks.poll(); buckets.clear(); cluster(automaton, suffix, block.iterator(), buckets); blocks.addAll(buckets.values()); } } } }