初始化 code
This commit is contained in:
commit
79b381f895
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
hunter
|
||||
.vscode
|
||||
*.log
|
||||
*.png
|
||||
*.tar
|
||||
*.gz
|
||||
*.7z
|
25
context.go
Normal file
25
context.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package hunter
|
||||
|
||||
// TaskContext 上下文
|
||||
type TaskContext struct {
|
||||
hunter *Hunter
|
||||
curNode ITaskNode
|
||||
}
|
||||
|
||||
// NewContext 任务上下文
|
||||
func NewContext() *TaskContext {
|
||||
return &TaskContext{}
|
||||
}
|
||||
|
||||
// AddTask 添加到当前子任务队列
|
||||
func (cxt *TaskContext) AddTask(itask ITask) {
|
||||
if children := cxt.curNode.Children(); children == nil {
|
||||
cxt.curNode.SetChildren(cxt.hunter.createQueue())
|
||||
}
|
||||
cxt.curNode.Children().Push(itask)
|
||||
}
|
||||
|
||||
// AddParentTask 添加到当前任务队列
|
||||
func (cxt *TaskContext) AddParentTask(itask ITask) {
|
||||
cxt.curNode.Parent().Children().Push(itask)
|
||||
}
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
|||
module github.com/474420502/hunter
|
||||
|
||||
go 1.14
|
||||
|
||||
require github.com/474420502/focus v0.8.1
|
5
go.sum
Normal file
5
go.sum
Normal file
|
@ -0,0 +1,5 @@
|
|||
github.com/474420502/focus v0.8.1 h1:PZwCgzcnxwx7ZZCWc/XKLVaZPH9e4YX9cP4ckyT2HDA=
|
||||
github.com/474420502/focus v0.8.1/go.mod h1:jrDXvK1CnUJ3PCR3ZJVYinbS2Yz5kM8OoAbCLe6AF7Y=
|
||||
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
82
hunter.go
Normal file
82
hunter.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package hunter
|
||||
|
||||
import (
|
||||
pqueue "github.com/474420502/focus/priority_queue"
|
||||
)
|
||||
|
||||
// IGobalBefore 全局任务执行之前
|
||||
type IGobalBefore interface {
|
||||
GobalBefore()
|
||||
}
|
||||
|
||||
// IGobalAfter 全局任务执行之后
|
||||
type IGobalAfter interface {
|
||||
GobalAfter()
|
||||
}
|
||||
|
||||
// Hunter 任务相关 必须有序
|
||||
type Hunter struct {
|
||||
cxt *TaskContext
|
||||
task ITaskNode
|
||||
createQueue func() *pqueue.PriorityQueue
|
||||
}
|
||||
|
||||
// NewHunter 默认最大优先
|
||||
func NewHunter() *Hunter {
|
||||
return NewPriorityMaxHunter()
|
||||
}
|
||||
|
||||
// NewPriorityHunter 自定义优先处理队列
|
||||
func NewPriorityHunter(queueCreator func() *pqueue.PriorityQueue) *Hunter {
|
||||
hunter := &Hunter{}
|
||||
hunter.createQueue = queueCreator
|
||||
hunter.task = &BaseTask{}
|
||||
hunter.task.SetParent(nil)
|
||||
hunter.task.SetChildren(hunter.createQueue())
|
||||
|
||||
hunter.cxt = NewContext()
|
||||
hunter.cxt.curNode = hunter.task
|
||||
return hunter
|
||||
}
|
||||
|
||||
// NewPriorityMaxHunter 最大优先
|
||||
func NewPriorityMaxHunter() *Hunter {
|
||||
return NewPriorityHunter(CreatePriorityMaxQueue)
|
||||
}
|
||||
|
||||
// NewPriorityMinHunter 最小优先
|
||||
func NewPriorityMinHunter() *Hunter {
|
||||
return NewPriorityHunter(CreatePriorityMinQueue)
|
||||
}
|
||||
|
||||
// Execute 执行任务
|
||||
func (hunter *Hunter) Execute() {
|
||||
hunter.recursionTasks(hunter.task)
|
||||
}
|
||||
|
||||
func (hunter *Hunter) recursionTasks(itask ITaskNode) {
|
||||
for children := itask.Children(); children != nil && children.Size() > 0; {
|
||||
if itask, ok := children.Pop(); ok {
|
||||
tasknode := itask.(ITaskNode)
|
||||
tasknode.Task().Execute(hunter.cxt)
|
||||
hunter.recursionTasks(tasknode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Stop 停止任务
|
||||
func (hunter *Hunter) Stop() {
|
||||
|
||||
}
|
||||
|
||||
// AddTask 执行任务
|
||||
func (hunter *Hunter) AddTask(task ITask) {
|
||||
hunter.cxt.AddTask(task)
|
||||
}
|
||||
|
||||
// Execute 执行
|
||||
// func (hunter *Hunter) Execute() {
|
||||
// if itask, ok := hunter.task.Children().Top(); ok {
|
||||
// task := itask.(ITask)
|
||||
// }
|
||||
// }
|
7
hunter_test.go
Normal file
7
hunter_test.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package hunter
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCase1(t *testing.T) {
|
||||
|
||||
}
|
86
priority.go
Normal file
86
priority.go
Normal file
|
@ -0,0 +1,86 @@
|
|||
package hunter
|
||||
|
||||
import pqueue "github.com/474420502/focus/priority_queue"
|
||||
|
||||
// IPriority 优先接口
|
||||
type IPriority interface {
|
||||
Priority() float64
|
||||
}
|
||||
|
||||
// PriorityInt Int优先级
|
||||
type PriorityInt int
|
||||
|
||||
// Priority Get priority
|
||||
func (pri PriorityInt) Priority() float64 {
|
||||
return (float64)(pri)
|
||||
}
|
||||
|
||||
// PriorityInt32 Int优先级
|
||||
type PriorityInt32 int32
|
||||
|
||||
// Priority Get priority
|
||||
func (pri PriorityInt32) Priority() float64 {
|
||||
return (float64)(pri)
|
||||
}
|
||||
|
||||
// PriorityInt64 Int优先级
|
||||
type PriorityInt64 int64
|
||||
|
||||
// Priority Get priority
|
||||
func (pri PriorityInt64) Priority() float64 {
|
||||
return (float64)(pri)
|
||||
}
|
||||
|
||||
// PriorityFloat32 Int优先级
|
||||
type PriorityFloat32 float32
|
||||
|
||||
// Priority Get priority
|
||||
func (pri PriorityFloat32) Priority() float64 {
|
||||
return (float64)(pri)
|
||||
}
|
||||
|
||||
// CreatePriorityMaxQueue 创建最大优先队列
|
||||
func CreatePriorityMaxQueue() *pqueue.PriorityQueue {
|
||||
return pqueue.New(priorityMax)
|
||||
}
|
||||
|
||||
// CreatePriorityMinQueue 创建最小优先队列
|
||||
func CreatePriorityMinQueue() *pqueue.PriorityQueue {
|
||||
return pqueue.New(priorityMin)
|
||||
}
|
||||
|
||||
// priorityMax 最大值优先
|
||||
func priorityMax(k1, k2 interface{}) int {
|
||||
p1, p2 := 0.0, 0.0
|
||||
|
||||
if priority, ok := k1.(IPriority); ok {
|
||||
p1 = priority.Priority()
|
||||
}
|
||||
|
||||
if priority, ok := k2.(IPriority); ok {
|
||||
p2 = priority.Priority()
|
||||
}
|
||||
|
||||
if p1 > p2 {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// priorityMin 最小值优先
|
||||
func priorityMin(k1, k2 interface{}) int {
|
||||
p1, p2 := 0.0, 0.0
|
||||
|
||||
if priority, ok := k1.(IPriority); ok {
|
||||
p1 = priority.Priority()
|
||||
}
|
||||
|
||||
if priority, ok := k2.(IPriority); ok {
|
||||
p2 = priority.Priority()
|
||||
}
|
||||
|
||||
if p1 < p2 {
|
||||
return 1
|
||||
}
|
||||
return -1
|
||||
}
|
79
task.go
Normal file
79
task.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
package hunter
|
||||
|
||||
import (
|
||||
pqueue "github.com/474420502/focus/priority_queue"
|
||||
)
|
||||
|
||||
// IBefore 执行任务前处理
|
||||
type IBefore interface {
|
||||
Before()
|
||||
}
|
||||
|
||||
// // IExecute 执行任务
|
||||
// type IExecute interface {
|
||||
// Execute(ctx *TaskContext)
|
||||
// }
|
||||
|
||||
// IAfter 执行任务后
|
||||
type IAfter interface {
|
||||
After()
|
||||
}
|
||||
|
||||
// ITask 任务接口
|
||||
type ITask interface {
|
||||
Execute(ctx *TaskContext)
|
||||
}
|
||||
|
||||
// ITaskNode 任务节点
|
||||
type ITaskNode interface {
|
||||
Parent() ITaskNode
|
||||
SetParent(task ITaskNode)
|
||||
|
||||
Children() *pqueue.PriorityQueue // ITaskNode
|
||||
SetChildren(children *pqueue.PriorityQueue)
|
||||
|
||||
Task() ITask // ITaskNode
|
||||
SetTask(itask ITask)
|
||||
}
|
||||
|
||||
// BaseTask 任务,必须包含子任务. 执行了第一个
|
||||
type BaseTask struct {
|
||||
parent ITaskNode
|
||||
children *pqueue.PriorityQueue // ITask类型
|
||||
task ITask
|
||||
}
|
||||
|
||||
// Parent 父
|
||||
func (task *BaseTask) Parent() ITaskNode {
|
||||
return task.parent
|
||||
}
|
||||
|
||||
// SetParent 设父
|
||||
func (task *BaseTask) SetParent(itask ITaskNode) {
|
||||
task.parent = itask
|
||||
}
|
||||
|
||||
// Children 孩子节点
|
||||
func (task *BaseTask) Children() *pqueue.PriorityQueue {
|
||||
return task.children
|
||||
}
|
||||
|
||||
// SetChildren 孩子节点
|
||||
func (task *BaseTask) SetChildren(children *pqueue.PriorityQueue) {
|
||||
task.children = children
|
||||
}
|
||||
|
||||
// Task 孩子节点
|
||||
func (task *BaseTask) Task() ITask {
|
||||
return task.task
|
||||
}
|
||||
|
||||
// SetTask 孩子节点
|
||||
func (task *BaseTask) SetTask(itask ITask) {
|
||||
task.task = itask
|
||||
}
|
||||
|
||||
// // Task 孩子节点
|
||||
// func (task *BaseTask) Task() ITask {
|
||||
// return *task
|
||||
// }
|
Loading…
Reference in New Issue
Block a user