umldiagramocl

How to express tree structure constraints


How can the following constraints be expressed:

1 - There is exactly one folder that is not a sub-directory to another directory. (I couldn't fully understand the folder/subfolder theme and how to describe the only one possible exclusion from the folder system)

And there are also some question which comes from the first question

2 The highest nesting of folders does not exceed the number n.

3) The total number of files on your system can not exceed the number n.

4) The total number of files (subdirectory) in a given system cannot exceed the number n.

The uml diagram


Solution

  • Your four constraints cannot be expressed simply using the multiplicities.

    In UML theses constraints can be written using OCL, see formal/2014-02-03

    Of course the constraints can be written in a class diagram, for instance see figure 7.14 Constraint in a note symbol page 37 of formal/2017-12-05.

    1 - There is exactly one folder that is not a sub-directory to another directory

    one way to write that is :

    Folder.allInstances()->select(f | f.upfolder->isEmpty())->size() = 1
    

    where

    2 The highest nesting of folders does not exceed the number n

    one way is to define a function computing the depth of a folder then to check all the folder have a depth less or equals to n

    context Folder
    def: depth() : Integer =
      if upfolder->notEmpty() then
        upfolder->first().depth() + 1
      else
        0
    
    Folder.allInstances()->forAll(f | f.depth() <= n)
    

    where forAll is true if the condition depth() <= n is true for all the elements

    But it is only useful to compute the depth of the folders without sub folder, so

    Folder.allInstances()
      ->select(f | f.subfolder->isEmpty())
         ->forAll(f | f.depth() <= n)
    

    3) The total number of files on your system can not exceed the number n.

    4) The total number of files (subdirectory) in a given system cannot exceed the number n.

    I do not understand why (subdirectory) in 4 nor why 3 says on your system and 4 says a given system while there is nothing about system in 1 and 2.

    Supposing the goal is to check the total number of files is less or equals to n and the files of a folder are given by the attribute file :

    Folder.allInstances()->collect(f | f.file.size()).sum() <= n
    

    where