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.transout.abstractimpl;
018
019import java.util.Collection;
020import java.util.List;
021
022import net.automatalib.automata.transout.TransitionOutputAutomaton;
023import net.automatalib.words.Word;
024import net.automatalib.words.WordBuilder;
025
026
027public abstract class AbstractTransOutAutomaton {
028        
029        public static <S,I,T,O> Word<O> computeOutput(TransitionOutputAutomaton<S,I,T,O> $this,
030                        Iterable<I> input) {
031                WordBuilder<O> result;
032                if(input instanceof Collection)
033                        result = new WordBuilder<O>(((Collection<I>)input).size());
034                else
035                        result = new WordBuilder<>();
036                $this.trace(input, result);
037                return result.toWord();
038        }
039        
040        public static <S,I,T,O> Word<O> computeSuffixOutput(TransitionOutputAutomaton<S,I,T,O> $this,
041                        Iterable<I> prefix, Iterable<I> suffix) {
042                WordBuilder<O> result;
043                if(suffix instanceof Collection)
044                        result = new WordBuilder<O>(((Collection<I>)suffix).size());
045                else
046                        result = new WordBuilder<>();
047                S state = $this.getState(prefix);
048                $this.trace(state, suffix, result);
049                return result.toWord();
050        }
051        
052        
053        public static <S,I,T,O> void trace(TransitionOutputAutomaton<S,I,T,O> $this,
054                        Iterable<I> input, List<O> output) {
055                $this.trace($this.getInitialState(), input, output);
056        }
057        
058        public static <S,I,T,O> void trace(TransitionOutputAutomaton<S, I, T, O> $this,
059                        S state, Iterable<I> input, List<O> output) {
060                
061                for(I sym : input) {
062                        T trans = $this.getTransition(state, sym);
063                        O out = $this.getTransitionOutput(trans);
064                        output.add(out);
065                        state = $this.getSuccessor(trans);
066                }
067        }
068        
069        public static <S,I,T,O> O getOutput(TransitionOutputAutomaton<S, I, T, O> $this,
070                        S state, I input) {
071                T trans = $this.getTransition(state, input);
072                if(trans == null)
073                        return null;
074                return $this.getTransitionOutput(trans);
075        }
076}
077