优化蓝图执行器

This commit is contained in:
boyce
2025-10-30 16:00:46 +08:00
parent 21e9b2cd4b
commit 1995d91cfc
7 changed files with 40 additions and 58 deletions

View File

@@ -84,7 +84,12 @@ func (bm *Blueprint) Create(graphName string) int64 {
} }
graphID := atomic.AddInt64(&bm.seedID, 1) graphID := atomic.AddInt64(&bm.seedID, 1)
bm.mapGraph[graphID] = bm.graphPool.Create(graphName, graphID) gr := bm.graphPool.Create(graphName, graphID)
if gr == nil {
return 0
}
bm.mapGraph[graphID] = gr
return graphID return graphID
} }

View File

@@ -32,8 +32,6 @@ type IExecNode interface {
Exec() (int, error) // 返回后续执行的Node的Index Exec() (int, error) // 返回后续执行的Node的Index
GetNextExecLen() int GetNextExecLen() int
getInnerExecNode() IInnerExecNode getInnerExecNode() IInnerExecNode
setVariableName(name string) bool
} }
type innerExecNode struct { type innerExecNode struct {
@@ -51,7 +49,7 @@ type innerExecNode struct {
} }
type BaseExecNode struct { type BaseExecNode struct {
*innerExecNode *innerExecNode // 内部注册的执行结点
// 执行时初始化的数据 // 执行时初始化的数据
*ExecContext *ExecContext
@@ -108,26 +106,13 @@ func (bc *BaseExecConfig) GetMaxOutPortId() int {
return maxPortId return maxPortId
} }
//func (em *innerExecNode) AppendInPort(port ...IPort) {
// if len(em.inPort) == 0 {
// em.inPortParamStartIndex = -1
// }
//
// for i := 0; i < len(port); i++ {
// if !port[i].IsPortExec() && em.inPortParamStartIndex < 0 {
// em.inPortParamStartIndex = len(em.inPort)
// }
//
// em.inPort = append(em.inPort, port[i])
// }
//}
func (em *innerExecNode) PrepareMaxInPortId(maxInPortId int) { func (em *innerExecNode) PrepareMaxInPortId(maxInPortId int) {
em.inPort = make([]IPort, 0, maxInPortId+1) em.inPort = make([]IPort, maxInPortId+1)
} }
func (em *innerExecNode) PrepareMaxOutPortId(maxOutPortId int) { func (em *innerExecNode) PrepareMaxOutPortId(maxOutPortId int) {
em.outPort = make([]IPort, 0, maxOutPortId+1) em.outPort = make([]IPort, maxOutPortId+1)
} }
func (em *innerExecNode) SetInPortById(id int, port IPort) bool { func (em *innerExecNode) SetInPortById(id int, port IPort) bool {
@@ -243,6 +228,10 @@ func (em *innerExecNode) GetOutPort(index int) IPort {
return em.outPort[index] return em.outPort[index]
} }
func (en *BaseExecNode) GetVariableName() string {
return en.execNode.variableName
}
func (en *BaseExecNode) GetBluePrintModule() IBlueprintModule { func (en *BaseExecNode) GetBluePrintModule() IBlueprintModule {
return en.gr.IBlueprintModule return en.gr.IBlueprintModule
} }
@@ -579,9 +568,6 @@ func (en *BaseExecNode) getInnerExecNode() IInnerExecNode {
return en.innerExecNode.IExecNode.(IInnerExecNode) return en.innerExecNode.IExecNode.(IInnerExecNode)
} }
func (en *BaseExecNode) setVariableName(name string) bool {
return false
}
func (en *BaseExecNode) GetBlueprintModule() IBlueprintModule { func (en *BaseExecNode) GetBlueprintModule() IBlueprintModule {
if en.gr == nil { if en.gr == nil {

View File

@@ -6,10 +6,11 @@ import (
"github.com/duanhf2012/origin/v2/service" "github.com/duanhf2012/origin/v2/service"
"github.com/goccy/go-json" "github.com/goccy/go-json"
"github.com/duanhf2012/origin/v2/log"
) )
const ReturnVarial = "g_Return" const ReturnVarial = "g_Return"
var IsDebug = false
type IGraph interface { type IGraph interface {
Do(entranceID int64, args ...any) (Port_Array, error) Do(entranceID int64, args ...any) (Port_Array, error)
Release() Release()
@@ -28,6 +29,7 @@ type baseGraph struct {
} }
type Graph struct { type Graph struct {
graphFileName string
graphID int64 graphID int64
*baseGraph *baseGraph
graphContext graphContext
@@ -137,6 +139,10 @@ func (gc *graphConfig) GetNodeByID(nodeID string) *nodeConfig {
} }
func (gr *Graph) Do(entranceID int64, args ...any) (Port_Array, error) { func (gr *Graph) Do(entranceID int64, args ...any) (Port_Array, error) {
if IsDebug {
log.Debug("Graph Do", log.String("graphName",gr.graphFileName),log.Int64("graphID", gr.graphID), log.Int64("entranceID", entranceID))
}
entranceNode := gr.entrance[entranceID] entranceNode := gr.entrance[entranceID]
if entranceNode == nil { if entranceNode == nil {
return nil, fmt.Errorf("entranceID:%d not found", entranceID) return nil, fmt.Errorf("entranceID:%d not found", entranceID)

View File

@@ -61,6 +61,7 @@ func (gp *GraphPool) Create(graphName string, graphID int64) IGraph {
var graph Graph var graph Graph
graph.baseGraph = gr graph.baseGraph = gr
graph.graphID = graphID graph.graphID = graphID
graph.graphFileName = graphName
graph.context = make(map[string]*ExecContext, 4) graph.context = make(map[string]*ExecContext, 4)
graph.IBlueprintModule = gp.blueprintModule graph.IBlueprintModule = gp.blueprintModule
return &graph return &graph
@@ -137,8 +138,6 @@ func (gp *GraphPool) genVarExec(nodeCfg *nodeConfig, graphConfig *graphConfig) (
} }
e := gp.execPool.GetExec(nodeName) e := gp.execPool.GetExec(nodeName)
e.(IExecNode).setVariableName(varName)
return e, varName return e, varName
} }

View File

@@ -2,6 +2,7 @@ package blueprint
import ( import (
"fmt" "fmt"
"github.com/duanhf2012/origin/v2/log"
) )
type prePortNode struct { type prePortNode struct {
@@ -102,20 +103,6 @@ func (en *execNode) exec(gr *Graph) (int, error) {
return -1, err return -1, err
} }
//defer func() {
inPort, outPort := node.GetPorts()
debugString := "inPort:"
for i := 0; i < len(inPort); i++ {
debugString += fmt.Sprintf("%+v,", inPort[i])
}
debugString += " outPort:"
for i := 0; i < len(outPort); i++ {
debugString += fmt.Sprintf("%+v,", outPort[i])
}
fmt.Printf("exec node %s,%s\n", en.execNode.GetName(), debugString)
//}()
return e.Exec() return e.Exec()
} }
@@ -155,6 +142,10 @@ func (en *execNode) doSetInPort(gr *Graph, index int, inPort IPort) error {
} }
func (en *execNode) Do(gr *Graph, outPortArgs ...any) error { func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
if IsDebug {
log.Debug("Start ExecNode", log.String("Name",en.execNode.GetName()))
}
// 重新初始化上下文 // 重新初始化上下文
inPorts, outPorts := en.execNode.CloneInOutPort() inPorts, outPorts := en.execNode.CloneInOutPort()
gr.context[en.Id] = &ExecContext{ gr.context[en.Id] = &ExecContext{
@@ -194,6 +185,10 @@ func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
return err return err
} }
if IsDebug {
log.Debug("End ExecNode", log.String("Name",en.execNode.GetName()),log.Any("InPort",inPorts ),log.Any("OutPort",outPorts))
}
if nextIndex == -1 || en.nextNode == nil { if nextIndex == -1 || en.nextNode == nil {
return nil return nil
} }

View File

@@ -213,7 +213,7 @@ func (em *Port[T]) convertInt64(v any) (int64, bool) {
func (em *Port[T]) setAnyVale(v any) error { func (em *Port[T]) setAnyVale(v any) error {
switch v.(type) { switch v.(type) {
case int, int64: case int8,int16,int32,int, int64,uint8,uint16,uint32,uint, uint64:
val, ok := em.convertInt64(v) val, ok := em.convertInt64(v)
if !ok { if !ok {
return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v) return fmt.Errorf("port type is %T, but value is %v", em.PortVal, v)

View File

@@ -12,13 +12,11 @@ const globalVariablesPrefix = "g_"
type GetVariablesNode struct { type GetVariablesNode struct {
BaseExecNode BaseExecNode
nodeName string nodeName string
varName string
} }
type SetVariablesNode struct { type SetVariablesNode struct {
BaseExecNode BaseExecNode
nodeName string nodeName string
varName string
} }
func (g *GetVariablesNode) GetName() string { func (g *GetVariablesNode) GetName() string {
@@ -27,14 +25,14 @@ func (g *GetVariablesNode) GetName() string {
func (g *GetVariablesNode) Exec() (int, error) { func (g *GetVariablesNode) Exec() (int, error) {
var port IPort var port IPort
if strings.HasPrefix(g.varName, globalVariablesPrefix) { if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
port = g.gr.globalVariables[g.varName] port = g.gr.globalVariables[g.GetVariableName()]
} else { } else {
port = g.gr.variables[g.varName] port = g.gr.variables[g.GetVariableName()]
} }
if port == nil { if port == nil {
return -1, fmt.Errorf("variable %s not found,node name %s", g.varName, g.nodeName) return -1, fmt.Errorf("variable %s not found,node name %s", g.GetVariableName(), g.nodeName)
} }
if !g.SetOutPort(0, port) { if !g.SetOutPort(0, port) {
@@ -44,10 +42,7 @@ func (g *GetVariablesNode) Exec() (int, error) {
return 0, nil return 0, nil
} }
func (g *GetVariablesNode) setVariableName(name string) bool {
g.varName = name
return true
}
func (g *SetVariablesNode) GetName() string { func (g *SetVariablesNode) GetName() string {
return g.nodeName return g.nodeName
@@ -60,10 +55,10 @@ func (g *SetVariablesNode) Exec() (int, error) {
} }
varPort := port.Clone() varPort := port.Clone()
if strings.HasPrefix(g.varName, globalVariablesPrefix) { if strings.HasPrefix(g.GetVariableName(), globalVariablesPrefix) {
g.gr.globalVariables[g.varName] = varPort g.gr.globalVariables[g.GetVariableName()] = varPort
} else { } else {
g.gr.variables[g.varName] = varPort g.gr.variables[g.GetVariableName()] = varPort
} }
if !g.SetOutPort(1, varPort) { if !g.SetOutPort(1, varPort) {
@@ -73,7 +68,3 @@ func (g *SetVariablesNode) Exec() (int, error) {
return 0, nil return 0, nil
} }
func (g *SetVariablesNode) setVariableName(name string) bool {
g.varName = name
return true
}