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.util.automata.asgraph; 018 019import java.util.Collection; 020import java.util.Iterator; 021 022import net.automatalib.automata.Automaton; 023import net.automatalib.automata.concepts.StateIDs; 024import net.automatalib.automata.graphs.TransitionEdge; 025import net.automatalib.commons.util.mappings.MutableMapping; 026import net.automatalib.graphs.Graph; 027import net.automatalib.graphs.concepts.NodeIDs; 028 029 030public class AutomatonAsGraph<S, I, T,A extends Automaton<S,I,T>> implements Graph<S, TransitionEdge<I, T>> { 031 032 033 protected final A automaton; 034 protected final Collection<? extends I> inputAlphabet; 035 036 037 public AutomatonAsGraph(A automaton, Collection<? extends I> inputAlphabet) { 038 this.automaton = automaton; 039 this.inputAlphabet = inputAlphabet; 040 } 041 042 @Override 043 public Iterator<S> iterator() { 044 return automaton.iterator(); 045 } 046 047 @Override 048 public Collection<S> getNodes() { 049 return automaton.getStates(); 050 } 051 052 @Override 053 public Collection<TransitionEdge<I, T>> getOutgoingEdges(S node) { 054 return AGHelper.outgoingEdges(automaton, node, inputAlphabet); 055 } 056 057 @Override 058 public S getTarget(TransitionEdge<I, T> edge) { 059 return automaton.getSuccessor(edge.getTransition()); 060 } 061 062 @Override 063 public int size() { 064 return automaton.size(); 065 } 066 067 @Override 068 public <V> MutableMapping<S, V> createStaticNodeMapping() { 069 return automaton.createStaticStateMapping(); 070 } 071 072 @Override 073 public <V> MutableMapping<S, V> createDynamicNodeMapping() { 074 return automaton.createDynamicStateMapping(); 075 } 076 077 @Override 078 public NodeIDs<S> nodeIDs() { 079 final StateIDs<S> stateIds = automaton.stateIDs(); 080 return new NodeIDs<S>() { 081 @Override 082 public int getNodeId(S node) { 083 return stateIds.getStateId(node); 084 } 085 @Override 086 public S getNode(int id) { 087 return stateIds.getState(id); 088 } 089 }; 090 } 091 092}