FileSourceNCol.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 "ncol" graph format.
42
 * 
43
 * <p>
44
 * The ncol graph format is a simple format where each line
45
 * describes an edge by giving two node names and an optional third
46
 * parameters giving the edge weight. The nodes are created implicitly.
47
 * </p>
48
 * 
49
 * <p>
50
 * Also, the format does not specify any direction for edges. By default all
51
 * edges are undirected. It is specified in the format that you will never
52
 * have directed edges and that the lines:
53
 * <pre>
54
 *     node1Name node2Name
55
 * </pre>
56
 * and
57
 * <pre>
58
 *     node2Name node1Name
59
 * </pre>
60
 * Cannot both appear at the same time in a file.
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 #FileSourceNCol(boolean)}, and giving "false" for the first
75
 * argument. </p>
76
 * 
77
 * The usual file name extension for this format is ".ncol".
78
 */
79
public class FileSourceNCol extends FileSourceBase {
80
	// Attribute
81
82
	/**
83
	 * Allocator for edge identifiers.
84
	 */
85
	protected int edgeid = 0;
86
87
	/**
88
	 * Set of existing nodes (if nodes are declared).
89
	 */
90
	protected HashSet<String> nodes;
91
92
	protected String graphName = "NCOL_";
93
94
	// Construction
95
96
	/**
97
	 * New reader for the "ncol" format.
98
	 */
99
	public FileSourceNCol() {
100
		this(false);
101
	}
102
103
	/**
104
	 * New reader for the "ncol" format.
105
	 * 
106
	 * @param declareNodes
107
	 *            If true (default=true) this reader outputs nodeAdded events.
108
	 */
109
	public FileSourceNCol(boolean declareNodes) {
110 1 1. : negated conditional → NO_COVERAGE
		nodes = declareNodes ? new HashSet<String>() : null;
111
	}
112
113
	// Commands
114
115
	@Override
116
	protected void continueParsingInInclude() throws IOException {
117
		// Should not happen, NCol files cannot be nested.
118
	}
119
120
	@Override
121
	public boolean nextEvents() throws IOException {
122
		String id1 = getWordOrNumberOrStringOrEolOrEof();
123
124 1 1. nextEvents : negated conditional → NO_COVERAGE
		if (id1.equals("EOL")) {
125
			// Empty line. Skip it.
126 1 1. nextEvents : negated conditional → NO_COVERAGE
		} else if (id1.equals("EOF")) {
127 1 1. nextEvents : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
			return false;
128
		} else {
129 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceNCol::declareNode → NO_COVERAGE
			declareNode(id1);
130
131
			String id2 = getWordOrNumberOrStringOrEolOrEof();
132
133 2 1. nextEvents : negated conditional → NO_COVERAGE
2. nextEvents : negated conditional → NO_COVERAGE
			if(!id2.equals("EOL") && !id2.equals("EOF")) {
134
				// Loops are not accepted by the format.
135 1 1. nextEvents : negated conditional → NO_COVERAGE
				if (!id1.equals(id2)) {
136
					// There may be a weight.
137
					String weight = getWordOrNumberOrStringOrEolOrEof();
138
					double w = 0.0;
139
					
140 2 1. nextEvents : negated conditional → NO_COVERAGE
2. nextEvents : negated conditional → NO_COVERAGE
					if(weight.equals("EOL") || weight.equals("EOF")) {
141
						weight = null;
142 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceNCol::pushBack → NO_COVERAGE
						pushBack();
143
					} else {
144
						try {
145
							w = Double.parseDouble(weight);
146
						} catch(Exception e) {
147
							throw new IOException(String.format("cannot transform weight %s into a number", weight));
148
						}
149
					}
150
					
151 1 1. nextEvents : Replaced integer addition with subtraction → NO_COVERAGE
					String edgeId = Integer.toString(edgeid++);
152
153 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceNCol::declareNode → NO_COVERAGE
					declareNode(id2);
154 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceNCol::sendEdgeAdded → NO_COVERAGE
					sendEdgeAdded(graphName, edgeId, id1, id2, false);
155
					
156 1 1. nextEvents : negated conditional → NO_COVERAGE
					if(weight != null)
157 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceNCol::sendEdgeAttributeAdded → NO_COVERAGE
						sendEdgeAttributeAdded(graphName, edgeId, "weight", (Double)w);
158
				}
159
			} else {
160
				throw new IOException("unexpected EOL or EOF");
161
			}
162
		}
163
164 1 1. nextEvents : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return true;
165
	}
166
167
	protected void declareNode(String id) {
168 1 1. declareNode : negated conditional → NO_COVERAGE
		if (nodes != null) {
169 1 1. declareNode : negated conditional → NO_COVERAGE
			if (!nodes.contains(id)) {
170 1 1. declareNode : removed call to org/graphstream/stream/file/FileSourceNCol::sendNodeAdded → NO_COVERAGE
				sendNodeAdded(graphName, id);
171
				nodes.add(id);
172
			}
173
		}
174
	}
175
176
	@Override
177
	public void begin(String filename) throws IOException {
178 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(filename);
179 1 1. begin : removed call to org/graphstream/stream/file/FileSourceNCol::init → NO_COVERAGE
		init();
180
	}
181
182
	@Override
183
	public void begin(URL url) throws IOException {
184 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(url);
185 1 1. begin : removed call to org/graphstream/stream/file/FileSourceNCol::init → NO_COVERAGE
		init();
186
	}
187
188
	@Override
189
	public void begin(InputStream stream) throws IOException {
190 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(stream);
191 1 1. begin : removed call to org/graphstream/stream/file/FileSourceNCol::init → NO_COVERAGE
		init();
192
	}
193
194
	@Override
195
	public void begin(Reader reader) throws IOException {
196 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(reader);
197 1 1. begin : removed call to org/graphstream/stream/file/FileSourceNCol::init → NO_COVERAGE
		init();
198
	}
199
200
	protected void init() throws IOException {
201 1 1. init : removed call to java/io/StreamTokenizer::eolIsSignificant → NO_COVERAGE
		st.eolIsSignificant(true);
202 1 1. init : removed call to java/io/StreamTokenizer::commentChar → NO_COVERAGE
		st.commentChar('#');
203
204
		graphName = String.format("%s_%d", graphName,
205 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));
206
	}
207
208
	public boolean nextStep() throws IOException {
209 1 1. nextStep : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return nextEvents();
210
	}
211
212
	@Override
213
	public void end() throws IOException {
214 1 1. end : removed call to org/graphstream/stream/file/FileSourceBase::end → NO_COVERAGE
		super.end();
215
	}
216
}

Mutations

110

1.1
Location :
Killed by : none
negated conditional → NO_COVERAGE

124

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

126

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

127

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

129

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

133

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

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

135

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

140

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

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

142

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

151

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

153

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

154

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

156

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

157

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

164

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

168

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

169

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

170

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

178

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

179

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

184

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

185

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

190

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

191

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

196

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

197

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

201

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

202

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

205

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

209

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

214

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