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.algorithm.generator; 033 034import org.graphstream.graph.Node; 035 036/** 037 * Full graph generator. 038 * 039 * <p> 040 * Probably not very useful, still sometimes needed. This generator creates 041 * fully connected graphs of any size. Calling {@link #begin()} put one unique 042 * node in the graph, then {@link #nextEvents()} will add a new node each time 043 * it is called. 044 * </p> 045 * 046 * <p> 047 * This generator has the ability to add randomly chosen numerical values on 048 * arbitrary attributes on edges or nodes of the graph, and to randomly choose a 049 * direction for edges. 050 * </p> 051 * 052 * <p> 053 * A list of attributes can be given for nodes and edges. In this case each new 054 * node or edge added will have this attribute and the value will be a randomly 055 * chosen number. The range in which these numbers are chosen can be specified. 056 * </p> 057 * 058 * <p> 059 * By default, edges are not oriented. It is possible to ask orientation, in 060 * which case the direction is chosen randomly. 061 * </p> 062 * 063 * @since 2007 064 */ 065public class FullGenerator extends BaseGenerator { 066 /** 067 * Used to generate node names. 068 */ 069 protected int nodeNames = 0; 070 071 /** 072 * New full graph generator. By default no attributes are added to nodes and 073 * edges, and edges are not directed. 074 */ 075 public FullGenerator() { 076 super(); 077 setUseInternalGraph(true); 078 } 079 080 /** 081 * New full graph generator. 082 * 083 * @param directed 084 * Are edge directed? 085 * @param randomlyDirectedEdges 086 * randomly direct generated edges. 087 */ 088 public FullGenerator(boolean directed, boolean randomlyDirectedEdges) { 089 super(directed, randomlyDirectedEdges); 090 setUseInternalGraph(true); 091 } 092 093 /** 094 * New full graph generator. 095 * 096 * @param directed 097 * Are edge directed?. 098 * @param randomlyDirectedEdges 099 * randomly direct generated edges. 100 * @param nodeAttribute 101 * put an attribute by that name on each node with a random 102 * numeric value. 103 * @param edgeAttribute 104 * put an attribute by that name on each edge with a random 105 * numeric value. 106 */ 107 public FullGenerator(boolean directed, boolean randomlyDirectedEdges, 108 String nodeAttribute, String edgeAttribute) { 109 super(directed, randomlyDirectedEdges, nodeAttribute, edgeAttribute); 110 setUseInternalGraph(true); 111 } 112 113 /** 114 * Begin the generator by adding a node. 115 * 116 * @see org.graphstream.algorithm.generator.Generator#begin() 117 */ 118 public void begin() { 119 String id = Integer.toString(nodeNames++); 120 121 addNode(id); 122 } 123 124 /** 125 * Add a new node and connect it with all others. 126 * 127 * @see org.graphstream.algorithm.generator.Generator#nextEvents() 128 */ 129 public boolean nextEvents() { 130 String id = Integer.toString(nodeNames++); 131 132 addNode(id); 133 134 for (Node n : internalGraph.getEachNode()) { 135 if (!n.getId().equals(id)) // We can compare refs safely here. 136 addEdge(null, id, n.getId()); 137 } 138 139 return true; 140 } 141}