1

I want to model a tree, which a situation where a class Node is composed of multiple instances of itself.

Now how to indicates a constraint like "The root of the tree may contains as many node as needed, but shall only have one instance of this specific type of node" (the special node itself may contain anything

With a diagram a indicated below, we have a conflict between the two composition, I shall be able to add as many specialElement as a want to a root, because they are treeNodes.

How could I represent this constraint?

enter image description here

sayanel
  • 301
  • 2
  • 12

2 Answers2

2

Your current model has a flaw: root and specialElement both inherit the composition with TreeNode that they soecialize. The composition that you represent between root and specialElement is therefore understood an additional one.

There are three easy ways out:

  • use Jim’s excellent answer base on subsetting. But this requires to document explicitly the roles at each end of the composition.
  • show visually that the composition between root and specialElement is a specialization of the reflexive composition of TreeNode (just add the generalization arrow on the diagram between the lines representing composition).
  • remove the additional redundant composition and instead add a constraint in a note attached to root for example in plaintext { can have at most one specialElement child node } (but I’m sure someone will manage to express this formally in OCL ;-) )

For more ideas, you may also look at this question about covariant associations in UML.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    Thanks, I also used https://stackoverflow.com/questions/20034010/how-to-use-subsetted-property-in-uml to understand subsetting – sayanel May 24 '21 at 06:08
1

You are missing properties at the ends of your associations. A more specific property can subset a more general property and have a multiplicity of exactly [1].

Jim L.
  • 6,177
  • 3
  • 21
  • 47