Skip to content

Commit

Permalink
fix #69 : always use US locale when saving matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayette committed Feb 28, 2013
1 parent 7d02322 commit 4bde2f1
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/main/org/nlogo/extensions/nw/jung/io/Matrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@
package org.nlogo.extensions.nw.jung.io

import java.util.Random

import org.nlogo.agent.AgentSet
import org.nlogo.agent.Link
import org.nlogo.agent.Turtle
import org.nlogo.api.ExtensionException
import org.nlogo.extensions.nw.jung.DummyGraph
import org.nlogo.extensions.nw.jung.factoryFor

import edu.uci.ics.jung
import edu.uci.ics.jung.algorithms.matrix.GraphMatrixOperations

object Matrix {

def save(graph: jung.graph.Graph[Turtle, Link], filename: String) {
if (org.nlogo.workspace.AbstractWorkspace.isApplet)
throw new ExtensionException("Cannot save matrix file when in applet mode.")
new jung.io.MatrixFile[Turtle, Link](
null, // TODO: provide weight key (null means 1) (issue #19)
null, null, null // no factories needed for save
).save(graph, filename)
/* This is almost a line for line copy of jung.io.MatrixFile.save, the major
* difference being that it explicitly uses the US locale to make sure entries
* use the dot decimal separator (see issue #69) */
try {
val writer = new java.io.BufferedWriter(new java.io.FileWriter(filename))
val matrix = GraphMatrixOperations.graphToSparseMatrix(graph, null) // TODO: provide weights
for (i <- 0 until matrix.rows) {
for (j <- 0 until matrix.columns) {
val w = matrix.getQuick(i, j)
writer.write("%4.2f ".formatLocal(java.util.Locale.US, w))
}
writer.write("\n")
}
writer.close()
} catch {
case e: Exception => throw new ExtensionException("Error saving file: " + filename, e)
}
}

def load(filename: String, turtleBreed: AgentSet, linkBreed: AgentSet, rng: Random) = {
Expand Down

0 comments on commit 4bde2f1

Please sign in to comment.