Skip to content

Commit

Permalink
feat : Introducing cluster deployment mode
Browse files Browse the repository at this point in the history
  • Loading branch information
taotao committed Jan 16, 2024
1 parent ee9765e commit e1ed5c3
Show file tree
Hide file tree
Showing 35 changed files with 1,374 additions and 269 deletions.
4 changes: 2 additions & 2 deletions bot/behavior/action_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type ConditionAction struct {
succ bool
}

func (a *ConditionAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *ConditionAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)

a.code = t.Code
}
Expand Down
4 changes: 2 additions & 2 deletions bot/behavior/action_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type LoopAction struct {
curLoop int64
}

func (a *LoopAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *LoopAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)
a.loop = int64(t.Loop)
if a.loop == 0 {
a.loop = 100000000000 // 无限循环 一千亿
Expand Down
4 changes: 2 additions & 2 deletions bot/behavior/action_parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ type ParallelAction struct {
base Node
}

func (a *ParallelAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *ParallelAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)
}

func (a *ParallelAction) AddChild(nod INod) {
Expand Down
9 changes: 6 additions & 3 deletions bot/behavior/action_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ type RootAction struct {
base Node
}

func (a *RootAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *RootAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)
a.base.threadNumber = 1
}

Expand Down Expand Up @@ -38,5 +38,8 @@ func (a *RootAction) onNext(t *Tick) {
}

func (a *RootAction) onReset() {

a.base.SetFreeze(false)
for _, child := range a.base.Children() {
child.onReset()
}
}
4 changes: 2 additions & 2 deletions bot/behavior/action_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type ScriptAction struct {
code string
}

func (a *ScriptAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *ScriptAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)

a.code = t.Code
}
Expand Down
4 changes: 2 additions & 2 deletions bot/behavior/action_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type SelectAction struct {
step int
}

func (a *SelectAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *SelectAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)
}

func (a *SelectAction) AddChild(nod INod) {
Expand Down
4 changes: 2 additions & 2 deletions bot/behavior/action_sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ type SequenceAction struct {
step int
}

func (a *SequenceAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *SequenceAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)
}

