def test_write_default_path2(self, tmpdir):
     # case where the path has been set, but not called in self.write()
     node_types = NodeTypes.from_csv()
     node_types.path = tmpdir
     # test if writing does not return in an error
     node_types.write()
     read = NodeTypes.from_csv(tmpdir)
     assert node_types == read
 def test_write_default_path(self, tmpdir):
     # case where path is not set and not called
     node_types = NodeTypes.from_csv()
     def_path = Settings.default_node_types_folder()
     Settings.alter_default_node_types_folder(tmpdir)
     # test if writing does not return in an error
     node_types.write()
     read = NodeTypes.from_csv(tmpdir)
     Settings.alter_default_node_types_folder(def_path)
     assert node_types == read
 def test_get_node_type_by_node(self):
     node_types = NodeTypes.from_csv()
     # assert that the node_type test does not exist
     for category, types in node_types.node_types.items():
         for node_type in types:
             assert node_type[0] != "test"
     assert [] == node_types.get_node_type_by_node(Node("test", "a"))
     # check for a existing node
     assert [('composites', ["Sequence"])] == node_types.get_node_type_by_node(Node('Sequence', 'a'))
 def test_from_csv(self):
     node_types = NodeTypes.from_csv(Settings.default_node_types_folder())
     assert '.hiddenfile.csv' not in node_types.node_types.keys()
     assert 'filewithotherextension.abc' not in node_types.node_types.keys()
     assert 'composites' in node_types.node_types.keys()
     assert 'conditions' in node_types.node_types.keys()
     assert 'decorators' in node_types.node_types.keys()
     assert 'other' in node_types.node_types.keys()
     assert 'skills' in node_types.node_types.keys()
 def node_from_selected_type(self):
     """
     pyqtSlot for adding a node from the selected node types to the view
     """
     if not self.selected:
         return
     node_type = self.selected.data(1, Qt.UserRole)
     node = NodeTypes.create_node_from_node_type(node_type)
     self.add_node_to_view(node)
 def test_remove_category(self, tmpdir):
     node_types = NodeTypes.from_csv()
     # test removing existing category
     assert 'conditions' in node_types.node_types
     node_types.remove_category("conditions")
     assert 'conditions' not in node_types.node_types
     # test removing a non existing node_type category
     assert 'test' not in node_types.node_types
     node_types.remove_category("conditions")
     assert 'test' not in node_types.node_types
 def test_get_node_type_by_name(self):
     node_types = NodeTypes.from_csv()
     # assert that the node_type test does not exist
     for category, types in node_types.node_types.items():
         for node_type in types:
             assert node_type[0] != "test"
     assert [] == node_types.get_node_type_by_name('test')
     # check for a existing node
     assert [('composites', ["Sequence"])] == node_types.get_node_type_by_name('Sequence')
     assert [('conditions', ["HasBall"]), ("conditions", ["HasBall"])] == node_types.get_node_type_by_name("HasBall")
 def test_add_category(self, tmpdir):
     node_types = NodeTypes.from_csv()
     # add a new category that already exists
     assert 'conditions' in node_types.node_types.keys()
     conditions = node_types.node_types.get('conditions')
     node_types.add_category('conditions')
     assert 'conditions' in node_types.node_types
     assert conditions == node_types.node_types.get('conditions')
     # test creating a new category
     assert 'test' not in node_types.node_types
     node_types.add_category("test")
     assert 'test' in node_types.node_types
 def test_add_node_type(self, tmpdir):
     node_types = NodeTypes.from_csv()
     # check if a new category gets created if it does not exist
     node_types.add_node_type("test", "test_node")
     assert 'test'in node_types.node_types.keys()
     assert 'test_node' in node_types.node_types.get('test')[0]
     # check a node type with attributes
     node_types.add_node_type("test1", "test_node1", ["a"])
     assert 'test_node1' in node_types.node_types.get("test1")[0]
     assert 2 == len(node_types.node_types.get('test1')[0])
     # add one to an existing category
     node_types.add_node_type('test', 'test_node2')
     assert 2 == len(node_types.node_types.get('test'))
 def test_remove_node_type(self, tmpdir):
     node_types = NodeTypes.from_csv()
     node_types.add_node_type("test", "test")
     node_types.remove_node_type("test", ["test"])
     assert 0 == len(node_types.node_types.get('test'))
     # remove a nde from a category that does not exist
     node_types.remove_node_type("abcdefg", ["sequence"])
     assert "abcdefg" not in node_types.node_types.keys()
     # remove a node that does not exist
     node_types.add_category("abcdefg")
     assert 0 == len(node_types.node_types.get("abcdefg"))
     node_types.remove_node_type("abcdefg", ["sequence"])
     assert 0 == len(node_types.node_types.get("abcdefg"))
 def test_update_node_type(self, tmpdir):
     node_types = NodeTypes.from_csv()
     # update a node type where the category does not exist
     node_types.update_node_type("test", "test", ['test', "a"])
     assert 'test' not in node_types.node_types.keys()
     # update a node type that does not exist
     node_types.add_category('test')
     node_types.update_node_type("test", "test", ['test', "a"])
     assert 'test' in node_types.node_types.keys()
     assert 0 == len(node_types.node_types.get('test'))
     # update a node type that exists
     node_types.add_node_type('test', 'test')
     node_types.update_node_type("test", ["test"], ['test', "a"])
     assert 'test' in node_types.node_types.keys()
     assert 1 == len(node_types.node_types.get('test'))
     assert ["test", "a"] == node_types.node_types.get('test')[0]
 def dragEnterEvent(self, drag_drop_event):
     if self.drag_drop_node:
         return
     mime_data = drag_drop_event.mimeData()
     if mime_data.hasText() and self.gui.tree:
         drag_drop_event.accept()
         node_type = json.loads(mime_data.text())
         node = NodeTypes.create_node_from_node_type(node_type)
         # setting this attribute starts node addition sequence in the scene
         self.gui.tree.add_node(node)
         node = ViewNode(*self.node_init_pos,
                         scene=self,
                         model_node=node,
                         title=node.title,
                         node_types=self.gui.load_node_types)
         self.drag_drop_node = node
         node.top_collapse_expand_button.hide()
         self.nodes[node.id] = node
         x, y = drag_drop_event.scenePos().x(), drag_drop_event.scenePos(
         ).y()
         node.moveBy(x - self.node_init_pos[0], y - self.node_init_pos[1])
         self.addItem(node)
     else:
         drag_drop_event.ignore()
 def open_node_types(self):
     """
     Reloads the node types and returns it to the listener as NodeTypes object
     """
     self.node_types = NodeTypes.from_csv()
     self.open_node_types_finished_signal.emit(self.node_types)
