001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of AutomataLib, http://www.automatalib.net/.
003 * 
004 * AutomataLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * AutomataLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with AutomataLib; if not, see
015 * http://www.gnu.de/documents/lgpl.en.html.
016 */
017package net.automatalib.automata.abstractimpl;
018
019import java.util.Collection;
020import java.util.LinkedList;
021import java.util.List;
022import java.util.ListIterator;
023
024import net.automatalib.automata.MutableAutomaton;
025import net.automatalib.automata.ShrinkableAutomaton;
026
027public abstract class AbstractShrinkableAutomaton<S, I, T, SP, TP> extends
028                AbstractMutableAutomaton<S, I, T, SP, TP> implements
029                ShrinkableAutomaton<S, I, T, SP, TP> {
030        
031        public static <S,I,T,SP,TP> void removeState(ShrinkableAutomaton<S,I,T,SP,TP> $this,
032                        S state) {
033                $this.removeState(state, null);
034        }
035        
036
037        public static <S,I,T,SP,TP> void unlinkState(MutableAutomaton<S,I,T,SP,TP> automaton,
038                        S state, S replacement, Collection<I> inputs) {
039                
040                for(S curr : automaton) {
041                        if(state.equals(curr))
042                                continue;
043                        
044                        for(I input : inputs) {
045                                Collection<T> transitions = automaton.getTransitions(curr, input);
046                                if(transitions == null || transitions.isEmpty())
047                                        continue;
048                                
049                                boolean modified = false;
050                                List<T> modTransitions = new LinkedList<T>(transitions); // TODO
051                                        
052                                ListIterator<T> it = modTransitions.listIterator();
053                                while(it.hasNext()) {
054                                        T trans = it.next();
055                                        if(automaton.getSuccessor(trans) == state) {
056                                                if(replacement == null)
057                                                        it.remove();
058                                                else {
059                                                        T transRep = automaton.copyTransition(trans, replacement);
060                                                        it.set(transRep);
061                                                }
062                                                modified = true;
063                                        }
064                                }
065                                        
066                                if(modified)
067                                        automaton.setTransitions(curr, input, modTransitions);
068                        }
069                }
070                
071                if(automaton.getInitialStates().contains(state)) {
072                        automaton.setInitial(state, false);
073                        if(replacement != null)
074                                automaton.setInitial(replacement, true);
075                }
076        }
077        
078        
079
080        @Override
081        public void removeState(S state) {
082                removeState(this, state);
083        }
084        
085        
086
087}