forked from onp/gmcr-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe_04_preferencePrioritization.py
320 lines (259 loc) · 12.3 KB
/
frame_04_preferencePrioritization.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Copyright: (c) Oskar Petersons 2013
"""Frame used to set decision maker's preferences for use in prioritization.
Loaded by the a_Main_Window module, and implements all of its required
interfaces.
"""
from tkinter import (Tk, StringVar, N, S, E, W, VERTICAL, HORIZONTAL,
PanedWindow)
from tkinter import ttk
from frame_00_frameTemplate import FrameTemplate
from widgets_f04_01_prefRadioButton import RadiobuttonEntry
from widgets_f04_02_prefElements import (PreferenceRankingMaster,
PreferenceStaging,
PreferenceListDisplay)
from widgets_f04_03_optionForm import OptionFormTable
NSEW = (N, S, E, W)
class PreferencesFrame(FrameTemplate):
"""Used to set decision maker's preferences for use in prioritization."""
# Label used for button to select frame in the main program.
buttonLabel = 'Prioritization'
# Image used on button to select frame, when frame is active.
activeIcon = 'icons/Prioritization_ON.gif'
# Image used on button to select frame, when frame is inactive.
inactiveIcon = 'icons/Prioritization_OFF.gif'
# Help text to be displayed when screen is active.
helpText = ("Select a decision maker from the column at left by clicking "
"its 'Edit' button. Enter preferred conditions using the "
"inputs to the right. Preferred conditions for the selected "
"decision maker are shown at the far right, from most "
"important at the top, to least important at the bottom.")
# ######################## 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.lastBuildDMs = None
self.lastBuildOptions = None
self.lastBuildInfeasibles = None
self.lastBuildRanking = 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:
self.conflict.recalculateFeasibleStates()
if len(self.conflict.feasibles) < 1:
return False
else:
return True
def dataChanged(self):
"""Check if data has changed since the last build of the Frame."""
if self.lastBuildDMs != self.conflict.decisionMakers.export_rep():
return True
if self.lastBuildOptions != self.conflict.options.export_rep():
return True
if self.lastBuildInfeasibles != self.conflict.infeasibles.export_rep():
return True
if self.lastBuildRanking != self.conflict.useManualPreferenceRanking:
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.lastBuildDMs = self.conflict.decisionMakers.export_rep()
self.lastBuildOptions = self.conflict.options.export_rep()
self.lastBuildInfeasibles = self.conflict.infeasibles.export_rep()
self.lastBuildRanking = self.conflict.useManualPreferenceRanking
# Define variables that will display in the infoFrame
numD = len(self.conflict.decisionMakers)
self.infoText = StringVar(
value='Valid Preferences set for {0}/{0} DMs.'.format(numD))
# Define frame-specific variables
self.dm = None
# infoFrame: frame and label definitions (with master 'self.infoFrame')
self.infoLabel = ttk.Label(self.infoFrame, textvariable=self.infoText)
# 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=VERTICAL, sashwidth=5,
sashrelief="raised", sashpad=2,
relief="sunken")
self.paneTop = ttk.Frame(self.paneMaster)
self.rankings = PreferenceRankingMaster(self.paneTop, self.conflict)
self.editor = RadiobuttonEntry(self.paneTop, self.conflict)
self.paneTopRightMaster = PanedWindow(self.paneTop, orient=HORIZONTAL,
sashwidth=5, sashrelief="raised",
sashpad=2, relief="sunken")
self.staging = PreferenceStaging(self.paneTopRightMaster,
self.conflict)
self.preferenceDisp = PreferenceListDisplay(self.paneTopRightMaster,
self.conflict)
self.paneBottom = ttk.Frame(self.paneMaster)
self.optionTable = OptionFormTable(self.paneBottom, self.conflict)
self.usePrioritizationButton = ttk.Button(
self,
text=("Use preference prioritization. Any manually set "
"preference rankings will be lost."),
command=self.usePrioritization)
# ######## preliminary gridding and option configuration
# configuring the input frame
self.grid(column=0, row=0, rowspan=5, sticky=NSEW)
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.grid_remove()
# 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(column=0, row=1, sticky=NSEW)
self.paneMaster.add(self.paneTop, minsize=200)
self.rankings.grid(column=0, row=1, sticky=NSEW)
ttk.Separator(self.paneTop, orient=VERTICAL).grid(
column=1, row=1, sticky=NSEW, padx=3)
self.editor.grid(column=2, row=1, sticky=NSEW)
ttk.Separator(self.paneTop, orient=VERTICAL).grid(
column=3, row=1, sticky=NSEW, padx=3)
self.paneTopRightMaster.grid(column=4, row=1, sticky=NSEW)
self.paneTop.columnconfigure(0, weight=0)
self.paneTop.columnconfigure(2, weight=0)
self.paneTop.columnconfigure(4, weight=1)
self.paneTop.rowconfigure(1, weight=1)
self.usePrioritizationButton.grid(column=0, row=0, columnspan=5,
sticky=NSEW)
self.paneTopRightMaster.add(self.staging)
self.paneTopRightMaster.add(self.preferenceDisp)
self.paneMaster.add(self.paneBottom)
self.optionTable.grid(column=0, row=0, sticky=NSEW)
self.paneBottom.columnconfigure(0, weight=1)
self.paneBottom.rowconfigure(0, weight=1)
# bindings
self.rankings.bind('<<DMchg>>', self.dmChgHandler)
self.editor.bind('<<AddPref>>', self.addPref)
self.editor.bind('<<StagePref>>', self.stagePref)
self.staging.bind('<<SelCond>>', self.selCondChg)
self.staging.bind('<<PullFromStage>>', self.pullFromStage)
self.preferenceDisp.bind('<<SelPref>>', self.selPrefChg)
self.preferenceDisp.bind('<<ValueChange>>', self.refresh)
self.dmChgHandler()
self.built = True
def enter(self, *args):
"""Re-grid the screen into the master. Perform required updates."""
if self.dataChanged():
self.clearFrame()
FrameTemplate.enter(self)
def refresh(self, *args):
"""Refresh data in all active display widgets."""
for dm in self.conflict.decisionMakers:
dm.calculatePreferences()
self.editor.reloadOpts()
self.rankings.refresh()
self.preferenceDisp.refresh()
self.optionTable.buildTable(self.dm)
self.checkIfUsingRankings()
def checkIfUsingRankings(self, event=None):
"""Disable screen if Manual Ranking has been assigned."""
if self.conflict.useManualPreferenceRanking:
self.usePrioritizationButton.grid()
self.rankings.disable()
self.editor.disable()
self.staging.disable()
self.preferenceDisp.disable()
else:
self.usePrioritizationButton.grid_remove()
def usePrioritization(self):
"""Reactivate the screen if user decides to use prioritization."""
self.conflict.useManualPreferenceRanking = False
self.conflict.preferenceErrors = None
self.refresh()
def dmChgHandler(self, event=None):
"""Bound to <<DMchg>>."""
self.dm = self.rankings.dm
self.preferenceDisp.changeDM(self.dm)
self.optionTable.buildTable(self.dm)
self.staging.clear()
self.editor.setStates('clear')
if self.dm is None:
self.preferenceDisp.disable()
self.editor.disable()
self.staging.disable()
else:
self.preferenceDisp.enable()
self.editor.enable()
def addPref(self, event=None):
"""Add a preference for the active decision maker."""
pref = self.editor.getStates()
self.dm.preferences.append(pref)
self.refresh()
def stagePref(self, event=None):
"""Send a condition to the staging area."""
if self.editor.hasValidIf:
for cond in self.editor.ifCond:
self.staging.addCondition(cond)
else:
condData = self.editor.getStates()
newCond = self.conflict.newCondition(condData)
self.staging.addCondition(newCond)
self.editor.setStates('clear')
def pullFromStage(self, event=None):
"""Move a compound condition from Staging to Preferences."""
newPref = self.staging.conditionList
self.staging.clear()
self.dm.preferences.append(newPref)
self.refresh()
def selCondChg(self, event=None):
"""Triggered when a condition is selected in staging."""
condition = self.staging.conditionList[event.x]
self.editor.setStates(condition.ynd())
def selPrefChg(self, event=None):
"""Triggered when a preference is select from preferences."""
condition = self.dm.preferences[event.x]
self.staging.setList(condition)
# #############################################################################
# ############### 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 = PreferencesFrame(cFrame, conf)
if testFrame.hasRequiredData():
testFrame.buildFrame()
else:
print("data missing")
return
testFrame.enter()
root.mainloop()
if __name__ == '__main__':
main()