table.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //dynamicaly loading from outer space
  2. package rules2
  3. import (
  4. "fw/cp/module"
  5. "fw/rt2"
  6. "fw/rt2/context"
  7. "fw/rt2/decision"
  8. "fw/rt2/frame"
  9. "fw/rt2/frame/std"
  10. _ "fw/rt2/rules2/wrap"
  11. "fw/utils"
  12. "log"
  13. "time"
  14. "ypk/assert"
  15. )
  16. type flow struct {
  17. root frame.Stack
  18. parent frame.Frame
  19. domain context.Domain
  20. fl []frame.Frame
  21. cl []frame.Frame
  22. this int
  23. }
  24. func (f *flow) Do() (ret frame.WAIT) {
  25. const Z frame.WAIT = -1
  26. x := Z
  27. if f.this >= 0 {
  28. x = f.fl[f.this].Do()
  29. }
  30. switch x {
  31. case frame.NOW, frame.WRONG, frame.LATER, frame.BEGIN:
  32. ret = x
  33. case frame.END:
  34. old := f.Root().(*std.RootFrame).Drop()
  35. assert.For(old != nil, 40)
  36. f.cl = append(f.cl, old)
  37. ret = frame.LATER
  38. case frame.STOP, Z:
  39. f.this--
  40. if f.this >= 0 {
  41. ret = frame.LATER
  42. } else {
  43. if len(f.cl) > 0 {
  44. for _, old := range f.cl {
  45. n := rt2.NodeOf(old)
  46. rt2.Push(rt2.New(n), old.Parent())
  47. }
  48. f.cl = nil
  49. ret = frame.LATER
  50. } else {
  51. ret = frame.STOP
  52. }
  53. }
  54. }
  55. utils.PrintFrame(">", ret)
  56. return ret
  57. }
  58. func (f *flow) OnPush(root frame.Stack, parent frame.Frame) {
  59. f.root = root
  60. f.parent = parent
  61. //fmt.Println("flow control pushed")
  62. f.this = len(f.fl) - 1
  63. }
  64. func (f *flow) OnPop() {
  65. //fmt.Println("flow control poped")
  66. }
  67. func (f *flow) Parent() frame.Frame { return f.parent }
  68. func (f *flow) Root() frame.Stack { return f.root }
  69. func (f *flow) Domain() context.Domain { return f.domain }
  70. func (f *flow) Init(d context.Domain) {
  71. assert.For(f.domain == nil, 20)
  72. assert.For(d != nil, 21)
  73. f.domain = d
  74. }
  75. func (f *flow) Handle(msg interface{}) {
  76. assert.For(msg != nil, 20)
  77. }
  78. func (f *flow) grow(global context.Domain, m *module.Module) {
  79. utils.PrintScope("queue", m.Name)
  80. nf := rt2.New(m.Enter)
  81. f.root.PushFor(nf, nil)
  82. f.fl = append(f.fl, nf)
  83. global.Attach(m.Name, nf.Domain())
  84. }
  85. func run(global context.Domain, init []*module.Module) {
  86. {
  87. fl := &flow{root: std.NewRoot()}
  88. global.Attach(context.STACK, fl.root.(context.ContextAware))
  89. global.Attach(context.MT, fl)
  90. for i := len(init) - 1; i >= 0; i-- {
  91. fl.grow(global, init[i])
  92. }
  93. fl.root.PushFor(fl, nil)
  94. i := 0
  95. t0 := time.Now()
  96. for x := frame.NOW; x == frame.NOW; x = fl.root.(frame.Frame).Do() {
  97. utils.PrintFrame("STEP", i)
  98. //assert.For(i < 1000, 40)
  99. i++
  100. }
  101. t1 := time.Now()
  102. log.Println("total steps", i)
  103. log.Println("spent", t1.Sub(t0))
  104. }
  105. }
  106. func init() {
  107. decision.Run = run
  108. }