-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathframe_08_stabilityAnalysis.py
224 lines (175 loc) · 8.54 KB
/
frame_08_stabilityAnalysis.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright: (c) Oskar Petersons 2013
"""Frame used to for Stability Analysis.
Loaded by the a_Main_Window module, and implements all of its required
interfaces.
"""
from tkinter import Tk, N, S, E, W, PanedWindow, HORIZONTAL, VERTICAL
from tkinter import ttk
from frame_00_frameTemplate import FrameTemplate
from data_02_conflictSolvers import GoalSeeker
from widgets_f06_01_logResultDisp import CoalitionSelector
from widgets_f04_03_optionForm import OptionFormTable
from widgets_f08_01_stabilityAnalysis import (StatusQuoAndGoals,
ReachableTreeViewer,
PatternNarrator)
NSEW = (N, S, E, W)
class StabilityFrame(FrameTemplate):
"""Frame used to for Stability Analysis."""
# Label used for button to select frame in the main program.
buttonLabel = 'Post Analysis'
# Image used on button to select frame, when frame is active.
activeIcon = 'icons/Post_Analysis_ON.gif'
# Image used on button to select frame, when frame is inactive.
inactiveIcon = 'icons/Post_Analysis_OFF.gif'
# Help text to be displayed when screen is active.
helpText = ("Selecting coalitions, status quo and goals at the top left"
"will generate a reachability tree below the status quo, and "
"detail the patterns that preferences must match for the goal "
"state to be reached.")
# ######################## INITIALIZATION ################################
def __init__(self, master, conflict, *args):
"""Initialize the Frame. Does not build widgets."""
FrameTemplate.__init__(self, master, conflict, self.buttonLabel,
self.activeIcon, self.inactiveIcon,
self.helpText)
self.lastBuildConflict = None
# ############################ METHODS ###################################
def hasRequiredData(self):
"""Check that minimum data required to render the frame exists."""
if len(self.conflict.decisionMakers) < 1:
return False
if len(self.conflict.options) < 1:
return False
if len(self.conflict.feasibles) < 1:
return False
if self.conflict.preferenceErrors:
return False
else:
return True
def dataChanged(self):
"""Check if data has changed since the last build of the Frame."""
if self.lastBuildConflict != self.conflict.export_rep():
return True
else:
return False
def buildFrame(self):
"""Contruct frame widgets and initialize data."""
if self.built:
return
# Ensure all required parts of the conflict model are properly set-up.
self.conflict.reorderOptionsByDM()
self.conflict.options.set_indexes()
self.conflict.infeasibles.validate()
self.conflict.recalculateFeasibleStates()
for dm in self.conflict.decisionMakers:
dm.calculatePerceived()
dm.calculatePreferences()
self.lastBuildConflict = self.conflict.export_rep()
# Define frame-specific variables
self.sol = GoalSeeker(self.conflict)
# infoFrame: frame and label definitions (with master 'self.infoFrame')
self.infoLabel = ttk.Label(self.infoFrame, text="")
# helpFrame: frame and label definitions (with master 'self.helpFrame')
self.helpLabel = ttk.Label(self.helpFrame, textvariable=self.helpVar,
wraplength=150)
# Define frame-specific input widgets (with 'self' as master)
self.paneMaster = PanedWindow(self, orient=HORIZONTAL, sashwidth=10,
sashrelief="raised", sashpad=3,
relief="sunken")
self.paneLeft = PanedWindow(self.paneMaster, orient=VERTICAL,
sashwidth=10, sashrelief="raised",
sashpad=3, relief="sunken")
self.paneLeftTop = ttk.Frame(self.paneLeft)
self.coalitionSelector = CoalitionSelector(self.paneLeftTop,
self.conflict, self)
self.statusQuoAndGoals = StatusQuoAndGoals(self.paneLeftTop,
self.conflict)
self.reachableTree = ReachableTreeViewer(self.paneLeftTop,
self.conflict, self)
self.paneLeftBottom = ttk.Frame(self.paneLeft)
self.optionFormTable = OptionFormTable(self.paneLeftBottom,
self.conflict)
self.paneRight = ttk.Frame(self.paneMaster)
self.patternNarrator = PatternNarrator(self.paneRight, self.conflict,
self)
# ######## preliminary gridding and option configuration
# configuring the input frame
self.grid(column=0, row=0, rowspan=5, sticky=NSEW)
self.grid_remove()
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
# configuring infoFrame & infoFrame widgets
self.infoFrame.grid(column=2, row=0, sticky=NSEW, padx=3, pady=3)
self.infoFrame.grid_remove()
self.infoLabel.grid(column=0, row=1, sticky=NSEW)
# configuring helpFrame & helpFrame widgets
self.helpFrame.grid(column=2, row=1, sticky=NSEW, padx=3, pady=3)
self.helpFrame.grid_remove()
self.helpLabel.grid(column=0, row=0, sticky=NSEW)
# configuring frame-specific options
self.paneMaster.grid(row=0, column=0, sticky=NSEW)
self.paneMaster.add(self.paneLeft)
self.paneLeft.add(self.paneLeftTop)
self.paneLeftTop.columnconfigure(1, weight=1)
self.paneLeftTop.rowconfigure(2, weight=1)
self.coalitionSelector.grid(row=0, column=0, sticky=NSEW)
ttk.Separator(self, orient=HORIZONTAL).grid(row=1, column=0,
sticky=NSEW, pady=3)
self.statusQuoAndGoals.grid(row=2, column=0, sticky=NSEW)
self.reachableTree.grid(row=0, column=1, rowspan=3, sticky=NSEW)
self.paneLeft.add(self.paneLeftBottom)
self.paneLeftBottom.rowconfigure(0, weight=1)
self.paneLeftBottom.columnconfigure(0, weight=1)
self.optionFormTable.grid(row=0, column=0, sticky=NSEW)
self.paneMaster.add(self.paneRight)
self.paneRight.rowconfigure(0, weight=1)
self.paneRight.columnconfigure(0, weight=1)
self.patternNarrator.grid(row=0, column=0, sticky=NSEW)
# bindings
self.statusQuoAndGoals.bind("<<StatusQuoChanged>>",
self.refresh)
self.statusQuoAndGoals.bind("<<GoalChanged>>",
self.refresh)
self.coalitionSelector.bind("<<CoalitionsChanged>>",
self.refresh)
self.built = True
def refresh(self, *args):
"""Refresh data in all active display widgets."""
sq = self.statusQuoAndGoals.statusQuoSelector.current()
goals = self.statusQuoAndGoals.getGoals()
if len(goals) > 0:
self.sol = GoalSeeker(self.conflict, goals)
else:
self.sol = GoalSeeker(self.conflict)
self.reachableTree.buildTree(sq, watchFor=[x[0] for x in goals])
self.patternNarrator.updateNarration(
goalInfo=self.reachableTree.goalInfo())
# #############################################################################
# ############### TESTING ###########
# #############################################################################
# Code in this section is only run when this module is run by itself. It serves
# as a test of module functionality.
def main():
"""Run screen in test window."""
from data_01_conflictModel import ConflictModel
root = Tk()
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
cFrame = ttk.Frame(root)
cFrame.columnconfigure(0, weight=1)
cFrame.rowconfigure(1, weight=1)
cFrame.grid(column=0, row=0, sticky=NSEW)
hSep = ttk.Separator(cFrame, orient=VERTICAL)
hSep.grid(column=1, row=0, rowspan=10, sticky=NSEW)
conf = ConflictModel()
conf.load_from_file("save_files/Garrison.gmcr")
testFrame = StabilityFrame(cFrame, conf)
if testFrame.hasRequiredData():
testFrame.buildFrame()
else:
print("data missing")
return
testFrame.enter()
root.mainloop()
if __name__ == '__main__':
main()