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.abstractimpl;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Iterator;
022import java.util.Set;
023
024import net.automatalib.ts.DeterministicTransitionSystem;
025import net.automatalib.ts.TransitionSystem;
026
027
028public abstract class AbstractDTS<S, I, T> extends AbstractTS<S, I, T> implements
029                DeterministicTransitionSystem<S, I, T> {
030        
031        /**
032         * Provides a realization of
033         * {@link DeterministicTransitionSystem#getInitialStates()} using
034         * {@link DeterministicTransitionSystem#getInitialState()}.
035         * @see TransitionSystem#getInitialStates()
036         */
037        public static <S,I,T> Set<S> getInitialStates(DeterministicTransitionSystem<S, I, T> $this) {
038                S init = $this.getInitialState();
039                if(init == null)
040                        return Collections.emptySet();
041                return Collections.singleton(init);
042        }
043        
044        /**
045         * Provides a realization of
046         * {@link DeterministicTransitionSystem#getTransitions(Object, Object)}
047         * using {@link DeterministicTransitionSystem#getTransition(Object, Object)}.
048         * @see TransitionSystem#getTransitions(Object, Object)
049         */
050        public static <S,I,T> Set<T> getTransitions(DeterministicTransitionSystem<S, I, T> $this, S state, I input) {
051                T trans = $this.getTransition(state, input);
052                if(trans == null)
053                        return Collections.emptySet();
054                return Collections.singleton(trans);
055        }
056        
057        /**
058         * Provides a realization of
059         * {@link DeterministicTransitionSystem#getSuccessor(Object)} using
060         * {@link DeterministicTransitionSystem#getTransition(Object, Object)}
061         * and {@link DeterministicTransitionSystem#getSuccessor(Object)}.
062         * @see DeterministicTransitionSystem#getSuccessor(Object)
063         */
064        public static <S,I,T> S getSuccessor(DeterministicTransitionSystem<S, I, T> $this, S state, I input) {
065                T trans = $this.getTransition(state, input);
066                if(trans == null)
067                        return null;
068                return $this.getSuccessor(trans);
069        }
070        
071        /**
072         * Provides a realization of
073         * {@link DeterministicTransitionSystem#getSuccessor(Object, Iterable)} using
074         * {@link DeterministicTransitionSystem#getSuccessor(Object, Object)}.
075         * @see DeterministicTransitionSystem#getSuccessor(Object, Iterable)
076         */
077        public static <S,I,T> S getSuccessor(DeterministicTransitionSystem<S, I, T> $this, S state, Iterable<I> input) {
078                S curr = state;
079                Iterator<I> it = input.iterator();
080                
081                while(curr != null && it.hasNext()) {
082                        I sym = it.next();
083                        curr = $this.getSuccessor(curr, sym);
084                }
085                
086                return curr;
087        }
088        
089        /**
090         * Provides a realization of
091         * {@link DeterministicTransitionSystem#getState(Iterable)} using
092         * {@link DeterministicTransitionSystem#getSuccessor(Object, Iterable)}
093         * and {@link DeterministicTransitionSystem#getInitialState()}.
094         * @see {@link DeterministicTransitionSystem#getState(Iterable)}
095         */
096        public static <S,I,T> S getState(DeterministicTransitionSystem<S, I, T> $this, Iterable<I> input) {
097                return $this.getSuccessor(
098                                $this.getInitialState(),
099                                input);
100        }
101        
102        //////////////////////////////////////////////////////////////////////////////////////////////
103
104        /*
105         * (non-Javadoc)
106         * @see net.automatalib.ts.SimpleTS#getInitialStates()
107         */
108        @Override
109        public Set<S> getInitialStates() {
110                return getInitialStates(this);
111        }
112
113        /*
114         * (non-Javadoc)
115         * @see net.automatalib.ts.TransitionSystem#getTransitions(java.lang.Object, java.lang.Object)
116         */
117        @Override
118        public Collection<T> getTransitions(S state, I input) {
119                return getTransitions(this, state, input);
120        }
121
122        /*
123         * (non-Javadoc)
124         * @see net.automatalib.ts.SimpleDTS#getSuccessor(java.lang.Object, java.lang.Object)
125         */
126        @Override
127        public S getSuccessor(S state, I input) {
128                return getSuccessor(this, state, input);
129        }
130
131        /*
132         * (non-Javadoc)
133         * @see net.automatalib.ts.SimpleDTS#getSuccessor(java.lang.Object, java.lang.Iterable)
134         */
135        @Override
136        public S getSuccessor(S state, Iterable<I> input) {
137                return getSuccessor(this, state, input);
138        }
139
140        /*
141         * (non-Javadoc)
142         * @see net.automatalib.ts.SimpleDTS#getState(java.lang.Iterable)
143         */
144        @Override
145        public S getState(Iterable<I> input) {
146                return getState(this, input);
147        }
148
149}