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.pajek; | |
33 | ||
34 | import java.util.ArrayList; | |
35 | import java.util.Locale; | |
36 | ||
37 | import org.graphstream.graph.implementations.AbstractElement.AttributeChangeEvent; | |
38 | import org.graphstream.stream.SourceBase.ElementType; | |
39 | import org.graphstream.stream.file.FileSourcePajek; | |
40 | import org.graphstream.util.parser.ParseException; | |
41 | import org.graphstream.util.parser.Token; | |
42 | ||
43 | public class PajekContext { | |
44 | FileSourcePajek pajek; | |
45 | String sourceId; | |
46 | ||
47 | protected boolean directed = false; | |
48 | ||
49 | protected String weightAttributeName = "weight"; | |
50 | ||
51 | public PajekContext(FileSourcePajek pajek) { | |
52 | this.pajek = pajek; | |
53 | this.sourceId = String.format("<Pajek stream %d>", System.currentTimeMillis()); | |
54 | } | |
55 | ||
56 | protected void setDirected(boolean on) { | |
57 | directed = on; | |
58 | } | |
59 | ||
60 | protected int addNodes(Token nb) throws ParseException { | |
61 | int n = getInt(nb); | |
62 | ||
63 |
3
1. addNodes : changed conditional boundary → NO_COVERAGE 2. addNodes : Changed increment from 1 to -1 → NO_COVERAGE 3. addNodes : negated conditional → NO_COVERAGE |
for (int i = 1; i <= n; ++i) { |
64 |
1
1. addNodes : removed call to org/graphstream/stream/file/FileSourcePajek::sendNodeAdded → NO_COVERAGE |
pajek.sendNodeAdded(sourceId, String.format("%d", i)); |
65 | } | |
66 | ||
67 |
1
1. addNodes : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return n; |
68 | } | |
69 | ||
70 | protected void addGraphAttribute(String attr, String value) { | |
71 |
1
1. addGraphAttribute : removed call to org/graphstream/stream/file/FileSourcePajek::sendAttributeChangedEvent → NO_COVERAGE |
pajek.sendAttributeChangedEvent(sourceId, sourceId, ElementType.GRAPH, |
72 | attr, AttributeChangeEvent.ADD, null, value); | |
73 | } | |
74 | ||
75 | protected void addNodeLabel(String nb, String label) { | |
76 |
1
1. addNodeLabel : removed call to org/graphstream/stream/file/FileSourcePajek::sendAttributeChangedEvent → NO_COVERAGE |
pajek.sendAttributeChangedEvent(sourceId, nb, ElementType.NODE, |
77 | "ui.label", AttributeChangeEvent.ADD, null, label); | |
78 | } | |
79 | ||
80 | protected void addNodeGraphics(String id, NodeGraphics graphics) { | |
81 |
1
1. addNodeGraphics : removed call to org/graphstream/stream/file/FileSourcePajek::sendAttributeChangedEvent → NO_COVERAGE |
pajek.sendAttributeChangedEvent(sourceId, id, ElementType.NODE, |
82 | "ui.style", AttributeChangeEvent.ADD, null, graphics.getStyle()); | |
83 | } | |
84 | ||
85 | protected void addNodePosition(String id, Token x, Token y, Token z) | |
86 | throws ParseException { | |
87 | Object pos[] = new Object[3]; | |
88 | pos[0] = (Double) getReal(x); | |
89 | pos[1] = (Double) getReal(y); | |
90 |
1
1. addNodePosition : negated conditional → NO_COVERAGE |
pos[2] = z != null ? (Double) getReal(z) : 0; |
91 | | |
92 |
1
1. addNodePosition : removed call to org/graphstream/stream/file/FileSourcePajek::sendAttributeChangedEvent → NO_COVERAGE |
pajek.sendAttributeChangedEvent(sourceId, id, ElementType.NODE, |
93 | "xyz", AttributeChangeEvent.ADD, null, pos); | |
94 | } | |
95 | ||
96 | protected String addEdge(String src, String trg) { | |
97 | String id = String.format("%s_%s_%d", src, trg, | |
98 |
2
1. addEdge : Replaced double multiplication with division → NO_COVERAGE 2. addEdge : Replaced long addition with subtraction → NO_COVERAGE |
(long) (Math.random() * 100000) + System.currentTimeMillis()); |
99 | ||
100 |
1
1. addEdge : removed call to org/graphstream/stream/file/FileSourcePajek::sendEdgeAdded → NO_COVERAGE |
pajek.sendEdgeAdded(sourceId, id, src, trg, directed); |
101 | ||
102 |
1
1. addEdge : mutated return of Object value for org/graphstream/stream/file/pajek/PajekContext::addEdge to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return id; |
103 | } | |
104 | ||
105 | protected void addEdges(EdgeMatrix mat) { | |
106 | int size = mat.size(); | |
107 | int edgeid = 0; | |
108 | ||
109 |
3
1. addEdges : changed conditional boundary → NO_COVERAGE 2. addEdges : Changed increment from 1 to -1 → NO_COVERAGE 3. addEdges : negated conditional → NO_COVERAGE |
for (int line = 0; line < size; line++) { |
110 |
3
1. addEdges : changed conditional boundary → NO_COVERAGE 2. addEdges : Changed increment from 1 to -1 → NO_COVERAGE 3. addEdges : negated conditional → NO_COVERAGE |
for (int col = 0; col < size; col++) { |
111 |
1
1. addEdges : negated conditional → NO_COVERAGE |
if (mat.hasEdge(line, col)) { |
112 |
2
1. addEdges : Replaced integer addition with subtraction → NO_COVERAGE 2. addEdges : Replaced integer addition with subtraction → NO_COVERAGE |
String id = String.format("%d_%d_%d", line + 1, col + 1, |
113 |
1
1. addEdges : Changed increment from 1 to -1 → NO_COVERAGE |
edgeid++); |
114 |
1
1. addEdges : negated conditional → NO_COVERAGE |
if (mat.hasEdge(col, line)) { |
115 |
1
1. addEdges : removed call to org/graphstream/stream/file/FileSourcePajek::sendEdgeAdded → NO_COVERAGE |
pajek.sendEdgeAdded(sourceId, id, |
116 |
1
1. addEdges : Replaced integer addition with subtraction → NO_COVERAGE |
String.format("%d", line + 1), |
117 |
1
1. addEdges : Replaced integer addition with subtraction → NO_COVERAGE |
String.format("%d", col + 1), false); |
118 |
1
1. addEdges : removed call to org/graphstream/stream/file/pajek/EdgeMatrix::set → NO_COVERAGE |
mat.set(col, line, false); |
119 | } else { | |
120 |
1
1. addEdges : removed call to org/graphstream/stream/file/FileSourcePajek::sendEdgeAdded → NO_COVERAGE |
pajek.sendEdgeAdded(sourceId, id, |
121 |
1
1. addEdges : Replaced integer addition with subtraction → NO_COVERAGE |
String.format("%d", line + 1), |
122 |
1
1. addEdges : Replaced integer addition with subtraction → NO_COVERAGE |
String.format("%d", col + 1), true); |
123 | } | |
124 | } | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
129 | protected void addEdgeWeight(String id, Token nb) throws ParseException { | |
130 |
1
1. addEdgeWeight : removed call to org/graphstream/stream/file/FileSourcePajek::sendAttributeChangedEvent → NO_COVERAGE |
pajek.sendAttributeChangedEvent(sourceId, id, ElementType.EDGE, |
131 | weightAttributeName, AttributeChangeEvent.ADD, null, getReal(nb)); | |
132 | } | |
133 | ||
134 | protected void addEdgeGraphics(String id, EdgeGraphics graphics) { | |
135 |
1
1. addEdgeGraphics : removed call to org/graphstream/stream/file/FileSourcePajek::sendAttributeChangedEvent → NO_COVERAGE |
pajek.sendAttributeChangedEvent(sourceId, id, ElementType.EDGE, |
136 | "ui.style", AttributeChangeEvent.ADD, null, graphics.getStyle()); | |
137 | } | |
138 | ||
139 | protected static int getInt(Token nb) throws ParseException { | |
140 | try { | |
141 |
1
1. getInt : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return Integer.parseInt(nb.image); |
142 | } catch (Exception e) { | |
143 | throw new ParseException(String.format("%d:%d: %s not an integer", | |
144 | nb.beginLine, nb.beginColumn, nb.image)); | |
145 | } | |
146 | } | |
147 | ||
148 | protected static double getReal(Token nb) throws ParseException { | |
149 | try { | |
150 |
1
1. getReal : replaced return of double value with -(x + 1) for org/graphstream/stream/file/pajek/PajekContext::getReal → NO_COVERAGE |
return Double.parseDouble(nb.image); |
151 | } catch (Exception e) { | |
152 | throw new ParseException(String.format("%d:%d: %s not a real", | |
153 | nb.beginLine, nb.beginColumn, nb.image)); | |
154 | } | |
155 | } | |
156 | ||
157 | public static String toColorValue(Token R, Token G, Token B) | |
158 | throws ParseException { | |
159 | double r = getReal(R); | |
160 | double g = getReal(G); | |
161 | double b = getReal(B); | |
162 | ||
163 |
2
1. toColorValue : Replaced double multiplication with division → NO_COVERAGE 2. toColorValue : mutated return of Object value for org/graphstream/stream/file/pajek/PajekContext::toColorValue to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return String.format("rgb(%d, %d, %d)", (int) (r * 255), |
164 |
2
1. toColorValue : Replaced double multiplication with division → NO_COVERAGE 2. toColorValue : Replaced double multiplication with division → NO_COVERAGE |
(int) (g * 255), (int) (b * 255)); |
165 | } | |
166 | } | |
167 | ||
168 | abstract class Graphics { | |
169 | protected StringBuffer graphics = new StringBuffer(); | |
170 | ||
171 | public abstract void addKey(String key, String value, Token tk) | |
172 | throws ParseException; | |
173 | ||
174 | public String getStyle() { | |
175 |
1
1. getStyle : mutated return of Object value for org/graphstream/stream/file/pajek/Graphics::getStyle to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return graphics.toString(); |
176 | } | |
177 | ||
178 | protected double getReal(String nb, Token tk) throws ParseException { | |
179 | try { | |
180 |
1
1. getReal : replaced return of double value with -(x + 1) for org/graphstream/stream/file/pajek/Graphics::getReal → NO_COVERAGE |
return Double.parseDouble(nb); |
181 | } catch (Exception e) { | |
182 | throw new ParseException(String.format("%d:%d: %s not a real", | |
183 | tk.beginLine, tk.beginColumn, nb)); | |
184 | } | |
185 | } | |
186 | ||
187 | protected int getInt(String nb, Token tk) throws ParseException { | |
188 | try { | |
189 |
1
1. getInt : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return Integer.parseInt(nb); |
190 | } catch (Exception e) { | |
191 | throw new ParseException(String.format("%d:%d: %s not an integer", | |
192 | tk.beginLine, tk.beginColumn, nb)); | |
193 | } | |
194 | } | |
195 | } | |
196 | ||
197 | class NodeGraphics extends Graphics { | |
198 | @Override | |
199 | public void addKey(String key, String value, Token tk) | |
200 | throws ParseException { | |
201 |
1
1. addKey : negated conditional → NO_COVERAGE |
if (key.equals("shape")) { |
202 | graphics.append(String.format("shape: %s;", value)); | |
203 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("ic")) { |
204 | graphics.append(String.format("fill-color: %s;", value)); | |
205 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("bc")) { |
206 | graphics.append(String.format( | |
207 | "stroke-color: %s; stroke-mode: plain;", value)); | |
208 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("bw")) { |
209 | graphics.append(String.format(Locale.US, "stroke-width: %fpx;", | |
210 | getReal(value, tk))); | |
211 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("s_size")) { |
212 | graphics.append(String.format(Locale.US, "size: %fpx;", | |
213 | getReal(value, tk))); | |
214 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("lc")) { |
215 | graphics.append(String.format("text-color: %s;", value)); | |
216 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("fos")) { |
217 | graphics.append(String.format("text-size: %d;", getInt(value, tk))); | |
218 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("font")) { |
219 | graphics.append(String.format("text-font: %s;", value)); | |
220 | } | |
221 | } | |
222 | } | |
223 | ||
224 | class EdgeGraphics extends Graphics { | |
225 | @Override | |
226 | public void addKey(String key, String value, Token tk) | |
227 | throws ParseException { | |
228 |
1
1. addKey : negated conditional → NO_COVERAGE |
if (key.equals("w")) { |
229 | graphics.append(String.format(Locale.US, "size: %fpx;", | |
230 | getReal(value, tk))); | |
231 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("c")) { |
232 | graphics.append(String.format("fill-color: %s;", value)); | |
233 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("s")) { |
234 | double s = getReal(value, tk); | |
235 |
1
1. addKey : Replaced double multiplication with division → NO_COVERAGE |
graphics.append(String.format("arrow-size: %spx, %spx;", s * 5, |
236 |
1
1. addKey : Replaced double multiplication with division → NO_COVERAGE |
s * 3)); |
237 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("l")) { |
238 | // ? | |
239 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("p")) { |
240 | // ? | |
241 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("lc")) { |
242 | graphics.append(String.format("text-color: %s;", value)); | |
243 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("fos")) { |
244 | graphics.append(String.format("text-size: %d;", getInt(value, tk))); | |
245 |
1
1. addKey : negated conditional → NO_COVERAGE |
} else if (key.equals("font")) { |
246 | graphics.append(String.format("text-font: %s;", value)); | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | class EdgeMatrix { | |
252 | // Line first, col second. | |
253 | // Line = from node, col = to node. | |
254 | protected boolean mat[][]; | |
255 | ||
256 | protected int curLine = 0; | |
257 | ||
258 | public EdgeMatrix(int size) { | |
259 | mat = new boolean[size][size]; // Horror ! | |
260 | } | |
261 | ||
262 | public int size() { | |
263 |
1
1. size : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return mat.length; |
264 | } | |
265 | ||
266 | public boolean hasEdge(int line, int col) { | |
267 |
1
1. hasEdge : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return mat[line][col]; |
268 | } | |
269 | ||
270 | public void set(int line, int col, boolean value) { | |
271 | mat[line][col] = value; | |
272 | } | |
273 | ||
274 | public void addLine(ArrayList<String> line) { | |
275 |
2
1. addLine : changed conditional boundary → NO_COVERAGE 2. addLine : negated conditional → NO_COVERAGE |
if (curLine < mat.length) { |
276 |
1
1. addLine : negated conditional → NO_COVERAGE |
if (line.size() == mat.length) { |
277 |
3
1. addLine : changed conditional boundary → NO_COVERAGE 2. addLine : Changed increment from 1 to -1 → NO_COVERAGE 3. addLine : negated conditional → NO_COVERAGE |
for (int i = 0; i < mat.length; i++) { |
278 | mat[curLine][i] = line.get(i).equals("1"); | |
279 | } | |
280 |
1
1. addLine : Replaced integer addition with subtraction → NO_COVERAGE |
curLine++; |
281 |
2
1. addLine : Replaced integer multiplication with division → NO_COVERAGE 2. addLine : negated conditional → NO_COVERAGE |
} else if (line.size() == mat.length * mat.length) { |
282 |
1
1. addLine : Replaced integer multiplication with division → NO_COVERAGE |
int n = mat.length * mat.length; |
283 | curLine = -1; | |
284 |
3
1. addLine : changed conditional boundary → NO_COVERAGE 2. addLine : Changed increment from 1 to -1 → NO_COVERAGE 3. addLine : negated conditional → NO_COVERAGE |
for (int i = 0; i < n; i++) { |
285 |
2
1. addLine : Replaced integer modulus with multiplication → NO_COVERAGE 2. addLine : negated conditional → NO_COVERAGE |
if (i % mat.length == 0) |
286 |
1
1. addLine : Replaced integer addition with subtraction → NO_COVERAGE |
curLine++; |
287 |
2
1. addLine : Replaced integer multiplication with division → NO_COVERAGE 2. addLine : Replaced integer subtraction with addition → NO_COVERAGE |
mat[curLine][i - (curLine * mat.length)] = line.get(i) |
288 | .equals("1"); | |
289 | } | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | @Override | |
295 | public String toString() { | |
296 | StringBuffer buffer = new StringBuffer(); | |
297 |
3
1. toString : changed conditional boundary → NO_COVERAGE 2. toString : Changed increment from 1 to -1 → NO_COVERAGE 3. toString : negated conditional → NO_COVERAGE |
for (int line = 0; line < mat.length; line++) { |
298 |
3
1. toString : changed conditional boundary → NO_COVERAGE 2. toString : Changed increment from 1 to -1 → NO_COVERAGE 3. toString : negated conditional → NO_COVERAGE |
for (int col = 0; col < mat.length; col++) { |
299 |
1
1. toString : negated conditional → NO_COVERAGE |
buffer.append(String.format("%s ", mat[line][col] ? "1" : "0")); |
300 | } | |
301 | buffer.append(String.format("%n")); | |
302 | } | |
303 | ||
304 |
1
1. toString : mutated return of Object value for org/graphstream/stream/file/pajek/EdgeMatrix::toString to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return buffer.toString(); |
305 | } | |
306 | } | |
Mutations | ||
63 |
1.1 2.2 3.3 |
|
64 |
1.1 |
|
67 |
1.1 |
|
71 |
1.1 |
|
76 |
1.1 |
|
81 |
1.1 |
|
90 |
1.1 |
|
92 |
1.1 |
|
98 |
1.1 2.2 |
|
100 |
1.1 |
|
102 |
1.1 |
|
109 |
1.1 2.2 3.3 |
|
110 |
1.1 2.2 3.3 |
|
111 |
1.1 |
|
112 |
1.1 2.2 |
|
113 |
1.1 |
|
114 |
1.1 |
|
115 |
1.1 |
|
116 |
1.1 |
|
117 |
1.1 |
|
118 |
1.1 |
|
120 |
1.1 |
|
121 |
1.1 |
|
122 |
1.1 |
|
130 |
1.1 |
|
135 |
1.1 |
|
141 |
1.1 |
|
150 |
1.1 |
|
163 |
1.1 2.2 |
|
164 |
1.1 2.2 |
|
175 |
1.1 |
|
180 |
1.1 |
|
189 |
1.1 |
|
201 |
1.1 |
|
203 |
1.1 |
|
205 |
1.1 |
|
208 |
1.1 |
|
211 |
1.1 |
|
214 |
1.1 |
|
216 |
1.1 |
|
218 |
1.1 |
|
228 |
1.1 |
|
231 |
1.1 |
|
233 |
1.1 |
|
235 |
1.1 |
|
236 |
1.1 |
|
237 |
1.1 |
|
239 |
1.1 |
|
241 |
1.1 |
|
243 |
1.1 |
|
245 |
1.1 |
|
263 |
1.1 |
|
267 |
1.1 |
|
275 |
1.1 2.2 |
|
276 |
1.1 |
|
277 |
1.1 2.2 3.3 |
|
280 |
1.1 |
|
281 |
1.1 2.2 |
|
282 |
1.1 |
|
284 |
1.1 2.2 3.3 |
|
285 |
1.1 2.2 |
|
286 |
1.1 |
|
287 |
1.1 2.2 |
|
297 |
1.1 2.2 3.3 |
|
298 |
1.1 2.2 3.3 |
|
299 |
1.1 |
|
304 |
1.1 |