-
Notifications
You must be signed in to change notification settings - Fork 2
/
branching_thing.py
119 lines (103 loc) · 2.95 KB
/
branching_thing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
try:
import common_base as base
except ImportError:
try:
import base
except ImportError:
import common.lib.base as base
try:
import common_util as util
except ImportError:
try:
import util
except ImportError:
import common.lib.util as util
import random
class Navigator(base.Extension):
def __init__(self, comp):
super().__init__(comp)
self._timer = comp.op('./timer')
self._options = comp.op('./options')
self._connections = comp.op('./connections')
self._indextrail = comp.op('./set_index_trail')
def Start(self):
self._timer.par.start.pulse()
self.Reset(hard=True)
def ChooseNext(self):
currentindex = int(self.comp.par.Current)
nextindex = int(self.comp.par.Next)
restartafterleaf = self.comp.par.Resetafterleaf
choices = [int(i) for i in self._options.col(0)] if self._options.numRows > 0 else []
if restartafterleaf:
choices = [i for i in choices if i > nextindex]
if not choices:
self.Reset(hard=False)
else:
newindex = random.choice(choices)
self.comp.par.Step += 1
self.comp.par.Current = nextindex
self.comp.par.Next = newindex
self._indextrail.appendRow([newindex])
def Reset(self, hard=False):
newindex = 0
self.comp.par.Step = 0
self.comp.par.Current = 0 if hard else self.comp.par.Next
self.comp.par.Next = 0
self._indextrail.clear()
self._indextrail.appendRow(['index'])
self._indextrail.appendRow([0])
class PathNavigator(base.Extension):
def __init__(self, comp):
super().__init__(comp)
self._timer = comp.op('./timer')
self._paths = comp.op('./paths')
self._currentpaths = comp.op('./current_path')
def Start(self):
self._timer.par.start.pulse()
self.Reset(hard=True)
@property
def _CurrentPathSteps(self):
if self._currentpaths.numRows == 0:
return []
return [int(c) for c in self._currentpaths.col(0)]
@property
def _NeedsNewPath(self):
currstep = self.comp.par.Step.eval()
if currstep == -1:
return True
steps = self._CurrentPathSteps
return not steps or currstep > len(steps)
def ChooseNext(self):
if self._NeedsNewPath:
self._ChooseNewPath()
else:
self.comp.par.Step += 1
# ....????
def _ChooseNewPath(self):
pass
def Reset(self, hard=False):
pass
class PathBuilder(base.Extension):
def __init__(self, comp):
super().__init__(comp)
self._connections = comp.op('./connections')
def _PathsFrom(self, fromindex):
# self._LogEvent('_PathsFrom(%d)' % fromindex)
nextpts = self._GetNextPoints(fromindex)
# self._LogEvent('_PathsFrom() -- next points: %r' % nextpts)
paths = []
for nextpt in nextpts:
subpaths = self._PathsFrom(nextpt)
if not subpaths:
paths.append([nextpt])
else:
for subpath in subpaths:
paths.append([nextpt] + subpath)
return paths
def _GetNextPoints(self, fromindex):
return [int(c) for c in self._connections.cells(str(fromindex), '*') if c.col == 1]
def BuildPaths(self, outdat):
outdat.clear()
paths = list(self._PathsFrom(0))
for path in paths:
outdat.appendCol([0] + path)