FileSourceEdge.java

1
/*
2
 * Copyright 2006 - 2013
3
 *     Stefan Balev     <stefan.balev@graphstream-project.org>
4
 *     Julien Baudry    <julien.baudry@graphstream-project.org>
5
 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
6
 *     Yoann Pign��      <yoann.pigne@graphstream-project.org>
7
 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
8
 * 
9
 * This file is part of GraphStream <http://graphstream-project.org>.
10
 * 
11
 * GraphStream is a library whose purpose is to handle static or dynamic
12
 * graph, create them from scratch, file or any source and display them.
13
 * 
14
 * This program is free software distributed under the terms of two licenses, the
15
 * CeCILL-C license that fits European law, and the GNU Lesser General Public
16
 * License. You can  use, modify and/ or redistribute the software under the terms
17
 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
18
 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
19
 * the Free Software Foundation, either version 3 of the License, or (at your
20
 * option) any later version.
21
 * 
22
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
23
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
24
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
25
 * 
26
 * You should have received a copy of the GNU Lesser General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 * 
29
 * The fact that you are presently reading this means that you have had
30
 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
31
 */
32
package org.graphstream.stream.file;
33
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.Reader;
37
import java.net.URL;
38
import java.util.HashSet;
39
40
/**
41
 * Reader for the "edge" graph format.
42
 * 
43
 * <p>
44
 * The edge graph format is a very simple and lightweight format where each line
45
 * describes an edge by giving two node names. The nodes are created implicitly.
46
 * </p>
47
 * 
48
 * <p>
49
 * This reader also understands the derivative format where a line contains a
50
 * first node name, followed by several node names separated by spaces. In this
51
 * case it links the first node with all other node name following on the line.
52
 * </p>
53
 * 
54
 * <p>
55
 * Also, the format does not specify any direction for edges. By default all
56
 * edges are undirected. You can choose to make all edges directed by passing
57
 * "true" as the first arguments to constructors
58
 * {@link #FileSourceEdge(boolean)} or {@link #FileSourceEdge(boolean, boolean)}
59
 * . The direction of edges goes from the first node name on each line toward
60
 * the second (or more) node names on each line.
61
 * </p>
62
 * 
63
 * <p>
64
 * This format only contains edges. To ensure the "add node" events are sent
65
 * before an edge referencing two nodes is created via an "add edge" event, this
66
 * reader has a hash set of already encountered nodes. The hash set allows to
67
 * issue "add node" events only when a node is encountered for the first time.
68
 * </p>
69
 * 
70
 * </p> This hash set consumes memory, but is the only way to ensure "add node"
71
 * events are correctly issued. If this input is directly connected to a graph,
72
 * as graphs can create non-existing nodes automatically, you can disable the
73
 * hash set of nodes using the constructor
74
 * {@link #FileSourceEdge(boolean, boolean)}, and giving "false" for the second
75
 * argument. </p>
76
 * 
77
 * The usual file name extension for this format is ".edge".
78
 */
