def __strip_relationship_attribute(self, rel_id: Tuple[str, str],
                                       attr_name: str) -> None:
        """Removes the attribute with attr_name for the specified relationship."""

        # Check whether both classes exist
        class_list: List[str] = self.__diagram.get_all_class_names()
        if rel_id[0] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[0]))
            return
        if rel_id[1] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[1]))
            return

        # Remove the relationship's attribute, checking for an error
        if not self.__diagram.remove_relationship_attribute(
                rel_id[0], rel_id[1], attr_name):
            print(
                "Relationship '{}' does not have an attribute with name '{}'".
                format(
                    uml_utilities.stringify_relationship_identifier(
                        rel_id[0], rel_id[1]),
                    attr_name,
                ))
        else:
            print("Removed attribute '{}' from relationship '{}'".format(
                attr_name,
                uml_utilities.stringify_relationship_identifier(
                    rel_id[0], rel_id[1]),
            ))
    def getAllRelationships(self, params: str) -> Dict[str, Dict[str, str]]:
        """Returns a dictionary containing all relationship infromation in the diagram.
Structure: dictionary[classPair][attributeName] == attributeValue"""

        response: Dict[str, Dict[str, str]] = {}

        # Populate response dictionary with relationships
        for class_pair in self.__diagram.get_all_relationship_pairs():

            relationship_attributes: Optional[
                Dict[str, str]
            ] = self.__diagram.get_relationship_between(class_pair[0], class_pair[1])

            if relationship_attributes is not None:
                relationship_id: str = uml_utilities.stringify_relationship_identifier(
                    class_pair[0], class_pair[1]
                )
                response[relationship_id] = relationship_attributes
            else:
                raise Exception(
                    "Relationship not found in diagram: {}".format(
                        uml_utilities.stringify_relationship_identifier(
                            class_pair[0], class_pair[1]
                        )
                    )
                )

        return response
    def __set_relationship_attribute(self, rel_id: Tuple[str, str],
                                     attr_name: str, attr_value: str) -> None:
        """Adds or modifies the attribute with attr_name for the specified relationship."""

        # Check whether both classes exist
        class_list: List[str] = self.__diagram.get_all_class_names()
        if rel_id[0] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[0]))
            return
        if rel_id[1] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[1]))
            return

        # Set the relationship's attribute, checking for an error
        if not self.__diagram.set_relationship_attribute(
                rel_id[0], rel_id[1], attr_name, attr_value):
            print("Relationship {} does not exist in the diagram".format(
                uml_utilities.stringify_relationship_identifier(
                    rel_id[0], rel_id[1])))
        else:
            print("Set attribute '{}' with value '{}' in relationship '{}'".
                  format(
                      attr_name,
                      attr_value,
                      uml_utilities.stringify_relationship_identifier(
                          rel_id[0], rel_id[1]),
                  ))
    def __add_relationship(self, rel_id: Tuple[str, str]) -> None:
        """Adds new relationship if one with that identifier does not already exist"""

        # Check whether both classes exist
        class_list: List[str] = self.__diagram.get_all_class_names()
        if rel_id[0] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[0]))
            return
        if rel_id[1] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[1]))
            return

        # Check if both args are the same class, no class can have a relationship with itself
        if rel_id[0] == rel_id[1]:
            print("A class cannot have a relationship with itself.")
            return

        # Add the relationship to the diagram, checking for an error
        if not self.__diagram.add_relationship(rel_id[0], rel_id[1]):
            print("Relationship '{}' already exists in the diagram".format(
                uml_utilities.stringify_relationship_identifier(
                    rel_id[0], rel_id[1])))
        else:
            print("Added relationship '{}'".format(
                uml_utilities.stringify_relationship_identifier(
                    rel_id[0], rel_id[1])))
    def removeRelationshipAttribute(self, attribute_data: Dict[str, str]) -> str:

        attribute_name: str = attribute_data["attribute_name"]
        relationship_id: str = attribute_data["relationship_id"]
        relationship_id_tuple: Optional[
            Tuple[str, str]
        ] = uml_utilities.parse_relationship_identifier(relationship_id)

        if not relationship_id_tuple:
            raise Exception(
                "Invalid relationship identifier provided: " + relationship_id
            )

        class_name_a: str = relationship_id_tuple[0]
        class_name_b: str = relationship_id_tuple[1]

        if not class_name_a in self.__diagram.get_all_class_names():
            return "Class " + class_name_a + " not found in the diagram."
        if not class_name_b in self.__diagram.get_all_class_names():
            return "Class " + class_name_b + " not found in the diagram."

        if not self.__diagram.remove_relationship_attribute(
            class_name_a, class_name_b, attribute_name
        ):
            return (
                "Attribute "
                + attribute_name
                + " not found in Class: "
                + uml_utilities.stringify_relationship_identifier(
                    class_name_a, class_name_b
                )
            )

        return ""
    def removeRelationship(self, relationship_id: str) -> str:

        relationship_id_tuple: Optional[
            Tuple[str, str]
        ] = uml_utilities.parse_relationship_identifier(relationship_id)

        if not relationship_id_tuple:
            raise Exception(
                "Invalid relationship identifier provided: " + relationship_id
            )

        class_name_a: str = relationship_id_tuple[0]
        class_name_b: str = relationship_id_tuple[1]

        if not class_name_a in self.__diagram.get_all_class_names():
            return "Class " + class_name_a + " not found in the diagram."
        if not class_name_b in self.__diagram.get_all_class_names():
            return "Class " + class_name_b + " not found in the diagram."

        if not self.__diagram.remove_relationship(class_name_a, class_name_b):
            return "Relationship not found in diagram: {}".format(
                uml_utilities.stringify_relationship_identifier(
                    class_name_a, class_name_b
                )
            )

        return ""
    def __remove_relationship(self, rel_id: Tuple[str, str]) -> None:
        """Removes relationship if one with that identifier exists"""

        # Check whether both classes exist
        class_list: List[str] = self.__diagram.get_all_class_names()
        if rel_id[0] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[0]))
            return
        if rel_id[1] not in class_list:
            print("Class '{}' does not exist in the diagram".format(rel_id[1]))
            return

        # Remove the relationship from the diagram, checking for an error
        if not self.__diagram.remove_relationship(rel_id[0], rel_id[1]):
            print("Relationship '{}' does not exist in the diagram".format(
                uml_utilities.stringify_relationship_identifier(
                    rel_id[0], rel_id[1])))
        else:
            print("Relationship '{}' has been removed from the diagram".format(
                uml_utilities.stringify_relationship_identifier(
                    rel_id[0], rel_id[1])))
    def addRelationship(self, relationship_properties: Dict[str, str]) -> str:

        class_name_a: str = relationship_properties["class_name_a"]
        class_name_b: str = relationship_properties["class_name_b"]

        if not class_name_a in self.__diagram.get_all_class_names():
            return "Class " + class_name_a + " not found in the diagram."
        if not class_name_b in self.__diagram.get_all_class_names():
            return "Class " + class_name_b + " not found in the diagram."
        if class_name_a == class_name_b:
            return "A class cannot have a relationship with itself."

        if not self.__diagram.add_relationship(class_name_a, class_name_b):
            return "Relationship already exists: {}".format(
                uml_utilities.stringify_relationship_identifier(
                    class_name_a, class_name_b
                )
            )

        return ""
    def setRelationshipAttribute(self, attribute_data: Dict[str, str]) -> str:

        attr_key: str = attribute_data["attr_key"]
        attr_value: str = attribute_data["attr_key"]

        relationship_id: str = attribute_data["relationship_id"]
        relationship_id_tuple: Optional[
            Tuple[str, str]
        ] = uml_utilities.parse_relationship_identifier(relationship_id)

        if not relationship_id_tuple:
            raise Exception(
                "Invalid relationship identifier provided: " + relationship_id
            )

        class_name_a: str = relationship_id_tuple[0]
        class_name_b: str = relationship_id_tuple[1]

        if not class_name_a in self.__diagram.get_all_class_names():
            return "Class " + class_name_a + " not found in the diagram."
        if not class_name_b in self.__diagram.get_all_class_names():
            return "Class " + class_name_b + " not found in the diagram."

        if (
            not "ignore_naming_rules" in attribute_data
            and not uml_utilities.parse_class_identifier(attr_key)
        ):
            return "Attribute name is invalid. (Cannot contain whitespace or quotes, and cannot be surrounded by brackets.)"

        if not self.__diagram.set_relationship_attribute(
            relationship_id_tuple[0], relationship_id_tuple[1], attr_key, attr_value
        ):
            return "Relationship not found in diagram: {}".format(
                uml_utilities.stringify_relationship_identifier(
                    class_name_a, class_name_b
                )
            )

        return ""
Beispiel #10
0
def test_stringify_relationship_identifier() -> None:
    assert (uml_utilities.stringify_relationship_identifier(
        "Alpha", "Beta") == "[Alpha,Beta]")