Functional coverage is a measure of how much a particular function of a system
has been verified as correct. It is used heavily in tracking the completeness
of the verification of complex System on Chip (SoC) integrated circuits, where
it can also be used to track how well the functional requirements of the
system have been verified.

This task uses a sub-set of the calculations sometimes used in tracking
functional coverage but uses a more familiar(?) scenario.

Task Description

The head of the clean-up crews for "The Men in a very dark shade of grey when
viewed at night" has been tasked with managing the cleansing of two properties
after an incident involving aliens.

She arranges the task hierarchically with a manager for the crews working on
each house who return with a breakdown of how they will report on progress in
each house.

The overall hierarchy of (sub)tasks is as follows,

cleaning
    house1
        bedrooms
        bathrooms
            bathroom1
            bathroom2
            outside lavatory
        attic
        kitchen
        living rooms
            lounge
            dining room
            conservatory
            playroom
        basement
        garage
        garden
    house2
        upstairs
            bedrooms
                suite 1
                suite 2
                bedroom 3
                bedroom 4
            bathroom
            toilet
            attics
        groundfloor
            kitchen
            living rooms
                lounge
                dining room
                conservatory
                playroom
            wet room & toilet
            garage
            garden
            hot tub suite
        basement
            cellars
            wine cellar
            cinema
The head of cleanup knows that her managers will report fractional completion of leaf tasks (tasks with no child tasks of their own), and she knows that she will want to modify the weight of values of completion as she sees fit.

Some time into the cleaning, and some coverage reports have come in and she thinks see needs to weight the big house2 60-40 with respect to coverage from house1 She prefers a tabular view of her data where missing weights are assumed to be 1.0 and missing coverage 0.0.

NAME_HIERARCHY                  |WEIGHT  |COVERAGE  |
cleaning                        |        |          |
    house1                      |40      |          |
        bedrooms                |        |0.25      |
        bathrooms               |        |          |
            bathroom1           |        |0.5       |
            bathroom2           |        |          |
            outside_lavatory    |        |1         |
        attic                   |        |0.75      |
        kitchen                 |        |0.1       |
        living_rooms            |        |          |
            lounge              |        |          |
            dining_room         |        |          |
            conservatory        |        |          |
            playroom            |        |1         |
        basement                |        |          |
        garage                  |        |          |
        garden                  |        |0.8       |
    house2                      |60      |          |
        upstairs                |        |          |
            bedrooms            |        |          |
                suite_1         |        |          |
                suite_2         |        |          |
                bedroom_3       |        |          |
                bedroom_4       |        |          |
            bathroom            |        |          |
            toilet              |        |          |
            attics              |        |0.6       |
        groundfloor             |        |          |
            kitchen             |        |          |
            living_rooms        |        |          |
                lounge          |        |          |
                dining_room     |        |          |
                conservatory    |        |          |
                playroom        |        |          |
            wet_room_&_toilet   |        |          |
            garage              |        |          |
            garden              |        |0.9       |
            hot_tub_suite       |        |1         |
        basement                |        |          |
            cellars             |        |1         |
            wine_cellar         |        |1         |
            cinema              |        |0.75      |
Calculation

The coverage of a node in the tree is calculated as the weighted average of the coverage of its children evaluated bottom-upwards in the tree.

The task is to calculate the overall coverage of the cleaning task and display the coverage at all levels of the hierarchy on this page, in a manner that visually shows the hierarchy, weights and coverage of all nodes.

Extra Credit

After calculating the coverage for all nodes, one can also calculate the additional/delta top level coverage that would occur if any (sub)task were to be fully covered from its current fractional coverage. This is done by multiplying the extra coverage that could be gained 



1
−
c
o
v
e
r
a
g
e


{\displaystyle 1-coverage}

 for any node, by the product of the `powers` of its parent nodes from the top down to the node.

The power of a direct child of any parent is given by the power of the parent multiplied by the weight of the child divided by the sum of the weights of all the direct children.