func (a *SequenceAction) AddChild(nod INod) {
Expand Down
4 changes: 2 additions & 2 deletions bot/behavior/action_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type WaitAction struct {
endtime int64
}

func (a *WaitAction) Init(t *Tree, parent INod, mode Mode) {
a.base.Init(t, parent, mode)
func (a *WaitAction) Init(t *Tree, parent INod) {
a.base.Init(t, parent)
a.wait = int64(t.Wait)
}

Expand Down
27 changes: 12 additions & 15 deletions bot/behavior/behavior.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Tree struct {
Code string `xml:"code"`

root INod
mode Mode

Children []*Tree `xml:"children"`
}
Expand All @@ -31,44 +30,42 @@ func (t *Tree) GetRoot() INod {
return t.root
}

func (t *Tree) GetMode() Mode {
return t.mode
}

func (t *Tree) link(self INod, parent INod, mode Mode) {
func (t *Tree) link(self INod, parent INod) {

self.Init(t, parent, mode)
self.Init(t, parent)

for k := range t.Children {
child := NewNode(t.Children[k].Ty).(INod)
self.AddChild(child)
t.Children[k].link(child, self, mode)
t.Children[k].link(child, self)
}

}

func Load(f []byte, mode Mode) (*Tree, error) {
func Load(f []byte) (*Tree, error) {

tree := &Tree{
mode: mode,
}
tree := &Tree{}

err := xml.Unmarshal([]byte(f), &tree)
if err != nil {
panic(err)
}

tree.root = NewNode(tree.Ty).(INod)
tree.root.Init(tree, nil, mode)
tree.root.Init(tree, nil)

for k := range tree.Children {

cn := NewNode(tree.Children[k].Ty).(INod)
cn.Init(tree.Children[k], tree.root, mode)
cn.Init(tree.Children[k], tree.root)
tree.root.AddChild(cn)

tree.Children[k].link(cn, tree.root, mode)
tree.Children[k].link(cn, tree.root)
}

return tree, nil
}

func (t *Tree) Reset() {
t.root.onReset()
}
64 changes: 64 additions & 0 deletions bot/behavior/behavior_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package behavior

import (
"fmt"
"sync"

"github.com/pojol/gobot/database"
)

type behaviorPool struct {
sync.Mutex
saved map[string][]*Tree
}

func _new_tree(name string) (*Tree, error) {

dat, err := database.GetBehavior().Find(name)
if err != nil {
return nil, err
}

tree, err := Load(dat.File)
if err != nil {
return nil, err
}

return tree, nil
}

func Get(name string) *Tree {
bp.Lock()
defer bp.Unlock()

lst := bp.saved[name]

n := len(lst)
if n == 0 {
tree, err := _new_tree(name)
if err != nil {
fmt.Println("behaviorPool.Get", err.Error())
return nil
}

return tree
}

x := lst[n-1]
bp.saved[name] = bp.saved[name][0 : n-1]

return x
}

func Put(name string, tree *Tree) {
bp.Lock()
defer bp.Unlock()

tree.Reset()
bp.saved[name] = append(bp.saved[name], tree)

}

var bp = &behaviorPool{
saved: make(map[string][]*Tree),
}
2 changes: 1 addition & 1 deletion bot/behavior/behavior_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ end

func TestLoadTree(t *testing.T) {

_, err := Load([]byte(compose), Thread)
_, err := Load([]byte(compose))
assert.Equal(t, err, nil)

}
10 changes: 2 additions & 8 deletions bot/behavior/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const (
SCRIPT = "ScriptNode"
)

//
const (
Error = "Error"
Succ = "Succ"
Expand All @@ -29,7 +28,7 @@ const (
)

type INod interface {
Init(*Tree, INod, Mode)
Init(*Tree, INod)
AddChild(INod)

getBase() *Node
Expand All @@ -53,10 +52,9 @@ type Node struct {
threadNumber int
}

func (n *Node) Init(t *Tree, parent INod, mode Mode) {
func (n *Node) Init(t *Tree, parent INod) {
n.id = t.ID
n.ty = t.Ty
n.mode = mode

n.parent = parent
}
Expand Down Expand Up @@ -89,10 +87,6 @@ func (a *Node) Children() []INod {
func (a *Node) onTick(t *Tick) {
}

func (a *Node) getMode() Mode {
return a.mode
}

func (a *Node) setThread(number int) {
if a.threadNumber == 0 {
a.threadNumber = number
Expand Down
4 changes: 2 additions & 2 deletions bot/behavior/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ ext:
return state, changestr, err
}

func (t *Tick) Do() (state string, end bool) {
func (t *Tick) Do(mod Mode) (state string, end bool) {

nods := t.blackboard.GetOpenNods()
t.blackboard.ThreadInfoReset()
Expand All @@ -93,7 +93,7 @@ func (t *Tick) Do() (state string, end bool) {
for _, n := range nods {
err = n.onTick(t)

state, msg, parseerr = t.stateCheck(n.getBase().getMode(), n.getType())
state, msg, parseerr = t.stateCheck(mod, n.getType())

threadInfo := ThreadInfo{
Number: n.getBase().getThread(),
Expand Down
4 changes: 2 additions & 2 deletions bot/behavior/tick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestTick(t *testing.T) {

tree, err := Load([]byte(compose), Step)
tree, err := Load([]byte(compose))
assert.Equal(t, err, nil)

bb := &Blackboard{
Expand All @@ -24,7 +24,7 @@ func TestTick(t *testing.T) {
}

for i := 0; i < 150; i++ {
tick.Do()
tick.Do(Step)
time.Sleep(time.Millisecond * 50)
}
}
Loading

0 comments on commit e1ed5c3

Please sign in to comment.