Browse Source

forbid deleting mandatory types and attributes in instance models

Lucas Heer 7 years ago
parent
commit
537f44a1f8
5 changed files with 31 additions and 23 deletions
  1. 11 0
      commons.py
  2. 5 2
      sketchUI/im_mainwindow.py
  3. 14 10
      sketchUI/im_scene.py
  4. 0 10
      sketchUI/mvops.py
  5. 1 1
      wrappers/modelverse_SCCD.py

+ 11 - 0
commons.py

@@ -283,6 +283,17 @@ def is_attribute_mandatory(node_type, attr_key):
                 return False
                 return False
     return True
     return True
 
 
+def is_attribute_valid(node_type, attr_key):
+    """
+    Check if attributing with key is allowed for a node of type node_type.
+    Goes through all example models and checks if they have such an attributes.
+    """
+    for exm in all_example_models():
+        all_attrs = get_all_attributes_of_type(exm, node_type)
+        if attr_key in [attr.key for attr in all_attrs]:
+            return True
+    return False
+
 def get_mandtory_edges():
 def get_mandtory_edges():
     """
     """
     Returns a list of mandatory edges from all example models.
     Returns a list of mandatory edges from all example models.

+ 5 - 2
sketchUI/im_mainwindow.py

@@ -231,8 +231,7 @@ class IMMainWindow(QMainWindow, Ui_MainWindow):
 
 
     def _on_attribute_edited(self, item):
     def _on_attribute_edited(self, item):
         # type: (QTableWidgetItem) -> None
         # type: (QTableWidgetItem) -> None
-        """ An attribute was edited, change it in the model but do not check (too expensive so
-        checking is done by verify method on demand).
+        """ An attribute was edited, change it in the model if supported.
         If the new entered value is empty, delete the attribute.
         If the new entered value is empty, delete the attribute.
         """
         """
         row = self.tableWidget.row(item)
         row = self.tableWidget.row(item)
@@ -241,6 +240,10 @@ class IMMainWindow(QMainWindow, Ui_MainWindow):
         node = self._scene.selectedItems()[0]
         node = self._scene.selectedItems()[0]
 
 
         if not attr_val:
         if not attr_val:
+            if commons.is_attribute_mandatory(node.get_type(), attr_key):
+                self.plainTextEdit.appendPlainText("Error: attribute {} mandatory".format(attr_key))
+                return
+
             self.plainTextEdit.appendPlainText("Deleting attribute {}".format(attr_key))
             self.plainTextEdit.appendPlainText("Deleting attribute {}".format(attr_key))
             mvops.delete_attribute_from_node(self._cur_model, node.node_id, attr_key)
             mvops.delete_attribute_from_node(self._cur_model, node.node_id, attr_key)
             self.tableWidget.removeRow(row)
             self.tableWidget.removeRow(row)

+ 14 - 10
sketchUI/im_scene.py

@@ -128,12 +128,16 @@ class CustomScene(QGraphicsScene):
         item = selected[0]
         item = selected[0]
 
 
         if isinstance(item, GraphicsNodeItem):
         if isinstance(item, GraphicsNodeItem):
+            # operation valid?
+            if commons.is_type_mandatory(item.get_type()) and commons.count_occurences(item.get_type(), self._cur_model) == 1:
+                self._parent.plainTextEdit.appendPlainText("Error: Type {} mandatory".format(item.get_type()))
+                return
             # delete node in model (also deletes edges connected to it in model)
             # delete node in model (also deletes edges connected to it in model)
             self._parent.plainTextEdit.appendPlainText("Deleting node of type {}".format(item.get_type()))
             self._parent.plainTextEdit.appendPlainText("Deleting node of type {}".format(item.get_type()))
             mvops.delete_node(self._cur_model, item.node_id)
             mvops.delete_node(self._cur_model, item.node_id)
 
 
             # in view, delete edges that were connected to this node as well
             # in view, delete edges that were connected to this node as well
