1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

make GraphStreamView label times and job names

parse seconds to time string
This commit is contained in:
oblonski 2014-09-08 07:21:08 +02:00
parent bd59d9478c
commit a94a492004
3 changed files with 97 additions and 42 deletions

View file

@ -16,6 +16,7 @@
******************************************************************************/ ******************************************************************************/
package jsprit.analysis.toolbox; package jsprit.analysis.toolbox;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
@ -28,6 +29,7 @@ import jsprit.core.problem.solution.route.activity.TourActivity;
import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity; import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
import jsprit.core.problem.vehicle.PenaltyVehicleType; import jsprit.core.problem.vehicle.PenaltyVehicleType;
import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.util.Time;
import org.graphstream.graph.Edge; import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph; import org.graphstream.graph.Graph;
import org.graphstream.graph.Node; import org.graphstream.graph.Node;
@ -88,7 +90,7 @@ public class GraphStreamViewer {
"}" ; "}" ;
public static enum Label { public static enum Label {
NO_LABEL, ID, ACTIVITY NO_LABEL, ID, JOB_NAME, ARRIVAL_TIME, DEPARTURE_TIME, ACTIVITY
} }
private static class Center { private static class Center {
@ -160,10 +162,10 @@ public class GraphStreamViewer {
* *
* <p>a zoomFactor < 1 zooms in and > 1 out. * <p>a zoomFactor < 1 zooms in and > 1 out.
* *
* @param centerX * @param centerX x coordinate of center
* @param centerY * @param centerY y coordinate of center
* @param zoomFactor * @param zoomFactor zoom factor
* @return * @return the viewer
*/ */
public GraphStreamViewer setCameraView(double centerX, double centerY, double zoomFactor){ public GraphStreamViewer setCameraView(double centerX, double centerY, double zoomFactor){
center = new Center(centerX,centerY); center = new Center(centerX,centerY);
@ -278,7 +280,7 @@ public class GraphStreamViewer {
Font font = Font.decode("couriernew"); Font font = Font.decode("couriernew");
JLabel jobs = new JLabel(new String("jobs")); JLabel jobs = new JLabel("jobs");
jobs.setFont(font); jobs.setFont(font);
jobs.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling))); jobs.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling)));
@ -288,22 +290,22 @@ public class GraphStreamViewer {
nJobs.setBorder(BorderFactory.createEmptyBorder()); nJobs.setBorder(BorderFactory.createEmptyBorder());
nJobs.setBackground(new Color(230,230,230)); nJobs.setBackground(new Color(230,230,230));
JLabel costs = new JLabel(new String("costs")); JLabel costs = new JLabel("costs");
costs.setFont(font); costs.setFont(font);
costs.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling))); costs.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling)));
JFormattedTextField costsVal = new JFormattedTextField(new Double(getSolutionCosts())); JFormattedTextField costsVal = new JFormattedTextField(getSolutionCosts());
costsVal.setFont(font); costsVal.setFont(font);
costsVal.setEditable(false); costsVal.setEditable(false);
costsVal.setBorder(BorderFactory.createEmptyBorder()); costsVal.setBorder(BorderFactory.createEmptyBorder());
costsVal.setBackground(new Color(230,230,230)); costsVal.setBackground(new Color(230,230,230));
JLabel vehicles = new JLabel(new String("routes")); JLabel vehicles = new JLabel("routes");
vehicles.setFont(font); vehicles.setFont(font);
vehicles.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling))); vehicles.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling)));
// vehicles.setForeground(Color.DARK_GRAY); // vehicles.setForeground(Color.DARK_GRAY);
JFormattedTextField vehVal = new JFormattedTextField(getNuRoutes()); JFormattedTextField vehVal = new JFormattedTextField(getNoRoutes());
vehVal.setFont(font); vehVal.setFont(font);
vehVal.setEditable(false); vehVal.setEditable(false);
vehVal.setBorder(BorderFactory.createEmptyBorder()); vehVal.setBorder(BorderFactory.createEmptyBorder());
@ -335,13 +337,13 @@ public class GraphStreamViewer {
return panel; return panel;
} }
private Integer getNuRoutes() { private Integer getNoRoutes() {
if(solution!=null) return Integer.valueOf(solution.getRoutes().size()); if(solution!=null) return solution.getRoutes().size();
return 0; return 0;
} }
private Double getSolutionCosts() { private Double getSolutionCosts() {
if(solution!=null) return Double.valueOf(solution.getCost()); if(solution!=null) return solution.getCost();
return 0.0; return 0.0;
} }
@ -373,8 +375,7 @@ public class GraphStreamViewer {
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
}; }
} }
private void renderService(Graph g, Service service, Label label) { private void renderService(Graph g, Service service, Label label) {
@ -387,7 +388,7 @@ public class GraphStreamViewer {
} }
private String makeId(String id, String locationId) { private String makeId(String id, String locationId) {
return new StringBuffer().append(id).append("_").append(locationId).toString(); return id + "_" + locationId;
} }
private void renderVehicle(Graph g, Vehicle vehicle, Label label) { private void renderVehicle(Graph g, Vehicle vehicle, Label label) {
@ -414,16 +415,29 @@ public class GraphStreamViewer {
private void renderRoute(Graph g, VehicleRoute route, int routeId, long renderDelay_in_ms, Label label) { private void renderRoute(Graph g, VehicleRoute route, int routeId, long renderDelay_in_ms, Label label) {
int vehicle_edgeId = 1; int vehicle_edgeId = 1;
String prevIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getStartLocationId()); String prevIdentifier = makeId(route.getVehicle().getId(),route.getVehicle().getStartLocationId());
if(label.equals(Label.ACTIVITY)){ if(label.equals(Label.ACTIVITY) || label.equals(Label.JOB_NAME)){
Node n = g.getNode(prevIdentifier); Node n = g.getNode(prevIdentifier);
n.addAttribute("ui.label", "start"); n.addAttribute("ui.label", "start");
} }
for(TourActivity act : route.getActivities()){ for(TourActivity act : route.getActivities()){
String currIdentifier = makeId(((JobActivity)act).getJob().getId(),act.getLocationId()); Job job = ((JobActivity) act).getJob();
String currIdentifier = makeId(job.getId(),act.getLocationId());
if(label.equals(Label.ACTIVITY)){ if(label.equals(Label.ACTIVITY)){
Node actNode = g.getNode(currIdentifier); Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", act.getName()); actNode.addAttribute("ui.label", act.getName());
} }
else if(label.equals(Label.JOB_NAME)){
Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", job.getName());
}
else if(label.equals(Label.ARRIVAL_TIME)){
Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", Time.parseSecondsToTime(act.getArrTime()));
}
else if(label.equals(Label.DEPARTURE_TIME)){
Node actNode = g.getNode(currIdentifier);
actNode.addAttribute("ui.label", Time.parseSecondsToTime(act.getEndTime()));
}
g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, currIdentifier, true); g.addEdge(makeEdgeId(routeId,vehicle_edgeId), prevIdentifier, currIdentifier, true);
if(act instanceof PickupActivity) g.getNode(currIdentifier).addAttribute("ui.class", "pickupInRoute"); if(act instanceof PickupActivity) g.getNode(currIdentifier).addAttribute("ui.class", "pickupInRoute");
else if (act instanceof DeliveryActivity) g.getNode(currIdentifier).addAttribute("ui.class", "deliveryInRoute"); else if (act instanceof DeliveryActivity) g.getNode(currIdentifier).addAttribute("ui.class", "deliveryInRoute");

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2014 Stefan Schroeder. * Copyright (C) 2014 Stefan Schroeder
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -8,18 +8,39 @@
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/ ******************************************************************************/
package jsprit.core.util; package jsprit.core.util;
public class Time { public class Time {
/**
* Parses seconds to this time format: hh:mm:ss {AM|PM}
*
* @param seconds seconds
* @return time string
*/
public static String parseSecondsToTime(double seconds){
int hours = (int)Math.floor(seconds / 3600.);
int min = (int)Math.floor((seconds - hours * 3600.) / 60.);
int secs = (int)(seconds - (hours * 3600. + min * 60.));
String dayTime = "AM";
if(hours > 12) {
dayTime = "PM";
hours -= 12;
}
String hourString = "0" + hours;
if(hours > 9) hourString = "" + hours;
String minString = "" + min;
if(min < 10) minString = "0" + min;
String secString = "" + secs;
if(secs < 10) secString = "0" + secs;
return hourString + ":" + minString + ":" + secString + " " + dayTime;
}
/** /**
* Parse time to seconds. * Parse time to seconds.
* *
@ -32,7 +53,7 @@ public class Time {
* 6:00 PM --> 6*3600. + 12.*3600. * 6:00 PM --> 6*3600. + 12.*3600.
* 6:00:12 --> 6*3600. + 12. * 6:00:12 --> 6*3600. + 12.
* *
* @return * @return seconds
*/ */
public static double parseTimeToSeconds(String timeString){ public static double parseTimeToSeconds(String timeString){
if(timeString.substring(0, 1).matches("\\D")) throw new IllegalArgumentException("timeString must start with digit [0-9]"); if(timeString.substring(0, 1).matches("\\D")) throw new IllegalArgumentException("timeString must start with digit [0-9]");

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2014 Stefan Schroeder. * Copyright (C) 2014 Stefan Schroeder
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -8,20 +8,18 @@
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public * You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* Stefan Schroeder - initial API and implementation
******************************************************************************/ ******************************************************************************/
package jsprit.core.util; package jsprit.core.util;
import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TimeTest { public class TimeTest {
@Test @Test
@ -164,4 +162,26 @@ public class TimeTest {
assertEquals(1.,sec,0.01); assertEquals(1.,sec,0.01);
} }
@Test
public void whenSecIs3600_shouldReturnCorrectTimeString(){
String time = Time.parseSecondsToTime(3600);
System.out.println(time);
assertEquals(3600.,Time.parseTimeToSeconds(time),0.01);
}
@Test
public void whenSecIs4000_shouldReturnCorrectTimeString(){
String time = Time.parseSecondsToTime(4000);
System.out.println(time);
assertEquals(4000.,Time.parseTimeToSeconds(time),0.01);
}
@Test
public void whenSecIs86399_shouldReturnCorrectTimeString(){
String time = Time.parseSecondsToTime(86399);
System.out.println(time);
assertEquals(86399.,Time.parseTimeToSeconds(time),0.01);
}
} }