Posted to tcl by hypnotoad at Thu May 11 11:12:59 GMT 2017view raw

  1. oo::class create layout.compartment.5compt { method compartment_do trial {} }
  2. oo::class create layout.generator.one { method generator_do trial {} }
  3. oo::class create layout.generator.two { method generator_do trial {} }
  4. oo::class create layout.generator.three { method generator_do trial {} }
  5. oo::class create layout.switchboard.one { method switchboard_do trial {} }
  6. oo::class create layout.switchboard.two { method switchboard_do trial {} }
  7. oo::class create layout.switchboard.three { method switchboard_do trial {} }
  8.  
  9. oo::class create builder {
  10.  
  11. method build {trial combo} {
  12. set classes {}
  13. foreach {f v} $combo {
  14. lappend classes layout.$f.$v
  15. }
  16. oo::objdefine [self] mixin {*}$classes
  17. foreach f [dict keys $combo] {
  18. my ${f}_do $trial
  19. }
  20. }
  21. }
  22.  
  23. proc permutation {combinations body {valuevar values} {values {}} {level 0}} {
  24. incr level
  25. set final 1
  26. foreach {field valuelist} $combinations {
  27. if {[dict exists $values $field]} continue
  28. set final 0
  29. if {[llength $valuelist]==0} {
  30. dict set values $field {}
  31. permutation $combinations $body $valuevar $values $level
  32. } else {
  33. foreach value $valuelist {
  34. dict set values $field $value
  35. permutation $combinations $body $valuevar $values $level
  36. }
  37. }
  38. return
  39. }
  40. uplevel $level [list set $valuevar $values]
  41. uplevel $level [list dict with $valuevar {}]
  42. uplevel $level $body
  43. }
  44.  
  45. set trial 0
  46. builder create ITERATOR
  47.  
  48. permutation {
  49. compartment {5compt}
  50. generator {one two three}
  51. switchboard {one two three}
  52. } {
  53. ITERATOR build [incr trial] $combo
  54. puts "Trial $trial $combo"
  55. } combo