Skip to content

Commit

Permalink
Merge pull request #240 from dlyongemallo/check_rule_modal_error
Browse files Browse the repository at this point in the history
Move UI code for showing errors out of `custom_rule.py`.
  • Loading branch information
jvdwetering authored May 6, 2024
2 parents 6a872e4 + f984789 commit aae8b09
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
30 changes: 9 additions & 21 deletions zxlive/custom_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,38 +315,26 @@ def get_vertex_positions(graph: GraphT, rhs_graph: nx.Graph, boundary_vertex_map
ret: dict[NodeView, tuple[float, float]] = nx.spring_layout(rhs_graph, k=k, pos=pos_dict, fixed=boundary_vertex_map.keys())
return ret

def check_rule(rule: CustomRule, show_error: bool = True) -> bool:

def check_rule(rule: CustomRule) -> None:
rule.lhs_graph.auto_detect_io()
rule.rhs_graph.auto_detect_io()
if len(rule.lhs_graph.inputs()) != len(rule.rhs_graph.inputs()) or \
len(rule.lhs_graph.outputs()) != len(rule.rhs_graph.outputs()):
if show_error:
from .dialogs import show_error_msg
show_error_msg("Warning!", "The left-hand side and right-hand side of the rule have different numbers of inputs or outputs.")
return False
raise ValueError("The left-hand side and right-hand side of the rule have different numbers of inputs or outputs.")
if not rule.lhs_graph.variable_types and not rule.rhs_graph.variable_types:
left_matrix, right_matrix = rule.lhs_graph.to_matrix(), rule.rhs_graph.to_matrix()
if not np.allclose(left_matrix, right_matrix):
if show_error:
from .dialogs import show_error_msg
if np.allclose(left_matrix / np.linalg.norm(left_matrix), right_matrix / np.linalg.norm(right_matrix)):
show_error_msg("Warning!", "The left-hand side and right-hand side of the rule differ by a scalar.")
else:
show_error_msg("Warning!", "The left-hand side and right-hand side of the rule have different semantics.")
return False
if np.allclose(left_matrix / np.linalg.norm(left_matrix), right_matrix / np.linalg.norm(right_matrix)):
raise ValueError("The left-hand side and right-hand side of the rule differ by a scalar.")
else:
raise ValueError("The left-hand side and right-hand side of the rule have different semantics.")
else:
if not (rule.rhs_graph.variable_types.items() <= rule.lhs_graph.variable_types.items()):
if show_error:
from .dialogs import show_error_msg
show_error_msg("Warning!", "The right-hand side has more free variables than the left-hand side.")
return False
raise ValueError("The right-hand side has more free variables than the left-hand side.")
for vertex in rule.lhs_graph.vertices():
if isinstance(rule.lhs_graph.phase(vertex), Poly):
try:
get_linear(rule.lhs_graph.phase(vertex))
except ValueError as e:
if show_error:
from .dialogs import show_error_msg
show_error_msg("Warning!", str(e))
return False
return True
raise ValueError(f"Error in left-hand side phase: {str(e)}")
7 changes: 6 additions & 1 deletion zxlive/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class ImportRuleOutput:
file_path: str
r: CustomRule


def show_error_msg(title: str, description: Optional[str] = None, parent: Optional[QWidget] = None) -> None:
"""Displays an error message box."""
msg = QMessageBox(parent) #Set the parent of the QMessageBox
Expand Down Expand Up @@ -290,12 +291,16 @@ def get_file(self: MainWindow, button: QPushButton, side: str) -> None:
rewrite_form.addRow(right_button)
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
rewrite_form.addRow(button_box)

def add_rewrite() -> None:
nonlocal left_graph, right_graph
if left_graph is None or right_graph is None or name.text() == "" or description.toPlainText() == "":
return
rule = CustomRule(left_graph, right_graph, name.text(), description.toPlainText())
check_rule(rule, show_error=True)
try:
check_rule(rule)
except Exception as e:
show_error_msg("Warning!", str(e), parent=parent)
if save_rule_dialog(rule, parent):
dialog.accept()
button_box.accepted.connect(add_rewrite)
Expand Down
10 changes: 8 additions & 2 deletions zxlive/mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ def handle_save_file_action(self) -> bool:
if isinstance(self.active_panel, ProofPanel):
data = self.active_panel.proof_model.to_json()
elif isinstance(self.active_panel, RulePanel):
check_rule(self.active_panel.get_rule(), show_error=True)
try:
check_rule(self.active_panel.get_rule())
except Exception as e:
show_error_msg("Warning!", str(e), parent=self)
data = self.active_panel.get_rule().to_json()
elif self.active_panel.file_type in (FileFormat.QGraph, FileFormat.Json):
data = self.active_panel.graph.to_json()
Expand All @@ -367,7 +370,10 @@ def handle_save_as_action(self) -> bool:
if isinstance(self.active_panel, ProofPanel):
out = save_proof_dialog(self.active_panel.proof_model, self)
elif isinstance(self.active_panel, RulePanel):
check_rule(self.active_panel.get_rule(), show_error=True)
try:
check_rule(self.active_panel.get_rule())
except Exception as e:
show_error_msg("Warning!", str(e), parent=self)
out = save_rule_dialog(self.active_panel.get_rule(), self)
else:
out = save_diagram_dialog(self.active_panel.graph_scene.g, self)
Expand Down

0 comments on commit aae8b09

Please sign in to comment.