diff --git a/integration/service/oasis/stationgraph/dijkstra.go b/integration/service/oasis/stationgraph/dijkstra.go index 348257801b0..45175e6d8a1 100644 --- a/integration/service/oasis/stationgraph/dijkstra.go +++ b/integration/service/oasis/stationgraph/dijkstra.go @@ -2,8 +2,8 @@ package stationgraph import "github.com/golang/glog" -// dijkstra accepts IGraph and returns all node ids in the shortest path, except start node and end node -func dijkstra(g IGraph, start, end nodeID) []nodeID { +// dijkstra accepts Graph and returns all node ids in the shortest path, except start node and end node +func dijkstra(g Graph, start, end nodeID) []nodeID { m := newQueryHeap() // init diff --git a/integration/service/oasis/stationgraph/dijkstra_test.go b/integration/service/oasis/stationgraph/dijkstra_test.go index 17828b64082..d225b6478ee 100644 --- a/integration/service/oasis/stationgraph/dijkstra_test.go +++ b/integration/service/oasis/stationgraph/dijkstra_test.go @@ -7,7 +7,7 @@ import ( func TestDijkstraAlgorithm(t *testing.T) { cases := []struct { - graph IGraph + graph Graph start nodeID end nodeID chargeStations []nodeID diff --git a/integration/service/oasis/stationgraph/graph_interface.go b/integration/service/oasis/stationgraph/graph_interface.go index 34407138483..f1fdcbadac2 100644 --- a/integration/service/oasis/stationgraph/graph_interface.go +++ b/integration/service/oasis/stationgraph/graph_interface.go @@ -2,8 +2,9 @@ package stationgraph import "github.com/Telenav/osrm-backend/integration/service/oasis/chargingstrategy" -// IGraph defines interface used for Graph -type IGraph interface { +// Graph defines interface used for Graph +type Graph interface { + // Node returns node object by its nodeID Node(id nodeID) *node @@ -15,10 +16,10 @@ type IGraph interface { Edge(from, to nodeID) *edge // SetStart generates start node for the graph - SetStart(stationID string, targetState chargingstrategy.State, location locationInfo) IGraph + SetStart(stationID string, targetState chargingstrategy.State, location locationInfo) Graph // SetEnd generates end node for the graph - SetEnd(stationID string, targetState chargingstrategy.State, location locationInfo) IGraph + SetEnd(stationID string, targetState chargingstrategy.State, location locationInfo) Graph // StartNodeID returns start node's ID for given graph StartNodeID() nodeID diff --git a/integration/service/oasis/stationgraph/graph_mock.go b/integration/service/oasis/stationgraph/graph_mock.go index 6226ff81ea7..b7f983605cb 100644 --- a/integration/service/oasis/stationgraph/graph_mock.go +++ b/integration/service/oasis/stationgraph/graph_mock.go @@ -9,7 +9,7 @@ import "github.com/Telenav/osrm-backend/integration/service/oasis/chargingstrate // node_2 -> node_3, duration = 50, distance = 50 // node_3 -> node_4, duration = 10, distance = 10 // Set charge information to fixed status to ignore situation of lack of energy -func NewMockGraph1() IGraph { +func NewMockGraph1() Graph { return &mockGraph{ []*node{ { @@ -181,7 +181,7 @@ func NewMockGraph1() IGraph { // node_6 -> node_8, duration = 20, distance = 20 // node_7 -> node_8, duration = 30, distance = 30 // Set charge information to fixed status to ignore situation of lack of energy -func NewMockGraph2() IGraph { +func NewMockGraph2() Graph { return &mockGraph{ []*node{ { @@ -516,7 +516,7 @@ func NewMockGraph2() IGraph { // node_6 -> node_8, duration = 20, distance = 20 // node_7 -> node_8, duration = 30, distance = 30 // Set charge information to fixed status to ignore situation of lack of energy -func NewMockGraph3() IGraph { +func NewMockGraph3() Graph { return &mockGraph{ []*node{ { @@ -852,7 +852,7 @@ func NewMockGraph3() IGraph { // node_7 -> node_8, duration = 30, distance = 30 // Charge information // each station only charges 16 unit of energy -func NewMockGraph4() IGraph { +func NewMockGraph4() Graph { return &mockGraph{ []*node{ { @@ -1178,6 +1178,7 @@ type mockGraph struct { strategy chargingstrategy.Strategy } +// Node returns node object by its nodeID func (graph *mockGraph) Node(id nodeID) *node { if graph.isValidNodeID(id) { return graph.nodes[id] @@ -1185,6 +1186,8 @@ func (graph *mockGraph) Node(id nodeID) *node { return nil } +// AdjacentNodes returns a group of node ids which connect with given node id +// The connectivity between nodes is build during running time. func (graph *mockGraph) AdjacentNodes(id nodeID) []nodeID { var nodeIDs []nodeID if graph.isValidNodeID(id) { @@ -1199,6 +1202,7 @@ func (graph *mockGraph) AdjacentNodes(id nodeID) []nodeID { return nodeIDs } +// Edge returns edge information between given two nodes func (graph *mockGraph) Edge(from, to nodeID) *edge { if graph.isValidNodeID(from) && graph.isValidNodeID(to) { edges, ok := graph.edges[from] @@ -1215,37 +1219,41 @@ func (graph *mockGraph) Edge(from, to nodeID) *edge { } // SetStart generates start node for the graph -func (graph *mockGraph) SetStart(stationID string, targetState chargingstrategy.State, location locationInfo) IGraph { +func (graph *mockGraph) SetStart(stationID string, targetState chargingstrategy.State, location locationInfo) Graph { return graph } // SetEnd generates end node for the graph -func (graph *mockGraph) SetEnd(stationID string, targetState chargingstrategy.State, location locationInfo) IGraph { +func (graph *mockGraph) SetEnd(stationID string, targetState chargingstrategy.State, location locationInfo) Graph { return graph } +// StartNodeID returns start node's ID for given graph func (graph *mockGraph) StartNodeID() nodeID { return invalidNodeID } +// EndNodeID returns end node's ID for given graph func (graph *mockGraph) EndNodeID() nodeID { return invalidNodeID } +// ChargeStrategy returns charge strategy used for graph construction func (graph *mockGraph) ChargeStrategy() chargingstrategy.Strategy { return graph.strategy } -func (graph *mockGraph) isValidNodeID(id nodeID) bool { - if id < 0 || int(id) >= len(graph.nodes) { - return false - } - return true -} - +// 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 graph.stationIDs[id] } + +func (graph *mockGraph) isValidNodeID(id nodeID) bool { + if id < 0 || int(id) >= len(graph.nodes) { + return false + } + return true +} diff --git a/integration/service/oasis/stationgraph/graph.go b/integration/service/oasis/stationgraph/node_graph.go similarity index 56% rename from integration/service/oasis/stationgraph/graph.go rename to integration/service/oasis/stationgraph/node_graph.go index 9d28a80dbe4..5f60430e822 100644 --- a/integration/service/oasis/stationgraph/graph.go +++ b/integration/service/oasis/stationgraph/node_graph.go @@ -7,24 +7,25 @@ import ( "github.com/golang/glog" ) -// NearByStationQuery(center Location, distanceLimit float64, limitCount int) []*RankedPointInfo +type nodeID2AdjacentNodes map[nodeID][]nodeID +type edgeID2EdgeData map[edgeID]*edge -type graph struct { +type nodeGraph struct { nodeContainer *nodeContainer - adjacentList map[nodeID][]nodeID - edgeData map[edgeID]*edge + adjacentList nodeID2AdjacentNodes + edgeData edgeID2EdgeData startNodeID nodeID endNodeID nodeID strategy chargingstrategy.Strategy querier connectivitymap.Querier } -// NewGraph creates new graph which implements IGraph -func NewGraph(strategy chargingstrategy.Strategy, query connectivitymap.Querier) IGraph { - return &graph{ +// NewNodeGraph creates new node based graph which implements Graph +func NewNodeGraph(strategy chargingstrategy.Strategy, query connectivitymap.Querier) Graph { + return &nodeGraph{ nodeContainer: newNodeContainer(), - adjacentList: make(map[nodeID][]nodeID), - edgeData: make(map[edgeID]*edge), + adjacentList: make(nodeID2AdjacentNodes), + edgeData: make(edgeID2EdgeData), startNodeID: invalidNodeID, endNodeID: invalidNodeID, strategy: strategy, @@ -32,11 +33,14 @@ func NewGraph(strategy chargingstrategy.Strategy, query connectivitymap.Querier) } } -func (g *graph) Node(id nodeID) *node { +// Node returns node object by its nodeID +func (g *nodeGraph) Node(id nodeID) *node { return g.nodeContainer.getNode(id) } -func (g *graph) AdjacentNodes(id nodeID) []nodeID { +// AdjacentNodes returns a group of node ids which connect with given node id +// The connectivity between nodes is build during running time. +func (g *nodeGraph) AdjacentNodes(id nodeID) []nodeID { if !g.nodeContainer.isNodeVisited(id) { glog.Errorf("While calling AdjacentNodes with un-added nodeID %#v, check your algorithm.\n", id) return nil @@ -52,7 +56,8 @@ func (g *graph) AdjacentNodes(id nodeID) []nodeID { } -func (g *graph) Edge(from, to nodeID) *edge { +// Edge returns edge information between given two nodes +func (g *nodeGraph) Edge(from, to nodeID) *edge { edgeID := edgeID{ fromNodeID: from, toNodeID: to, @@ -61,37 +66,41 @@ func (g *graph) Edge(from, to nodeID) *edge { return g.edgeData[edgeID] } -// SetStart generates start node for the graph -func (g *graph) SetStart(stationID string, targetState chargingstrategy.State, location locationInfo) IGraph { +// SetStart generates start node for the nodeGraph +func (g *nodeGraph) SetStart(stationID string, targetState chargingstrategy.State, location locationInfo) Graph { n := g.nodeContainer.addNode(stationID, targetState, location) g.startNodeID = n.id return g } -// SetEnd generates end node for the graph -func (g *graph) SetEnd(stationID string, targetState chargingstrategy.State, location locationInfo) IGraph { +// SetEnd generates end node for the nodeGraph +func (g *nodeGraph) SetEnd(stationID string, targetState chargingstrategy.State, location locationInfo) Graph { n := g.nodeContainer.addNode(stationID, targetState, location) g.endNodeID = n.id return g } -func (g *graph) StartNodeID() nodeID { +// StartNodeID returns start node's ID for given graph +func (g *nodeGraph) StartNodeID() nodeID { return g.startNodeID } -func (g *graph) EndNodeID() nodeID { +// EndNodeID returns end node's ID for given graph +func (g *nodeGraph) EndNodeID() nodeID { return g.endNodeID } -func (g *graph) ChargeStrategy() chargingstrategy.Strategy { +// ChargeStrategy returns charge strategy used for graph construction +func (g *nodeGraph) ChargeStrategy() chargingstrategy.Strategy { return g.strategy } -func (g *graph) StationID(id nodeID) string { +// StationID returns original stationID from internal nodeID +func (g *nodeGraph) StationID(id nodeID) string { return g.nodeContainer.stationID(id) } -func (g *graph) getPhysicalAdjacentNodes(id nodeID) []*connectivitymap.QueryResult { +func (g *nodeGraph) getPhysicalAdjacentNodes(id nodeID) []*connectivitymap.QueryResult { stationID := g.nodeContainer.stationID(id) if stationID == invalidStationID { glog.Errorf("Query getPhysicalAdjacentNodes with invalid node %#v and result %#v\n", id, invalidStationID) @@ -100,7 +109,7 @@ func (g *graph) getPhysicalAdjacentNodes(id nodeID) []*connectivitymap.QueryResu return g.querier.NearByStationQuery(stationID) } -func (g *graph) createLogicalNodes(from nodeID, toStationID string, toLocation *nav.Location, distance, duration float64) []*node { +func (g *nodeGraph) createLogicalNodes(from nodeID, toStationID string, toLocation *nav.Location, distance, duration float64) []*node { results := make([]*node, 0, 10) for _, state := range g.strategy.CreateChargingStates() { @@ -119,7 +128,7 @@ func (g *graph) createLogicalNodes(from nodeID, toStationID string, toLocation * return results } -func (g *graph) buildAdjacentList(id nodeID) []nodeID { +func (g *nodeGraph) buildAdjacentList(id nodeID) []nodeID { adjacentNodeIDs := make([]nodeID, 0, 500) physicalNodes := g.getPhysicalAdjacentNodes(id) diff --git a/integration/service/oasis/stationgraph/node_graph_test.go b/integration/service/oasis/stationgraph/node_graph_test.go new file mode 100644 index 00000000000..237c74fdd44 --- /dev/null +++ b/integration/service/oasis/stationgraph/node_graph_test.go @@ -0,0 +1 @@ +package stationgraph