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.minimizer;
018
019import java.util.Arrays;
020import java.util.List;
021
022/**
023 * A "block automaton", i.e. an automaton-style representation of the
024 * minimization result in which each block forms a state.
025 * 
026 * @author Malte Isberner <malte.isberner@gmail.com>
027 *
028 * @param <S> state class.
029 * @param <L> transition label class.
030 */
031public class BlockAutomaton<S, L> {
032        
033        // Edges array
034        private final BlockEdge<S,L>[][] edges;
035        
036        /**
037         * Constructor. Creates the block automaton.
038         * @param minResult the minimization result.
039         */
040        @SuppressWarnings("unchecked")
041        BlockAutomaton(MinimizationResult<S, L> minResult) {
042                edges = new BlockEdge[minResult.getNumBlocks()][];
043                
044                for(Block<S,L> block : minResult.getBlocks()) {
045                        int id = block.getId();
046                        State<S,L> rep = block.getStates().choose();
047                        List<Edge<S,L>> outgoing = rep.getOutgoing();
048                        BlockEdge<S,L>[] array = new BlockEdge[outgoing.size()];
049                        int i = 0;
050                        for(Edge<S,L> e : outgoing) {
051                                array[i++] = new BlockEdge<S,L>(block,
052                                                e.getTarget().getBlock(),
053                                                e.getTransitionLabel().getOriginalLabel());
054                        }
055                        edges[id] = array;
056                }
057        }
058        
059        /**
060         * Retrieves a list of outgoing edges of a block (state).
061         * @param block the block (state).
062         * @return the outgoing edges of the given block (state).
063         */
064        public List<BlockEdge<S,L>> getOutgoingEdges(Block<S,L> block) {
065                return Arrays.asList(edges[block.getId()]);
066        }
067        
068        /**
069         * Retrieves an array of outgoing edges of a block (state).
070         * @see #getOutgoingEdges(Block)
071         */
072        public BlockEdge<S,L>[] getOutgoingEdgeArray(Block<S,L> block) {
073                return edges[block.getId()];
074        }
075}