You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

graph.go 2.0 kB

3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package object
  15. type Node struct {
  16. Id string `json:"id"`
  17. Name string `json:"name"`
  18. Value int `json:"val"`
  19. Color string `json:"color"`
  20. Tag string `json:"tag"`
  21. Weight int `json:"weight"`
  22. }
  23. func newNode(id string, name string, value int, color string, tag string, weight int) *Node {
  24. n := Node{}
  25. n.Id = id
  26. n.Name = name
  27. n.Value = value
  28. n.Color = color
  29. n.Tag = tag
  30. n.Weight = weight
  31. return &n
  32. }
  33. type Link struct {
  34. Name string `json:"name"`
  35. Source string `json:"source"`
  36. Target string `json:"target"`
  37. Value int `json:"value"`
  38. Color string `json:"color"`
  39. Tag string `json:"tag"`
  40. }
  41. func newLink(name string, source string, target string, value int, color string, tag string) *Link {
  42. l := Link{}
  43. l.Name = name
  44. l.Source = source
  45. l.Target = target
  46. l.Value = value
  47. l.Color = color
  48. l.Tag = tag
  49. return &l
  50. }
  51. type Graph struct {
  52. Nodes []*Node `json:"nodes"`
  53. Links []*Link `json:"links"`
  54. }
  55. func newGraph() *Graph {
  56. g := Graph{}
  57. return &g
  58. }
  59. func (g *Graph) addNode(id string, name string, value int, color string, tag string, weight int) {
  60. n := newNode(id, name, value, color, tag, weight)
  61. g.Nodes = append(g.Nodes, n)
  62. }
  63. func (g *Graph) addLink(name string, source string, target string, value int, color string, tag string) {
  64. l := newLink(name, source, target, value, color, tag)
  65. g.Links = append(g.Links, l)
  66. }