From: Stefan F. <ste...@us...> - 2010-05-24 20:37:26
|
Update of /cvsroot/rails/18xx/rails/algorithms In directory sfp-cvsdas-4.v30.ch3.sourceforge.com:/tmp/cvs-serv30941/rails/algorithms Modified Files: RevenueTrainRun.java NetworkVertex.java Log Message: Some minor improvements to pretty print Index: NetworkVertex.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/NetworkVertex.java,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** NetworkVertex.java 23 May 2010 18:10:44 -0000 1.16 --- NetworkVertex.java 24 May 2010 20:37:17 -0000 1.17 *************** *** 31,34 **** --- 31,38 ---- HQ, } + public static enum StationType { + MAJOR, + MINOR + } // vertex types and flag for virtual (thus not related to a rails object) *************** *** 40,45 **** // general vertex properties ! private boolean major = false; ! private boolean minor = false; private int value = 0; private boolean sink = false; --- 44,48 ---- // general vertex properties ! private StationType stationType; private int value = 0; private boolean sink = false; *************** *** 107,111 **** void addToRevenueCalculator(RevenueCalculator rc, int vertexId) { ! rc.setVertex(vertexId, major, minor, sink); } --- 110,114 ---- void addToRevenueCalculator(RevenueCalculator rc, int vertexId) { ! rc.setVertex(vertexId, isMajor(), isMinor(), sink); } *************** *** 141,160 **** } - public boolean isMajor(){ ! return major; ! } ! ! public NetworkVertex setMajor(boolean major) { ! this.major = major; ! return this; } public boolean isMinor(){ ! return minor; } ! public NetworkVertex setMinor(boolean minor) { ! this.minor = minor; return this; } --- 144,161 ---- } public boolean isMajor(){ ! return (stationType != null && stationType == StationType.MAJOR); } public boolean isMinor(){ ! return (stationType != null && stationType == StationType.MINOR); ! } ! ! public StationType getStationType() { ! return stationType; } ! public NetworkVertex setStationType(StationType stationType) { ! this.stationType = stationType; return this; } *************** *** 166,172 **** public int getValueByTrain(NetworkTrain train) { int valueByTrain; ! if (major) { valueByTrain = value * train.getMultiplyMajors(); ! } else if (minor) { if (train.ignoresMinors()) { valueByTrain = 0; --- 167,173 ---- public int getValueByTrain(NetworkTrain train) { int valueByTrain; ! if (isMajor()) { valueByTrain = value * train.getMultiplyMajors(); ! } else if (isMinor()) { if (train.ignoresMinors()) { valueByTrain = 0; *************** *** 219,224 **** return side; } ! ! /** * Initialize for rails vertexes --- 220,228 ---- return side; } ! ! public boolean isOfType(VertexType vertexType, StationType stationType) { ! return (type == vertexType && (!isStation() || getStationType() == stationType)); ! } ! /** * Initialize for rails vertexes *************** *** 232,239 **** // check if it is a major or minor if (station.getType().equals(Station.CITY) || station.getType().equals(Station.OFF_MAP_AREA)) { ! major = true; } else if (station.getType().equals(Station.TOWN) || station.getType().equals(Station.PORT) || station.getType().equals(Station.HALT)) { ! minor = true; } --- 236,243 ---- // check if it is a major or minor if (station.getType().equals(Station.CITY) || station.getType().equals(Station.OFF_MAP_AREA)) { ! setStationType(StationType.MAJOR); } else if (station.getType().equals(Station.TOWN) || station.getType().equals(Station.PORT) || station.getType().equals(Station.HALT)) { ! setStationType(StationType.MINOR); } *************** *** 304,307 **** --- 308,321 ---- } + public static void initAllRailsVertices(Collection<NetworkVertex> vertices, + PublicCompanyI company, PhaseI phase) { + for (NetworkVertex v:vertices) { + if (company != null) + v.initRailsVertex(company); + if (phase != null) + v.setRailsVertexValue(phase); + } + } + /** * Returns the maximum positive value (lower bound zero) *************** *** 315,326 **** } ! public static void initAllRailsVertices(Collection<NetworkVertex> vertices, ! PublicCompanyI company, PhaseI phase) { ! for (NetworkVertex v:vertices) { ! if (company != null) ! v.initRailsVertex(company); ! if (phase != null) ! v.setRailsVertexValue(phase); } } --- 329,343 ---- } ! ! /** ! * Returns the number of specified vertex type in a vertex collection ! * If station then specify station type ! */ ! public static int numberOfVertexType(Collection<NetworkVertex> vertices, VertexType vertexType, StationType stationType) { ! int number = 0; ! for (NetworkVertex vertex:vertices) { ! if (vertex.isOfType(vertexType, stationType)) number++; } + return number; } *************** *** 333,338 **** NetworkVertex newVertex = NetworkVertex.getVirtualVertex(vertex.type, newIdentifier); // copy values ! newVertex.major = vertex.major; ! newVertex.minor = vertex.minor; newVertex.value = vertex.value; newVertex.sink = vertex.sink; --- 350,354 ---- NetworkVertex newVertex = NetworkVertex.getVirtualVertex(vertex.type, newIdentifier); // copy values ! newVertex.stationType = vertex.stationType; newVertex.value = vertex.value; newVertex.sink = vertex.sink; Index: RevenueTrainRun.java =================================================================== RCS file: /cvsroot/rails/18xx/rails/algorithms/RevenueTrainRun.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** RevenueTrainRun.java 24 May 2010 07:49:26 -0000 1.6 --- RevenueTrainRun.java 24 May 2010 20:37:17 -0000 1.7 *************** *** 4,13 **** --- 4,18 ---- import java.awt.geom.Point2D; import java.util.ArrayList; + import java.util.HashSet; import java.util.List; import java.util.Map; + import java.util.Set; import org.apache.log4j.Logger; + import rails.algorithms.NetworkVertex.StationType; + import rails.algorithms.NetworkVertex.VertexType; import rails.ui.swing.hexmap.HexMap; + import rails.util.LocalText; /** *************** *** 20,23 **** --- 25,29 ---- private static final int PRETTY_PRINT_LENGTH = 100; + private static final int PRETTY_PRINT_INDENT = 10; protected static Logger log = *************** *** 37,44 **** } ! public List<NetworkVertex> getVertices() { return vertices; } public NetworkTrain getTrain() { return train; --- 43,54 ---- } ! public List<NetworkVertex> getRunVertices() { return vertices; } + public Set<NetworkVertex> getUniqueVertices() { + return new HashSet<NetworkVertex>(vertices); + } + public NetworkTrain getTrain() { return train; *************** *** 61,64 **** --- 71,84 ---- return value; } + + boolean hasButtomRun() { + boolean buttomRun = false; + NetworkVertex startVertex = null; + for (NetworkVertex vertex:vertices) { + if (startVertex == vertex) buttomRun = true; + if (startVertex == null) startVertex = vertex; + } + return buttomRun; + } void addVertex(NetworkVertex vertex) { *************** *** 75,82 **** private int prettyPrintNewLine(StringBuffer runPrettyPrint, int multiple, int initLength) { ! if (runPrettyPrint.length() / PRETTY_PRINT_LENGTH != multiple) { ! multiple = runPrettyPrint.length() / PRETTY_PRINT_LENGTH; runPrettyPrint.append("\n"); ! for (int i=0; i < initLength; i++) runPrettyPrint.append(" ") ; } --- 95,103 ---- private int prettyPrintNewLine(StringBuffer runPrettyPrint, int multiple, int initLength) { ! int length = runPrettyPrint.length() - initLength; ! if (length / PRETTY_PRINT_LENGTH != multiple) { ! multiple = length / PRETTY_PRINT_LENGTH; runPrettyPrint.append("\n"); ! for (int i=0; i < PRETTY_PRINT_INDENT; i++) runPrettyPrint.append(" ") ; } *************** *** 86,92 **** String prettyPrint() { StringBuffer runPrettyPrint = new StringBuffer(); ! runPrettyPrint.append("Train " + train + ": " + getRunValue() + " -> "); int initLength = runPrettyPrint.length(); ! int multiple = runPrettyPrint.length() / PRETTY_PRINT_LENGTH; String currentHexName = null; NetworkVertex startVertex = null; --- 107,124 ---- String prettyPrint() { StringBuffer runPrettyPrint = new StringBuffer(); ! runPrettyPrint.append(LocalText.getText("N_Train", train.toString())); ! runPrettyPrint.append(": " + getRunValue()); ! ! Set<NetworkVertex> uniqueVertices = getUniqueVertices(); ! int majors = NetworkVertex.numberOfVertexType(uniqueVertices, VertexType.STATION, StationType.MAJOR); ! int minors = NetworkVertex.numberOfVertexType(uniqueVertices, VertexType.STATION, StationType.MINOR); ! if (train.ignoresMinors()) { ! runPrettyPrint.append(LocalText.getText("RevenueStationsIgnoreMinors", majors)); ! } else { ! runPrettyPrint.append(LocalText.getText("RevenueStations", majors, minors)); ! } ! int initLength = runPrettyPrint.length(); ! int multiple = prettyPrintNewLine(runPrettyPrint, -1, initLength); String currentHexName = null; NetworkVertex startVertex = null; |