mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
removed constructor from StateManager and its according implications
This commit is contained in:
parent
279826f061
commit
00eef79679
6 changed files with 28 additions and 196 deletions
|
|
@ -23,9 +23,7 @@ package jsprit.core.problem.solution.route.state;
|
|||
import jsprit.core.problem.HasIndex;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class StateFactory {
|
||||
|
|
@ -33,105 +31,53 @@ public class StateFactory {
|
|||
|
||||
public interface StateId extends HasIndex{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public interface State {
|
||||
double toDouble();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public interface States {
|
||||
|
||||
State getState(StateId key);
|
||||
|
||||
void putState(StateId id, State state);
|
||||
|
||||
}
|
||||
|
||||
|
||||
static class StateImpl implements State{
|
||||
double state;
|
||||
|
||||
public StateImpl(double state) {
|
||||
super();
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double toDouble() {
|
||||
return state;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class StatesImpl implements States{
|
||||
|
||||
private Map<StateId,State> states = new HashMap<StateId, State>();
|
||||
|
||||
public void putState(StateId key, State state) {
|
||||
states.put(key, state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public State getState(StateId key) {
|
||||
return states.get(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static StateId MAXLOAD = new StateIdImpl("maxload", 0);
|
||||
public final static StateId MAXLOAD = new StateIdImpl("max_load", 0);
|
||||
|
||||
public final static StateId LOAD = new StateIdImpl("load", 1);
|
||||
|
||||
public final static StateId COSTS = new StateIdImpl("costs", 2);
|
||||
|
||||
public final static StateId LOAD_AT_BEGINNING = new StateIdImpl("loadAtBeginning", 3);
|
||||
public final static StateId LOAD_AT_BEGINNING = new StateIdImpl("load_at_beginning", 3);
|
||||
|
||||
public final static StateId LOAD_AT_END = new StateIdImpl("loadAtEnd", 4);
|
||||
public final static StateId LOAD_AT_END = new StateIdImpl("load_at_end", 4);
|
||||
|
||||
public final static StateId DURATION = new StateIdImpl("duration", 5);
|
||||
|
||||
public final static StateId LATEST_OPERATION_START_TIME = new StateIdImpl("latestOST", 6);
|
||||
public final static StateId LATEST_OPERATION_START_TIME = new StateIdImpl("latest_ost", 6);
|
||||
|
||||
public final static StateId EARLIEST_OPERATION_START_TIME = new StateIdImpl("earliestOST", 7);
|
||||
public final static StateId EARLIEST_OPERATION_START_TIME = new StateIdImpl("earliest_ost", 7);
|
||||
|
||||
public final static StateId FUTURE_MAXLOAD = new StateIdImpl("futureMaxload", 8);
|
||||
public final static StateId FUTURE_MAXLOAD = new StateIdImpl("future_max_load", 8);
|
||||
|
||||
public final static StateId PAST_MAXLOAD = new StateIdImpl("pastMaxload", 9);
|
||||
public final static StateId PAST_MAXLOAD = new StateIdImpl("past_max_load", 9);
|
||||
|
||||
final static List<String> reservedIds = Arrays.asList("maxload","load","costs","loadAtBeginning","loadAtEnd","duration","latestOST","earliestOST"
|
||||
,"futureMaxload","pastMaxload");
|
||||
final static List<String> reservedIds = Arrays.asList("max_load","load","costs","load_at_beginning","load_at_end","duration","latest_ost","earliest_ost"
|
||||
,"future_max_load","past_max_load");
|
||||
|
||||
|
||||
public static States createStates(){
|
||||
return new StatesImpl();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Deprecated
|
||||
public static StateId createId(String name){
|
||||
if(reservedIds.contains(name)){ throwReservedIdException(name); }
|
||||
return new StateIdImpl(name, -1);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static StateId createId(String name, int index){
|
||||
if(reservedIds.contains(name)) throwReservedIdException(name);
|
||||
if(index < 10) throwReservedIdException(name);
|
||||
return new StateIdImpl(name, index);
|
||||
}
|
||||
|
||||
public static State createState(double value){
|
||||
return new StateImpl(value);
|
||||
}
|
||||
|
||||
|
||||
public static boolean isReservedId(String stateId){
|
||||
if(reservedIds.contains(stateId)) return true;
|
||||
return false;
|
||||
return reservedIds.contains(stateId);
|
||||
}
|
||||
|
||||
public static boolean isReservedId(StateId stateId){
|
||||
if(reservedIds.contains(stateId.toString())) return true;
|
||||
return false;
|
||||
return reservedIds.contains(stateId.toString());
|
||||
}
|
||||
|
||||
public static void throwReservedIdException(String name) {
|
||||
|
|
|
|||
|
|
@ -1,83 +0,0 @@
|
|||
/*******************************************************************************
|
||||
* 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
|
||||
* 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
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Contributors:
|
||||
* Stefan Schroeder - initial API and implementation
|
||||
******************************************************************************/
|
||||
package jsprit.core.algorithm.state;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import jsprit.core.problem.Capacity;
|
||||
import jsprit.core.problem.job.Job;
|
||||
import jsprit.core.problem.job.Service;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory.State;
|
||||
|
||||
public class GenericsTest {
|
||||
|
||||
|
||||
|
||||
// static class State<T> {
|
||||
// Class<T> type;
|
||||
// T state;
|
||||
// public State(Class<T> type, T state) {
|
||||
// super();
|
||||
// this.type = type;
|
||||
// this.state = state;
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
static class States {
|
||||
|
||||
private Map<String,Object> states = new HashMap<String,Object>();
|
||||
|
||||
public <T> void putState(String id, Class<T> type, T state){
|
||||
states.put(id, type.cast(state));
|
||||
}
|
||||
|
||||
public <T> T getState(String id, Class<T> type){
|
||||
T s = type.cast(states.get(id));
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
States states = new States();
|
||||
states.putState("load", Double.class, 0.1);
|
||||
states.putState("state", String.class, "foo");
|
||||
states.putState("job", Job.class, Service.Builder.newInstance("foo").setLocationId("loc").build());
|
||||
states.putState("cap", Capacity.class, Capacity.Builder.newInstance().addDimension(0, 1).build());
|
||||
|
||||
Double load = states.getState("load", Double.class);
|
||||
String state = states.getState("state", String.class);
|
||||
Job service = states.getState("job", Job.class);
|
||||
Capacity capacity = states.getState("cap", Capacity.class);
|
||||
|
||||
states.putState("st", State.class, StateFactory.createState(10.));
|
||||
|
||||
System.out.println(load);
|
||||
System.out.println(state);
|
||||
System.out.println(service);
|
||||
System.out.println(capacity);
|
||||
System.out.println(states.getState("st", State.class).toDouble());
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -24,7 +24,6 @@ import jsprit.core.problem.job.Service;
|
|||
import jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import jsprit.core.problem.solution.route.activity.TourActivity;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory.State;
|
||||
import jsprit.core.problem.solution.route.state.StateFactory.StateId;
|
||||
import jsprit.core.problem.vehicle.Vehicle;
|
||||
import jsprit.core.problem.vehicle.VehicleImpl;
|
||||
|
|
@ -34,20 +33,9 @@ import org.junit.Test;
|
|||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class StateManagerTest {
|
||||
|
||||
@Test
|
||||
public void whenRouteStateIsSetWithGenericMethod_itMustBeSetCorrectly(){
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().build();
|
||||
VehicleRoute route = getRoute(mock(Vehicle.class));
|
||||
StateManager stateManager = new StateManager(vrp);
|
||||
StateId id = stateManager.createStateId("myState");
|
||||
State state = StateFactory.createState(1.);
|
||||
stateManager.putRouteState(route, id, state);
|
||||
assertEquals(1.,stateManager.getRouteState(route, id, State.class).toDouble(),0.01);
|
||||
}
|
||||
|
||||
private VehicleRoute getRoute(Vehicle vehicle) {
|
||||
return VehicleRoute.Builder.newInstance(vehicle).addService(Service.Builder.newInstance("s").setLocationId("loc").build()).build();
|
||||
|
|
@ -84,16 +72,7 @@ public class StateManagerTest {
|
|||
assertEquals(500, getCap.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenActivityStateIsSetWithGenericMethod_itMustBeSetCorrectly(){
|
||||
TourActivity activity = mock(TourActivity.class);
|
||||
when(activity.getIndex()).thenReturn(0);
|
||||
StateManager stateManager = new StateManager(mock(VehicleRoutingProblem.class));
|
||||
StateId id = stateManager.createStateId("myState");
|
||||
State state = StateFactory.createState(1.);
|
||||
stateManager.putActivityState(activity, id, state);
|
||||
assertEquals(1.,stateManager.getActivityState(activity, id, State.class).toDouble(),0.01);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenActivityStateIsSetWithGenericMethodAndBoolean_itMustBeSetCorrectly(){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue