From 3219e2771e63d18a55894c0801228594174473ce Mon Sep 17 00:00:00 2001 From: codebear801 Date: Thu, 21 May 2020 16:53:53 -0700 Subject: [PATCH] refactor: Optimize implementation of nearbyquery, remove definition of QueryResult issue: https://github.com/Telenav/osrm-backend/issues/348 --- .../connectivitymap/connectivity_map_mock.go | 13 +- .../oasis/connectivitymap/interface.go | 15 +- .../service/oasis/internal/common/place.go | 4 + .../service/oasis/internal/mock/osrm_table.go | 5 +- .../oasis/internal/mock/spatialfinder_type.go | 12 +- .../station_conn_querier.go | 77 ++-- .../station_conn_querier_test.go | 362 ++++++++++++------ .../stationfinderalg/dest_iterator.go | 2 +- .../stationfinderalg/orig_iterator.go | 2 +- .../querier_interface_impl.go | 25 +- .../querier_interface_impl_test.go | 246 +++++++----- .../stationfinder/stationfindertype/const.go | 22 +- .../stationfinder/stationfindertype/type.go | 66 +++- .../stationfindertype/type_test.go | 46 +++ integration/service/oasis/stationgraph/doc.go | 4 +- .../service/oasis/stationgraph/graph_mock.go | 3 +- .../oasis/stationgraph/node_container.go | 5 +- .../service/oasis/stationgraph/node_graph.go | 16 +- .../oasis/stationgraph/node_graph_test.go | 110 +++--- .../oasis/stationgraph/station_graph.go | 16 +- .../oasis/stationgraph/station_graph_test.go | 199 ++++++---- 21 files changed, 808 insertions(+), 442 deletions(-) create mode 100644 integration/service/oasis/stationfinder/stationfindertype/type_test.go diff --git a/integration/service/oasis/connectivitymap/connectivity_map_mock.go b/integration/service/oasis/connectivitymap/connectivity_map_mock.go index 4bb9a67d46b..d3679fb7e09 100644 --- a/integration/service/oasis/connectivitymap/connectivity_map_mock.go +++ b/integration/service/oasis/connectivitymap/connectivity_map_mock.go @@ -1,12 +1,19 @@ package connectivitymap -import "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" +import ( + "github.com/Telenav/osrm-backend/integration/api/nav" + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" +) var fakeID2NearByIDsMap3 = ID2NearByIDsMap{ 1: []*common.RankedPlaceInfo{ { PlaceInfo: common.PlaceInfo{ ID: 2, + Location: &nav.Location{ + Lat: 37.399331, + Lon: -121.981193, + }, }, Weight: &common.Weight{ Distance: 1, @@ -18,6 +25,10 @@ var fakeID2NearByIDsMap3 = ID2NearByIDsMap{ { PlaceInfo: common.PlaceInfo{ ID: 3, + Location: &nav.Location{ + Lat: 37.401948, + Lon: -121.977384, + }, }, Weight: &common.Weight{ Distance: 2, diff --git a/integration/service/oasis/connectivitymap/interface.go b/integration/service/oasis/connectivitymap/interface.go index f081082634e..963156a37aa 100644 --- a/integration/service/oasis/connectivitymap/interface.go +++ b/integration/service/oasis/connectivitymap/interface.go @@ -1,21 +1,16 @@ package connectivitymap -import "github.com/Telenav/osrm-backend/integration/api/nav" - -// QueryResult records topological query result -type QueryResult struct { - StationID string - StationLocation *nav.Location - Distance float64 - Duration float64 -} +import ( + "github.com/Telenav/osrm-backend/integration/api/nav" + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" +) // Querier used to return topological information of charge stations type Querier interface { // NearByStationQuery finds near by stations by given stationID and return them in recorded sequence // Returns nil if given stationID is not found or no connectivity - NearByStationQuery(stationID string) []*QueryResult + NearByStationQuery(stationID string) []*common.RankedPlaceInfo // GetLocation returns location of given station id // Returns nil if given stationID is not found diff --git a/integration/service/oasis/internal/common/place.go b/integration/service/oasis/internal/common/place.go index 600b41c1b21..8c3951e092e 100644 --- a/integration/service/oasis/internal/common/place.go +++ b/integration/service/oasis/internal/common/place.go @@ -6,6 +6,10 @@ import ( "github.com/Telenav/osrm-backend/integration/api/nav" ) +// todo codebear801 change to a more variable name +// PlaceInfo -> PlaceWithLocation +// RankedPlaceInfo -> TransferInfo + // PlaceInfo records place related information such as ID and location type PlaceInfo struct { ID PlaceID diff --git a/integration/service/oasis/internal/mock/osrm_table.go b/integration/service/oasis/internal/mock/osrm_table.go index 3cffcf3469b..c50f1722f7c 100644 --- a/integration/service/oasis/internal/mock/osrm_table.go +++ b/integration/service/oasis/internal/mock/osrm_table.go @@ -3,6 +3,7 @@ package mock import ( "github.com/Telenav/osrm-backend/integration/api/osrm" "github.com/Telenav/osrm-backend/integration/api/osrm/table" + "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" ) // 1 * 4 @@ -16,7 +17,7 @@ var Mock1To4TableResponse1 = table.Response{ }, Sources: []*osrm.Waypoint{ { - Name: "orig_location", + Name: stationfindertype.OrigLocationID.String(), }, }, Destinations: []*osrm.Waypoint{ @@ -66,7 +67,7 @@ var Mock4To1TableResponse1 = table.Response{ }, Destinations: []*osrm.Waypoint{ { - Name: "dest_location", + Name: stationfindertype.DestLocationID.String(), }, }, } diff --git a/integration/service/oasis/internal/mock/spatialfinder_type.go b/integration/service/oasis/internal/mock/spatialfinder_type.go index 10d86f6e2c9..cfc7fe5425a 100644 --- a/integration/service/oasis/internal/mock/spatialfinder_type.go +++ b/integration/service/oasis/internal/mock/spatialfinder_type.go @@ -93,7 +93,7 @@ var MockChargeStationInfo1 = []*stationfindertype.ChargeStationInfo{ var NeighborInfoArray0 = []stationfindertype.NeighborInfo{ { - FromID: "orig_location", + FromID: stationfindertype.OrigLocationIDStr, FromLocation: nav.Location{ Lat: 1.1, Lon: 1.1, @@ -109,7 +109,7 @@ var NeighborInfoArray0 = []stationfindertype.NeighborInfo{ }, }, { - FromID: "orig_location", + FromID: stationfindertype.OrigLocationIDStr, FromLocation: nav.Location{ Lat: 1.1, Lon: 1.1, @@ -125,7 +125,7 @@ var NeighborInfoArray0 = []stationfindertype.NeighborInfo{ }, }, { - FromID: "orig_location", + FromID: stationfindertype.OrigLocationIDStr, FromLocation: nav.Location{ Lat: 1.1, Lon: 1.1, @@ -141,7 +141,7 @@ var NeighborInfoArray0 = []stationfindertype.NeighborInfo{ }, }, { - FromID: "orig_location", + FromID: stationfindertype.OrigLocationIDStr, FromLocation: nav.Location{ Lat: 1.1, Lon: 1.1, @@ -296,7 +296,7 @@ var NeighborInfoArray2 = []stationfindertype.NeighborInfo{ Lat: 30.333, Lon: 122.333, }, - ToID: "dest_location", + ToID: stationfindertype.DestLocationIDStr, ToLocation: nav.Location{ Lat: 4.4, Lon: 4.4, @@ -312,7 +312,7 @@ var NeighborInfoArray2 = []stationfindertype.NeighborInfo{ Lat: -10.333, Lon: 122.333, }, - ToID: "dest_location", + ToID: stationfindertype.DestLocationIDStr, ToLocation: nav.Location{ Lat: 4.4, Lon: 4.4, diff --git a/integration/service/oasis/stationconnquerier/station_conn_querier.go b/integration/service/oasis/stationconnquerier/station_conn_querier.go index c8132d06b4a..3ccc66a2f59 100644 --- a/integration/service/oasis/stationconnquerier/station_conn_querier.go +++ b/integration/service/oasis/stationconnquerier/station_conn_querier.go @@ -16,8 +16,8 @@ import ( type StationConnectivityQuerier struct { stationLocationQuerier spatialindexer.PlaceLocationQuerier stationConnectivity *connectivitymap.ConnectivityMap - reachableStationsByStart []*connectivitymap.QueryResult - reachableStationToEnd map[string]*connectivitymap.QueryResult + reachableStationsByStart []*common.RankedPlaceInfo + reachableStationToEnd map[string]*common.RankedPlaceInfo startLocation *nav.Location endLocation *nav.Location } @@ -48,18 +48,7 @@ func (querier *StationConnectivityQuerier) connectStartIntoStationGraph(stationF nearByPoints := stationFinder.FindNearByPlaceIDs(center, currEnergyLevel, spatialindexer.UnlimitedCount) rankedPoints := stationRanker.RankPlaceIDsByShortestDistance(center, nearByPoints) - reachableStationsByStart := make([]*connectivitymap.QueryResult, 0, len(rankedPoints)) - for _, rankedPointInfo := range rankedPoints { - tmp := &connectivitymap.QueryResult{ - StationID: rankedPointInfo.ID.String(), - StationLocation: &nav.Location{Lat: rankedPointInfo.Location.Lat, Lon: rankedPointInfo.Location.Lon}, - Distance: rankedPointInfo.Weight.Distance, - Duration: rankedPointInfo.Weight.Duration, - } - reachableStationsByStart = append(reachableStationsByStart, tmp) - } - - querier.reachableStationsByStart = reachableStationsByStart + querier.reachableStationsByStart = rankedPoints glog.Infof("Add %d stations connects Start.\n", len(querier.reachableStationsByStart)) } @@ -69,13 +58,14 @@ func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFin nearByPoints := stationFinder.FindNearByPlaceIDs(center, maxEnergyLevel, spatialindexer.UnlimitedCount) rankedPoints := stationRanker.RankPlaceIDsByShortestDistance(center, nearByPoints) - reachableStationToEnd := make(map[string]*connectivitymap.QueryResult) + reachableStationToEnd := make(map[string]*common.RankedPlaceInfo) for _, rankedPointInfo := range rankedPoints { - reachableStationToEnd[rankedPointInfo.ID.String()] = &connectivitymap.QueryResult{ - StationID: stationfindertype.DestLocationID, - StationLocation: end, - Distance: rankedPointInfo.Weight.Distance, - Duration: rankedPointInfo.Weight.Duration, + reachableStationToEnd[rankedPointInfo.ID.String()] = &common.RankedPlaceInfo{ + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: end, + }, + Weight: rankedPointInfo.Weight, } } @@ -88,13 +78,13 @@ func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFin // For start point, directly returns reachableStationsByStart which is generated by considering current energy level. // For end point, return nil, no connectivity expected from end to others // For charge stations, it retrieves connectivity from pre-build data. If a charge station is reachable to destination/end point, it must connects that into graph. -func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string) []*connectivitymap.QueryResult { +func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string) []*common.RankedPlaceInfo { - if stationID == stationfindertype.OrigLocationID { + if stationID == stationfindertype.OrigLocationIDStr { return querier.reachableStationsByStart } - if stationID == stationfindertype.DestLocationID { + if stationID == stationfindertype.DestLocationIDStr { return nil } @@ -105,27 +95,20 @@ func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string) } if connectivityResults, ok := querier.stationConnectivity.QueryConnectivity((common.PlaceID)(placeID)); ok { - - size := len(connectivityResults) - if querier.isStationConnectsToEnd(stationID) { - size += 1 - } - - results := make([]*connectivitymap.QueryResult, 0, size) - for _, idAndWeight := range connectivityResults { - tmp := &connectivitymap.QueryResult{ - StationID: idAndWeight.ID.String(), - StationLocation: querier.GetLocation(idAndWeight.ID.String()), - Distance: idAndWeight.Weight.Distance, - Duration: idAndWeight.Weight.Duration, + if !querier.isStationConnectsToEnd(stationID) { + return connectivityResults + } else { + size := len(connectivityResults) + 1 + results := make([]*common.RankedPlaceInfo, 0, size) + for _, result := range connectivityResults { + results = append(results, result) } - results = append(results, tmp) + return querier.connectEndIntoGraph(stationID, results) } - - return querier.connectEndIntoGraph(stationID, results) } else { + // end position just find one charge station, and this charge station didn't has other conductivities if querier.isStationConnectsToEnd(stationID) { - results := make([]*connectivitymap.QueryResult, 0, 1) + results := make([]*common.RankedPlaceInfo, 0, 1) return querier.connectEndIntoGraph(stationID, results) } } @@ -137,9 +120,9 @@ func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string) // Returns nil if given stationID is not found func (querier *StationConnectivityQuerier) GetLocation(stationID string) *nav.Location { switch stationID { - case stationfindertype.OrigLocationID: + case stationfindertype.OrigLocationIDStr: return querier.startLocation - case stationfindertype.DestLocationID: + case stationfindertype.DestLocationIDStr: return querier.endLocation default: return querier.stationLocationQuerier.GetLocation(stationID) @@ -151,19 +134,23 @@ func (querier *StationConnectivityQuerier) isStationConnectsToEnd(stationID stri return ok } -func (querier *StationConnectivityQuerier) connectEndIntoGraph(stationID string, results []*connectivitymap.QueryResult) []*connectivitymap.QueryResult { +func (querier *StationConnectivityQuerier) connectEndIntoGraph(stationID string, results []*common.RankedPlaceInfo) []*common.RankedPlaceInfo { if queryResult4End, ok := querier.reachableStationToEnd[stationID]; ok { return appendIntoSortedSlice(queryResult4End, results) } return results } -func appendIntoSortedSlice(item *connectivitymap.QueryResult, results []*connectivitymap.QueryResult) []*connectivitymap.QueryResult { +func appendIntoSortedSlice(item *common.RankedPlaceInfo, results []*common.RankedPlaceInfo) []*common.RankedPlaceInfo { + + // todo @codebear801 this code assumes result is sorted by Distance, better hide in array itself or sortStrategy insertIndex := sort.Search(len(results), func(i int) bool { - return results[i].Distance > item.Distance + return results[i].Weight.Distance > item.Weight.Distance }) + results = append(results, nil) copy(results[insertIndex+1:], results[insertIndex:]) results[insertIndex] = item + return results } diff --git a/integration/service/oasis/stationconnquerier/station_conn_querier_test.go b/integration/service/oasis/stationconnquerier/station_conn_querier_test.go index 20122e268bc..30fc26237c2 100644 --- a/integration/service/oasis/stationconnquerier/station_conn_querier_test.go +++ b/integration/service/oasis/stationconnquerier/station_conn_querier_test.go @@ -1,6 +1,7 @@ package stationconnquerier import ( + "fmt" "reflect" "testing" @@ -13,136 +14,208 @@ import ( func TestAppendIntoSortedSlice(t *testing.T) { cases := []struct { - sortedArray []*connectivitymap.QueryResult - itemToBeInserted *connectivitymap.QueryResult - expectedArray []*connectivitymap.QueryResult + sortedArray []*common.RankedPlaceInfo + itemToBeInserted *common.RankedPlaceInfo + expectedArray []*common.RankedPlaceInfo }{ // case: insert into empty array { nil, - &connectivitymap.QueryResult{ - StationID: "3", - StationLocation: mockStation3Location, - Distance: 4622.08948420977, - Duration: 208.2022290184581, + &common.RankedPlaceInfo{ + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, }, - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "3", - StationLocation: mockStation3Location, - Distance: 4622.08948420977, - Duration: 208.2022290184581, + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, }, }, }, // case: insert to the head of sorted array { - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "3", - StationLocation: mockStation3Location, - Distance: 4622.08948420977, - Duration: 208.2022290184581, + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, }, { - StationID: "2", - StationLocation: mockStation2Location, - Distance: 4999.134247893073, - Duration: 225.18622738257085, + PlaceInfo: common.PlaceInfo{ + ID: 2, + Location: mockStation2Location, + }, + Weight: &common.Weight{ + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, }, { - StationID: "1", - StationLocation: mockStation1Location, - Distance: 6310.598332634715, - Duration: 284.2611861547169, + PlaceInfo: common.PlaceInfo{ + ID: 1, + Location: mockStation1Location, + }, + Weight: &common.Weight{ + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, }, }, - &connectivitymap.QueryResult{ - StationID: "4", - StationLocation: mockStation4Location, - Distance: 222.0, - Duration: 1.0, + &common.RankedPlaceInfo{ + PlaceInfo: common.PlaceInfo{ + ID: 4, + Location: mockStation4Location, + }, + Weight: &common.Weight{ + Distance: 222.0, + Duration: 1.0, + }, }, - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "4", - StationLocation: mockStation4Location, - Distance: 222.0, - Duration: 1.0, + PlaceInfo: common.PlaceInfo{ + ID: 4, + Location: mockStation4Location, + }, + Weight: &common.Weight{ + Distance: 222.0, + Duration: 1.0, + }, }, { - StationID: "3", - StationLocation: mockStation3Location, - Distance: 4622.08948420977, - Duration: 208.2022290184581, + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, }, { - StationID: "2", - StationLocation: mockStation2Location, - Distance: 4999.134247893073, - Duration: 225.18622738257085, + PlaceInfo: common.PlaceInfo{ + ID: 2, + Location: mockStation2Location, + }, + Weight: &common.Weight{ + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, }, { - StationID: "1", - StationLocation: mockStation1Location, - Distance: 6310.598332634715, - Duration: 284.2611861547169, + PlaceInfo: common.PlaceInfo{ + ID: 1, + Location: mockStation1Location, + }, + Weight: &common.Weight{ + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, }, }, }, // case: insert into sorted array { - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "3", - StationLocation: mockStation3Location, - Distance: 4622.08948420977, - Duration: 208.2022290184581, + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, }, { - StationID: "2", - StationLocation: mockStation2Location, - Distance: 4999.134247893073, - Duration: 225.18622738257085, + PlaceInfo: common.PlaceInfo{ + ID: 2, + Location: mockStation2Location, + }, + Weight: &common.Weight{ + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, }, { - StationID: "1", - StationLocation: mockStation1Location, - Distance: 6310.598332634715, - Duration: 284.2611861547169, + PlaceInfo: common.PlaceInfo{ + ID: 1, + Location: mockStation1Location, + }, + Weight: &common.Weight{ + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, }, }, - &connectivitymap.QueryResult{ - StationID: "4", - StationLocation: mockStation4Location, - Distance: 4623.0, - Duration: 1.0, + &common.RankedPlaceInfo{ + PlaceInfo: common.PlaceInfo{ + ID: 4, + Location: mockStation4Location, + }, + Weight: &common.Weight{ + Distance: 4623.0, + Duration: 1.0, + }, }, - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "3", - StationLocation: mockStation3Location, - Distance: 4622.08948420977, - Duration: 208.2022290184581, + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, }, { - StationID: "4", - StationLocation: mockStation4Location, - Distance: 4623.0, - Duration: 1.0, + PlaceInfo: common.PlaceInfo{ + ID: 4, + Location: mockStation4Location, + }, + Weight: &common.Weight{ + Distance: 4623.0, + Duration: 1.0, + }, }, { - StationID: "2", - StationLocation: mockStation2Location, - Distance: 4999.134247893073, - Duration: 225.18622738257085, + PlaceInfo: common.PlaceInfo{ + ID: 2, + Location: mockStation2Location, + }, + Weight: &common.Weight{ + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, }, { - StationID: "1", - StationLocation: mockStation1Location, - Distance: 6310.598332634715, - Duration: 284.2611861547169, + PlaceInfo: common.PlaceInfo{ + ID: 1, + Location: mockStation1Location, + }, + Weight: &common.Weight{ + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, }, }, }, @@ -193,11 +266,11 @@ func TestStationConnQuerier(t *testing.T) { expectLocation *nav.Location }{ { - stationfindertype.OrigLocationID, + stationfindertype.OrigLocationIDStr, mockOrigLocation, }, { - stationfindertype.DestLocationID, + stationfindertype.DestLocationIDStr, mockDestLocation, }, { @@ -228,77 +301,109 @@ func TestStationConnQuerier(t *testing.T) { // verify connectivity connectivityCases := []struct { stationID string - expectQueryResult []*connectivitymap.QueryResult + expectQueryResult []*common.RankedPlaceInfo }{ { - stationfindertype.OrigLocationID, - []*connectivitymap.QueryResult{ + stationfindertype.OrigLocationIDStr, + []*common.RankedPlaceInfo{ { - StationID: "3", - StationLocation: mockStation3Location, - Distance: 4622.08948420977, - Duration: 208.2022290184581, + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 4622.08948420977, + Duration: 208.2022290184581, + }, }, { - StationID: "2", - StationLocation: mockStation2Location, - Distance: 4999.134247893073, - Duration: 225.18622738257085, + PlaceInfo: common.PlaceInfo{ + ID: 2, + Location: mockStation2Location, + }, + Weight: &common.Weight{ + Distance: 4999.134247893073, + Duration: 225.18622738257085, + }, }, { - StationID: "1", - StationLocation: mockStation1Location, - Distance: 6310.598332634715, - Duration: 284.2611861547169, + PlaceInfo: common.PlaceInfo{ + ID: 1, + Location: mockStation1Location, + }, + Weight: &common.Weight{ + Distance: 6310.598332634715, + Duration: 284.2611861547169, + }, }, }, }, { - stationfindertype.DestLocationID, + stationfindertype.DestLocationIDStr, nil, }, { "1", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "2", - StationLocation: mockStation2Location, - Distance: 1, // hard code value from mock ConnectivityMap - Duration: 1, // hard code value from mock ConnectivityMap + PlaceInfo: common.PlaceInfo{ + ID: 2, + Location: mockStation2Location, + }, + Weight: &common.Weight{ + Distance: 1, // hard code value from mock ConnectivityMap + Duration: 1, // hard code value from mock ConnectivityMap + }, }, { - StationID: stationfindertype.DestLocationID, - StationLocation: mockDestLocation, - Distance: 4873.817197753869, - Duration: 219.54131521413822, + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: mockDestLocation, + }, + Weight: &common.Weight{ + Distance: 4873.817197753869, + Duration: 219.54131521413822, + }, }, }, }, { "3", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: stationfindertype.DestLocationID, - StationLocation: mockDestLocation, - Distance: 7083.8672907090095, - Duration: 319.0931212031085, + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: mockDestLocation, + }, + Weight: &common.Weight{ + Distance: 7083.8672907090095, + Duration: 319.0931212031085, + }, }, }, }, { "2", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "3", - StationLocation: mockStation3Location, - Distance: 2, // hard code value from mock ConnectivityMap - Duration: 2, // hard code value from mock ConnectivityMap + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: mockStation3Location, + }, + Weight: &common.Weight{ + Distance: 2, // hard code value from mock ConnectivityMap + Duration: 2, // hard code value from mock ConnectivityMap + }, }, { - StationID: stationfindertype.DestLocationID, - StationLocation: mockDestLocation, - Distance: 7277.313067724465, - Duration: 327.80689494254347, + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: mockDestLocation, + }, + Weight: &common.Weight{ + Distance: 7277.313067724465, + Duration: 327.80689494254347, + }, }, }, }, @@ -307,7 +412,14 @@ func TestStationConnQuerier(t *testing.T) { for _, c := range connectivityCases { actualQueryResult := querier.NearByStationQuery(c.stationID) if !reflect.DeepEqual(actualQueryResult, c.expectQueryResult) { - t.Errorf("Incorrect result for connectivitymap.Querier.NearByStationQuery, expect %+v but got %+v\n", c.expectQueryResult, actualQueryResult) + for _, r := range c.expectQueryResult { + fmt.Printf("+++ %v, %v, %v, %v, %v\n", r.ID, r.Location.Lat, r.Location.Lon, r.Weight.Distance, r.Weight.Duration) + } + + for _, r := range actualQueryResult { + fmt.Printf("+++ %v, %v, %v, %v, %v\n", r.ID, r.Location.Lat, r.Location.Lon, r.Weight.Distance, r.Weight.Duration) + } + t.Errorf("Incorrect result for connectivitymap.Querier.NearByStationQuery, expect %#v but got %#v\n", c.expectQueryResult, actualQueryResult) } } } diff --git a/integration/service/oasis/stationfinder/stationfinderalg/dest_iterator.go b/integration/service/oasis/stationfinder/stationfinderalg/dest_iterator.go index 375822a240e..6eb51d4482d 100644 --- a/integration/service/oasis/stationfinder/stationfinderalg/dest_iterator.go +++ b/integration/service/oasis/stationfinder/stationfinderalg/dest_iterator.go @@ -23,7 +23,7 @@ func (di *destIterator) IterateNearbyStations() <-chan *stationfindertype.Charge go func() { defer close(c) station := stationfindertype.ChargeStationInfo{ - ID: stationfindertype.DestLocationID, + ID: stationfindertype.DestLocationIDStr, Location: *di.location, } c <- &station diff --git a/integration/service/oasis/stationfinder/stationfinderalg/orig_iterator.go b/integration/service/oasis/stationfinder/stationfinderalg/orig_iterator.go index a1451520d24..e65b9a3667d 100644 --- a/integration/service/oasis/stationfinder/stationfinderalg/orig_iterator.go +++ b/integration/service/oasis/stationfinder/stationfinderalg/orig_iterator.go @@ -23,7 +23,7 @@ func (oi *origIterator) IterateNearbyStations() <-chan *stationfindertype.Charge go func() { defer close(c) station := stationfindertype.ChargeStationInfo{ - ID: stationfindertype.OrigLocationID, + ID: stationfindertype.OrigLocationIDStr, Location: *oi.location, } c <- &station diff --git a/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl.go b/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl.go index c1a7c8463d4..ed6089c0fdd 100644 --- a/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl.go +++ b/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl.go @@ -3,11 +3,12 @@ package stationfinderalg import ( "github.com/Telenav/osrm-backend/integration/api/nav" "github.com/Telenav/osrm-backend/integration/service/oasis/connectivitymap" + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" "github.com/golang/glog" ) -type stationID2QueryResults map[string][]*connectivitymap.QueryResult +type stationID2QueryResults map[string][]*common.RankedPlaceInfo type stationID2Location map[string]*nav.Location type querier struct { @@ -31,18 +32,22 @@ func NewQuerierBasedOnWeightBetweenNeighborsChan(c chan stationfindertype.Weight for _, neighborInfo := range item.NeighborsInfo { if _, ok := querier.id2QueryResults[neighborInfo.FromID]; !ok { - results := make([]*connectivitymap.QueryResult, 0, 10) + results := make([]*common.RankedPlaceInfo, 0, 10) querier.id2QueryResults[neighborInfo.FromID] = results } querier.id2QueryResults[neighborInfo.FromID] = append(querier.id2QueryResults[neighborInfo.FromID], - &connectivitymap.QueryResult{ - StationID: neighborInfo.ToID, - StationLocation: &nav.Location{ - Lat: neighborInfo.ToLocation.Lat, - Lon: neighborInfo.ToLocation.Lon, + &common.RankedPlaceInfo{ + PlaceInfo: common.PlaceInfo{ + ID: neighborInfo.ToPlaceID(), + Location: &nav.Location{ + Lat: neighborInfo.ToLocation.Lat, + Lon: neighborInfo.ToLocation.Lon, + }, + }, + Weight: &common.Weight{ + Distance: neighborInfo.Distance, + Duration: neighborInfo.Duration, }, - Distance: neighborInfo.Distance, - Duration: neighborInfo.Duration, }) if _, ok := querier.id2Location[neighborInfo.FromID]; !ok { @@ -64,7 +69,7 @@ func NewQuerierBasedOnWeightBetweenNeighborsChan(c chan stationfindertype.Weight return querier } -func (q *querier) NearByStationQuery(stationID string) []*connectivitymap.QueryResult { +func (q *querier) NearByStationQuery(stationID string) []*common.RankedPlaceInfo { if results, ok := q.id2QueryResults[stationID]; ok { return results } else { diff --git a/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl_test.go b/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl_test.go index 2fb53927630..078f35b1ff8 100644 --- a/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl_test.go +++ b/integration/service/oasis/stationfinder/stationfinderalg/querier_interface_impl_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/Telenav/osrm-backend/integration/api/nav" - "github.com/Telenav/osrm-backend/integration/service/oasis/connectivitymap" + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" "github.com/Telenav/osrm-backend/integration/service/oasis/internal/mock" "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" ) @@ -20,47 +20,63 @@ import ( func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { cases := []struct { stationID string - expectQueryResult []*connectivitymap.QueryResult + expectQueryResult []*common.RankedPlaceInfo expectLocation *nav.Location }{ { - "orig_location", - []*connectivitymap.QueryResult{ + stationfindertype.OrigLocationIDStr, + []*common.RankedPlaceInfo{ { - StationID: "station1", - StationLocation: &nav.Location{ - Lat: 32.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 1, + Location: &nav.Location{ + Lat: 32.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 22.2, + Duration: 22.2, }, - Distance: 22.2, - Duration: 22.2, }, { - StationID: "station2", - StationLocation: &nav.Location{ - Lat: -32.333, - Lon: -122.333, + PlaceInfo: common.PlaceInfo{ + ID: 2, + Location: &nav.Location{ + Lat: -32.333, + Lon: -122.333, + }, + }, + Weight: &common.Weight{ + Distance: 11.1, + Duration: 11.1, }, - Distance: 11.1, - Duration: 11.1, }, { - StationID: "station3", - StationLocation: &nav.Location{ - Lat: 32.333, - Lon: -122.333, + PlaceInfo: common.PlaceInfo{ + ID: 3, + Location: &nav.Location{ + Lat: 32.333, + Lon: -122.333, + }, + }, + Weight: &common.Weight{ + Distance: 33.3, + Duration: 33.3, }, - Distance: 33.3, - Duration: 33.3, }, { - StationID: "station4", - StationLocation: &nav.Location{ - Lat: -32.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 4, + Location: &nav.Location{ + Lat: -32.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 44.4, + Duration: 44.4, }, - Distance: 44.4, - Duration: 44.4, }, }, &nav.Location{ @@ -70,24 +86,32 @@ func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { }, { "station1", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "station6", - StationLocation: &nav.Location{ - Lat: 30.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 6, + Location: &nav.Location{ + Lat: 30.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 2, + Duration: 2, }, - Distance: 2, - Duration: 2, }, { - StationID: "station7", - StationLocation: &nav.Location{ - Lat: -10.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 7, + Location: &nav.Location{ + Lat: -10.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 3, + Duration: 3, }, - Distance: 3, - Duration: 3, }, }, &nav.Location{ @@ -97,24 +121,32 @@ func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { }, { "station2", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "station6", - StationLocation: &nav.Location{ - Lat: 30.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 6, + Location: &nav.Location{ + Lat: 30.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 4, + Duration: 4, }, - Distance: 4, - Duration: 4, }, { - StationID: "station7", - StationLocation: &nav.Location{ - Lat: -10.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 7, + Location: &nav.Location{ + Lat: -10.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 5, + Duration: 5, }, - Distance: 5, - Duration: 5, }, }, &nav.Location{ @@ -124,24 +156,32 @@ func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { }, { "station3", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "station6", - StationLocation: &nav.Location{ - Lat: 30.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 6, + Location: &nav.Location{ + Lat: 30.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 6, + Duration: 6, }, - Distance: 6, - Duration: 6, }, { - StationID: "station7", - StationLocation: &nav.Location{ - Lat: -10.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 7, + Location: &nav.Location{ + Lat: -10.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 7, + Duration: 7, }, - Distance: 7, - Duration: 7, }, }, &nav.Location{ @@ -151,24 +191,32 @@ func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { }, { "station4", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "station6", - StationLocation: &nav.Location{ - Lat: 30.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 6, + Location: &nav.Location{ + Lat: 30.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 8, + Duration: 8, }, - Distance: 8, - Duration: 8, }, { - StationID: "station7", - StationLocation: &nav.Location{ - Lat: -10.333, - Lon: 122.333, + PlaceInfo: common.PlaceInfo{ + ID: 7, + Location: &nav.Location{ + Lat: -10.333, + Lon: 122.333, + }, + }, + Weight: &common.Weight{ + Distance: 9, + Duration: 9, }, - Distance: 9, - Duration: 9, }, }, &nav.Location{ @@ -178,15 +226,19 @@ func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { }, { "station6", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "dest_location", - StationLocation: &nav.Location{ - Lat: 4.4, - Lon: 4.4, + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: &nav.Location{ + Lat: 4.4, + Lon: 4.4, + }, + }, + Weight: &common.Weight{ + Distance: 66.6, + Duration: 66.6, }, - Distance: 66.6, - Duration: 66.6, }, }, &nav.Location{ @@ -196,15 +248,19 @@ func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { }, { "station7", - []*connectivitymap.QueryResult{ + []*common.RankedPlaceInfo{ { - StationID: "dest_location", - StationLocation: &nav.Location{ - Lat: 4.4, - Lon: 4.4, + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: &nav.Location{ + Lat: 4.4, + Lon: 4.4, + }, + }, + Weight: &common.Weight{ + Distance: 11.1, + Duration: 11.1, }, - Distance: 11.1, - Duration: 11.1, }, }, &nav.Location{ @@ -213,7 +269,7 @@ func TestQuerierBasedOnWeightBetweenNeighborsChan(t *testing.T) { }, }, { - "dest_location", + stationfindertype.DestLocationIDStr, nil, &nav.Location{ Lat: 4.4, diff --git a/integration/service/oasis/stationfinder/stationfindertype/const.go b/integration/service/oasis/stationfinder/stationfindertype/const.go index 2c7de154c41..82ccb9fd190 100644 --- a/integration/service/oasis/stationfinder/stationfindertype/const.go +++ b/integration/service/oasis/stationfinder/stationfindertype/const.go @@ -1,7 +1,21 @@ package stationfindertype -// OrigLocationID defines name for orig -const OrigLocationID string = "orig_location" +import ( + "math" -// DestLocationID defines name for dest -const DestLocationID string = "dest_location" + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" +) + +// todo @codebear801 move this file to internal/common + +// OrigLocationIDStr defines name for orig +const OrigLocationIDStr string = "orig_location" +const OrigLocationID common.PlaceID = math.MaxInt64 - 1 + +// DestLocationIDStr defines name for dest +const DestLocationIDStr string = "dest_location" +const DestLocationID common.PlaceID = math.MaxInt64 - 2 + +// InvalidPlaceID defines name for InvalidPlaceID +const InvalidPlaceIDStr = "invalid_place_id" +const InvalidPlaceID = math.MaxInt64 diff --git a/integration/service/oasis/stationfinder/stationfindertype/type.go b/integration/service/oasis/stationfinder/stationfindertype/type.go index e6a1960c754..7d072163763 100644 --- a/integration/service/oasis/stationfinder/stationfindertype/type.go +++ b/integration/service/oasis/stationfinder/stationfindertype/type.go @@ -1,6 +1,13 @@ package stationfindertype -import "github.com/Telenav/osrm-backend/integration/api/nav" +import ( + "regexp" + "strconv" + + "github.com/Telenav/osrm-backend/integration/api/nav" + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" + "github.com/golang/glog" +) // NearbyStationsIterator provide interator for near by stations type NearbyStationsIterator interface { @@ -35,3 +42,60 @@ type WeightBetweenNeighbors struct { NeighborsInfo []NeighborInfo Err error } + +// FromPlaceID converts from String to PlaceID for From +func (neighbor NeighborInfo) FromPlaceID() common.PlaceID { + return convertIDFromStringToPlaceID(neighbor.FromID) +} + +// ToPlaceID converts from String to PlaceID for To +func (neighbor NeighborInfo) ToPlaceID() common.PlaceID { + return convertIDFromStringToPlaceID(neighbor.ToID) +} + +// Telenav web response format b-12345678 +// While pre-processed result is value itself +func convertIDFromStringToPlaceID(s string) common.PlaceID { + switch s { + case OrigLocationIDStr: + return OrigLocationID + case DestLocationIDStr: + return DestLocationID + case InvalidPlaceIDStr: + return InvalidPlaceID + default: + num, err := strconv.ParseInt(s, 10, 64) + if err == nil { // pre-processed result + placeID := (common.PlaceID)(num) + if !isPredefinedValueTakenPlaceIDValue(placeID) { + return placeID + } + // else: assert false + } else { // from Web Service + re := regexp.MustCompile("[0-9]+") + idStrArray := re.FindAllString(s, -1) + if len(idStrArray) != 1 { + glog.Fatalf("Assumption of ID from search response is wrong, expect format is b- with number, but got %v.\n", s) + } + if num, err := strconv.ParseInt(idStrArray[0], 10, 64); err == nil { + placeID := (common.PlaceID)(num) + if !isPredefinedValueTakenPlaceIDValue(placeID) { + return placeID + } + // else: assert false + } else { + glog.Fatalf("Assumption of ID from search response is wrong, expect format is b- with number, but got %v.\n", s) + } + } + } + glog.Fatalf("PlaceID %v could not be decodinged", s) + return InvalidPlaceID +} + +func isPredefinedValueTakenPlaceIDValue(id common.PlaceID) bool { + if id == OrigLocationID || id == DestLocationID || id == InvalidPlaceID { + glog.Fatal("Predefined ID use the same value as value from PlaceID, please adjust either one of them.\n") + return true + } + return false +} diff --git a/integration/service/oasis/stationfinder/stationfindertype/type_test.go b/integration/service/oasis/stationfinder/stationfindertype/type_test.go new file mode 100644 index 00000000000..4100fbbe7b5 --- /dev/null +++ b/integration/service/oasis/stationfinder/stationfindertype/type_test.go @@ -0,0 +1,46 @@ +package stationfindertype + +import ( + "testing" + + "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common" +) + +func TestConvertIDFromStringToPlaceID(t *testing.T) { + cases := []struct { + idStr string + expectPlaceID common.PlaceID + }{ + { + OrigLocationIDStr, + OrigLocationID, + }, + { + DestLocationIDStr, + DestLocationID, + }, + { + InvalidPlaceIDStr, + InvalidPlaceID, + }, + { + "1234567", + 1234567, + }, + { + "b-1234567", + 1234567, + }, + { + "station1", + 1, + }, + } + + for _, c := range cases { + actualPlaceID := convertIDFromStringToPlaceID(c.idStr) + if actualPlaceID != c.expectPlaceID { + t.Errorf("For case %v, expect result is %v while actualPlace id is %v\n", c, c.expectPlaceID, actualPlaceID) + } + } +} diff --git a/integration/service/oasis/stationgraph/doc.go b/integration/service/oasis/stationgraph/doc.go index 278f29588f0..2a48cc4919f 100644 --- a/integration/service/oasis/stationgraph/doc.go +++ b/integration/service/oasis/stationgraph/doc.go @@ -7,8 +7,8 @@ Package stationgraph builds charge station based graph and provide optimum charg + strategy: Charge strategy be used for each charge station, it hides logic of different strategy of {time used for charging/wating, new energy got}. + querier: topological querier which generates connectivity for building graph * querier could be implemented by querying OSRM or pre-build conductivities - * querier must contains stationfindertype.OrigLocationID and stationfindertype.DestLocationID - * querier must provide ways to integrate stationfindertype.OrigLocationID and stationfindertype.DestLocationID into connectivity graph + * querier must contains stationfindertype.OrigLocationIDStr and stationfindertype.DestLocationIDStr + * querier must provide ways to integrate stationfindertype.OrigLocationIDStr and stationfindertype.DestLocationIDStr into connectivity graph - station graph returns + solution.Solution via GenerateChargeSolutions() diff --git a/integration/service/oasis/stationgraph/graph_mock.go b/integration/service/oasis/stationgraph/graph_mock.go index 2a90a9e71bc..6e8201fb7d7 100644 --- a/integration/service/oasis/stationgraph/graph_mock.go +++ b/integration/service/oasis/stationgraph/graph_mock.go @@ -3,6 +3,7 @@ package stationgraph import ( "github.com/Telenav/osrm-backend/integration/api/nav" "github.com/Telenav/osrm-backend/integration/service/oasis/chargingstrategy" + "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" ) // node_0 -> node_1, duration = 30, distance = 30 @@ -1163,7 +1164,7 @@ func (graph *mockGraph) ChargeStrategy() chargingstrategy.Strategy { // StationID returns original stationID from internal nodeID func (graph *mockGraph) StationID(id nodeID) string { if id < 0 || int(id) >= len(graph.stationIDs) { - return invalidStationID + return stationfindertype.InvalidPlaceIDStr } return graph.stationIDs[id] } diff --git a/integration/service/oasis/stationgraph/node_container.go b/integration/service/oasis/stationgraph/node_container.go index 1daf32833ca..e453aa506da 100644 --- a/integration/service/oasis/stationgraph/node_container.go +++ b/integration/service/oasis/stationgraph/node_container.go @@ -2,6 +2,7 @@ package stationgraph import ( "github.com/Telenav/osrm-backend/integration/service/oasis/chargingstrategy" + "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" ) type logicNodeIdentifier2NodePtr map[logicNodeIdentifier]*node @@ -67,12 +68,10 @@ func (nc *nodeContainer) stationID(id nodeID) string { if stationID, ok := nc.id2StationID[id]; ok { return stationID } else { - return invalidStationID + return stationfindertype.InvalidPlaceIDStr } } -const invalidStationID = "InvalidStationID" - type logicNodeIdentifier struct { stationID string targetState chargingstrategy.State diff --git a/integration/service/oasis/stationgraph/node_graph.go b/integration/service/oasis/stationgraph/node_graph.go index e766b9c992a..e794c1dcc92 100644 --- a/integration/service/oasis/stationgraph/node_graph.go +++ b/integration/service/oasis/stationgraph/node_graph.go @@ -4,6 +4,8 @@ import ( "github.com/Telenav/osrm-backend/integration/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/internal/common" + "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" "github.com/golang/glog" ) @@ -100,10 +102,10 @@ func (g *nodeGraph) StationID(id nodeID) string { return g.nodeContainer.stationID(id) } -func (g *nodeGraph) getPhysicalAdjacentNodes(id nodeID) []*connectivitymap.QueryResult { +func (g *nodeGraph) getPhysicalAdjacentNodes(id nodeID) []*common.RankedPlaceInfo { stationID := g.nodeContainer.stationID(id) - if stationID == invalidStationID { - glog.Errorf("Query getPhysicalAdjacentNodes with invalid node %#v and result %#v\n", id, invalidStationID) + if stationID == stationfindertype.InvalidPlaceIDStr { + glog.Errorf("Query getPhysicalAdjacentNodes with invalid node %#v and result %#v\n", id, stationfindertype.InvalidPlaceIDStr) return nil } return g.querier.NearByStationQuery(stationID) @@ -145,7 +147,7 @@ func (g *nodeGraph) buildAdjacentList(id nodeID) []nodeID { numOfPhysicalNodesNeeded := 0 for _, physicalNode := range physicalNodes { // filter nodes which is un-reachable by current energy, nodes are sorted based on distance - if !g.Node(id).reachableByDistance(physicalNode.Distance) { + if !g.Node(id).reachableByDistance(physicalNode.Weight.Distance) { break } numOfPhysicalNodesNeeded++ @@ -155,12 +157,12 @@ func (g *nodeGraph) buildAdjacentList(id nodeID) []nodeID { for _, physicalNode := range physicalNodes { // filter nodes which is un-reachable by current energy, nodes are sorted based on distance - if !g.Node(id).reachableByDistance(physicalNode.Distance) { + if !g.Node(id).reachableByDistance(physicalNode.Weight.Distance) { break } - nodes := g.createLogicalNodes(id, physicalNode.StationID, physicalNode.StationLocation, - physicalNode.Distance, physicalNode.Duration) + nodes := g.createLogicalNodes(id, physicalNode.ID.String(), physicalNode.Location, + physicalNode.Weight.Distance, physicalNode.Weight.Duration) for _, node := range nodes { adjacentNodeIDs = append(adjacentNodeIDs, node.id) diff --git a/integration/service/oasis/stationgraph/node_graph_test.go b/integration/service/oasis/stationgraph/node_graph_test.go index 6af0bb306e9..d4646eb9136 100644 --- a/integration/service/oasis/stationgraph/node_graph_test.go +++ b/integration/service/oasis/stationgraph/node_graph_test.go @@ -7,6 +7,7 @@ import ( "github.com/Telenav/osrm-backend/integration/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/internal/common" "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" "github.com/golang/glog" ) @@ -23,15 +24,15 @@ func TestAddAndGetStartAndEndNodeForNodeGraph(t *testing.T) { Lat: 34.44, Lon: -124.44} - graph.SetStart(stationfindertype.OrigLocationID, expectStartChargeState, expectStartLocation). - SetEnd(stationfindertype.DestLocationID, expectEndChargeState, expectEndLocation) + graph.SetStart(stationfindertype.OrigLocationID.String(), expectStartChargeState, expectStartLocation). + SetEnd(stationfindertype.DestLocationID.String(), expectEndChargeState, expectEndLocation) - if graph.StationID(graph.StartNodeID()) != stationfindertype.OrigLocationID { - t.Errorf("Incorrect result for start's stationID, expect %s but got %s.\n", stationfindertype.OrigLocationID, graph.StationID(graph.StartNodeID())) + if graph.StationID(graph.StartNodeID()) != stationfindertype.OrigLocationID.String() { + t.Errorf("Incorrect result for start's stationID, expect %s but got %s.\n", stationfindertype.OrigLocationID.String(), graph.StationID(graph.StartNodeID())) } - if graph.StationID(graph.EndNodeID()) != stationfindertype.DestLocationID { - t.Errorf("Incorrect result for end's stationID, expect %s but got %s.\n", stationfindertype.DestLocationID, graph.StationID(graph.EndNodeID())) + if graph.StationID(graph.EndNodeID()) != stationfindertype.DestLocationID.String() { + t.Errorf("Incorrect result for end's stationID, expect %s but got %s.\n", stationfindertype.DestLocationID.String(), graph.StationID(graph.EndNodeID())) } startNode := graph.Node(graph.StartNodeID()) @@ -241,15 +242,15 @@ func TestStationIDInterfaceForNodeGraph(t *testing.T) { nodeIDs := graph.AdjacentNodes(graph.StartNodeID()) expectStationIDs := []string{ - testStationID2, - testStationID2, - testStationID2, - testStationID3, - testStationID3, - testStationID3, - testStationID4, - testStationID4, - testStationID4, + testStationID2.String(), + testStationID2.String(), + testStationID2.String(), + testStationID3.String(), + testStationID3.String(), + testStationID3.String(), + testStationID4.String(), + testStationID4.String(), + testStationID4.String(), } actualStationIDs := make([]string, 0, len(nodeIDs)) @@ -268,67 +269,84 @@ func generateMockNodeGraph() Graph { querier := newMockQuerier() graph := NewNodeGraph(strategy, querier) - origLocation := querier.GetLocation(testStationID1) - graph.SetStart(testStationID1, chargingstrategy.State{Energy: currEnergyLevel}, &nav.Location{Lat: origLocation.Lat, Lon: origLocation.Lon}) + origLocation := querier.GetLocation(testStationID1Str) + graph.SetStart(testStationID1Str, chargingstrategy.State{Energy: currEnergyLevel}, &nav.Location{Lat: origLocation.Lat, Lon: origLocation.Lon}) return graph } type mockQuerier4NodeGraph struct { - mockStationID2QueryResult map[string][]*connectivitymap.QueryResult + mockStationID2QueryResult map[string][]*common.RankedPlaceInfo mockStationID2Location map[string]*nav.Location } func newMockQuerier() connectivitymap.Querier { return &mockQuerier4NodeGraph{ - mockStationID2QueryResult: map[string][]*connectivitymap.QueryResult{ - testStationID1: { + mockStationID2QueryResult: map[string][]*common.RankedPlaceInfo{ + testStationID1Str: { { - StationID: testStationID2, - StationLocation: &nav.Location{Lat: 2.2, Lon: 2.2}, - Distance: 2, - Duration: 2, + PlaceInfo: common.PlaceInfo{ + ID: testStationID2, + Location: &nav.Location{Lat: 2.2, Lon: 2.2}, + }, + Weight: &common.Weight{ + Distance: 2, + Duration: 2, + }, }, { - StationID: testStationID3, - StationLocation: &nav.Location{Lat: 3.3, Lon: 3.3}, - Distance: 3, - Duration: 3, + PlaceInfo: common.PlaceInfo{ + ID: testStationID3, + Location: &nav.Location{Lat: 3.3, Lon: 3.3}, + }, + Weight: &common.Weight{ + Distance: 3, + Duration: 3, + }, }, { - StationID: testStationID4, - StationLocation: &nav.Location{Lat: 4.4, Lon: 4.4}, - Distance: 4, - Duration: 4, + PlaceInfo: common.PlaceInfo{ + ID: testStationID4, + Location: &nav.Location{Lat: 4.4, Lon: 4.4}, + }, + Weight: &common.Weight{ + Distance: 4, + Duration: 4, + }, }, }, }, mockStationID2Location: map[string]*nav.Location{ - testStationID1: {Lat: 1.1, Lon: 1.1}, - testStationID2: {Lat: 2.2, Lon: 2.2}, - testStationID3: {Lat: 3.3, Lon: 3.3}, - testStationID4: {Lat: 4.4, Lon: 4.4}, + testStationID1Str: {Lat: 1.1, Lon: 1.1}, + testStationID2Str: {Lat: 2.2, Lon: 2.2}, + testStationID3Str: {Lat: 3.3, Lon: 3.3}, + testStationID4Str: {Lat: 4.4, Lon: 4.4}, }, } } -func (querier *mockQuerier4NodeGraph) NearByStationQuery(stationID string) []*connectivitymap.QueryResult { - if stationID == testStationID1 { - return querier.mockStationID2QueryResult[testStationID1] +func (querier *mockQuerier4NodeGraph) NearByStationQuery(stationID string) []*common.RankedPlaceInfo { + if stationID == testStationID1Str { + return querier.mockStationID2QueryResult[testStationID1Str] } - glog.Fatal("Un-implemented mapping key for mockStationID2QueryResult.\n") + glog.Fatalf("Un-implemented mapping key %v for mockStationID2QueryResult.\n", stationID) return nil } func (querier *mockQuerier4NodeGraph) GetLocation(stationID string) *nav.Location { - if stationID == testStationID1 { - return querier.mockStationID2Location[testStationID1] + if stationID == testStationID1Str { + return querier.mockStationID2Location[testStationID1Str] } - glog.Fatal("Un-implemented mapping key for mockStationID2Location.\n") + glog.Fatalf("Un-implemented mapping key for mockStationID2Location id = %v.\n", stationID) return nil } +var testStationID1Str = stationfindertype.OrigLocationID.String() +var testStationID2Str = "test_station_id_2" +var testStationID3Str = "test_station_id_3" +var testStationID4Str = "test_station_id_4" + var testStationID1 = stationfindertype.OrigLocationID -var testStationID2 = "test_station_id_2" -var testStationID3 = "test_station_id_3" -var testStationID4 = "test_station_id_4" +var testStationID2 common.PlaceID = 2 +var testStationID3 common.PlaceID = 3 +var testStationID4 common.PlaceID = 4 diff --git a/integration/service/oasis/stationgraph/station_graph.go b/integration/service/oasis/stationgraph/station_graph.go index f1ba3a50e50..6238d45593d 100644 --- a/integration/service/oasis/stationgraph/station_graph.go +++ b/integration/service/oasis/stationgraph/station_graph.go @@ -29,25 +29,25 @@ func NewStationGraph(currEnergyLevel, maxEnergyLevel float64, strategy chargings } func (sg *stationGraph) setStartAndEndForGraph(currEnergyLevel, maxEnergyLevel float64) bool { - startLocation := sg.querier.GetLocation(stationfindertype.OrigLocationID) + startLocation := sg.querier.GetLocation(stationfindertype.OrigLocationID.String()) if startLocation == nil { - glog.Errorf("Failed to find %#v from Querier GetLocation()\n", stationfindertype.OrigLocationID) + glog.Errorf("Failed to find %#v from Querier GetLocation()\n", stationfindertype.OrigLocationID.String()) return false } - endLocation := sg.querier.GetLocation(stationfindertype.DestLocationID) + endLocation := sg.querier.GetLocation(stationfindertype.DestLocationID.String()) if startLocation == nil { - glog.Errorf("Failed to find %#v from Querier GetLocation()\n", stationfindertype.DestLocationID) + glog.Errorf("Failed to find %#v from Querier GetLocation()\n", stationfindertype.DestLocationID.String()) return false } - sg.g = sg.g.SetStart(stationfindertype.OrigLocationID, + sg.g = sg.g.SetStart(stationfindertype.OrigLocationID.String(), chargingstrategy.State{ Energy: currEnergyLevel, }, startLocation) - sg.g = sg.g.SetEnd(stationfindertype.DestLocationID, + sg.g = sg.g.SetEnd(stationfindertype.DestLocationID.String(), chargingstrategy.State{}, endLocation) @@ -158,9 +158,9 @@ func (sg *stationGraph) getLocationInfo(g Graph, n nodeID) *nav.Location { } func (sg *stationGraph) isStart(id string) bool { - return id == stationfindertype.OrigLocationID + return id == stationfindertype.OrigLocationID.String() } func (sg *stationGraph) isEnd(id string) bool { - return id == stationfindertype.DestLocationID + return id == stationfindertype.DestLocationID.String() } diff --git a/integration/service/oasis/stationgraph/station_graph_test.go b/integration/service/oasis/stationgraph/station_graph_test.go index 9cdbca44d2a..f3d232397d1 100644 --- a/integration/service/oasis/stationgraph/station_graph_test.go +++ b/integration/service/oasis/stationgraph/station_graph_test.go @@ -7,6 +7,7 @@ import ( "github.com/Telenav/osrm-backend/integration/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/internal/common" "github.com/Telenav/osrm-backend/integration/service/oasis/internal/solution" "github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype" "github.com/Telenav/osrm-backend/integration/util" @@ -67,119 +68,169 @@ start ------- station 2 \ / end - 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" +var testSGStationID1Str = "1" +var testSGStationID2Str = "2" +var testSGStationID3Str = "3" +var testSGStationID4Str = "4" +var testSGStationID5Str = "5" + +var testSGStationID1 common.PlaceID = 1 +var testSGStationID2 common.PlaceID = 2 +var testSGStationID3 common.PlaceID = 3 +var testSGStationID4 common.PlaceID = 4 +var testSGStationID5 common.PlaceID = 5 type mockQuerier4StationGraph struct { - mockStationID2QueryResult map[string][]*connectivitymap.QueryResult + mockStationID2QueryResult map[string][]*common.RankedPlaceInfo mockStationID2Location map[string]*nav.Location } func newMockQuerier4StationGraph() connectivitymap.Querier { querier := &mockQuerier4StationGraph{ - mockStationID2QueryResult: map[string][]*connectivitymap.QueryResult{ - stationfindertype.OrigLocationID: { + mockStationID2QueryResult: map[string][]*common.RankedPlaceInfo{ + stationfindertype.OrigLocationID.String(): { { - StationID: testSGStationID2, - StationLocation: &nav.Location{Lat: 2.2, Lon: 2.2}, - Distance: 11.1, - Duration: 11.1, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID2, + Location: &nav.Location{Lat: 2.2, Lon: 2.2}, + }, + Weight: &common.Weight{ + Distance: 11.1, + Duration: 11.1, + }, }, { - StationID: testSGStationID1, - StationLocation: &nav.Location{Lat: 1.1, Lon: 1.1}, - Distance: 22.2, - Duration: 22.2, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID1, + Location: &nav.Location{Lat: 1.1, Lon: 1.1}, + }, + Weight: &common.Weight{ + Distance: 22.2, + Duration: 22.2, + }, }, { - StationID: testSGStationID3, - StationLocation: &nav.Location{Lat: 3.3, Lon: 3.3}, - Distance: 33.3, - Duration: 33.3, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID3, + Location: &nav.Location{Lat: 3.3, Lon: 3.3}, + }, + Weight: &common.Weight{ + Distance: 33.3, + Duration: 33.3, + }, }, }, - testSGStationID1: { + testSGStationID1Str: { { - StationID: testSGStationID5, - StationLocation: &nav.Location{Lat: 5.5, Lon: 5.5}, - Distance: 34.4, - Duration: 34.4, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID5, + Location: &nav.Location{Lat: 5.5, Lon: 5.5}, + }, + Weight: &common.Weight{ + Distance: 34.4, + Duration: 34.4, + }, }, { - StationID: testSGStationID4, - StationLocation: &nav.Location{Lat: 4.4, Lon: 4.4}, - Distance: 44.4, - Duration: 44.4, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID4, + Location: &nav.Location{Lat: 4.4, Lon: 4.4}, + }, + Weight: &common.Weight{ + Distance: 44.4, + Duration: 44.4, + }, }, }, - testSGStationID2: { + testSGStationID2Str: { { - StationID: testSGStationID4, - StationLocation: &nav.Location{Lat: 4.4, Lon: 4.4}, - Distance: 11.1, - Duration: 11.1, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID4, + Location: &nav.Location{Lat: 4.4, Lon: 4.4}, + }, + Weight: &common.Weight{ + Distance: 11.1, + Duration: 11.1, + }, }, { - StationID: testSGStationID5, - StationLocation: &nav.Location{Lat: 5.5, Lon: 5.5}, - Distance: 14.4, - Duration: 14.4, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID5, + Location: &nav.Location{Lat: 5.5, Lon: 5.5}, + }, + Weight: &common.Weight{ + Distance: 14.4, + Duration: 14.4, + }, }, }, - testSGStationID3: { + testSGStationID3Str: { { - StationID: testSGStationID5, - StationLocation: &nav.Location{Lat: 5.5, Lon: 5.5}, - Distance: 15.5, - Duration: 15.5, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID5, + Location: &nav.Location{Lat: 5.5, Lon: 5.5}, + }, + Weight: &common.Weight{ + Distance: 15.5, + Duration: 15.5, + }, }, { - StationID: testSGStationID4, - StationLocation: &nav.Location{Lat: 4.4, Lon: 4.4}, - Distance: 22.2, - Duration: 22.2, + PlaceInfo: common.PlaceInfo{ + ID: testSGStationID4, + Location: &nav.Location{Lat: 4.4, Lon: 4.4}, + }, + Weight: &common.Weight{ + Distance: 22.2, + Duration: 22.2, + }, }, }, - testSGStationID4: { + testSGStationID4Str: { { - StationID: stationfindertype.DestLocationID, - StationLocation: &nav.Location{Lat: 6.6, Lon: 6.6}, - Distance: 44.4, - Duration: 44.4, + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: &nav.Location{Lat: 6.6, Lon: 6.6}, + }, + Weight: &common.Weight{ + Distance: 44.4, + Duration: 44.4, + }, }, }, - testSGStationID5: { + testSGStationID5Str: { { - StationID: stationfindertype.DestLocationID, - StationLocation: &nav.Location{Lat: 6.6, Lon: 6.6}, - Distance: 33.3, - Duration: 33.3, + PlaceInfo: common.PlaceInfo{ + ID: stationfindertype.DestLocationID, + Location: &nav.Location{Lat: 6.6, Lon: 6.6}, + }, + Weight: &common.Weight{ + Distance: 33.3, + Duration: 33.3, + }, }, }, - stationfindertype.DestLocationID: {}, + stationfindertype.DestLocationID.String(): {}, }, 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}, + stationfindertype.OrigLocationID.String(): {Lat: 0.0, Lon: 0.0}, + testSGStationID1Str: {Lat: 1.1, Lon: 1.1}, + testSGStationID2Str: {Lat: 2.2, Lon: 2.2}, + testSGStationID3Str: {Lat: 3.3, Lon: 3.3}, + testSGStationID4Str: {Lat: 4.4, Lon: 4.4}, + testSGStationID5Str: {Lat: 5.5, Lon: 5.5}, + stationfindertype.DestLocationID.String(): {Lat: 6.6, Lon: 6.6}, }, } return querier } -func (querier *mockQuerier4StationGraph) NearByStationQuery(stationID string) []*connectivitymap.QueryResult { +func (querier *mockQuerier4StationGraph) NearByStationQuery(stationID string) []*common.RankedPlaceInfo { if queryResult, ok := querier.mockStationID2QueryResult[stationID]; ok { return queryResult } - glog.Fatal("Un-implemented mapping key for mockStationID2QueryResult.\n") + glog.Fatalf("Un-implemented mapping key %v for mockStationID2QueryResult.\n", stationID) return nil } @@ -187,7 +238,7 @@ func (querier *mockQuerier4StationGraph) GetLocation(stationID string) *nav.Loca if location, ok := querier.mockStationID2Location[stationID]; ok { return location } - glog.Fatal("Un-implemented mapping key for mockStationID2Location.\n") + glog.Fatalf("Un-implemented mapping key for mockStationID2Location id = %v.\n", stationID) return nil } @@ -227,7 +278,7 @@ func TestStationGraphGenerateSolutions1(t *testing.T) { Lat: 2.2, Lon: 2.2, }, - StationID: "station2", + StationID: "2", ArrivalEnergy: 8.9, WaitTime: 0, ChargeTime: 2532, @@ -242,7 +293,7 @@ func TestStationGraphGenerateSolutions1(t *testing.T) { Lat: 5.5, Lon: 5.5, }, - StationID: "station5", + StationID: "5", ArrivalEnergy: 15.6, WaitTime: 0, ChargeTime: 5328, @@ -256,7 +307,7 @@ func TestStationGraphGenerateSolutions1(t *testing.T) { // mockedGraph4StationGraph defines compatible topological representation after mockQuerier4StationGraph is built var mockedGraph4StationGraph = mockGraph{ []*node{ - // stationfindertype.OrigLocationID, + // stationfindertype.OrigLocationID.String(), { 0, chargeInfo{ @@ -269,7 +320,7 @@ var mockedGraph4StationGraph = mockGraph{ // Lon: 0.0, // }, }, - // stationfindertype.DestLocationID, + // stationfindertype.DestLocationID.String(), { 1, chargeInfo{ @@ -477,8 +528,8 @@ var mockedGraph4StationGraph = mockGraph{ }, }, []string{ - stationfindertype.OrigLocationID, - stationfindertype.DestLocationID, + stationfindertype.OrigLocationID.String(), + stationfindertype.DestLocationID.String(), "station1", "station1", "station1",