001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.stream;
033
034import org.graphstream.graph.Edge;
035import org.graphstream.graph.Graph;
036import org.graphstream.graph.Node;
037
038/**
039 * A simple source of graph events that takes an existing graph and creates a
040 * flow of events by enumerating all nodes, edges and attributes of the graph.
041 * 
042 * <p>
043 * The only method of this class is {@link #replay(Graph)} that takes a graph as
044 * argument and :
045 * <ul>
046 * <li>First exports all graph attributes as attribute-addition events.</li>
047 * <li>Then exports all nodes as node-creation events.
048 * <ul>
049 * <li>For each node exports all the node attributes as attribute-addition
050 * events.</li>
051 * </ul>
052 * </li>
053 * <li>Then exports all edges ad edge-creation events.
054 * <ul>
055 * <li>For each edge exports all the edge attribute as attribute-addition
056 * events.</li>
057 * </ul>
058 * </li>
059 * </ul>
060 * In this order.
061 * </p>
062 * 
063 * <p>
064 * Note that this is a source, not a pipe. This means that it has its own
065 * identifier and is a producer of "new" events. Also note that is does not
066 * export the dynamics of the graph, only its structure at the present time (the
067 * evolution of the graph is not stored in the graph, to produce a dynamic flow
068 * of events of the evolution of a graph you have to register the sinks in the
069 * graph itself just after its creation).
070 * </p>
071 */
072public class GraphReplay extends SourceBase implements Source {
073        public GraphReplay(String id) {
074                super(id);
075        }
076
077        /**
078         * Echo each element and attribute of the graph to the registered sinks.
079         * 
080         * @param graph
081         *            The graph to export.
082         */
083        public void replay(Graph graph) {
084                for (String key : graph.getAttributeKeySet())
085                        sendGraphAttributeAdded(sourceId, key, graph.getAttribute(key));
086
087                for (Node node : graph) {
088                        String nodeId = node.getId();
089                        sendNodeAdded(sourceId, nodeId);
090
091                        if (node.getAttributeCount() > 0)
092                                for (String key : node.getAttributeKeySet())
093                                        sendNodeAttributeAdded(sourceId, nodeId, key,
094                                                        node.getAttribute(key));
095                }
096
097                for (Edge edge : graph.getEachEdge()) {
098                        String edgeId = edge.getId();
099                        sendEdgeAdded(sourceId, edgeId, edge.getNode0().getId(), edge
100                                        .getNode1().getId(), edge.isDirected());
101
102                        if (edge.getAttributeCount() > 0)
103                                for (String key : edge.getAttributeKeySet())
104                                        sendEdgeAttributeAdded(sourceId, edgeId, key,
105                                                        edge.getAttribute(key));
106                }
107        }
108}