Skip to content

Commit

Permalink
fix: Change representation of stationID to PlaceID
Browse files Browse the repository at this point in the history
issue: #350
  • Loading branch information
CodeBear801 committed May 28, 2020
1 parent 3219e27 commit 457b34b
Show file tree
Hide file tree
Showing 17 changed files with 278 additions and 286 deletions.
10 changes: 5 additions & 5 deletions integration/service/oasis/connectivitymap/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
// 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) []*common.RankedPlaceInfo
// NearByStationQuery finds near by stations by given placeID and return them in recorded sequence
// Returns nil if given placeID is not found or no connectivity
NearByStationQuery(placeID common.PlaceID) []*common.RankedPlaceInfo

// GetLocation returns location of given station id
// Returns nil if given stationID is not found
GetLocation(stationID string) *nav.Location
// Returns nil if given placeID is not found
GetLocation(placeID common.PlaceID) *nav.Location
}
3 changes: 2 additions & 1 deletion integration/service/oasis/internal/common/place.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ type RankedPlaceInfo struct {
}

// PlaceID defines ID for given place(location, point of interest)
// The data used for pre-processing should contain valid PlaceID
// The data used for pre-processing must contain valid PlaceID, which means it
// either a int64 directly or be processed as int64
type PlaceID int64

// String converts PlaceID to string
Expand Down
2 changes: 1 addition & 1 deletion integration/service/oasis/internal/mock/doc.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Package mock provides mock object(e.g., OSRM Table) that may be shared for OASIS' testing.
// Package mock provides mock object(e.g., OSRM Table) that will be shared for OASIS' testing.
package mock
2 changes: 1 addition & 1 deletion integration/service/oasis/internal/solution/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Solution struct {
// ChargeStation contains all information related with specific charge station
type ChargeStation struct {
Location nav.Location
StationID string
PlaceID string
ArrivalEnergy float64
WaitTime float64
ChargeTime float64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package stationconnquerier

import (
"sort"
"strconv"

"github.com/Telenav/osrm-backend/integration/api/nav"
"github.com/Telenav/osrm-backend/integration/service/oasis/connectivitymap"
Expand All @@ -17,7 +16,7 @@ type StationConnectivityQuerier struct {
stationLocationQuerier spatialindexer.PlaceLocationQuerier
stationConnectivity *connectivitymap.ConnectivityMap
reachableStationsByStart []*common.RankedPlaceInfo
reachableStationToEnd map[string]*common.RankedPlaceInfo
reachableStationToEnd map[common.PlaceID]*common.RankedPlaceInfo
startLocation *nav.Location
endLocation *nav.Location
}
Expand Down Expand Up @@ -58,9 +57,9 @@ func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFin
nearByPoints := stationFinder.FindNearByPlaceIDs(center, maxEnergyLevel, spatialindexer.UnlimitedCount)
rankedPoints := stationRanker.RankPlaceIDsByShortestDistance(center, nearByPoints)

reachableStationToEnd := make(map[string]*common.RankedPlaceInfo)
reachableStationToEnd := make(map[common.PlaceID]*common.RankedPlaceInfo)
for _, rankedPointInfo := range rankedPoints {
reachableStationToEnd[rankedPointInfo.ID.String()] = &common.RankedPlaceInfo{
reachableStationToEnd[rankedPointInfo.ID] = &common.RankedPlaceInfo{
PlaceInfo: common.PlaceInfo{
ID: stationfindertype.DestLocationID,
Location: end,
Expand All @@ -73,69 +72,64 @@ func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFin
glog.Infof("Add %d stations connects End node.\n", len(querier.reachableStationToEnd))
}

// 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 finds near by stations by given placeID and return them in recorded sequence
// Returns nil if given placeID is not found or no connectivity
// 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) []*common.RankedPlaceInfo {
func (querier *StationConnectivityQuerier) NearByStationQuery(placeID common.PlaceID) []*common.RankedPlaceInfo {

if stationID == stationfindertype.OrigLocationIDStr {
if placeID == stationfindertype.OrigLocationID {
return querier.reachableStationsByStart
}

if stationID == stationfindertype.DestLocationIDStr {
if placeID == stationfindertype.DestLocationID {
return nil
}

placeID, err := strconv.Atoi(stationID)
if err != nil {
glog.Errorf("Incorrect station ID passed to NearByStationQuery %+v, got error %#v", stationID, err)
return nil
}

if connectivityResults, ok := querier.stationConnectivity.QueryConnectivity((common.PlaceID)(placeID)); ok {
if !querier.isStationConnectsToEnd(stationID) {
if connectivityResults, ok := querier.stationConnectivity.QueryConnectivity(placeID); ok {
if !querier.isStationConnectsToEnd(placeID) {
return connectivityResults
} else {
size := len(connectivityResults) + 1
results := make([]*common.RankedPlaceInfo, 0, size)
for _, result := range connectivityResults {
results = append(results, result)
}
return querier.connectEndIntoGraph(stationID, results)
return querier.connectEndIntoGraph(placeID, results)
}
} else {
// end position just find one charge station, and this charge station didn't has other conductivities
if querier.isStationConnectsToEnd(stationID) {
// end position just find one charge station, and this charge station didn't has other conductivities.
// this only exists theoretically or in mock environment
if querier.isStationConnectsToEnd(placeID) {
results := make([]*common.RankedPlaceInfo, 0, 1)
return querier.connectEndIntoGraph(stationID, results)
return querier.connectEndIntoGraph(placeID, results)
}
}

return nil
}

// GetLocation returns location of given station id
// Returns nil if given stationID is not found
func (querier *StationConnectivityQuerier) GetLocation(stationID string) *nav.Location {
switch stationID {
case stationfindertype.OrigLocationIDStr:
// Returns nil if given placeID is not found
func (querier *StationConnectivityQuerier) GetLocation(placeID common.PlaceID) *nav.Location {
switch placeID {
case stationfindertype.OrigLocationID:
return querier.startLocation
case stationfindertype.DestLocationIDStr:
case stationfindertype.DestLocationID:
return querier.endLocation
default:
return querier.stationLocationQuerier.GetLocation(stationID)
return querier.stationLocationQuerier.GetLocation(placeID.String())
}
}

func (querier *StationConnectivityQuerier) isStationConnectsToEnd(stationID string) bool {
_, ok := querier.reachableStationToEnd[stationID]
func (querier *StationConnectivityQuerier) isStationConnectsToEnd(placeID common.PlaceID) bool {
_, ok := querier.reachableStationToEnd[placeID]
return ok
}

func (querier *StationConnectivityQuerier) connectEndIntoGraph(stationID string, results []*common.RankedPlaceInfo) []*common.RankedPlaceInfo {
if queryResult4End, ok := querier.reachableStationToEnd[stationID]; ok {
func (querier *StationConnectivityQuerier) connectEndIntoGraph(placeID common.PlaceID, results []*common.RankedPlaceInfo) []*common.RankedPlaceInfo {
if queryResult4End, ok := querier.reachableStationToEnd[placeID]; ok {
return appendIntoSortedSlice(queryResult4End, results)
}
return results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,49 +262,49 @@ func TestStationConnQuerier(t *testing.T) {

// verify location
locationCases := []struct {
queryStr string
queryID common.PlaceID
expectLocation *nav.Location
}{
{
stationfindertype.OrigLocationIDStr,
stationfindertype.OrigLocationID,
mockOrigLocation,
},
{
stationfindertype.DestLocationIDStr,
stationfindertype.DestLocationID,
mockDestLocation,
},
{
"1",
1,
mockStation1Location,
},
{
"2",
2,
mockStation2Location,
},
{
"3",
3,
mockStation3Location,
},
{
"incorrect_station_id",
stationfindertype.InvalidPlaceID,
nil,
},
}

for _, c := range locationCases {
actualLocation := querier.GetLocation(c.queryStr)
actualLocation := querier.GetLocation(c.queryID)
if !reflect.DeepEqual(actualLocation, c.expectLocation) {
t.Errorf("Incorrect result for connectivitymap.Querier.GetLocation, expect %+v but got %+v\n", c.expectLocation, actualLocation)
}
}

// verify connectivity
connectivityCases := []struct {
stationID string
placeID common.PlaceID
expectQueryResult []*common.RankedPlaceInfo
}{
{
stationfindertype.OrigLocationIDStr,
stationfindertype.OrigLocationID,
[]*common.RankedPlaceInfo{
{
PlaceInfo: common.PlaceInfo{
Expand Down Expand Up @@ -339,11 +339,11 @@ func TestStationConnQuerier(t *testing.T) {
},
},
{
stationfindertype.DestLocationIDStr,
stationfindertype.DestLocationID,
nil,
},
{
"1",
1,
[]*common.RankedPlaceInfo{
{
PlaceInfo: common.PlaceInfo{
Expand All @@ -368,7 +368,7 @@ func TestStationConnQuerier(t *testing.T) {
},
},
{
"3",
3,
[]*common.RankedPlaceInfo{
{
PlaceInfo: common.PlaceInfo{
Expand All @@ -383,7 +383,7 @@ func TestStationConnQuerier(t *testing.T) {
},
},
{
"2",
2,
[]*common.RankedPlaceInfo{
{
PlaceInfo: common.PlaceInfo{
Expand All @@ -410,7 +410,7 @@ func TestStationConnQuerier(t *testing.T) {
}

for _, c := range connectivityCases {
actualQueryResult := querier.NearByStationQuery(c.stationID)
actualQueryResult := querier.NearByStationQuery(c.placeID)
if !reflect.DeepEqual(actualQueryResult, c.expectQueryResult) {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
"github.com/golang/glog"
)

type stationID2QueryResults map[string][]*common.RankedPlaceInfo
type stationID2Location map[string]*nav.Location
type placeID2QueryResults map[common.PlaceID][]*common.RankedPlaceInfo
type placeID2Location map[common.PlaceID]*nav.Location

type querier struct {
id2QueryResults stationID2QueryResults
id2Location stationID2Location
id2QueryResults placeID2QueryResults
id2Location placeID2Location
}

// NewQuerierBasedOnWeightBetweenNeighborsChan creates connectivitymap.Querier based on channel of charge station's WeightBetweenNeighbors
func NewQuerierBasedOnWeightBetweenNeighborsChan(c chan stationfindertype.WeightBetweenNeighbors) connectivitymap.Querier {
querier := &querier{
id2QueryResults: make(stationID2QueryResults),
id2Location: make(stationID2Location),
id2QueryResults: make(placeID2QueryResults),
id2Location: make(placeID2Location),
}

for item := range c {
Expand All @@ -31,11 +31,11 @@ func NewQuerierBasedOnWeightBetweenNeighborsChan(c chan stationfindertype.Weight

for _, neighborInfo := range item.NeighborsInfo {

if _, ok := querier.id2QueryResults[neighborInfo.FromID]; !ok {
if _, ok := querier.id2QueryResults[neighborInfo.FromPlaceID()]; !ok {
results := make([]*common.RankedPlaceInfo, 0, 10)
querier.id2QueryResults[neighborInfo.FromID] = results
querier.id2QueryResults[neighborInfo.FromPlaceID()] = results
}
querier.id2QueryResults[neighborInfo.FromID] = append(querier.id2QueryResults[neighborInfo.FromID],
querier.id2QueryResults[neighborInfo.FromPlaceID()] = append(querier.id2QueryResults[neighborInfo.FromPlaceID()],
&common.RankedPlaceInfo{
PlaceInfo: common.PlaceInfo{
ID: neighborInfo.ToPlaceID(),
Expand All @@ -50,15 +50,15 @@ func NewQuerierBasedOnWeightBetweenNeighborsChan(c chan stationfindertype.Weight
},
})

if _, ok := querier.id2Location[neighborInfo.FromID]; !ok {
querier.id2Location[neighborInfo.FromID] = &nav.Location{
if _, ok := querier.id2Location[neighborInfo.FromPlaceID()]; !ok {
querier.id2Location[neighborInfo.FromPlaceID()] = &nav.Location{
Lat: neighborInfo.FromLocation.Lat,
Lon: neighborInfo.FromLocation.Lon,
}
}

if _, ok := querier.id2Location[neighborInfo.ToID]; !ok {
querier.id2Location[neighborInfo.ToID] = &nav.Location{
if _, ok := querier.id2Location[neighborInfo.ToPlaceID()]; !ok {
querier.id2Location[neighborInfo.ToPlaceID()] = &nav.Location{
Lat: neighborInfo.ToLocation.Lat,
Lon: neighborInfo.ToLocation.Lon,
}
Expand All @@ -69,16 +69,16 @@ func NewQuerierBasedOnWeightBetweenNeighborsChan(c chan stationfindertype.Weight
return querier
}

func (q *querier) NearByStationQuery(stationID string) []*common.RankedPlaceInfo {
if results, ok := q.id2QueryResults[stationID]; ok {
func (q *querier) NearByStationQuery(placeID common.PlaceID) []*common.RankedPlaceInfo {
if results, ok := q.id2QueryResults[placeID]; ok {
return results
} else {
return nil
}
}

func (q *querier) GetLocation(stationID string) *nav.Location {
if location, ok := q.id2Location[stationID]; ok {
func (q *querier) GetLocation(placeID common.PlaceID) *nav.Location {
if location, ok := q.id2Location[placeID]; ok {
return location
} else {
return nil
Expand Down
Loading

0 comments on commit 457b34b

Please sign in to comment.