FileSourceLGL.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 "LGL" graph format.
42
 * 
43
 * <p>
44
 * The LGL graph format is a simple format where each line beginning by a
45
 * sharp sign "#" describes a source vertex, and each subsequent line
46
 * not beginning by a sharp sign describe an edge target for this source.
47
 * </p>
48
 * 
49
 * <p>
50
 * Also, the format does not specify any direction for edges. By default all
51
 * edges are undirected.
52
 * </p>
53
 * 
54
 * <p>
55
 * This format only contains edges. To ensure the "add node" events are sent
56
 * before an edge referencing two nodes is created via an "add edge" event, this
57
 * reader has a hash set of already encountered nodes. The hash set allows to
58
 * issue "add node" events only when a node is encountered for the first time.
59
 * </p>
60
 * 
61
 * </p> This hash set consumes memory, but is the only way to ensure "add node"
62
 * events are correctly issued. If this input is directly connected to a graph,
63
 * as graphs can create non-existing nodes automatically, you can disable the
64
 * hash set of nodes using the constructor
65
 * {@link #FileSourceLGL(boolean)}, and giving "false" for the first
66
 * argument. </p>
67
 * 
68
 * The usual file name extension for this format is ".lgl".
69
 */
70
public class FileSourceLGL extends FileSourceBase {
71
	// Attribute
72
73
	/**
74
	 * Allocator for edge identifiers.
75
	 */
76
	protected int edgeid = 0;
77
78
	/**
79
	 * Set of existing nodes (if nodes are declared).
80
	 */
81
	protected HashSet<String> nodes;
82
83
	/**
84
	 * The current source node.
85
	 */
86
	protected String source;
87
88
	protected String graphName = "LGL_";
89
	
90
	// Construction
91
92
	/**
93
	 * New reader for the "LGL" format.
94
	 */
95
	public FileSourceLGL() {
96
		this(false);
97
	}
98
99
	/**
100
	 * New reader for the "LGL" format.
101
	 * 
102
	 * @param declareNodes
103
	 *            If true (default=true) this reader outputs nodeAdded events.
104
	 */
105
	public FileSourceLGL(boolean declareNodes) {
106 1 1. : negated conditional → NO_COVERAGE
		nodes = declareNodes ? new HashSet<String>() : null;
107
	}
108
109
	// Commands
110
111
	@Override
112
	protected void continueParsingInInclude() throws IOException {
113
		// Should not happen, NCol files cannot be nested.
114
	}
115
116
	@Override
117
	public boolean nextEvents() throws IOException {
118
		String id1 = getWordOrSymbolOrNumberOrStringOrEolOrEof();
119
120 1 1. nextEvents : negated conditional → NO_COVERAGE
		if (id1.equals("EOL")) {
121
			// Empty line. Skip it.
122 1 1. nextEvents : negated conditional → NO_COVERAGE
		} else if (id1.equals("EOF")) {
123 1 1. nextEvents : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
			return false;
124 1 1. nextEvents : negated conditional → NO_COVERAGE
		} else if (id1.equals("#")) {
125
			// A new sequence of edges starts
126
			String src = getWordOrNumberOrStringOrEolOrEof();
127
			
128 2 1. nextEvents : negated conditional → NO_COVERAGE
2. nextEvents : negated conditional → NO_COVERAGE
			if(!src.equals("EOL") && !src.equals("EOF")) {
129
				source = src;
130
			} else {
131
				source = null;
132
			}
133
		} else {
134
			// we got a new target.
135 1 1. nextEvents : negated conditional → NO_COVERAGE
			if(source != null) {
136
				String weight = getWordOrNumberOrStringOrEolOrEof();
137
				double w = 0.0;
138
				
139 2 1. nextEvents : negated conditional → NO_COVERAGE
2. nextEvents : negated conditional → NO_COVERAGE
				if(weight.equals("EOL") || weight.equals("EOF")) {
140
					weight = null;
141 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceLGL::pushBack → NO_COVERAGE
					pushBack();
142
				} else {
143
					try {
144
						w = Double.parseDouble(weight);
145
					} catch(Exception e) {
146
						throw new IOException(String.format("cannot transform weight %s into a number", weight));
147
					}
148
				}
149
				
150 1 1. nextEvents : Replaced integer addition with subtraction → NO_COVERAGE
				String edgeId = Integer.toString(edgeid++);
151
				
152 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceLGL::sendEdgeAdded → NO_COVERAGE
				sendEdgeAdded(graphName, edgeId, source, id1, false);
153
				
154 1 1. nextEvents : negated conditional → NO_COVERAGE
				if(weight != null) {
155 1 1. nextEvents : removed call to org/graphstream/stream/file/FileSourceLGL::sendEdgeAttributeAdded → NO_COVERAGE
					sendEdgeAttributeAdded(graphName, edgeId, "weight", (Double)w);
156
				}
157
			}
158
		}
159
160 1 1. nextEvents : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return true;
161
	}
162
163
	protected void declareNode(String id) {
164 1 1. declareNode : negated conditional → NO_COVERAGE
		if (nodes != null) {
165 1 1. declareNode : negated conditional → NO_COVERAGE
			if (!nodes.contains(id)) {
166 1 1. declareNode : removed call to org/graphstream/stream/file/FileSourceLGL::sendNodeAdded → NO_COVERAGE
				sendNodeAdded(graphName, id);
167
				nodes.add(id);
168
			}
169
		}
170
	}
171
172
	@Override
173
	public void begin(String filename) throws IOException {
174 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(filename);
175 1 1. begin : removed call to org/graphstream/stream/file/FileSourceLGL::init → NO_COVERAGE
		init();
176
	}
177
178
	@Override
179
	public void begin(URL url) throws IOException {
180 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(url);
181 1 1. begin : removed call to org/graphstream/stream/file/FileSourceLGL::init → NO_COVERAGE
		init();
182
	}
183
184
	@Override
185
	public void begin(InputStream stream) throws IOException {
186 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(stream);
187 1 1. begin : removed call to org/graphstream/stream/file/FileSourceLGL::init → NO_COVERAGE
		init();
188
	}
189
190
	@Override
191
	public void begin(Reader reader) throws IOException {
192 1 1. begin : removed call to org/graphstream/stream/file/FileSourceBase::begin → NO_COVERAGE
		super.begin(reader);
193 1 1. begin : removed call to org/graphstream/stream/file/FileSourceLGL::init → NO_COVERAGE
		init();
194
	}
195
196
	protected void init() throws IOException {
197 1 1. init : removed call to java/io/StreamTokenizer::eolIsSignificant → NO_COVERAGE
		st.eolIsSignificant(true);
198 1 1. init : removed call to java/io/StreamTokenizer::commentChar → NO_COVERAGE
		st.commentChar('%');
199
200
		graphName = String.format("%s_%d", graphName,
201 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));
202
	}
203
204
	public boolean nextStep() throws IOException {
205 1 1. nextStep : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		return nextEvents();
206
	}
207
208
	@Override
209
	public void end() throws IOException {
210 1 1. end : removed call to org/graphstream/stream/file/FileSourceBase::end → NO_COVERAGE
		super.end();
211
	}
212
}

Mutations

106

1.1
Location :
Killed by : none
negated conditional → NO_COVERAGE

120

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

122

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

123

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

124

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

128

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

139

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

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

141

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

150

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

152

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

154

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

155

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

160

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

164

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

165

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

166

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

174

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

175

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

180

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

181

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

186

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

187

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

192

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

193

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

197

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

198

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

201

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

205

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

210

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