Skip to content

Commit

Permalink
refactor: change interface from IGraph to Graph
Browse files Browse the repository at this point in the history
issue: #293
  • Loading branch information
CodeBear801 committed Apr 21, 2020
1 parent b88fe0f commit 7ab4922
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 43 deletions.
4 changes: 2 additions & 2 deletions integration/service/oasis/stationgraph/dijkstra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion integration/service/oasis/stationgraph/dijkstra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func TestDijkstraAlgorithm(t *testing.T) {
cases := []struct {
graph IGraph
graph Graph
start nodeID
end nodeID
chargeStations []nodeID
Expand Down
9 changes: 5 additions & 4 deletions integration/service/oasis/stationgraph/graph_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
34 changes: 21 additions & 13 deletions integration/service/oasis/stationgraph/graph_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -1178,13 +1178,16 @@ 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]
}
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) {
Expand All @@ -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]
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,40 @@ 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,
querier: query,
}
}

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
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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() {
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions integration/service/oasis/stationgraph/node_graph_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package stationgraph

0 comments on commit 7ab4922

Please sign in to comment.