From a94a4920045418eb44510564e4a31c81dd6a9a01 Mon Sep 17 00:00:00 2001 From: oblonski <4sschroeder@gmail.com> Date: Mon, 8 Sep 2014 07:21:08 +0200 Subject: [PATCH] make GraphStreamView label times and job names parse seconds to time string --- .../analysis/toolbox/GraphStreamViewer.java | 52 ++++++++++++------- .../src/main/java/jsprit/core/util/Time.java | 43 +++++++++++---- .../test/java/jsprit/core/util/TimeTest.java | 44 +++++++++++----- 3 files changed, 97 insertions(+), 42 deletions(-) diff --git a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java index 83252ce6..6a85b7dd 100644 --- a/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java +++ b/jsprit-analysis/src/main/java/jsprit/analysis/toolbox/GraphStreamViewer.java @@ -16,6 +16,7 @@ ******************************************************************************/ package jsprit.analysis.toolbox; + import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.job.Job; 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.vehicle.PenaltyVehicleType; import jsprit.core.problem.vehicle.Vehicle; +import jsprit.core.util.Time; import org.graphstream.graph.Edge; import org.graphstream.graph.Graph; import org.graphstream.graph.Node; @@ -88,7 +90,7 @@ public class GraphStreamViewer { "}" ; public static enum Label { - NO_LABEL, ID, ACTIVITY + NO_LABEL, ID, JOB_NAME, ARRIVAL_TIME, DEPARTURE_TIME, ACTIVITY } private static class Center { @@ -160,10 +162,10 @@ public class GraphStreamViewer { * *

a zoomFactor < 1 zooms in and > 1 out. * - * @param centerX - * @param centerY - * @param zoomFactor - * @return + * @param centerX x coordinate of center + * @param centerY y coordinate of center + * @param zoomFactor zoom factor + * @return the viewer */ public GraphStreamViewer setCameraView(double centerX, double centerY, double zoomFactor){ center = new Center(centerX,centerY); @@ -278,7 +280,7 @@ public class GraphStreamViewer { Font font = Font.decode("couriernew"); - JLabel jobs = new JLabel(new String("jobs")); + JLabel jobs = new JLabel("jobs"); jobs.setFont(font); jobs.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling))); @@ -288,22 +290,22 @@ public class GraphStreamViewer { nJobs.setBorder(BorderFactory.createEmptyBorder()); nJobs.setBackground(new Color(230,230,230)); - JLabel costs = new JLabel(new String("costs")); + JLabel costs = new JLabel("costs"); costs.setFont(font); 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.setEditable(false); costsVal.setBorder(BorderFactory.createEmptyBorder()); costsVal.setBackground(new Color(230,230,230)); - JLabel vehicles = new JLabel(new String("routes")); + JLabel vehicles = new JLabel("routes"); vehicles.setFont(font); vehicles.setPreferredSize(new Dimension((int)(40*scaling),(int)(25*scaling))); // vehicles.setForeground(Color.DARK_GRAY); - JFormattedTextField vehVal = new JFormattedTextField(getNuRoutes()); + JFormattedTextField vehVal = new JFormattedTextField(getNoRoutes()); vehVal.setFont(font); vehVal.setEditable(false); vehVal.setBorder(BorderFactory.createEmptyBorder()); @@ -335,13 +337,13 @@ public class GraphStreamViewer { return panel; } - private Integer getNuRoutes() { - if(solution!=null) return Integer.valueOf(solution.getRoutes().size()); + private Integer getNoRoutes() { + if(solution!=null) return solution.getRoutes().size(); return 0; } private Double getSolutionCosts() { - if(solution!=null) return Double.valueOf(solution.getCost()); + if(solution!=null) return solution.getCost(); return 0.0; } @@ -373,9 +375,8 @@ public class GraphStreamViewer { } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); - }; - - } + } + } private void renderService(Graph g, Service service, Label label) { Node n = g.addNode(makeId(service.getId(),service.getLocationId())); @@ -387,7 +388,7 @@ public class GraphStreamViewer { } 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) { @@ -414,16 +415,29 @@ public class GraphStreamViewer { private void renderRoute(Graph g, VehicleRoute route, int routeId, long renderDelay_in_ms, Label label) { int vehicle_edgeId = 1; 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); n.addAttribute("ui.label", "start"); } 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)){ Node actNode = g.getNode(currIdentifier); 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); if(act instanceof PickupActivity) g.getNode(currIdentifier).addAttribute("ui.class", "pickupInRoute"); else if (act instanceof DeliveryActivity) g.getNode(currIdentifier).addAttribute("ui.class", "deliveryInRoute"); diff --git a/jsprit-core/src/main/java/jsprit/core/util/Time.java b/jsprit-core/src/main/java/jsprit/core/util/Time.java index 6c5047d2..7f1f92e8 100644 --- a/jsprit-core/src/main/java/jsprit/core/util/Time.java +++ b/jsprit-core/src/main/java/jsprit/core/util/Time.java @@ -1,25 +1,46 @@ /******************************************************************************* - * Copyright (c) 2014 Stefan Schroeder. - * + * Copyright (C) 2014 Stefan Schroeder + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * 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. - * - * You should have received a copy of the GNU Lesser General Public + * 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 * License along with this library. If not, see . - * - * Contributors: - * Stefan Schroeder - initial API and implementation ******************************************************************************/ package jsprit.core.util; 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. * @@ -32,7 +53,7 @@ public class Time { * 6:00 PM --> 6*3600. + 12.*3600. * 6:00:12 --> 6*3600. + 12. * - * @return + * @return seconds */ public static double parseTimeToSeconds(String timeString){ if(timeString.substring(0, 1).matches("\\D")) throw new IllegalArgumentException("timeString must start with digit [0-9]"); diff --git a/jsprit-core/src/test/java/jsprit/core/util/TimeTest.java b/jsprit-core/src/test/java/jsprit/core/util/TimeTest.java index 351b4026..bd927bfd 100644 --- a/jsprit-core/src/test/java/jsprit/core/util/TimeTest.java +++ b/jsprit-core/src/test/java/jsprit/core/util/TimeTest.java @@ -1,27 +1,25 @@ /******************************************************************************* - * Copyright (c) 2014 Stefan Schroeder. - * + * Copyright (C) 2014 Stefan Schroeder + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * 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. - * - * You should have received a copy of the GNU Lesser General Public + * 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 * License along with this library. If not, see . - * - * Contributors: - * Stefan Schroeder - initial API and implementation ******************************************************************************/ package jsprit.core.util; -import static org.junit.Assert.*; - import org.junit.Test; +import static org.junit.Assert.assertEquals; + public class TimeTest { @Test @@ -164,4 +162,26 @@ public class TimeTest { 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); + } + + }