-            # modelverse does this on its own so do not delete edges explicitly here
+            # modelverse does this on its own so do not delete edges explicitly in model
             for edge in self.items():
             for edge in self.items():
                 if not isinstance(edge, GraphicsEdgeItem):
                 if not isinstance(edge, GraphicsEdgeItem):
                     continue
                     continue
@@ -155,21 +159,21 @@ class CustomScene(QGraphicsScene):
         if not isinstance(item, GraphicsNodeItem):
         if not isinstance(item, GraphicsNodeItem):
             return
             return
 
 
-        # quickly check if attributing of such a node is allowed
+        # ask user for key value
+        key, ok = QInputDialog.getText(self._parent, "New attribute", "Key value")
+        if not ok or not key:
+            return
+
+        # is operation supported?
         QApplication.setOverrideCursor(Qt.WaitCursor)
         QApplication.setOverrideCursor(Qt.WaitCursor)
-        self._parent.plainTextEdit.appendPlainText("Checking if attributing nodes of type {} is allowed ...".format(item.get_type()))
-        if not mvops.is_attributing_allowed(item.get_type()):
-            self._parent.plainTextEdit.appendPlainText("Error: Not allowed".format(item.get_type()))
+        self._parent.plainTextEdit.appendPlainText("Checking if attributing node is allowed ...".format(item.get_type()))
+        if not commons.is_attribute_valid(item.get_type(), key):
+            self._parent.plainTextEdit.appendPlainText("Error: Attribute {} not valid for type {}".format(key, item.get_type()))
             QApplication.restoreOverrideCursor()
             QApplication.restoreOverrideCursor()
             return
             return
         QApplication.restoreOverrideCursor()
         QApplication.restoreOverrideCursor()
         self._parent.plainTextEdit.appendPlainText("Yes")
         self._parent.plainTextEdit.appendPlainText("Yes")
 
 
-        # ask user for key value
-        key, ok = QInputDialog.getText(self._parent, "New attribute", "Key value")
-        if not ok or not key:
-            return
-
         # check if key value already used for this node
         # check if key value already used for this node
         attrs = commons.get_attributes_of_node(self._cur_model, item.node_id)
         attrs = commons.get_attributes_of_node(self._cur_model, item.node_id)
         for attr in attrs:
         for attr in attrs:

+ 0 - 10
sketchUI/mvops.py

@@ -103,16 +103,6 @@ def add_attribute(model, node_id, key, val):
     mv.attr_assign(model, attr_id, "value", val)
     mv.attr_assign(model, attr_id, "value", val)
     mv.instantiate(model, "NodeAttribute", (node_id, attr_id))
     mv.instantiate(model, "NodeAttribute", (node_id, attr_id))
 
 
-def is_attributing_allowed(node_type):
-    """
-    Check if attributing is allowed for a node of type node_type.
-    Goes through all example models and checks if they have attributes.
-    """
-    for exm in commons.all_example_models():
-        if commons.get_all_attributes_of_type(exm, node_type):
-            return True
-    return False
-
 def update_attribute_val(model, node_id, key, new_val):
 def update_attribute_val(model, node_id, key, new_val):
     """
     """
     Update the attribute identified by its key of node node_id in model to new_val
     Update the attribute identified by its key of node node_id in model to new_val

+ 1 - 1
wrappers/modelverse_SCCD.py

@@ -1,7 +1,7 @@
 """
 """
 Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
 Generated by Statechart compiler by Glenn De Jonghe, Joeri Exelmans, Simon Van Mierlo, and Yentl Van Tendeloo (for the inspiration)
 
 
-Date:   Sun May  6 19:20:07 2018
+Date:   Tue May  8 12:16:40 2018
 
 
 Model author: Yentl Van Tendeloo
 Model author: Yentl Van Tendeloo
 Model name:   MvK Server
 Model name:   MvK Server