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.graphs.dot;
018
019import java.io.IOException;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.Map;
023
024import net.automatalib.commons.util.mappings.Mapping;
025
026public class DefaultDOTHelper<N, E> implements GraphDOTHelper<N, E> {
027        
028        protected static final String START_PREFIX = "__start";
029        
030        
031        private static final DefaultDOTHelper<Object,Object> DEFAULT_INSTANCE
032                = new DefaultDOTHelper<Object,Object>();
033        
034        public static DefaultDOTHelper<Object,Object> getInstance() {
035                return DEFAULT_INSTANCE;
036        }
037        
038        protected Collection<? extends N> initialNodes() {
039                return Collections.emptySet();
040        }
041
042        /*
043         * (non-Javadoc)
044         * @see net.automatalib.graphs.dot.GraphDOTHelper#writePreamble(java.lang.Appendable)
045         */
046        @Override
047        public void writePreamble(Appendable a) throws IOException {
048                int size = initialNodes().size();
049                
050                for(int i = 0; i < size; i++) {
051                        a.append(START_PREFIX).append(Integer.toString(i));
052                        a.append(" [label=\"\" shape=\"none\"];\n");
053                }
054        }
055
056        /*
057         * (non-Javadoc)
058         * @see net.automatalib.graphs.dot.GraphDOTHelper#writePostamble(java.lang.Appendable)
059         */
060        @Override
061        public void writePostamble(Mapping<N,String> identifiers, Appendable a) throws IOException {
062                Collection<? extends N> initials = initialNodes();
063                
064                int i = 0;
065                for(N init : initials) {
066                        a.append(START_PREFIX).append(Integer.toString(i++));
067                        a.append(" -> ").append(identifiers.get(init)).append(";\n");
068                }
069        }
070
071        /*
072         * (non-Javadoc)
073         * @see net.automatalib.graphs.dot.GraphDOTHelper#getNodeProperties(java.lang.Object, java.util.Map)
074         */
075        @Override
076        public boolean getNodeProperties(N node, Map<String,String> properties) {
077                String label = String.valueOf(node);
078                properties.put(LABEL, label);
079                properties.put(SHAPE, "circle");
080                return true;
081        }
082
083        /*
084         * (non-Javadoc)
085         * @see net.automatalib.graphs.dot.GraphDOTHelper#getEdgeProperties(java.lang.Object, java.util.Map)
086         */
087        @Override
088        public boolean getEdgeProperties(N src, E edge, N tgt, Map<String,String> properties) {
089                return true;
090        }
091
092}