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.brics;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.Set;
023
024import net.automatalib.automata.fsa.abstractimpl.AbstractFSA;
025import net.automatalib.automata.graphs.AbstractAutomatonGraph;
026import net.automatalib.commons.util.mappings.MutableMapping;
027import net.automatalib.graphs.UniversalGraph;
028import net.automatalib.graphs.concepts.NodeIDs;
029import net.automatalib.graphs.dot.DOTPlottableGraph;
030import net.automatalib.graphs.dot.GraphDOTHelper;
031import dk.brics.automaton.Automaton;
032import dk.brics.automaton.State;
033import dk.brics.automaton.Transition;
034
035/**
036 * Base class for Brics automata adapters.
037 * 
038 * @author Malte Isberner <malte.isberner@gmail.com>
039 *
040 */
041public abstract class AbstractBricsAutomaton extends AbstractFSA<State, Character> implements
042                DOTPlottableGraph<State, Transition>, UniversalGraph<State,Transition,Boolean,BricsTransitionProperty> {
043        
044        protected final Automaton automaton;
045
046        /**
047         * Constructor.
048         * @param automaton the Brics automaton object. 
049         */
050        public AbstractBricsAutomaton(Automaton automaton) {
051                this.automaton = automaton;
052        }
053        
054        /**
055         * Retrieves the Brics automaton object.
056         * @return the brics automaton object
057         */
058        public Automaton getBricsAutomaton() {
059                return automaton;
060        }
061        
062        /*
063         * (non-Javadoc)
064         * @see net.automatalib.graphs.Graph#getNodes()
065         */
066        @Override
067        public Collection<State> getNodes() {
068                return AbstractAutomatonGraph.getNodes(this);
069        }
070
071        /*
072         * (non-Javadoc)
073         * @see net.automatalib.graphs.Graph#nodeIDs()
074         */
075        @Override
076        public NodeIDs<State> nodeIDs() {
077                return AbstractAutomatonGraph.nodeIDs(this);
078        }
079
080        /*
081         * (non-Javadoc)
082         * @see net.automatalib.graphs.IndefiniteGraph#getOutgoingEdges(java.lang.Object)
083         */
084        @Override
085        public Collection<Transition> getOutgoingEdges(State node) {
086                return node.getTransitions();
087        }
088
089        /*
090         * (non-Javadoc)
091         * @see net.automatalib.graphs.IndefiniteGraph#getTarget(java.lang.Object)
092         */
093        @Override
094        public State getTarget(Transition edge) {
095                return edge.getDest();
096        }
097
098        /*
099         * (non-Javadoc)
100         * @see net.automatalib.graphs.IndefiniteGraph#createStaticNodeMapping()
101         */
102        @Override
103        public <V> MutableMapping<State, V> createStaticNodeMapping() {
104                return AbstractAutomatonGraph.createStaticNodeMapping(this);
105        }
106
107        /*
108         * (non-Javadoc)
109         * @see net.automatalib.graphs.IndefiniteGraph#createDynamicNodeMapping()
110         */
111        @Override
112        public <V> MutableMapping<State, V> createDynamicNodeMapping() {
113                return AbstractAutomatonGraph.createDynamicNodeMapping(this);
114        }
115
116        /*
117         * (non-Javadoc)
118         * @see net.automatalib.ts.acceptors.AcceptorTS#isAccepting(java.lang.Object)
119         */
120        @Override
121        public boolean isAccepting(State state) {
122                return state.isAccept();
123        }
124
125        /*
126         * (non-Javadoc)
127         * @see net.automatalib.ts.TransitionSystem#getTransitions(java.lang.Object, java.lang.Object)
128         */
129        @Override
130        public Collection<State> getTransitions(State state, Character input) {
131                Collection<Transition> transitions = state.getSortedTransitions(false);
132                
133                Set<State> result = new HashSet<>();
134                
135                for(Transition t : transitions) {
136                        char min = t.getMin();
137                        if(input < min)
138                                break;
139                        char max = t.getMax();
140                        if(input > max)
141                                continue;
142                        result.add(t.getDest());
143                }
144                return result;
145        }
146
147        /*
148         * (non-Javadoc)
149         * @see net.automatalib.ts.simple.SimpleTS#getInitialStates()
150         */
151        @Override
152        public Set<State> getInitialStates() {
153                return Collections.singleton(automaton.getInitialState());
154        }
155
156        /*
157         * (non-Javadoc)
158         * @see net.automatalib.automata.simple.SimpleAutomaton#getStates()
159         */
160        @Override
161        public Collection<State> getStates() {
162                return automaton.getStates();
163        }
164
165        /*
166         * (non-Javadoc)
167         * @see net.automatalib.graphs.dot.DOTPlottableGraph#getGraphDOTHelper()
168         */
169        @Override
170        public GraphDOTHelper<State, Transition> getGraphDOTHelper() {
171                return new BricsDOTHelper(this);
172        }
173
174        /*
175         * (non-Javadoc)
176         * @see net.automatalib.graphs.UniversalIndefiniteGraph#getNodeProperties(java.lang.Object)
177         */
178        @Override
179        public Boolean getNodeProperty(State node) {
180                return node.isAccept();
181        }
182
183        /*
184         * (non-Javadoc)
185         * @see net.automatalib.graphs.UniversalIndefiniteGraph#getEdgeProperties(java.lang.Object)
186         */
187        @Override
188        public BricsTransitionProperty getEdgeProperty(Transition edge) {
189                return new BricsTransitionProperty(edge);
190        }
191}