mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-06 20:11:27 +08:00
33
kit/flow/edge.go
Normal file
33
kit/flow/edge.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package flow
|
||||
|
||||
import "pandax/kit/utils"
|
||||
|
||||
type Edge struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
SourceNodeId string `json:"sourceNodeId"` //当前节点
|
||||
TargetNodeId string `json:"targetNodeId"` //下一节点
|
||||
StartPoint Point `json:"startPoint"`
|
||||
EndPoint Point `json:"endPoint"`
|
||||
Properties Properties `json:"properties"`
|
||||
PointsList []Point `json:"pointsList"`
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
}
|
||||
|
||||
func (edge *Edge) getTargetNode(sourceNodeId string) string {
|
||||
if edge.SourceNodeId == sourceNodeId {
|
||||
return edge.TargetNodeId
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (edge *Edge) GetProperties(data any) error {
|
||||
if err := utils.Map2Struct(edge.Properties, data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
40
kit/flow/flow.go
Normal file
40
kit/flow/flow.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package flow
|
||||
|
||||
type Flow struct {
|
||||
Nodes []Node `json:"nodes"`
|
||||
Edges []Edge `json:"edges"`
|
||||
}
|
||||
|
||||
func (f *Flow) GetStartNode(ty string) *Node {
|
||||
for _, node := range f.Nodes {
|
||||
if node.IsStartNode(ty) {
|
||||
return &node
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Flow) GetTargetNodeId(sourceNodeId string) string {
|
||||
for _, edge := range f.Edges {
|
||||
return edge.getTargetNode(sourceNodeId)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (f *Flow) GetTargetNode(sourceNodeId string) *Node {
|
||||
for _, edge := range f.Edges {
|
||||
if edge.SourceNodeId == sourceNodeId {
|
||||
return f.GetNode(edge.TargetNodeId)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Flow) GetNode(nodeId string) *Node {
|
||||
for _, node := range f.Nodes {
|
||||
if node.Id == nodeId {
|
||||
return &node
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
42
kit/flow/node.go
Normal file
42
kit/flow/node.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package flow
|
||||
|
||||
import (
|
||||
"pandax/kit/utils"
|
||||
)
|
||||
|
||||
type Properties map[string]any
|
||||
|
||||
type Node struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
Text Text `json:"text"`
|
||||
Properties Properties `json:"properties"`
|
||||
}
|
||||
|
||||
type Text struct {
|
||||
X int `json:"x"`
|
||||
Y int `json:"y"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func (node *Node) IsStartNode(ty string) bool {
|
||||
if node.Type == ty {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (node *Node) GetProperties(data any) error {
|
||||
if err := utils.Map2Struct(node.Properties, data); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NodeFunc func(*Node)
|
||||
|
||||
func (node *Node) RunNodeFunc(nodeFunc NodeFunc) {
|
||||
nodeFunc(node)
|
||||
}
|
||||
Reference in New Issue
Block a user