Skip to content

Commit

Permalink
feat: robot params
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangliang committed Jul 13, 2022
1 parent 4635364 commit c55f5b8
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 170 deletions.
20 changes: 10 additions & 10 deletions examples/hello/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ func HelloHandlers() agent.Handlers {

func HelloA(tick agent.Ticker) (behavior3go.Status, error) {
var p *Player
one := tick.Marget().InviteAcc()
one := tick.Market().InviteAcc()
if one == nil {
p = NewPlayer(xid.New().String())
tick.Marget().UseAcc(p)
tick.Market().UseAcc(p)
fmt.Println("new player id", p.ID())
} else {
p = one.(*Player)
Expand All @@ -66,15 +66,15 @@ func HelloB(tick agent.Ticker) (behavior3go.Status, error) {

func HelloC(tick agent.Ticker) (behavior3go.Status, error) {
p := tick.Blackboard().GetMem("player").(*Player)
if tick.Marget().Index()%2 == 0 {
m := tick.Marget().InviteOne()
if tick.Market().Index()%2 == 0 {
m := tick.Market().InviteOne()
if m != nil {
fmt.Println("invite player success: ", m.(*Player).id)
} else {
fmt.Println("invite player failed")
}
} else {
tick.Marget().JoinOne(p)
tick.Market().JoinOne(p)
fmt.Println("player join team: ", p.id)
}
return behavior3go.SUCCESS, nil
Expand All @@ -93,14 +93,14 @@ func HelloE(tick agent.Ticker) (behavior3go.Status, error) {

func newUser(tick agent.Ticker) (behavior3go.Status, error) {
player := NewPlayer(xid.New().String())
tick.Marget().PushOne(player)
tick.Market().PushOne(player)
tick.Blackboard().SetMem("userId", player.id)
return behavior3go.SUCCESS, nil
}

func addFriend(tick agent.Ticker) (behavior3go.Status, error) {
userID := tick.Blackboard().GetMem("userId").(string)
one := tick.Marget().RequireOne(func(one agent.One) bool {
one := tick.Market().RequireOne(func(one agent.One) bool {
player := one.(*Player)
return player.ID() != userID
})
Expand All @@ -114,12 +114,12 @@ func addFriend(tick agent.Ticker) (behavior3go.Status, error) {
}

func buildTeam(tick agent.Ticker) (behavior3go.Status, error) {
index := tick.Marget().Index()
index := tick.Market().Index()
userID := tick.Blackboard().GetMem("userId").(string)
if index%2 == 1 {
tick.Marget().JoinOne(NewPlayer(userID))
tick.Market().JoinOne(NewPlayer(userID))
} else {
one := tick.Marget().InviteOne()
one := tick.Market().InviteOne()
if one == nil {
fmt.Println("no found player to build team")
} else {
Expand Down
3 changes: 2 additions & 1 deletion pkg/agent/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ func (m *manager) startExecutor(executor *executor, market *Market) {
withStatPID(actuaryID).
withBeTree(executor.treeCreator()).
withMarket(market).
withAlert(m.alert)
withAlert(m.alert).
withParams(executor.plan.Params)
if i%parallel == 0 && i/parallel > 0 {
<-ticker.C
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/agent/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type job struct {
ctx context.Context
market *Market
alert Alert
params map[string]string
}

func newJob() *job {
Expand Down Expand Up @@ -59,6 +60,19 @@ func (t *job) withAlert(alert Alert) *job {
return t
}

func (t *job) withParams(params map[string]string) *job {
t.params = params
return t
}

func (t *job) newBoard() *core.Blackboard {
board := core.NewBlackboard()
for key, val := range t.params {
board.SetMem(key, val)
}
return board
}

type robot struct {
task *job
}
Expand All @@ -70,7 +84,7 @@ func newRobot(j *job) *robot {
func (r *robot) execute(rootCtx *actor.RootContext) {
start := time.Now()
outcome := transfer.Outcome{Name: "whole_process"}
board := core.NewBlackboard()
board := r.task.newBoard()
tick := NewTick()
tick.market = r.task.market
tick.ctx = r.task.ctx
Expand Down
73 changes: 73 additions & 0 deletions pkg/agent/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ func (s *Signal) Slave() <-chan One {
return s.market.getSlave()
}

type ParamNotFound struct {
}

func (p ParamNotFound) Error() string {
return "NotFoundParam"
}

var paramError = ParamNotFound{}

type One interface {
ID() string
}
Expand Down Expand Up @@ -174,6 +183,10 @@ type Ticker interface {
actorCtx() *actor.RootContext
RecvTime(unixNano string)
SendTime(unixNano string)
GetParamInt(key string) (int, error)
GetParamBool(key string) (bool, error)
GetParamFloat(key string) (float64, error)
GetParamString(key string) (string, error)
}

type Tick struct {
Expand Down Expand Up @@ -242,6 +255,66 @@ func (t *Tick) SendAlertMsg(msg *ErrMsg) error {
return nil
}

func (t *Tick) GetParamString(key string) (string, error) {
val := t.Blackboard().GetMem(key)
if val == nil {
return "", paramError
}
rs, ok := val.(string)
if !ok {
return "", paramError
}
return rs, nil
}

func (t *Tick) GetParamInt(key string) (int, error) {
val := t.Blackboard().GetMem(key)
if val == nil {
return 0, paramError
}
str, ok := val.(string)
if !ok {
return 0, paramError
}
num, err := strconv.Atoi(str)
if err != nil {
return 0, err
}
return num, nil
}

func (t *Tick) GetParamBool(key string) (bool, error) {
val := t.Blackboard().GetMem(key)
if val == nil {
return false, paramError
}
str, ok := val.(string)
if !ok {
return false, paramError
}
rs, err := strconv.ParseBool(str)
if err != nil {
return false, err
}
return rs, nil
}

func (t *Tick) GetParamFloat(key string) (float64, error) {
val := t.Blackboard().GetMem(key)
if val == nil {
return 0, paramError
}
str, ok := val.(string)
if !ok {
return 0, paramError
}
rs, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, err
}
return rs, nil
}

func strToInt64(s string) int64 {
v, err := strconv.ParseInt(s, 10, 64)
if err != nil {
Expand Down
Loading

0 comments on commit c55f5b8

Please sign in to comment.