diff --git a/integration/service/oasis/stationgraph/node_graph_test.go b/integration/service/oasis/stationgraph/node_graph_test.go index 21b6e2080b0..e2b51aa4422 100644 --- a/integration/service/oasis/stationgraph/node_graph_test.go +++ b/integration/service/oasis/stationgraph/node_graph_test.go @@ -268,14 +268,14 @@ func generateMockNodeGraph() Graph { return graph } -type mockQuerier struct { +type mockQuerier4NodeGraph struct { } func newMockQuerier() connectivitymap.Querier { - return &mockQuerier{} + return &mockQuerier4NodeGraph{} } -func (querier *mockQuerier) NearByStationQuery(stationID string) []*connectivitymap.QueryResult { +func (querier *mockQuerier4NodeGraph) NearByStationQuery(stationID string) []*connectivitymap.QueryResult { if stationID == testStationID1 { return mockStationID2QueryResult[testStationID1] } @@ -283,7 +283,7 @@ func (querier *mockQuerier) NearByStationQuery(stationID string) []*connectivity return nil } -func (querier *mockQuerier) GetLocation(stationID string) *nav.Location { +func (querier *mockQuerier4NodeGraph) GetLocation(stationID string) *nav.Location { if stationID == testStationID1 { return mockStationID2Location[testStationID1] } diff --git a/integration/service/oasis/stationgraph/station_graph.go b/integration/service/oasis/stationgraph/station_graph.go index 49f5ed46ae4..e9508f016dd 100644 --- a/integration/service/oasis/stationgraph/station_graph.go +++ b/integration/service/oasis/stationgraph/station_graph.go @@ -31,13 +31,13 @@ func NewStationGraph(currEnergyLevel, maxEnergyLevel float64, strategy chargings func (sg *stationGraph) setStartAndEndForGraph(currEnergyLevel, maxEnergyLevel float64) bool { startLocation := sg.querier.GetLocation(stationfindertype.OrigLocationID) if startLocation == nil { - glog.Errorf("Failed to find %#v from Querier's GetLocation()\n", stationfindertype.OrigLocationID) + glog.Errorf("Failed to find %#v from Querier GetLocation()\n", stationfindertype.OrigLocationID) return false } endLocation := sg.querier.GetLocation(stationfindertype.DestLocationID) if startLocation == nil { - glog.Errorf("Failed to find %#v from Querier's GetLocation()\n", stationfindertype.DestLocationID) + glog.Errorf("Failed to find %#v from Querier GetLocation()\n", stationfindertype.DestLocationID) return false } diff --git a/integration/service/oasis/stationgraph/station_graph_test.go b/integration/service/oasis/stationgraph/station_graph_test.go index 181efcb8901..258e04ced6d 100644 --- a/integration/service/oasis/stationgraph/station_graph_test.go +++ b/integration/service/oasis/stationgraph/station_graph_test.go @@ -1,5 +1,15 @@ package stationgraph +import ( + "testing" + + "github.com/Telenav/osrm-backend/integration/pkg/api/nav" + "github.com/Telenav/osrm-backend/integration/service/oasis/chargingstrategy" + "github.com/Telenav/osrm-backend/integration/service/oasis/connectivitymap" + "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" + "github.com/golang/glog" +) + // import ( // "fmt" // "math" @@ -16,12 +26,22 @@ package stationgraph // /* // Construct test graph of // - start connects to staion 1, station 2, station 3 -// - station 1 connects to station 4, station 5 +// - start -> station 1: 22.2, +// - start -> station 2: 11.1, +// - start -> station 3: 33.3, +// - station 1 connects to station 4, station 5, +// - station 1 -> station 4: 44.4, +// - station 1 -> station 5: 34.4, // - station 2 connects to station 4, station 5 +// - station 2 -> station 4: 11.1, +// - station 2 -> station 5: 14.4, // - station 3 connects to station 4, station 5 +// - station 3 -> station 4: 22.2, +// - station 3 -> station 5: 15.5, // - station 4 connects to end +// - station 4 -> end : 44.4, // - station 5 connects to end - +// - station 5 -> end : 33.3, // station 1 // / \ \ // / \ \ @@ -54,7 +74,564 @@ package stationgraph // + connection to station 5's node with 80% of total energy // + connection to station 5's node with 100% of total energy // Each node with name `station1` will have different id, and each of them will have 6 neighbor nodes +// - For default graph, initial energy is 20.0, max energy is 50.0 // */ + +var testSGStationID1 = "station1" +var testSGStationID2 = "station2" +var testSGStationID3 = "station3" +var testSGStationID4 = "station4" +var testSGStationID5 = "station5" + +type mockQuerier4StationGraph struct { + mockStationID2QueryResult map[string][]*connectivitymap.QueryResult + mockStationID2Location map[string]*nav.Location +} + +func newMockQuerier4StationGraph() connectivitymap.Querier { + querier := &mockQuerier4StationGraph{ + mockStationID2QueryResult: map[string][]*connectivitymap.QueryResult{ + stationfindertype.OrigLocationID: { + { + StationID: testSGStationID1, + StationLocation: &nav.Location{Lat: 1.1, Lon: 1.1}, + Distance: 22.2, + Duration: 22.2, + }, + { + StationID: testSGStationID2, + StationLocation: &nav.Location{Lat: 2.2, Lon: 2.2}, + Distance: 11.1, + Duration: 11.1, + }, + { + StationID: testSGStationID3, + StationLocation: &nav.Location{Lat: 3.3, Lon: 3.3}, + Distance: 33.3, + Duration: 33.3, + }, + }, + testSGStationID1: { + { + StationID: testSGStationID4, + StationLocation: &nav.Location{Lat: 4.4, Lon: 4.4}, + Distance: 44.4, + Duration: 44.4, + }, + { + StationID: testSGStationID5, + StationLocation: &nav.Location{Lat: 5.5, Lon: 5.5}, + Distance: 34.4, + Duration: 34.4, + }, + }, + testSGStationID2: { + { + StationID: testSGStationID4, + StationLocation: &nav.Location{Lat: 4.4, Lon: 4.4}, + Distance: 11.1, + Duration: 11.1, + }, + { + StationID: testSGStationID5, + StationLocation: &nav.Location{Lat: 5.5, Lon: 5.5}, + Distance: 14.4, + Duration: 14.4, + }, + }, + testSGStationID3: { + { + StationID: testSGStationID4, + StationLocation: &nav.Location{Lat: 4.4, Lon: 4.4}, + Distance: 22.2, + Duration: 22.2, + }, + { + StationID: testSGStationID5, + StationLocation: &nav.Location{Lat: 5.5, Lon: 5.5}, + Distance: 15.5, + Duration: 15.5, + }, + }, + testSGStationID4: { + { + StationID: stationfindertype.DestLocationID, + StationLocation: &nav.Location{Lat: 6.6, Lon: 6.6}, + Distance: 44.4, + Duration: 44.4, + }, + }, + testSGStationID5: { + { + StationID: stationfindertype.DestLocationID, + StationLocation: &nav.Location{Lat: 6.6, Lon: 6.6}, + Distance: 33.3, + Duration: 33.3, + }, + }, + stationfindertype.DestLocationID: {}, + }, + mockStationID2Location: map[string]*nav.Location{ + stationfindertype.OrigLocationID: {Lat: 0.0, Lon: 0.0}, + testSGStationID1: {Lat: 1.1, Lon: 1.1}, + testSGStationID2: {Lat: 2.2, Lon: 2.2}, + testSGStationID3: {Lat: 3.3, Lon: 3.3}, + testSGStationID4: {Lat: 4.4, Lon: 4.4}, + testSGStationID5: {Lat: 5.5, Lon: 5.5}, + stationfindertype.DestLocationID: {Lat: 6.6, Lon: 6.6}, + }, + } + + return querier +} + +func (querier *mockQuerier4StationGraph) NearByStationQuery(stationID string) []*connectivitymap.QueryResult { + if queryResult, ok := querier.mockStationID2QueryResult[stationID]; ok { + return queryResult + } + glog.Fatal("Un-implemented mapping key for mockStationID2QueryResult.\n") + return nil +} + +func (querier *mockQuerier4StationGraph) GetLocation(stationID string) *nav.Location { + if location, ok := querier.mockStationID2Location[stationID]; ok { + return location + } + glog.Fatal("Un-implemented mapping key for mockStationID2Location.\n") + return nil +} + +func TestStationGraphGenerateSolutions1(t *testing.T) { + maxEnergyLevel := 50.0 + currEnergyLevel := 20.0 + strategy := chargingstrategy.NewFakeChargingStrategy(maxEnergyLevel) + querier := newMockQuerier4StationGraph() + + solutions := NewStationGraph(currEnergyLevel, maxEnergyLevel, strategy, querier).GenerateChargeSolutions() + + if len(solutions) != 0 { + t.Errorf("expect to have 1 solution but got %d.\n", len(solutions)) + } + // sol := solutions[0] + // // 58.8 = 11.1 + 14.4 + 33.3 + // if !util.FloatEquals(sol.Distance, 58.8) { + // t.Errorf("Incorrect distance calculated for fakeGraph1 expect 58.89 but got %#v.\n", sol.Distance) + // } + + // // 7918.8 = 11.1 + 2532(60% charge) + 14.4 + 5328(80% charge) + 33.3 + // if !util.FloatEquals(sol.Duration, 7918.8) { + // t.Errorf("Incorrect duration calculated for fakeGraph1 expect 10858.8 but got %#v.\n", sol.Duration) + // } + + // // 6.7 = 40 - 33.3 + // if !util.FloatEquals(sol.RemainingRage, 6.7) { + // t.Errorf("Incorrect duration calculated for fakeGraph1 expect 10858.8 but got %#v.\n", sol.RemainingRage) + // } + + // if len(sol.ChargeStations) != 2 { + // t.Errorf("Expect to have 2 charge stations for fakeGraph1 but got %d.\n", len(sol.ChargeStations)) + // } + + // expectStation1 := &solution.ChargeStation{ + // Location: nav.Location{ + // Lat: 2.2, + // Lon: 2.2, + // }, + // StationID: "station2", + // ArrivalEnergy: 8.9, + // WaitTime: 0, + // ChargeTime: 2532, + // ChargeRange: 30, + // } + // if !reflect.DeepEqual(sol.ChargeStations[0], expectStation1) { + // t.Errorf("Expect first charge stations info for fakeGraph1 is %#v but got %#v\n", expectStation1, sol.ChargeStations[0]) + // } + + // expectStation2 := &solution.ChargeStation{ + // Location: nav.Location{ + // Lat: 5.5, + // Lon: 5.5, + // }, + // StationID: "station5", + // ArrivalEnergy: 15.6, + // WaitTime: 0, + // ChargeTime: 5328, + // ChargeRange: 40, + // } + // if !reflect.DeepEqual(sol.ChargeStations[1], expectStation2) { + // t.Errorf("Expect second charge stations info for fakeGraph1 is %#v but got %#v\n", expectStation2, sol.ChargeStations[1]) + // } +} + +var mockedGraph = mockGraph{ + []*node{ + { + 0, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 20.0, + }, + }, + locationInfo{ + lat: 0.0, + lon: 0.0, + }, + }, + { + 1, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 30.0, + }, + }, + locationInfo{ + lat: 1.1, + lon: 1.1, + }, + }, + { + 2, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 40.0, + }, + }, + locationInfo{ + lat: 1.1, + lon: 1.1, + }, + }, + { + 3, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 50.0, + }, + }, + locationInfo{ + lat: 1.1, + lon: 1.1, + }, + }, + { + 4, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 30.0, + }, + }, + locationInfo{ + lat: 2.2, + lon: 2.2, + }, + }, + { + 5, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 40.0, + }, + }, + locationInfo{ + lat: 2.2, + lon: 2.2, + }, + }, + { + 6, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 50.0, + }, + }, + locationInfo{ + lat: 2.2, + lon: 2.2, + }, + }, + { + 7, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 30.0, + }, + }, + locationInfo{ + lat: 3.3, + lon: 3.3, + }, + }, + { + 8, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 40.0, + }, + }, + locationInfo{ + lat: 3.3, + lon: 3.3, + }, + }, + { + 9, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 50.0, + }, + }, + locationInfo{ + lat: 3.3, + lon: 3.3, + }, + }, + { + 10, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 30.0, + }, + }, + locationInfo{ + lat: 4.4, + lon: 4.4, + }, + }, + { + 11, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 40.0, + }, + }, + locationInfo{ + lat: 4.4, + lon: 4.4, + }, + }, + { + 12, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 50.0, + }, + }, + locationInfo{ + lat: 4.4, + lon: 4.4, + }, + }, + { + 13, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 30.0, + }, + }, + locationInfo{ + lat: 5.5, + lon: 5.5, + }, + }, + { + 14, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 40.0, + }, + }, + locationInfo{ + lat: 5.5, + lon: 5.5, + }, + }, + { + 15, + chargeInfo{ + targetState: chargingstrategy.State{ + Energy: 50.0, + }, + }, + locationInfo{ + lat: 5.5, + lon: 5.5, + }, + }, + { + 16, + chargeInfo{ + targetState: chargingstrategy.State{}, + }, + locationInfo{ + lat: 6.6, + lon: 6.6, + }, + }, + }, + []string{ + stationfindertype.OrigLocationID, + "station1", + "station1", + "station1", + "station2", + "station2", + "station2", + "station3", + "station3", + "station3", + "station4", + "station4", + "station4", + "station5", + "station5", + "station5", + stationfindertype.DestLocationID, + }, + map[nodeID][]*edgeIDAndData{ + 0: { + // orig -> station 1 + {edgeID{0, 1}, &edge{22.2, 22.2}}, + {edgeID{0, 2}, &edge{22.2, 22.2}}, + {edgeID{0, 3}, &edge{22.2, 22.2}}, + // orig -> station 2 + {edgeID{0, 4}, &edge{11.1, 11.1}}, + {edgeID{0, 5}, &edge{11.1, 11.1}}, + {edgeID{0, 6}, &edge{11.1, 11.1}}, + // orig -> station 3 + {edgeID{0, 7}, &edge{33.3, 33.3}}, + {edgeID{0, 8}, &edge{33.3, 33.3}}, + {edgeID{0, 9}, &edge{33.3, 33.3}}, + }, + 1: { + // station 1 -> station 4 + {edgeID{1, 10}, &edge{44.4, 44.4}}, + {edgeID{1, 11}, &edge{44.4, 44.4}}, + {edgeID{1, 12}, &edge{44.4, 44.4}}, + // station 1 -> station 5 + {edgeID{1, 13}, &edge{34.4, 34.4}}, + {edgeID{1, 14}, &edge{34.4, 34.4}}, + {edgeID{1, 15}, &edge{34.4, 34.4}}, + }, + 2: { + // station 1 -> station 4 + {edgeID{2, 10}, &edge{44.4, 44.4}}, + {edgeID{2, 11}, &edge{44.4, 44.4}}, + {edgeID{2, 12}, &edge{44.4, 44.4}}, + // station 1 -> station 5 + {edgeID{2, 13}, &edge{34.4, 34.4}}, + {edgeID{2, 14}, &edge{34.4, 34.4}}, + {edgeID{2, 15}, &edge{34.4, 34.4}}, + }, + 3: { + // station 1 -> station 4 + {edgeID{3, 10}, &edge{44.4, 44.4}}, + {edgeID{3, 11}, &edge{44.4, 44.4}}, + {edgeID{3, 12}, &edge{44.4, 44.4}}, + // station 1 -> station 5 + {edgeID{3, 13}, &edge{34.4, 34.4}}, + {edgeID{3, 14}, &edge{34.4, 34.4}}, + {edgeID{3, 15}, &edge{34.4, 34.4}}, + }, + 4: { + // station 2 -> station 4 + {edgeID{4, 10}, &edge{11.1, 11.1}}, + {edgeID{4, 11}, &edge{11.1, 11.1}}, + {edgeID{4, 12}, &edge{11.1, 11.1}}, + // station 2 -> station 5 + {edgeID{4, 13}, &edge{14.4, 14.4}}, + {edgeID{4, 14}, &edge{14.4, 14.4}}, + {edgeID{4, 15}, &edge{14.4, 14.4}}, + }, + 5: { + // station 2 -> station 4 + {edgeID{5, 10}, &edge{11.1, 11.1}}, + {edgeID{5, 11}, &edge{11.1, 11.1}}, + {edgeID{5, 12}, &edge{11.1, 11.1}}, + // station 2 -> station 5 + {edgeID{5, 13}, &edge{14.4, 14.4}}, + {edgeID{5, 14}, &edge{14.4, 14.4}}, + {edgeID{5, 15}, &edge{14.4, 14.4}}, + }, + 6: { + // station 2 -> station 4 + {edgeID{6, 10}, &edge{11.1, 11.1}}, + {edgeID{6, 11}, &edge{11.1, 11.1}}, + {edgeID{6, 12}, &edge{11.1, 11.1}}, + // station 2 -> station 5 + {edgeID{6, 13}, &edge{14.4, 14.4}}, + {edgeID{6, 14}, &edge{14.4, 14.4}}, + {edgeID{6, 15}, &edge{14.4, 14.4}}, + }, + 7: { + // station 3 -> station 4 + {edgeID{7, 10}, &edge{22.2, 22.2}}, + {edgeID{7, 11}, &edge{22.2, 22.2}}, + {edgeID{7, 12}, &edge{22.2, 22.2}}, + // station 3 -> station 5 + {edgeID{7, 13}, &edge{15.5, 15.5}}, + {edgeID{7, 14}, &edge{15.5, 15.5}}, + {edgeID{7, 15}, &edge{15.5, 15.5}}, + }, + 8: { + // station 3 -> station 4 + {edgeID{8, 10}, &edge{22.2, 22.2}}, + {edgeID{8, 11}, &edge{22.2, 22.2}}, + {edgeID{8, 12}, &edge{22.2, 22.2}}, + // station 3 -> station 5 + {edgeID{8, 13}, &edge{15.5, 15.5}}, + {edgeID{8, 14}, &edge{15.5, 15.5}}, + {edgeID{8, 15}, &edge{15.5, 15.5}}, + }, + 9: { + // station 3 -> station 4 + {edgeID{9, 10}, &edge{22.2, 22.2}}, + {edgeID{9, 11}, &edge{22.2, 22.2}}, + {edgeID{9, 12}, &edge{22.2, 22.2}}, + // station 3 -> station 5 + {edgeID{9, 13}, &edge{15.5, 15.5}}, + {edgeID{9, 14}, &edge{15.5, 15.5}}, + {edgeID{9, 15}, &edge{15.5, 15.5}}, + }, + 10: { + // station 4 -> end + {edgeID{10, 16}, &edge{44.4, 44.4}}, + {edgeID{10, 16}, &edge{44.4, 44.4}}, + {edgeID{10, 16}, &edge{44.4, 44.4}}, + }, + 11: { + // station 4 -> end + {edgeID{11, 16}, &edge{44.4, 44.4}}, + {edgeID{11, 16}, &edge{44.4, 44.4}}, + {edgeID{11, 16}, &edge{44.4, 44.4}}, + }, + 12: { + // station 4 -> end + {edgeID{12, 16}, &edge{44.4, 44.4}}, + {edgeID{12, 16}, &edge{44.4, 44.4}}, + {edgeID{12, 16}, &edge{44.4, 44.4}}, + }, + 13: { + // station 5 -> end + {edgeID{13, 16}, &edge{33.3, 33.3}}, + {edgeID{13, 16}, &edge{33.3, 33.3}}, + {edgeID{13, 16}, &edge{33.3, 33.3}}, + }, + 14: { + // station 5 -> end + {edgeID{14, 16}, &edge{33.3, 33.3}}, + {edgeID{14, 16}, &edge{33.3, 33.3}}, + {edgeID{14, 16}, &edge{33.3, 33.3}}, + }, + 15: { + // station 5 -> end + {edgeID{15, 16}, &edge{33.3, 33.3}}, + {edgeID{15, 16}, &edge{33.3, 33.3}}, + {edgeID{15, 16}, &edge{33.3, 33.3}}, + }, + }, + chargingstrategy.NewFakeChargingStrategy(50.0), +} + // var fakeNeighborsGraph = [][]stationfindertype.NeighborInfo{ // []stationfindertype.NeighborInfo{ // stationfindertype.NeighborInfo{