79
public class FileSourceEdge extends FileSourceBase {
80
	// Attribute
81
82
	/**
83
	 * Allocator for edge identifiers.
84
	 */
85
	protected int edgeid = 0;
86
87
	/**
88
	 * By default, consider edges as undirected.
89
	 */
90
	protected boolean directed = false;
91
92
	/**
93
	 * Set of existing nodes (if nodes are declared).
94
	 */
95
	protected HashSet<String> nodes;
96
97
	protected String graphName = "EDGE_";
98
99
	// Construction
100
101
	/**
102
	 * New reader for the "edge" format.
103
	 */
104
	public FileSourceEdge() {
105
		this(false);
106
	}
107
108
	/**
109
	 * New reader for the "edge" format.
110
	 * 
111
	 * @param edgesAreDirected
112
	 *            If true (default=false) edges are considered directed.
113
	 */
114
	public FileSourceEdge(boolean edgesAreDirected) {
115
		this(edgesAreDirected, true);
116
	}
117
118
	/**
119
	 * New reader for the "edge" format.
120
	 * 
121
	 * @param edgesAreDirected
122
	 *            If true (default=false) edges are considered directed.
123
	 * @param declareNodes
124
	 *            If true (default=true) this reader outputs nodeAdded events.
125
	 */
126
	public FileSourceEdge(boolean edgesAreDirected, boolean declareNodes) {
127
		directed = edgesAreDirected;
128 1 1. : negated conditional → NO_COVERAGE
		nodes = declareNodes ? new HashSet<String>() : null;
129
	}
130
131
	// Commands
132
133
	@Override
134
	protected void continueParsingInInclude() throws IOException {
135
		// Should not happen, EDGE files cannot be nested.
136
	}
137
138
	@Override
139
	public boolean nextEvents() throws IOException {
140
		String id1 = getWordOrNumberOrStringOrEolOrEof();
141
142 1 1. nextEvents : negated conditional → NO_COVERAGE
		if (id1.equals("EOL")) {
143
			// Empty line.
144 1 1. nextEvents : negated conditional → NO_COVERAGE
		} else if (id1.equals("EOF")) {
145 1 1. nextEvents : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
			return false;
146
		} else {
147 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceEdge::declareNode → NO_COVERAGE
			declareNode(id1);
148
149
			String id2 = getWordOrNumberOrStringOrEolOrEof();
150
151 1 1. nextEvents : negated conditional → NO_COVERAGE
			while (!id2.equals("EOL")) {
152 1 1. nextEvents : negated conditional → NO_COVERAGE
				if (!id1.equals(id2)) {
153 1 1. nextEvents : Replaced integer addition with subtraction → NO_COVERAGE
					String edgeId = Integer.toString(edgeid++);
154
155 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceEdge::declareNode → NO_COVERAGE
					declareNode(id2);
156 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceEdge::sendEdgeAdded → NO_COVERAGE
					sendEdgeAdded(graphName, edgeId, id1, id2, directed);
157
				}
158
159
				id2 = getWordOrNumberOrStringOrEolOrEof();
160
			}
161
		}
162
163 1 1. nextEvents : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return true;
164
	}
165
166
	protected void declareNode(String id) {
167 1 1. declareNode : negated conditional → NO_COVERAGE
		if (nodes != null) {
168 1 1. declareNode : negated conditional → NO_COVERAGE
			if (!nodes.contains(id)) {
169 1 1. declareNode : removed call to org/graphstream/stream/file/FileSourceEdge::sendNodeAdded → NO_COVERAGE
				sendNodeAdded(graphName, id);
170
				nodes.add(id);
171
			}
172
		}
173
	}
174
175
	@Override
176
	public void begin(String filename) throws IOException {
177 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(filename);
178 1 1. begin : removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE
		init();
179
	}
180
181
	@Override
182
	public void begin(URL url) throws IOException {
183 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(url);
184 1 1. begin : removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE
		init();
185
	}
186
187
	@Override
188
	public void begin(InputStream stream) throws IOException {
189 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(stream);
190 1 1. begin : removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE
		init();
191
	}
192
193
	@Override
194
	public void begin(Reader reader) throws IOException {
195 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(reader);
196 1 1. begin : removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE
		init();
197
	}
198
199
	protected void init() throws IOException {
200 1 1. init : removed call to java/io/StreamTokenizer::eolIsSignificant → NO_COVERAGE
		st.eolIsSignificant(true);
201 1 1. init : removed call to java/io/StreamTokenizer::commentChar → NO_COVERAGE
		st.commentChar('#');
202
203
		graphName = String.format("%s_%d", graphName,
204 2 1. init : Replaced long multiplication with division → NO_COVERAGE
2. init : Replaced long addition with subtraction → NO_COVERAGE
				System.currentTimeMillis() + ((long) Math.random() * 10));
205
	}
206
207
	public boolean nextStep() throws IOException {
208 1 1. nextStep : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return nextEvents();
209
	}
210
211
	@Override
212
	public void end() throws IOException {
213 1 1. end : removed call to org/graphstream/stream/file/FileSourceBase::end → NO_COVERAGE
		super.end();
214
	}
215
}

Mutations

128

1.1
Location :
Killed by : none
negated conditional → NO_COVERAGE

142

1.1
Location : nextEvents
Killed by : none
negated conditional → NO_COVERAGE

144

1.1
Location : nextEvents
Killed by : none
negated conditional → NO_COVERAGE

145

1.1
Location : nextEvents
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

147

1.1
Location : nextEvents
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::declareNode → NO_COVERAGE

151

1.1
Location : nextEvents
Killed by : none
negated conditional → NO_COVERAGE

152

1.1
Location : nextEvents
Killed by : none
negated conditional → NO_COVERAGE

153

1.1
Location : nextEvents
Killed by : none
Replaced integer addition with subtraction → NO_COVERAGE

155

1.1
Location : nextEvents
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::declareNode → NO_COVERAGE

156

1.1
Location : nextEvents
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::sendEdgeAdded → NO_COVERAGE

163

1.1
Location : nextEvents
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

167

1.1
Location : declareNode
Killed by : none
negated conditional → NO_COVERAGE

168

1.1
Location : declareNode
Killed by : none
negated conditional → NO_COVERAGE

169

1.1
Location : declareNode
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::sendNodeAdded → NO_COVERAGE

177

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE

178

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE

183

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE

184

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE

189

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE

190

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE

195

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE

196

1.1
Location : begin
Killed by : none
removed call to org/graphstream/stream/file/FileSourceEdge::init → NO_COVERAGE

200

1.1
Location : init
Killed by : none
removed call to java/io/StreamTokenizer::eolIsSignificant → NO_COVERAGE

201

1.1
Location : init
Killed by : none
removed call to java/io/StreamTokenizer::commentChar → NO_COVERAGE

204

1.1
Location : init
Killed by : none
Replaced long multiplication with division → NO_COVERAGE

2.2
Location : init
Killed by : none
Replaced long addition with subtraction → NO_COVERAGE

208

1.1
Location : nextStep
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

213

1.1
Location : end
Killed by : none
removed call to org/graphstream/stream/file/FileSourceBase::end → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.33