Exemple #14
0
 def __init__(self, x: float, y: float, scene: QGraphicsScene, model_node: ModelNode, title: str = None,
              parent: QGraphicsItem = None, node_types: NodeTypes = None):
     """
     The constructor for a UI node
     :param x: x position for the center of the node
     :param y: y position for the center of the node
     :param title: title of the node displayed in the ui
     :param parent: parent of this graphics item
     """
     if title:
         self.title = title
     else:
         # give node a unique title
         self.title = "node {}".format(Node.i)
     self.id = model_node.id
     self.x = x
     self.y = y
     Node.i += 1
     self.scene = scene
     self.model_node = model_node
     self.children = []
     self.edges = []
     # store node positional data when detaching from parent
     self.expand_data = None
     # add node name label centered in the eclipse, elide if title is too long
     self.node_text = QGraphicsSimpleTextItem()
     metrics = QFontMetrics(self.node_text.font())
     elided_title = metrics.elidedText(self.title, Qt.ElideRight, self.NODE_MAX_WIDTH)
     self.node_text.setText(elided_title)
     self.node_text.setAcceptedMouseButtons(Qt.NoButton)
     self.node_text.setAcceptHoverEvents(False)
     self.text_width = self.node_text.boundingRect().width()
     self.text_height = self.node_text.boundingRect().height()
     self.node_text.setX(x - (self.text_width / 2))
     # call super function now we know the node size
     super(Node, self).__init__(parent)
     self.node_text.setParentItem(self)
     # indicates if node is being dragged
     self.dragging = False
     self.setCursor(Qt.PointingHandCursor)
     self.setAcceptHoverEvents(True)
     # give the node a default color
     self.brush = QBrush(QColor(*self.NODE_COLOR))
     self.simulator_brush = QBrush(self.DEFAULT_SIMULATOR_COLOR)
     # give node another color
     if node_types:
         # check for node types and color them
         types = node_types.get_node_type_by_name(model_node.title)
         if len(types) > 0:
             category, node_type = types[0]
             if category == 'decorators':
                 self.brush.setColor(QColor(*self.DECORATOR_COLOR))
             elif category == 'composites':
                 self.brush.setColor(QColor(*self.COMPOSITE_COLOR))
             else:
                 self.brush.setColor(QColor(*self.OTHER_NODE_TYPES_COLOR))
         # check for a strategy, role, tactic or keeper
         if 'name' in model_node.attributes.keys() or 'role' in model_node.attributes.keys():
             if model_node.title == 'Tactic':
                 self.brush.setColor(QColor(*self.TACTIC_COLOR))
             elif model_node.title == 'Strategy':
                 self.brush.setColor(QColor(*self.STRATEGY_COLOR))
             elif model_node.title == 'Keeper':
                 self.brush.setColor(QColor(*self.KEEPER_COLOR))
             elif model_node.title == 'Role':
                 self.brush.setColor(QColor(*self.ROLE_COLOR))
             else:
                 self.brush.setColor(QColor(*self.OTHER_SUBTREE_COLOR))
     self.info_display = []
     self.max_width = 0
     self.total_height = 0
     self.bottom_collapse_expand_button = None
     self.top_collapse_expand_button = None
     self._rect = None
     self.initiate_view()
 def test_str(self):
     node_types = NodeTypes.from_csv()
     assert str(node_types.node_types) == str(node_types)
     assert repr(node_types) == str(node_types)
 def test_from_csv_custom_path(self, tmpdir):
     node_types = NodeTypes.from_csv()
     node_types.write(tmpdir)
     node_types_custom = NodeTypes.from_csv(tmpdir)
     assert node_types == node_types_custom
 def test_node_type_validity(self):
     with pytest.raises(InvalidNodeTypeException):
         NodeTypes.check_node_type_validity([])
     with pytest.raises(InvalidNodeTypeException):
         NodeTypes.check_node_type_validity([True])
     assert True is NodeTypes.check_node_type_validity(['a'])
 def test_create_node_from_type_exists(self):
     node_from_node_type = NodeTypes.create_node_from_node_type(["Sequence", "a", "b"])
     assert "Sequence" == node_from_node_type.title
     assert "a" in node_from_node_type.attributes.get("properties").keys()
     assert "b" in node_from_node_type.attributes.get("properties").keys()
 def __init__(self):
     super().__init__()
     # crete collection variable and initialize from settings
     self.collection = Collection.from_path()
     # create node types variable and initialize from settings
     self.node_types = NodeTypes.from_csv()