Saturday, January 4, 2025

UML Diagrams relationships






 import pydot


# Create a new graph
graph = pydot.Dot(graph_type="digraph", rankdir="BT")  # Bottom-to-Top layout for hierarchy

# Define nodes
team = pydot.Node("Team", shape="box")
player = pydot.Node("Player", shape="box")
book = pydot.Node("Book", shape="box")
chapter = pydot.Node("Chapter", shape="box")
teacher = pydot.Node("Teacher", shape="box")
student = pydot.Node("Student", shape="box")
animal = pydot.Node("Animal", shape="box")
dog = pydot.Node("Dog", shape="box")
cat = pydot.Node("Cat", shape="box")
hospital = pydot.Node("Hospital", shape="box")  # Fixed: Node named 'Hospital' instead of 'doctor'
patient = pydot.Node("Patient", shape="box")
order = pydot.Node("Order", shape="box")
customer = pydot.Node("Customer", shape="box")
animal_interface = pydot.Node("AnimalInterface", shape="box")

# Add nodes to the graph
graph.add_node(team)
graph.add_node(player)
graph.add_node(book)
graph.add_node(chapter)
graph.add_node(teacher)
graph.add_node(student)
graph.add_node(animal)
graph.add_node(dog)
graph.add_node(cat)
graph.add_node(hospital)  # Corrected to 'hospital'
graph.add_node(patient)
graph.add_node(order)
graph.add_node(customer)
graph.add_node(animal_interface)

# Define aggregation relationship between Team and Player (Aggregation)
aggregation = pydot.Edge(
    team,  # Source is Team
    player,  # Target is Player
    arrowhead="none",  # No arrow
    headlabel="*",  # Add cardinality * for Player
    taillabel="*",  # Add cardinality * for Team
    labeldistance=1.2,  # Adjust label distance for better positioning
    style="dashed",  # Dashed for aggregation
    arrowtail="odiamond",  # Hollow diamond for aggregation
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    label="Aggregation"  # Visible label for Aggregation relationship
)
graph.add_edge(aggregation)

# Define composition relationship between Book and Chapter (Composition)
composition = pydot.Edge(
    book,  # Source is Book
    chapter,  # Target is Chapter
    arrowhead="none",  # No arrow
    headlabel="*",  # Add cardinality * for Chapter
    taillabel="1",  # Add cardinality 1 for Book
    labeldistance=1.2,  # Adjust label distance for better positioning
    dir="both",  # Both directions
    arrowtail="diamond",  # Solid diamond for composition
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    label="Composition"  # Visible label for Composition relationship
)
graph.add_edge(composition)

# Define directed association relationship between Teacher and Student (Directed Association)
directed_association = pydot.Edge(
    teacher,  # Source is Teacher
    student,  # Target is Student
    headlabel="*",  # Add cardinality * for Student
    arrowhead="normal",  # Normal arrowhead for directed association
    labeldistance=1.2,  # Adjust label distance for better positioning
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    label="Directed Association"  # Visible label for Directed Association relationship
)
graph.add_edge(directed_association)

# Define usage (dependency) relationship between Order and Customer (Dependency)
dependency = pydot.Edge(
    order,  # Source is Order
    customer,  # Target is Customer
    style="dashed",  # Dashed for dependency
    arrowhead="vee",  # Arrowhead for dependency
    label="Usage",  # Label for usage
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    labeldistance=1.2  # Adjust label distance for better positioning
)
graph.add_edge(dependency)

# Define generalization relationship between Animal and Dog/Cat (Generalization)
generalization_dog = pydot.Edge(
    animal,  # Source is Animal
    dog,  # Target is Dog
    arrowhead="onormal",  # Hollow arrowhead for generalization
    labeldistance=1.2,  # Adjust label distance for better positioning
    dir="both",  # Both directions
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    label="Generalization"  # Visible label for Generalization relationship
)
graph.add_edge(generalization_dog)

generalization_cat = pydot.Edge(
    animal,  # Source is Animal
    cat,  # Target is Cat
    arrowhead="onormal",  # Hollow arrowhead for generalization
    labeldistance=1.2,  # Adjust label distance for better positioning
    dir="both",  # Both directions
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    label="Generalization"  # Visible label for Generalization relationship
)
graph.add_edge(generalization_cat)

# Define association relationship between Doctor and Patient (Association)
association = pydot.Edge(
    hospital,  # Source is Hospital (corrected)
    patient,  # Target is Patient
    arrowhead="none",  # No arrowhead for association
    headlabel="*",  # Add cardinality * for Patient
    taillabel="*",  # Add cardinality * for Doctor
    labeldistance=1.2,  # Adjust label distance for better positioning
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    label="Association"  # Visible label for Association relationship
)
graph.add_edge(association)

# Define realization relationship between AnimalInterface and Dog/Cat (Realization)
realization_dog = pydot.Edge(
    animal_interface,  # Source is AnimalInterface
    dog,  # Target is Dog
    style="dashed",  # Dashed for realization
    arrowhead="onormal",  # Hollow arrowhead for realization
    label="Realization",  # Label for realization
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    labeldistance=1.2  # Adjust label distance for better positioning
)
graph.add_edge(realization_dog)

realization_cat = pydot.Edge(
    animal_interface,  # Source is AnimalInterface
    cat,  # Target is Cat
    style="dashed",  # Dashed for realization
    arrowhead="onormal",  # Hollow arrowhead for realization
    label="Realization",  # Label for realization
    labelfontsize=10,  # Font size for the labels
    fontsize=10,  # Font size for edge labels
    labeldistance=1.2  # Adjust label distance for better positioning
)
graph.add_edge(realization_cat)

# Output the graph to an image file (e.g., PNG)
graph.write_png("uml_relationships_with_labels_expanded.png")

print("UML diagram saved as uml_relationships_with_labels_expanded.png")

Pages