Functional coverage tree: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 2 users not shown)
Line 136:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 289:
fmt.Printf("%8.6f to %8.6f\n", diff, topCoverage+diff)
h2Basement[2].setCoverage(0.75) // restore to original value if required
}</langsyntaxhighlight>
 
{{out}}
Line 348:
The raw table (supplied in the task description) is read in from a text file, parsed to a tree structure, and updated by two traversals (one bottom-up and one top down) before being serialised back to a completed outline text, with an additional 'Share of Residue' column:
{{Trans|Python}}
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
 
import Data.Bifunctor (first)
Line 523:
 
showN :: Int -> Float -> String
showN p n = justifyRight 7 ' ' (showFFloat (Just p) n "")</langsyntaxhighlight>
{{Out}}
<pre>NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
Line 573:
Implementation part 1 of 4 (raw data):
 
<langsyntaxhighlight Jlang="j">raw=: 0 :0
NAME_HIERARCHY |WEIGHT |COVERAGE |
cleaning | | |
Line 617:
wine_cellar | |1 |
cinema | |0.75 |
)</langsyntaxhighlight>
 
Implementation part 2 of 4 (unpacking raw data):
 
<langsyntaxhighlight Jlang="j">labels=: {.<;._2;._2 raw
'hier wspec cspec'=:|:}.<;._2;._2 raw
level=: (%+./) (0 i.~' '&=)"1 hier
weight=: (+ 0=]) ,".wspec
coverage=: ,".cspec</langsyntaxhighlight>
 
To understand this implementation, it's best to run it and inspect the data.
Line 641:
Implementation part 3 of 4 (translation of leaf coverage to functional coverage):
 
