新增结点实现

This commit is contained in:
boyce
2025-10-02 11:48:58 +08:00
parent 6511fc4ac0
commit d4c0bd22ad
8 changed files with 278 additions and 11 deletions

View File

@@ -5,15 +5,14 @@ import (
)
func TestExecMgr(t *testing.T) {
var bp Blueprint
err := bp.Init("D:\\Develop\\OriginNodeEditor\\json", "D:\\Develop\\OriginNodeEditor\\vgf")
if err != nil {
t.Fatalf("init failed,err:%v", err)
}
graphTest1 := bp.Create("testArray")
err = graphTest1.Do(EntranceID_ArrayParam, 1, []int64{10, 11, 12})
graphTest1 := bp.Create("testMath")
err = graphTest1.Do(EntranceID_IntParam, 1, 2, 3)
if err != nil {
t.Fatalf("Do EntranceID_IntParam failed,err:%v", err)
}

View File

@@ -5,6 +5,7 @@ import "fmt"
type IBaseExecNode interface {
initInnerExecNode(innerNode *innerExecNode)
initExecNode(gr *Graph, en *execNode) error
GetPorts() ([]IPort, []IPort)
}
type IInnerExecNode interface {
@@ -147,6 +148,7 @@ func (em *innerExecNode) IsInPortExec(index int) bool {
return em.inPort[index].IsPortExec()
}
func (em *innerExecNode) IsOutPortExec(index int) bool {
if index >= len(em.outPort) || index < 0 {
return false
@@ -201,6 +203,10 @@ func (en *BaseExecNode) initExecNode(gr *Graph, node *execNode) error {
return nil
}
func (en *BaseExecNode) GetPorts() ([]IPort, []IPort) {
return en.InputPorts, en.OutputPorts
}
func (en *BaseExecNode) GetInPort(index int) IPort {
if en.InputPorts == nil {
return nil
@@ -481,6 +487,10 @@ func (en *BaseExecNode) DoNext(index int) error {
return fmt.Errorf("next index %d not found", index)
}
if en.execNode.nextNode[index] == nil {
return nil
}
return en.execNode.nextNode[index].Do(en.gr)
}

View File

@@ -179,15 +179,17 @@ func (gp *GraphPool) prepareOneNode(mapNodeExec map[string]*execNode, nodeExec *
for ; nodeExec.execNode.IsOutPortExec(idx) && idx < nodeExec.execNode.GetOutPortCount(); idx++ {
// 找到出口结点
nextExecNode := gp.findOutNextNode(graphConfig, mapNodeExec, nodeExec.Id, idx)
if nextExecNode == nil {
continue
}
nextExecNode.beConnect = true
nodeExec.nextNode = append(nodeExec.nextNode, nextExecNode)
if nextExecNode != nil {
nextExecNode.beConnect = true
}
}
// 将所有的next填充next
for _, nextOne := range nodeExec.nextNode {
if nextOne == nil {
continue
}
// 对出口进行预处理
err := gp.prepareOneNode(mapNodeExec, nextOne, graphConfig, recursion)
if err != nil {

237
util/blueprint/mathnode.go Normal file
View File

@@ -0,0 +1,237 @@
package blueprint
import "fmt"
func init() {
RegExecNode(&AddInt{})
RegExecNode(&SubInt{})
RegExecNode(&MulInt{})
RegExecNode(&DivInt{})
RegExecNode(&ModInt{})
}
type AddInt struct {
BaseExecNode
}
func (em *AddInt) GetName() string {
return "AddInt"
}
func (em *AddInt) Exec() (int, error) {
inPortA := em.GetInPort(0)
if inPortA == nil {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inPortB := em.GetInPort(1)
if inPortB == nil {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
outPortRet := em.GetOutPort(0)
if outPortRet == nil {
return -1, fmt.Errorf("AddInt outParam not found")
}
inA, ok := inPortA.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inB, ok := inPortB.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
ret := inA + inB
outPortRet.SetInt(ret)
return -1, nil
}
type SubInt struct {
BaseExecNode
}
func (em *SubInt) GetName() string {
return "SubInt"
}
func (em *SubInt) Exec() (int, error) {
inPortA := em.GetInPort(0)
if inPortA == nil {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inPortB := em.GetInPort(1)
if inPortB == nil {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
inPortAbs := em.GetInPort(2)
if inPortAbs == nil {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
outPortRet := em.GetOutPort(0)
if outPortRet == nil {
return -1, fmt.Errorf("AddInt outParam not found")
}
inA, ok := inPortA.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inB, ok := inPortB.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
isAbs, ok := inPortAbs.GetBool()
if !ok {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
ret := inA - inB
if isAbs && ret < 0 {
ret *= -1
}
outPortRet.SetInt(ret)
return -1, nil
}
type MulInt struct {
BaseExecNode
}
func (em *MulInt) GetName() string {
return "MulInt"
}
func (em *MulInt) Exec() (int, error) {
inPortA := em.GetInPort(0)
if inPortA == nil {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inPortB := em.GetInPort(1)
if inPortB == nil {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
outPortRet := em.GetOutPort(0)
if outPortRet == nil {
return -1, fmt.Errorf("AddInt outParam not found")
}
inA, ok := inPortA.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inB, ok := inPortB.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
outPortRet.SetInt(inA * inB)
return -1, nil
}
type DivInt struct {
BaseExecNode
}
func (em *DivInt) GetName() string {
return "DivInt"
}
func (em *DivInt) Exec() (int, error) {
inPortA := em.GetInPort(0)
if inPortA == nil {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inPortB := em.GetInPort(1)
if inPortB == nil {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
inPortRound := em.GetInPort(2)
if inPortRound == nil {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
outPortRet := em.GetOutPort(0)
if outPortRet == nil {
return -1, fmt.Errorf("AddInt outParam not found")
}
inA, ok := inPortA.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inB, ok := inPortB.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
isRound, ok := inPortRound.GetBool()
if !ok {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
if inB == 0 {
return -1, fmt.Errorf("div zero error")
}
var ret int64
if isRound {
ret = (inA + inB/2) / inB
} else {
ret = inA / inB
}
outPortRet.SetInt(ret)
return -1, nil
}
type ModInt struct {
BaseExecNode
}
func (em *ModInt) GetName() string {
return "ModInt"
}
func (em *ModInt) Exec() (int, error) {
inPortA := em.GetInPort(0)
if inPortA == nil {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inPortB := em.GetInPort(1)
if inPortB == nil {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
outPortRet := em.GetOutPort(0)
if outPortRet == nil {
return -1, fmt.Errorf("AddInt outParam not found")
}
inA, ok := inPortA.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 1 not found")
}
inB, ok := inPortB.GetInt()
if !ok {
return -1, fmt.Errorf("AddInt inParam 2 not found")
}
if inB == 0 {
return -1, fmt.Errorf("mod zero error")
}
outPortRet.SetInt(inA % inB)
return -1, nil
}

View File

@@ -71,7 +71,20 @@ func (en *execNode) exec(gr *Graph) (int, error) {
return -1, err
}
//log.Debug("exec node %s", en.execNode.GetName())
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()
}
@@ -158,5 +171,8 @@ func (en *execNode) Do(gr *Graph, outPortArgs ...any) error {
return fmt.Errorf("next index %d not found", nextIndex)
}
if en.nextNode[nextIndex] == nil {
return nil
}
return en.nextNode[nextIndex].Do(gr)
}

View File

@@ -8,7 +8,7 @@ import (
// 系统入口ID定义1000以内
const (
EntranceID_ArrayParam = 2
EntranceID_IntParam = 3
EntranceID_IntParam = 1
)
func init() {
@@ -60,7 +60,7 @@ func (em *Output) Exec() (int, error) {
return 0, fmt.Errorf("Output Exec inParam not found")
}
fmt.Printf("Output Exec inParam %d", val)
fmt.Printf("Output Exec inParam %d\n", val)
return 0, nil
}