github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/graph/sub_graph.go (about)

     1  package graph
     2  
     3  import (
     4  	"github.com/15mga/kiwi/util"
     5  )
     6  
     7  type ISubGraph interface {
     8  	IGraph
     9  	INode
    10  	InNode() INode
    11  	SetInNode(name string) *util.Err
    12  	OutNode() INode
    13  	SetOutNode(name string) *util.Err
    14  }
    15  
    16  func newSubGraph(parent IGraph, name string) *subGraph {
    17  	sg := &subGraph{
    18  		IGraph:    NewGraph(name, Plugin(parent.Plugin())),
    19  		graphElem: newGraphElem(parent, name),
    20  	}
    21  	return sg
    22  }
    23  
    24  type subGraph struct {
    25  	IGraph
    26  	graphElem IGraphElem
    27  	inNode    INode
    28  	outNode   INode
    29  }
    30  
    31  func (g *subGraph) Path() string {
    32  	return g.graphElem.Path()
    33  }
    34  
    35  func (g *subGraph) Enable() *util.Err {
    36  	return g.graphElem.Enable()
    37  }
    38  
    39  func (g *subGraph) Disable() *util.Err {
    40  	return g.graphElem.Disable()
    41  }
    42  
    43  func (g *subGraph) SetEnable(enable bool) *util.Err {
    44  	return g.graphElem.SetEnable(enable)
    45  }
    46  
    47  func (g *subGraph) Enabled() bool {
    48  	return g.graphElem.Enabled()
    49  }
    50  
    51  func (g *subGraph) Graph() IGraph {
    52  	return g.graphElem.Graph()
    53  }
    54  
    55  func (g *subGraph) RootGraph() IGraph {
    56  	return g.graphElem.RootGraph()
    57  }
    58  
    59  func (g *subGraph) Comment() string {
    60  	return g.graphElem.Comment()
    61  }
    62  
    63  func (g *subGraph) SetComment(c string) {
    64  	g.graphElem.SetComment(c)
    65  }
    66  
    67  func (g *subGraph) Start() *util.Err {
    68  	err := g.graphElem.Start()
    69  	if err != nil {
    70  		return err
    71  	}
    72  	err = g.IGraph.Start()
    73  	if err != nil {
    74  		return err
    75  	}
    76  	return nil
    77  }
    78  
    79  func (g *subGraph) AddBeforeEnable(fn util.BoolToErr) {
    80  	g.graphElem.AddBeforeEnable(fn)
    81  }
    82  
    83  func (g *subGraph) DelBeforeEnable(fn util.BoolToErr) {
    84  	g.graphElem.DelBeforeEnable(fn)
    85  }
    86  
    87  func (g *subGraph) AddAfterEnable(fn util.FnBool) {
    88  	g.graphElem.AddAfterEnable(fn)
    89  }
    90  
    91  func (g *subGraph) DelAfterEnable(fn util.FnBool) {
    92  	g.graphElem.DelAfterEnable(fn)
    93  }
    94  
    95  func (g *subGraph) AddIn(t TPoint, name string) *util.Err {
    96  	if g.inNode == nil {
    97  		return util.NewErr(util.EcNotExist, util.M{
    98  			"error":    "not exist in node",
    99  			"subGraph": g.Path(),
   100  		})
   101  	}
   102  	return g.inNode.AddIn(t, name)
   103  }
   104  
   105  func (g *subGraph) AddOut(t TPoint, name string) *util.Err {
   106  	if g.outNode == nil {
   107  		return util.NewErr(util.EcNotExist, util.M{
   108  			"error":    "not exist out node",
   109  			"subGraph": g.Path(),
   110  		})
   111  	}
   112  	return g.outNode.AddOut(t, name)
   113  }
   114  
   115  func (g *subGraph) InNode() INode {
   116  	return g.inNode
   117  }
   118  
   119  func (g *subGraph) SetInNode(name string) *util.Err {
   120  	if g.inNode != nil {
   121  		return util.NewErr(util.EcExist, util.M{
   122  			"error":     "in node not nil",
   123  			"sub graph": g.Name(),
   124  		})
   125  	}
   126  	nd, err := g.GetNodeByPath(name)
   127  	if err != nil {
   128  		return err
   129  	}
   130  	g.inNode = nd
   131  	return nil
   132  }
   133  
   134  func (g *subGraph) OutNode() INode {
   135  	return g.outNode
   136  }
   137  
   138  func (g *subGraph) SetOutNode(name string) *util.Err {
   139  	if g.outNode != nil {
   140  		return util.NewErr(util.EcExist, util.M{
   141  			"error":     "out node not nil",
   142  			"sub graph": g.Name(),
   143  		})
   144  	}
   145  	nd, err := g.GetNodeByPath(name)
   146  	if err != nil {
   147  		return err
   148  	}
   149  	g.outNode = nd
   150  	return nil
   151  }
   152  
   153  func (g *subGraph) GetIn(name string) (IIn, *util.Err) {
   154  	if g.inNode == nil {
   155  		return nil, util.NewErr(util.EcNil, util.M{
   156  			"error":     "not set in node",
   157  			"sub graph": g.Name(),
   158  		})
   159  	}
   160  	return g.inNode.GetIn(name)
   161  }
   162  
   163  func (g *subGraph) GetOut(name string) (IOut, *util.Err) {
   164  	if g.outNode == nil {
   165  		return nil, util.NewErr(util.EcNil, util.M{
   166  			"error":     "not set out node",
   167  			"sub graph": g.Name(),
   168  		})
   169  	}
   170  	return g.outNode.GetOut(name)
   171  }
   172  
   173  func (g *subGraph) Out(name string, m any) *util.Err {
   174  	return g.outNode.Out(name, m)
   175  }
   176  
   177  func (g *subGraph) ProcessData(msg IMsg) *util.Err {
   178  	return g.inNode.ProcessData(msg)
   179  }
   180  
   181  func (g *subGraph) HasIn(tag string) bool {
   182  	return g.inNode.HasIn(tag)
   183  }
   184  
   185  func (g *subGraph) HasOut(tag string) bool {
   186  	return g.outNode.HasOut(tag)
   187  }
   188  
   189  func (g *subGraph) BindFn(name string, fn MsgToErr) {
   190  	g.inNode.BindFn(name, fn)
   191  }