File: visualize.py

package info (click to toggle)
gringo 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,260 kB
  • ctags: 10,755
  • sloc: cpp: 55,049; python: 629; yacc: 569; sh: 124; makefile: 23
file content (163 lines) | stat: -rwxr-xr-x 5,067 bytes parent folder | download
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/python

import urwid
import sys
import gringo

class Board:

    def __init__(self, plan):
        self.display = urwid.Text("", align='center')
        self.plan    = plan
        self.current = plan.first()
        self.update()

    def next(self, button):
        self.current = self.plan.next(self.current)
        self.update()

    def prev(self, button):
        self.current = self.plan.prev(self.current)
        self.update()

    def update(self):
        brd = [[" "] * (self.plan.width * 2 + 1) for _ in range(0, (self.plan.height * 2 + 1))]

        for (x, y) in self.plan.field:
            brd[2 * y + 1][2 * x + 0] = "|"
            brd[2 * y + 1][2 * x + 2] = "|"

        for (x, y) in self.plan.field:
            brd[2 * y + 0][2 * x + 0] = "-"
            brd[2 * y + 0][2 * x + 1] = "-"
            brd[2 * y + 0][2 * x + 2] = "-"
            brd[2 * y + 2][2 * x + 0] = "-"
            brd[2 * y + 2][2 * x + 1] = "-"
            brd[2 * y + 2][2 * x + 2] = "-"

        for (x, y) in self.plan.steps(self.current):
            brd[2 * y + 1][2 * x + 1] = "o"

        j = self.plan.jumped(self.current)
        if j != None:
            (x, y) = j
            brd[2 * y  + 1][2 * x  + 1] = ("blue", "o")


        j = self.plan.jump(self.current)
        if j != None:
            (x, y, xx, yy) = j
            brd[2 * y  + 1][2 * x  + 1] = ("red",   "o")
            brd[2 * yy + 1][2 * xx + 1] = ("green", " ")

        markup = []
        for row in brd:
            markup.extend(row)
            markup.append("\n")

        self.display.set_text(markup)

class MainWindow:
    def __init__(self):
           self.loop = None

    def exit(self, button):
        raise urwid.ExitMainLoop()

    def run(self, plan):

        c = Board(plan)

        ba = urwid.Button("previous")
        bb = urwid.Button("quit")
        bc = urwid.Button("next")

        urwid.connect_signal(bb, 'click', self.exit)
        urwid.connect_signal(bc, 'click', c.next)
        urwid.connect_signal(ba, 'click', c.prev)

        sf = urwid.Text("")

        b = urwid.Columns([sf, ('fixed', len(bc.label) + 4, bc), ('fixed', len(ba.label) + 4, ba), ('fixed', len(bb.label) + 4, bb), sf], 1)
        f = urwid.Frame(urwid.Filler(c.display), None, b, 'footer')

        palette = [
            ('red', 'black', 'light red'),
            ('green', 'black', 'light green'),
            ('blue', 'black', 'light blue'), ]

        self.loop = urwid.MainLoop(f, palette)
        self.loop.run()

class Plan:
    def __init__(self, field, init, jumps):
        mx          = min([x for (x, y) in field])
        my          = min([y for (x, y) in field])
        self.width  = max([x for (x, y) in field]) - mx + 1
        self.height = max([y for (x, y) in field]) - my + 1
        self.field  = [ (x - mx, y - my) for (x, y) in field ]

        pjumps = {}
        for (t, k) in jumps.items(): pjumps[t] = [ (x - mx, y - my, xx - mx, yy - my) for (x, y, xx, yy) in k ]
        self._jumps = []
        self._steps = []
        self._steps.append([ (x - mx, y - my) for (x, y) in init ])

        for t in sorted(pjumps.keys()):
            for (x, y, xx, yy) in pjumps[t]:
                self._jumps.append((x, y, xx, yy))
                self._steps.append(self._steps[-1][:])
                self._steps[-1].append((xx, yy))
                self._steps[-1].remove((x, y))
                self._steps[-1].remove(((x + xx) // 2, (y + yy) // 2))

    def jump(self, i):
        return self._jumps[i] if i < len(self._jumps) else None

    def jumped(self, i):
        return None if i <= 0 else self.jump(i - 1)[2:]

    def steps(self, i):
        return self._steps[i]

    def next(self, i):
        return i + 1 if i + 1 < len(self._steps) else i

    def prev(self, i):
        return i - 1 if i - 1 >= 0 else i

    def first(self):
        return 0

c = gringo.Control()
for f in sys.argv[1:]: c.load(f)
def make_on_model(field, init, jumps):
    sx = { "east": 2, "west": -2, "north":  0, "south": 0 }
    sy = { "east": 0, "west":  0, "north": -2, "south": 2 }
    def on_model(m):
        for atom in m.atoms(gringo.Model.ATOMS):
            if atom.name() == "field" and len(atom.args()) == 2:
                x, y = atom.args()
                field.append((x, y))
            elif atom.name() == "stone" and len(atom.args()) == 2:
                x, y = atom.args()
                init.append((x, y))
            elif atom.name() == "jump" and len(atom.args()) == 4:
                ox, oy, d, t = atom.args()
                jumps.setdefault(t, []).append((ox, oy, ox + sx[str(d)], oy + sy[str(d)]))
        return False
    return on_model

t, field, init, jumps = 0, [], [], {}
on_model = make_on_model(field, init, jumps)
c.ground([("base", [])])
while True:
    t += 1
    c.ground([("cumulative", [t])])
    c.release_external(gringo.Fun("query", [t-1]))
    c.assign_external(gringo.Fun("query", [t]), True)
    if c.solve(None, on_model) == gringo.SolveResult.SAT:
        break

MainWindow().run(Plan(field, init, jumps))