mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
新增数组支持
This commit is contained in:
@@ -11,12 +11,12 @@ func TestExecMgr(t *testing.T) {
|
||||
t.Fatalf("init failed,err:%v", err)
|
||||
}
|
||||
|
||||
graphTest1 := bp.Create("testSwitch")
|
||||
graphTest1 := bp.Create("testArrayOperator")
|
||||
err = graphTest1.Do(EntranceID_IntParam, 20, 1, 3)
|
||||
if err != nil {
|
||||
t.Fatalf("Do EntranceID_IntParam failed,err:%v", err)
|
||||
}
|
||||
|
||||
graphTest1.Release()
|
||||
//graphTest2 := bp.Create("testForeach")
|
||||
//err = graphTest2.Do(EntranceID_IntParam, 1, 2, 3)
|
||||
//if err != nil {
|
||||
|
||||
@@ -42,7 +42,7 @@ type edgeConfig struct {
|
||||
}
|
||||
|
||||
type MultiTypeValue struct {
|
||||
Value interface{}
|
||||
Value any
|
||||
}
|
||||
|
||||
// 实现json.Unmarshaler接口,自定义解码逻辑
|
||||
@@ -75,6 +75,11 @@ func (v *MultiTypeValue) UnmarshalJSON(data []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var arrayVal []any
|
||||
if err := json.Unmarshal(data, &arrayVal); err == nil {
|
||||
v.Value = arrayVal
|
||||
return nil
|
||||
}
|
||||
// 如果都失败,返回错误
|
||||
return fmt.Errorf("cannot unmarshal JSON value: %s", string(data))
|
||||
}
|
||||
|
||||
@@ -22,11 +22,18 @@ type Port[T iPortType] struct {
|
||||
}
|
||||
|
||||
func (em *Port[T]) Clone() IPort {
|
||||
arrayData, ok := any(em.PortVal).(Port_Array)
|
||||
if !ok {
|
||||
return &Port[T]{
|
||||
PortVal: em.PortVal,
|
||||
}
|
||||
}
|
||||
|
||||
portArray := Port[Port_Array]{}
|
||||
portArray.PortVal = append(portArray.PortVal, arrayData...)
|
||||
return &portArray
|
||||
}
|
||||
|
||||
func (em *Port[T]) Reset() {
|
||||
var v T
|
||||
em.PortVal = v
|
||||
|
||||
@@ -21,6 +21,10 @@ func init() {
|
||||
RegExecNode(&GetArrayInt{})
|
||||
RegExecNode(&GetArrayString{})
|
||||
RegExecNode(&GetArrayLen{})
|
||||
RegExecNode(&CreateIntArray{})
|
||||
RegExecNode(&CreateStringArray{})
|
||||
RegExecNode(&AppendIntegerToArray{})
|
||||
RegExecNode(&AppendStringToArray{})
|
||||
|
||||
RegExecNode(&BoolIf{})
|
||||
RegExecNode(&GreaterThanInteger{})
|
||||
@@ -458,3 +462,136 @@ func (em *Probability) Exec() (int, error) {
|
||||
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// CreateIntArray 创建整型数组
|
||||
type CreateIntArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *CreateIntArray) GetName() string {
|
||||
return "CreateIntArray"
|
||||
}
|
||||
|
||||
func (em *CreateIntArray) Exec() (int, error) {
|
||||
intArray := em.execNode.GetInPortDefaultIntArrayValue(0)
|
||||
if intArray == nil {
|
||||
return -1, fmt.Errorf("CreateIntArray inParam 0 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
||||
}
|
||||
|
||||
for _, v := range intArray {
|
||||
outPort.AppendArrayValInt(v)
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// CreateStringArray 创建字符串数组
|
||||
type CreateStringArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *CreateStringArray) GetName() string {
|
||||
return "CreateStringArray"
|
||||
}
|
||||
|
||||
func (em *CreateStringArray) Exec() (int, error) {
|
||||
intArray := em.execNode.GetInPortDefaultStringArrayValue(0)
|
||||
if intArray == nil {
|
||||
return -1, fmt.Errorf("CreateIntArray inParam 0 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("GetArrayInt outParam 0 not found")
|
||||
}
|
||||
|
||||
for _, v := range intArray {
|
||||
outPort.AppendArrayValStr(v)
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// AppendIntegerToArray 数组追加整型
|
||||
type AppendIntegerToArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *AppendIntegerToArray) GetName() string {
|
||||
return "AppendIntegerToArray"
|
||||
}
|
||||
|
||||
func (em *AppendIntegerToArray) Exec() (int, error) {
|
||||
inPortArray := em.GetInPort(0)
|
||||
if inPortArray == nil {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 0 not found")
|
||||
}
|
||||
|
||||
inPortVal := em.GetInPort(1)
|
||||
if inPortVal == nil {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 1 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray outParam 0 not found")
|
||||
}
|
||||
|
||||
intArray, ok := inPortArray.GetArray()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 0 error")
|
||||
}
|
||||
|
||||
intVal, ok := inPortVal.GetInt()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AppendIntegerToArray inParam 1 error")
|
||||
}
|
||||
|
||||
for i := range intArray {
|
||||
outPort.AppendArrayValInt(intArray[i].IntVal)
|
||||
}
|
||||
outPort.AppendArrayValInt(intVal)
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
// AppendStringToArray 数组追加字符串
|
||||
type AppendStringToArray struct {
|
||||
BaseExecNode
|
||||
}
|
||||
|
||||
func (em *AppendStringToArray) GetName() string {
|
||||
return "AppendStringToArray"
|
||||
}
|
||||
|
||||
func (em *AppendStringToArray) Exec() (int, error) {
|
||||
inPortArray := em.GetInPort(0)
|
||||
if inPortArray == nil {
|
||||
return -1, fmt.Errorf("AppendStringToArray inParam 0 not found")
|
||||
}
|
||||
|
||||
inPortVal := em.GetInPort(1)
|
||||
if inPortVal == nil {
|
||||
return -1, fmt.Errorf("AppendStringToArray inParam 1 not found")
|
||||
}
|
||||
|
||||
outPort := em.GetOutPort(0)
|
||||
if outPort == nil {
|
||||
return -1, fmt.Errorf("AppendStringToArray outParam 0 not found")
|
||||
}
|
||||
|
||||
intArray, ok := inPortArray.GetArray()
|
||||
if !ok {
|
||||
return -1, fmt.Errorf("AppendStringToArray inParam 0 error")
|
||||
}
|
||||
|
||||
for i := range intArray {
|
||||
outPort.AppendArrayValStr(intArray[i].StrVal)
|
||||
}
|
||||
|
||||
return -1, nil
|
||||
}
|
||||
|
||||
@@ -60,7 +60,6 @@ func (g *SetVariablesNode) Exec() (int, error) {
|
||||
}
|
||||
|
||||
varPort := port.Clone()
|
||||
varPort.SetValue(port)
|
||||
if strings.HasPrefix(g.varName, globalVariablesPrefix) {
|
||||
g.gr.globalVariables[g.varName] = varPort
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user