The pseudo code would be:

   method delta_calculation(this, power):
       sum_of_weights = sum(node.weight for node in children)
       this.delta  = (1 - this.coverage) * power
       for node in self.children:
           node.delta_calculation(power * node.weight / sum_of_weights)
       return this.delta

Followed by a call to:

   top.delta_calculation(power=1)


Note: to aid in getting the data into your program you might want to use an alternative, more functional description of the starting data given on the discussion page.

#lang racket/base
(require racket/list racket/string racket/match racket/format racket/file)
 
(struct Coverage (name weight coverage weighted-coverage children) #:transparent #:mutable)
 
;; -| read/parse |------------------------------------------------------------------------------------
(define (build-hierarchies parsed-lines)
  (define inr
    (match-lambda
      ['() (values null null)]
      [`((,head-indent . ,C) ,tail-lines ...)
       (define child? (match-lambda [(cons i _) #:when (> i head-indent) #t] [_ #f]))
       (define-values (chlds rels) (splitf-at tail-lines child?))
       (define-values (rels-tree rels-rem) (inr rels))
       (values (cons (struct-copy Coverage C (children (build-hierarchies chlds))) rels-tree)
               rels-rem)]))
  (define-values (hierarchies remaining-lines) (inr parsed-lines))
  hierarchies)
 
(define report-line->indent.c/e-line
  (match-lambda
    [(regexp #px"^( *)([^ ]*) *\\| *([^ ]*) *\\| *([^ ]*) *\\|$"
             (list _
                   (app string-length indent-length)
                   name
                   (or (and (not "") (app string->number wght)) (app (λ (x) 1) wght))
                   (or (and (not "") (app string->number cvrg)) (app (λ (x) 0) cvrg))))
     (cons indent-length (Coverage name wght cvrg 0 #f))]))
 
(define (report->indent.c/e-list rprt)
  (map report-line->indent.c/e-line (drop (string-split rprt "\n") 1)))
 
;; -| evaluate |--------------------------------------------------------------------------------------
(define find-wght-cvrg
  (match-lambda
    [(and e (Coverage _ w c _ '())) (struct-copy Coverage e (weighted-coverage (* w c)))]
    [(and e (Coverage _ _ _ _ `(,(app find-wght-cvrg (and cdn+ (Coverage _ c-ws _ c-w/cs _))) ...)))
     (define chld-wghtd-avg (for/sum ((w (in-list c-ws)) (w/c (in-list c-w/cs))) (* w w/c)))
     (struct-copy Coverage e (weighted-coverage (/ chld-wghtd-avg (apply + c-ws))) (children cdn+))]))
 
;; -| printing |--------------------------------------------------------------------------------------
(define max-description-length
  (match-lambda
    [(Coverage (app string-length name-length) _ _ _
               (list (app max-description-length children-lengths) ...))
     (apply max name-length (map add1 children-lengths))]))
 
(define (~a/right w x)
  (~a x #:width w #:align 'right))
 
(define (~a/decimal n dec-dgts)
  (~a/right (+ dec-dgts 3) (if (zero? n) "" (real->decimal-string n dec-dgts))))
 
(define (print-coverage-tree tree)
  (define mdl (max-description-length tree))
  (printf "| ~a |WEIGT| COVER |WGHTD CVRG|~%" (~a "NAME" #:width mdl #:align 'center))
  (let inr ((depth 0) (tree tree))
    (unless (null? tree)
      (match tree
        [(Coverage name w c w/c chlds)
         (printf "| ~a | ~a | ~a | ~a |~%"
                 (~a (string-append (make-string depth #\space) name) #:width mdl)
                 (~a/right 3 w) (~a/decimal c 2) (~a/decimal w/c 5))
         (for ((c chlds)) (inr (add1 depth) c))]))))
 
;; ---------------------------------------------------------------------------------------------------
(module+ main
;; data/functional-coverage.txt contains a verbatim copy of
;; the table in the task's description
(for-each
 (compose print-coverage-tree find-wght-cvrg)
 (build-hierarchies (report->indent.c/e-list (file->string "data/functional-coverage.txt")))))