-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraphOptionsData.go
145 lines (119 loc) · 3.94 KB
/
graphOptionsData.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package dot
import (
"fmt"
"strings"
"github.com/wwmoraes/dot/generators"
)
// graphOptionsData contains the parameters used for graph creation
type graphOptionsData struct {
parent Graph
// Generator is used to create IDs for objects to prevent colision
generator generators.IDGenerator
// ID immutable id
id string
// Type the graph type (directed, undirected or sub)
graphType GraphType
// Cluster forbids the creation of multi-edges i.e.:
//
// on directed graphs, only one one edge between a given pair of head and tail nodes is allowed
//
// on undirected graphs, only one edge between the same two nodes is allowed
strict bool
// Cluster denotes if the graph is the special cluster subgraph, whose name
// starts with "cluster_"
cluster bool
// NodeInitializer applies defaults to newly created nodes
nodeInitializer NodeInitializerFn
// EdgeInitializer applies defaults to newly created edges
edgeInitializer EdgeInitializerFn
}
// NewGraphOptions creates a new GraphOptions value that has all values
// initialized properly
func NewGraphOptions(optionsFn ...GraphOptionFn) (options GraphOptions, err error) {
options = &graphOptionsData{
generator: generators.NewRandTimeIDGenerator(24),
graphType: GraphTypeDirected,
}
for _, graphOptionFn := range optionsFn {
err = graphOptionFn(options)
if err != nil {
return nil, err
}
}
return options, err
}
// SetID changes the id
func (options *graphOptionsData) SetID(id string) {
options.id = id
hasClusterPreffix := strings.HasPrefix(options.id, "cluster_")
if options.cluster && !hasClusterPreffix {
options.id = fmt.Sprintf("cluster_%s", options.id)
} else if !options.cluster && hasClusterPreffix {
options.id = strings.TrimPrefix(options.id, "cluster_")
}
}
// ID returns the id
func (options *graphOptionsData) ID() string {
return options.id
}
// SetParent changes the parent graph
func (options *graphOptionsData) SetParent(graph Graph) {
options.parent = graph
options.graphType = GraphTypeSub
}
// Parent return the parent graph
func (options *graphOptionsData) Parent() Graph {
return options.parent
}
// SetType changes the graph type
func (options *graphOptionsData) SetType(graphType GraphType) {
options.graphType = graphType
}
// Type returns the graph type
func (options *graphOptionsData) Type() GraphType {
return options.graphType
}
// SetStrict changes the strict flag
func (options *graphOptionsData) SetStrict(strict bool) {
options.strict = strict
}
// Strict returns the strict flag
func (options *graphOptionsData) Strict() bool {
return options.strict
}
// SetGenerator changes the ID generator
func (options *graphOptionsData) SetGenerator(generator generators.IDGenerator) {
options.generator = generator
}
// Generator return the ID generator
func (options *graphOptionsData) Generator() generators.IDGenerator {
return options.generator
}
// SetNodeInitializer changes the node initializer function
func (options *graphOptionsData) SetNodeInitializer(nodeInitializer NodeInitializerFn) {
options.nodeInitializer = nodeInitializer
}
// NodeInitializer returns the node initializer function
func (options *graphOptionsData) NodeInitializer() NodeInitializerFn {
return options.nodeInitializer
}
// SetEdgeInitializer changes the node initializer function
func (options *graphOptionsData) SetEdgeInitializer(edgeInitializer EdgeInitializerFn) {
options.edgeInitializer = edgeInitializer
}
// EdgeInitializer returns the node initializer function
func (options *graphOptionsData) EdgeInitializer() EdgeInitializerFn {
return options.edgeInitializer
}
// SetCluster changes the cluster flag
func (options *graphOptionsData) SetCluster(cluster bool) {
options.cluster = cluster
hasClusterPreffix := strings.HasPrefix(options.id, "cluster_")
if options.cluster && !hasClusterPreffix {
options.id = fmt.Sprintf("cluster_%s", options.id)
}
}
// Cluster returns if the graph is a cluster
func (options *graphOptionsData) Cluster() bool {
return options.cluster
}