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.ts.comp;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Set;
025
026import net.automatalib.commons.util.Pair;
027import net.automatalib.ts.TransitionSystem;
028import net.automatalib.ts.abstractimpl.AbstractTS;
029
030
031public class TSComposition<S1, S2, I, T1, T2,
032                TS1 extends TransitionSystem<S1, I, T1>,
033                TS2 extends TransitionSystem<S2, I, T2>> extends
034                AbstractTS<Pair<S1, S2>, I, Pair<T1, T2>> {
035        
036        protected final TS1 ts1;
037        protected final TS2 ts2;
038        
039        /**
040         * Constructor.
041         * @param ts1 first transition system
042         * @param ts2 second transition system
043         */
044        public TSComposition(TS1 ts1,
045                        TS2 ts2) {
046                this.ts1 = ts1;
047                this.ts2 = ts2;
048        }
049
050        /*
051         * (non-Javadoc)
052         * @see de.ls5.ts.TransitionSystem#getInitialStates()
053         */
054        @Override
055        public Set<Pair<S1,S2>> getInitialStates() {
056                Collection<S1> init1 = ts1.getInitialStates();
057                Collection<S2> init2 = ts2.getInitialStates();
058                
059                Set<Pair<S1,S2>> result = new HashSet<Pair<S1,S2>>(init1.size() * init2.size());
060                
061                for(S1 s1 : init1) {
062                        for(S2 s2 : init2)
063                                result.add(Pair.make(s1, s2));
064                }
065                
066                return result;
067        }
068
069        /*
070         * (non-Javadoc)
071         * @see de.ls5.ts.TransitionSystem#getTransitions(java.lang.Object, java.lang.Object)
072         */
073        @Override
074        public Collection<Pair<T1, T2>> getTransitions(Pair<S1, S2> state, I input) {
075                S1 s1 = state.getFirst();
076                S2 s2 = state.getSecond();
077                Collection<T1> trans1 = ts1.getTransitions(s1, input);
078                Collection<T2> trans2 = ts2.getTransitions(s2, input);
079                
080                if(trans1 == null || trans1.isEmpty() || trans2 == null || trans2.isEmpty())
081                        return Collections.emptySet();
082                
083                List<Pair<T1,T2>> result = new ArrayList<Pair<T1,T2>>(trans1.size() * trans2.size());
084                
085                for(T1 t1 : trans1) {
086                        for(T2 t2 : trans2)
087                                result.add(Pair.make(t1, t2));
088                }
089                
090                return result;
091        }
092
093        /*
094         * (non-Javadoc)
095         * @see de.ls5.ts.TransitionSystem#getSuccessor(java.lang.Object)
096         */
097        @Override
098        public Pair<S1, S2> getSuccessor(Pair<T1, T2> transition) {
099                T1 t1 = transition.getFirst();
100                T2 t2 = transition.getSecond();
101                return Pair.make(ts1.getSuccessor(t1), ts2.getSuccessor(t2));
102        }
103
104}