<langsyntaxhighlight Jlang="j">merge=: ;@(({.@[,(+}.)~)&.> [: +/\1,_1}.#@>)
unrooted=: ([: merge <@(_1,$:@}.);.1)^:(0<#)
parent=: unrooted level
parent_cover=: (] (1}.~.parent)}~ 1}. * %&(parent +//. ]) [)^:_</langsyntaxhighlight>
 
<code>unrooted</code> translates indentation information to a [[Tree_traversal#J:_Alternate_implementation|parent tree structure]]. However, the limitations of recursion require we distinguish the parent node from its children, so we use _1 to denote the parent node of the recursive intermediate result unrooted trees. (This works well with using arithmetic to adjust sub-tree indices based on the lengths of preceding sub-trees.) <code>merge</code> combines a boxed sequence of these subtrees to form a single tree - we also rely on the first node of each tree being both _1 and the root node.
Line 652:
Task example part 4 of 4 (format and show result):
 
<langsyntaxhighlight Jlang="j"> 1 1 }._1 }.":labels,each ":each hier;(,.weight);,.weight parent_cover coverage
NAME_HIERARCHY │WEIGHT │COVERAGE │
cleaning │ 1 │0.409167 │
Line 695:
cellars │ 1 │ 1 │
wine_cellar │ 1 │ 1 │
cinema │ 1 │ 0.75 │</langsyntaxhighlight>
 
Extra credit:
 
<langsyntaxhighlight Jlang="j">trace=: (~.@,each (0 >. parent)&{)^:_ i.#parent
power=: */@:{&(parent (] % (i.~ ~.)@[ { +//.) weight)@> trace
 
power*1-weight parent_cover coverage
0.590833 0.2675 0.0375 0.025 0.00833333 0.0166667 0 0.0125 0.045 0.0375 0.0125 0.0125 0.0125 0 0.05 0.05 0.01 0.323333 0.17 0.05 0.0125 0.0125 0.0125 0.0125 0.05 0.05 0.02 0.136667 0.0333333 0.0333333 0.00833333 0.00833333 0.00833333 0.00833333 0.0333333 0.0333333 0.00333333 0 0.0166667 0 0 0.0166667</langsyntaxhighlight>
 
Explanation:
Line 714:
 
And, <code>weight parent_cover coverage</code> was the functional coverage for each node.
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
 
public final class FunctionalCoverageTree {
 
public static void main(String[] aArgs) {
FCNode cleaning = new FCNode("Cleaning", 1, 0.0);
List<FCNode> houses = List.of(
new FCNode("House_1", 40, 0.0),
new FCNode("House_2", 60, 0.0) );
cleaning.addChildren(houses);
List<FCNode> house_1 = List.of(
new FCNode("Bedrooms", 1, 0.25),
new FCNode("Bathrooms", 1, 0.0),
new FCNode("Attic", 1, 0.75),
new FCNode("Kitchen", 1, 0.1),
new FCNode("Living_rooms", 1, 0.0),
new FCNode("Basement", 1, 0.0),
new FCNode("Garage", 1, 0.0),
new FCNode("Garden",1, 0.8) );
houses.get(0).addChildren(house_1);
List<FCNode> bathrooms_house_1 = List.of(
new FCNode("Bathroom_1", 1, 0.5),
new FCNode("Bathroom_2", 1, 0.0),
new FCNode("Outside_lavatory", 1, 1.0) );
house_1.get(1).addChildren(bathrooms_house_1);
List<FCNode> living_rooms_house_1 = List.of(
new FCNode("lounge", 1, 0.0),
new FCNode("Dining_room", 1, 0.0),
new FCNode("Conservatory", 1, 0.0),
new FCNode("Playroom", 1, 1.0) );
house_1.get(4).addChildren(living_rooms_house_1);
List<FCNode> house_2 = List.of(
new FCNode("Upstairs", 1, 0.15),
new FCNode("Ground_floor", 1, 0.316667),
new FCNode("Basement", 1, 0.916667));
houses.get(1).addChildren(house_2);
List<FCNode> upstairs = List.of(
new FCNode("Bedrooms", 1, 0.0),
new FCNode("Bathroom", 1, 0.0),
new FCNode("Toilet", 1, 0.0),
new FCNode("Attics", 1, 0.6) );
house_2.get(0).addChildren(upstairs);
List<FCNode> ground_floor = List.of(
new FCNode("Kitchen", 1, 0.0),
new FCNode("Living_rooms", 1, 0.0),
new FCNode("Wet_room_&_toilet", 1, 0.0),
new FCNode("Garage", 1, 0.0),
new FCNode("Garden", 1, 0.9),
new FCNode("Hot_tub_suite", 1, 1.0) );
house_2.get(1).addChildren(ground_floor);
List<FCNode> basement = List.of(
new FCNode("Cellars", 1, 1.0),
new FCNode("Wine_cellar", 1, 1.0),
new FCNode("Cinema", 1, 0.75) );
house_2.get(2).addChildren(basement);
List<FCNode> bedrooms = List.of(
new FCNode("Suite_1", 1, 0.0),
new FCNode("Suite_2", 1, 0.0),
new FCNode("Bedroom_3",1, 0.0),
new FCNode("Bedroom_4",1, 0.0) );
upstairs.get(0).addChildren(bedrooms);
List<FCNode> living_rooms_house_2 = List.of(
new FCNode("lounge", 1, 0.0),
new FCNode("Dining_room", 1, 0.0),
new FCNode("Conservatory", 1, 0.0),
new FCNode("Playroom", 1, 0.0) );
ground_floor.get(1).addChildren(living_rooms_house_2);
final double overallCoverage = cleaning.getCoverage();
System.out.println("OVERALL COVERAGE = " + String.format("%.6f", overallCoverage) + System.lineSeparator());
System.out.println("NAME HIERARCHY | WEIGHT | COVERAGE |" );
System.out.println("--------------------------------|--------|----------|");
cleaning.display();
System.out.println();
basement.get(2).setCoverage(1.0); // Change House_2 Cinema node coverage to 1.0
final double updatedCoverage = cleaning.getCoverage();
final double difference = updatedCoverage - overallCoverage;
System.out.println("If the coverage of the House_2 Cinema node were increased from 0.75 to 1.0");
System.out.print("the overall coverage would increase by ");
System.out.println(String.format("%.6f%s%.6f", difference, " to ", updatedCoverage));;
basement.get(2).setCoverage(0.75); // Restore to House_2 Cinema node coverage to its original value
}
 
}
 
final class FCNode {
public FCNode(String aName, int aWeight, double aCoverage) {
name = aName;
weight = aWeight;
coverage = aCoverage;
}
public void addChildren(List<FCNode> aNodes) {
for ( FCNode node : aNodes ) {
node.parent = this;
children.add(node);
updateCoverage();
}
}
public double getCoverage() {
return coverage;
}
public void setCoverage(double aCoverage) {
if ( coverage != aCoverage ) {
coverage = aCoverage;
if ( parent != null ) {
parent.updateCoverage();
}
}
}
public void display() {
display(0);
}
private void updateCoverage() {
double value1 = 0.0;
int value2 = 0;
for ( FCNode node : children ) {
value1 += node.weight * node.coverage;
value2 += node.weight;
}
setCoverage(value1 / value2);
}
private void display(int aLevel) {
final String initial = " ".repeat(4 * aLevel) + name;
final String padding = " ".repeat(NAME_FIELD_WIDTH - initial.length());
System.out.print(initial + padding + "|");
System.out.print(" " + String.format("%3d", weight) + " |");
System.out.println(" " + String.format("%.6f", coverage) + " |");
 
for ( FCNode child : children ) {
child.display(aLevel + 1);
}
}
private String name;
private int weight;
private double coverage;
private FCNode parent;
private List<FCNode> children = new ArrayList<FCNode>();
private static final int NAME_FIELD_WIDTH = 32;
}
</syntaxhighlight>
{{ out }}
<pre>
OVERALL COVERAGE = 0.409167
 
NAME HIERARCHY | WEIGHT | COVERAGE |
--------------------------------|--------|----------|
Cleaning | 1 | 0.409167 |
House_1 | 40 | 0.331250 |
Bedrooms | 1 | 0.250000 |
Bathrooms | 1 | 0.500000 |
Bathroom_1 | 1 | 0.500000 |
Bathroom_2 | 1 | 0.000000 |
Outside_lavatory | 1 | 1.000000 |
Attic | 1 | 0.750000 |
Kitchen | 1 | 0.100000 |
Living_rooms | 1 | 0.250000 |
lounge | 1 | 0.000000 |
Dining_room | 1 | 0.000000 |
Conservatory | 1 | 0.000000 |
Playroom | 1 | 1.000000 |
Basement | 1 | 0.000000 |
Garage | 1 | 0.000000 |
Garden | 1 | 0.800000 |
House_2 | 60 | 0.461111 |
Upstairs | 1 | 0.150000 |
Bedrooms | 1 | 0.000000 |
Suite_1 | 1 | 0.000000 |
Suite_2 | 1 | 0.000000 |
Bedroom_3 | 1 | 0.000000 |
Bedroom_4 | 1 | 0.000000 |
Bathroom | 1 | 0.000000 |
Toilet | 1 | 0.000000 |
Attics | 1 | 0.600000 |
Ground_floor | 1 | 0.316667 |
Kitchen | 1 | 0.000000 |
Living_rooms | 1 | 0.000000 |
lounge | 1 | 0.000000 |
Dining_room | 1 | 0.000000 |
Conservatory | 1 | 0.000000 |
Playroom | 1 | 0.000000 |
Wet_room_&_toilet | 1 | 0.000000 |
Garage | 1 | 0.000000 |
Garden | 1 | 0.900000 |
Hot_tub_suite | 1 | 1.000000 |
Basement | 1 | 0.916667 |
Cellars | 1 | 1.000000 |
Wine_cellar | 1 | 1.000000 |
Cinema | 1 | 0.750000 |
 
If the coverage of the House_2 Cinema node were increased from 0.75 to 1.0
the overall coverage would increase by 0.016667 to 0.425833
</pre>
 
=={{header|JavaScript}}==
Line 720 ⟶ 938:
{{Trans|Haskell}}
{{Trans|Python}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,158 ⟶ 1,376:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
Line 1,207 ⟶ 1,425:
Most implementations of functional coverage are going to store values in a database. The implementation
stores the tree in a CSV file with an index to the parent of each entry to allow reconstitution of the tree.
<langsyntaxhighlight Julialang="julia">using CSV, DataFrames, Formatting
 
function updatecoverage(dfname, outputname)
Line 1,269 ⟶ 1,487:
println("\nUpdated data:")
displaycoveragedb(newdbname)
</langsyntaxhighlight>{{out}}
<pre>
Input data:
Line 1,409 ⟶ 1,627:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.2.10
 
class FCNode(val name: String, val weight: Int = 1, coverage: Double = 0.0) {
Line 1,546 ⟶ 1,764:
println("${"%8.6f".format(diff)} to ${"%8.6f".format(topCoverage + diff)}")
h2Basement[2].coverage = 0.75 // restore to original value if required
}</langsyntaxhighlight>
 
{{out}}
Line 1,600 ⟶ 1,818:
</pre>
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- DATA:
local function node(name, weight, coverage, children)
return { name=name, weight=weight or 1.0, coverage=coverage or 0.0, sumofweights=0, delta=0, children=children }
Line 1,693 ⟶ 1,911:
end
print("NAME_HIERARCHY |WEIGHT |COVERAGE |DELTA |")
printnode(root,0)</langsyntaxhighlight>
{{out}}
<pre>NAME_HIERARCHY |WEIGHT |COVERAGE |DELTA |
Line 1,741 ⟶ 1,959:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
type
Line 1,871 ⟶ 2,089:
echo "\nIf the coverage of the Cinema node were increased from 0.75 to 1"
echo &"the top level coverage would increase by {diff:8.6f} to {topCoverage + diff:8.6f}"
h2Basement[2].setCoverage(0.75) # Restore to original value if required.</langsyntaxhighlight>
 
{{out}}
Line 1,924 ⟶ 2,142:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 1,997 ⟶ 2,215:
wine_cellar | |1 |
cinema | |0.75 |
</syntaxhighlight>
</lang>
{{out}}
<pre style="height:20ex" style="font-size:80%;">NAME_HIERARCHY |WEIGHT |COVERAGE |
Line 2,044 ⟶ 2,262:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Functional_coverage_tree.exw
Line 2,156 ⟶ 2,374:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,208 ⟶ 2,426:
It's actually some of the raw code used when researching this task.
 
<langsyntaxhighlight lang="python">from itertools import zip_longest
 
 
Line 2,360 ⟶ 2,578:
print('\n\nTOP COVERAGE = %f\n' % covercalc(lstc))
depth_first(lstc)
pptreefields(['NAME_HIERARCHY WEIGHT COVERAGE'.split()] + lstc)</langsyntaxhighlight>
 
{{out}}
Line 2,412 ⟶ 2,630:
A cleaner implementation that uses the class static variable path2node as in the previous example so you don't have to traverse the tree to work out the position to add new nodes. This relies on parent nodes appearing before their children which is the case in the order of the add_node calls.
 
<langsyntaxhighlight lang="python"># -*- coding: utf-8 -*-
 
SPACES = 4
Line 2,538 ⟶ 2,756:
assert isclose((delta + cover), 1.0), "Top level delta + coverage should " \
"equal 1 instead of (%f + %f)" % (delta, cover)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,605 ⟶ 2,823:
Mainly uses pre-existing generic functions, including '''forestFromLineIndents''', '''foldTree''' and '''fmapTree''':
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Functional coverage tree'''
 
from itertools import chain, product
Line 3,067 ⟶ 3,285:
wine_cellar | |1 |
cinema | |0.75 |'''
main()</langsyntaxhighlight>
{{Out}}
<pre>NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
Line 3,118 ⟶ 3,336:
(in this case <code>data/functional-coverage.txt</code>).
 
<langsyntaxhighlight lang="racket">#lang racket/base
(require racket/list racket/string racket/match racket/format racket/file)
 
Line 3,189 ⟶ 3,407:
(for-each
(compose print-coverage-tree find-wght-cvrg)
(build-hierarchies (report->indent.c/e-list (file->string "data/functional-coverage.txt")))))</langsyntaxhighlight>
 
{{out}}
Line 3,240 ⟶ 3,458:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>sub walktree ($data) {
my (@parts, $cnt);
 
Line 3,311 ⟶ 3,529:
cinema | |0.75 |
END
</syntaxhighlight>
</lang>
{{out}}
<pre style="font-size:70%;">NAME_HIERARCHY |WEIGHT |COVERAGE |
Line 3,362 ⟶ 3,580:
{{trans|Kotlin}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension String {
Line 3,528 ⟶ 3,746:
print("\nIf the coverage of the Cinema node were increased from 0.75 to 1.0")
print("the top level coverage would increase by ")
print("\(String(format: "%8.6f", diff)) to \(String(format: "%8.6f", top))")</langsyntaxhighlight>
 
{{out}}
Line 3,584 ⟶ 3,802:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
class FCNode {
Line 3,730 ⟶ 3,948:
System.write("the top level coverage would increase by ")
Fmt.print("$8.6f to $8.6f", diff, topCoverage + diff)
h2Basement[2].coverage = 0.75 // restore to original value if required</langsyntaxhighlight>
 
{{out}}
9,485

edits