-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
174 lines (146 loc) · 6.14 KB
/
main.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
import tkinter as tk
from hassediagram import *
import sys
def GUI_mode():
def get_parents():
try:
idim = int(varDimension.get())
hd = HasseDiagram(idim)
f = Function.fromString(idim, varFunction.get())
s1Parents, s2Parents, s3Parents = hd.get_f_parents(f)
lParents = ['R1 ' + str(f) for f in s1Parents]\
+ ['R2 ' + str(f) for f in s2Parents]\
+ ['R3 ' + str(f) for f in s3Parents]
output = '** Parents(f) ***\n' + '\n'.join(lParents)
except ValueError as e:
output = "ERROR! " + str(e)
outputText.delete(1.0, "end") # Clear previous output
outputText.insert(1.0, output)
def get_children():
try:
idim = int(varDimension.get())
hd = HasseDiagram(idim)
f = Function.fromString(idim, varFunction.get())
s1Children, s2Children, s3Children = hd.get_f_children(f)
lChildren = ['R1 ' + str(f) for f in s1Children]\
+ ['R2 ' + str(f) for f in s2Children]\
+ ['R3 ' + str(f) for f in s3Children]
output = '** Children(f) ***\n' + '\n'.join(lChildren)
except ValueError as e:
output = "ERROR! " + str(e)
outputText.delete(1.0, "end") # Clear previous output
outputText.insert(1.0, output)
def default_values():
varFunction.set('{{1,2,3},{1,3,4},{2,3,4}}')
varDimension.set(4)
outputText.delete(1.0, "end") # Clear previous output
outputText.insert(1.0, '')
def set_infimum():
try:
idim = int(varDimension.get())
hd = HasseDiagram(idim)
f = hd.get_infimum()
varFunction.set(str(f))
except ValueError as e:
output = "ERROR! " + str(e)
def set_supremum():
try:
idim = int(varDimension.get())
hd = HasseDiagram(idim)
f = hd.get_supremum()
varFunction.set(str(f))
except ValueError as e:
output = "ERROR! " + str(e)
def close_window():
"""
Close the main window (exits the interface).
"""
root.destroy()
# Create the main window
root = tk.Tk()
root.title("pyFunctionHood GUI")
root.grid_columnconfigure(0, minsize=1, weight=0)
root.grid_columnconfigure(1, minsize=1, weight=1)
root.grid_columnconfigure(2, minsize=1, weight=1)
row = 0
# Entry field for user function
labelDimension = tk.Label(root, text="Dimension")
labelDimension.grid(row=row, column=0, sticky='w')
labelFunction = tk.Label(root, text="Monotone Boolean Function")
labelFunction.grid(row=row, column=1, columnspan=2)
row += 1
# Spinbox with Dimension
varDimension = tk.IntVar(value=4) # default dimension
spinboxDimension = tk.Spinbox(root, from_=2, to=14, textvariable=varDimension, width=6)
spinboxDimension.grid(row=row, column=0, sticky='w')
varFunction = tk.StringVar(value='{{1,2,3},{1,3,4},{2,3,4}}')
entryFunction = tk.Entry(root, textvariable=varFunction, width=48)
entryFunction.grid(row=row, column=1, columnspan=2)
row += 1
bottomFrame = tk.Frame(root, padx=5, pady=5)
bottomFrame.grid(row=row, columnspan=2)
row = 0
# Buttons for Infimum/Supremum
buttonInfimum = tk.Button(bottomFrame, text="Set Infimum", command=set_infimum)
buttonInfimum.grid(row=row, column=0)
buttonSupremum = tk.Button(bottomFrame, text="Set Supremum", command=set_supremum)
buttonSupremum.grid(row=row, column=1)
row += 1
canvas = tk.Canvas(bottomFrame, height=2, bg="darkgray") # Adjust height and color
canvas.grid(row=row, columnspan=3, sticky="ew", pady=10)
row += 1
# Buttons to call parents/children methods
buttonParents = tk.Button(bottomFrame, text="Generate Parents", command=get_parents)
buttonParents.grid(row=row, column=0)
buttonChildren = tk.Button(bottomFrame, text="Generate Children", command=get_children)
buttonChildren.grid(row=row, column=1)
row += 1
# Text area with grid placement and set state to disabled
outputText = tk.Text(bottomFrame, width=60, height=10)#, state="disabled")
outputText.grid(row=row, columnspan=2) # Span across two columns
row += 1
# Default values
buttonDefault = tk.Button(bottomFrame, text="Default values", command=default_values)
buttonDefault.grid(row=row, column=0)
# Close button
buttonClose = tk.Button(bottomFrame, text="Close", command=close_window)
buttonClose.grid(row=row, column=1)
# Run the main loop
root.mainloop()
#--------------------------------------------------------------------
def usage():
print('\nFor graphical mode, run without arguments: python', sys.argv[0])
print('\nFor command line mode, use the following arguments:')
print('Usage example: python', sys.argv[0], '<type>', '<dimension>', '<function>')
print(' <type>: [p]arents or [c]hildren')
print(' <dimension>: 4')
print(' <function>: "{{1,2,3},{1,3,4},{2,3,4}}"')
sys.exit(1)
def command_line_mode():
if len(sys.argv) != 4:
print('ERROR: invalid number of arguments!')
usage()
if sys.argv[1] not in ('p','parents','c','children'):
print('ERROR: invalid <type>')
usage()
ndim = int(sys.argv[2])
hd = HasseDiagram(ndim)
f = Function.fromString(ndim, sys.argv[3])
if sys.argv[1] in ('p','parents'):
s1Parents, s2Parents, s3Parents = hd.get_f_parents(f)
lParents = ['R1 ' + str(f) for f in s1Parents]\
+ ['R2 ' + str(f) for f in s2Parents]\
+ ['R3 ' + str(f) for f in s3Parents]
print('\n'.join(lParents))
else:
s1Children, s2Children, s3Children = hd.get_f_children(f)
lChildren = ['R1 ' + str(f) for f in s1Children]\
+ ['R2 ' + str(f) for f in s2Children]\
+ ['R3 ' + str(f) for f in s3Children]
print('\n'.join(lChildren))
#--------------------------------------------------------------------
if __name__ == '__main__':
if len(sys.argv) == 1:
GUI_mode()
else:
command_line_mode()