Skip to content

Commit

Permalink
Added total debt metric, formatting (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealryan authored Feb 1, 2023
1 parent 82d48c4 commit 61c9d0d
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,29 +116,32 @@ void messageHashes() {
void inheritanceHealth() {
new InheritanceHealth( 0, 150, 20, Assertions::assertEquals )
.expect( ExampleSystem.MODEL,
"Actual | Optimal ",
"roots 362 | roots 120",
"edges 398 | edges 444",
"total 760 | total 564",
" 3 25.00% | 3 21.43%",
" 1 8.33% | 2 14.29%",
" 1 8.33% | 3 21.43%",
" 2 16.67% | 3 21.43%",
" 1 8.33% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 2 16.67% | 0 0.00%",
" 1 8.33% | 1 7.14%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 1 7.14%",
" 0 0.00% | 0 0.00%",
" 1 8.33% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 1 7.14%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 196│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 362│roots 120|",
"|edges 398│edges 444|",
"|total 760│total 564|",
"| 3 25.00%│ 3 21.43%|",
"| 1 8.33%│ 2 14.29%|",
"| 1 8.33%│ 3 21.43%|",
"| 2 16.67%│ 3 21.43%|",
"| 1 8.33%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 2 16.67%│ 0 0.00%|",
"| 1 8.33%│ 1 7.14%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 1 7.14%|",
"| 0 0.00%│ 0 0.00%|",
"| 1 8.33%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 1 7.14%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,23 @@ public void expect( Model model, String... expected ) {
StructureCost actual = actualCost( allFlows, total.get() );
StructureCost optimal = optimalCost( model, allFlows, total.get() );

int totalDebt = actual.totalCost() - optimal.totalCost();

Histograph hstg = new Histograph( min, max, Math.min( max - min + 1, heightLimit ) );
Deque<String> actualLines = Stream.of( actual.toString( "Actual", hstg ).split( "\n" ) )
Deque<String> actualLines = Stream.of( actual.toString( hstg ).split( "\n" ) )
.collect( toCollection( ArrayDeque::new ) );
Deque<String> optimalLines = Stream.of( optimal.toString( "Optimal", hstg ).split( "\n" ) )
Deque<String> optimalLines = Stream.of( optimal.toString( hstg ).split( "\n" ) )
.collect( toCollection( ArrayDeque::new ) );

List<String> stitched = new ArrayList<>();
stitched.add( "┌───────────────────────────────────┐" );
stitched.add( String.format( "│Total Debt : %22s│", totalDebt ) );

stitched.add( "├─────Actual──────┬─────Optimal─────┤" );
while( !actualLines.isEmpty() && !optimalLines.isEmpty() ) {
stitched.add( actualLines.poll() + " | " + optimalLines.poll() );
stitched.add( "|" + actualLines.poll() + "" + optimalLines.poll() + "|" );
}
stitched.add( "└─────────────────┴─────────────────┘" );

assertion.accept(
MessageHash.copypasta( Stream.of( expected ) ),
Expand Down Expand Up @@ -273,7 +280,13 @@ protected StructureCost( int rootWeight, TreeMap<Integer, Integer> edgeCosts ) {
this.edgeCosts = edgeCosts;
}

String toString( String name, Histograph hstg ) {
int totalCost() {
return rootWeight + edgeCosts.entrySet().stream()
.mapToInt( e -> e.getKey() * e.getValue() )
.sum();
}

String toString( Histograph hstg ) {

if( !edgeCosts.isEmpty() && edgeCosts.firstKey() < hstg.getMinimum() ) {
throw new IllegalArgumentException(
Expand All @@ -294,12 +307,12 @@ String toString( String name, Histograph hstg ) {
.mapToInt( e -> e.getKey() * e.getValue() )
.sum();
return String.format( ""
+ "%-17s\n"

+ "roots %11s\n"
+ "edges %11s\n"
+ "total %11s\n"
+ "%s\n",
name,

rootWeight,
edgeTotal,
edgeTotal + rootWeight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,23 @@ void lonesome() {
InheritanceHealth ih = new InheritanceHealth( 0, 20, 10, Assertions::assertEquals );
Model mdl = mdl( "root" );
ih.expect( mdl,
"Actual | Optimal ",
"roots 12 | roots 12",
"edges 0 | edges 0",
"total 12 | total 12",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 0│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 12│roots 12|",
"|edges 0│edges 0|",
"|total 12│total 12|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );

assertThrows( AssertionError.class, () -> ih.expect( mdl, "wrong!" ) );
}
Expand All @@ -119,20 +122,23 @@ void lonesome() {
void pair() {
new InheritanceHealth( 0, 20, 10, Assertions::assertEquals )
.expect( mdl( "root", "roof" ),
"Actual | Optimal ",
"roots 24 | roots 12",
"edges 0 | edges 1",
"total 24 | total 13",
" 0 0.00% | 1 100.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 11│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 24│roots 12|",
"|edges 0│edges 1|",
"|total 24│total 13|",
"| 0 0.00%│ 1 100.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );
}

/**
Expand Down Expand Up @@ -176,15 +182,18 @@ void pair() {
void optimise() {
new InheritanceHealth( 0, 4, 5, Assertions::assertEquals )
.expect( mdl( "root", "root>slap", "slap>soot", "soot>slat", "slat>slot", "slot>root" ),
"Actual | Optimal ",
"roots 12 | roots 12",
"edges 13 | edges 5",
"total 25 | total 17",
" 0 0.00% | 1 20.00%",
" 1 20.00% | 3 60.00%",
" 1 20.00% | 1 20.00%",
" 2 40.00% | 0 0.00%",
" 1 20.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 8│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 12│roots 12|",
"|edges 13│edges 5|",
"|total 25│total 17|",
"| 0 0.00%│ 1 20.00%|",
"| 1 20.00%│ 3 60.00%|",
"| 1 20.00%│ 1 20.00%|",
"| 2 40.00%│ 0 0.00%|",
"| 1 20.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );
}

/**
Expand All @@ -203,13 +212,16 @@ void occultedFlows() {
// but 4 are considered in the inheritance health (1 root and three children)
new InheritanceHealth( 0, 2, 3, Assertions::assertEquals )
.expect( model,
"Actual | Optimal ",
"roots 12 | roots 12",
"edges 3 | edges 3",
"total 15 | total 15",
" 0 0.00% | 0 0.00%",
" 3 100.00% | 3 100.00%",
" 0 0.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 0│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 12│roots 12|",
"|edges 3│edges 3|",
"|total 15│total 15|",
"| 0 0.00%│ 0 0.00%|",
"| 3 100.00%│ 3 100.00%|",
"| 0 0.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );
}

/**
Expand All @@ -230,15 +242,18 @@ void listener() {
assertSame( ih, r );
}
ih.expect( mdl( "root", "root>slap", "slap>soot", "soot>slat", "slat>slot" ),
"Actual | Optimal ",
"roots 12 | roots 12",
"edges 10 | edges 5",
"total 22 | total 17",
" 0 0.00% | 0 0.00%",
" 1 25.00% | 3 75.00%",
" 1 25.00% | 1 25.00%",
" 1 25.00% | 0 0.00%",
" 1 25.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 5│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 12│roots 12|",
"|edges 10│edges 5|",
"|total 22│total 17|",
"| 0 0.00%│ 0 0.00%|",
"| 1 25.00%│ 3 75.00%|",
"| 1 25.00%│ 1 25.00%|",
"| 1 25.00%│ 0 0.00%|",
"| 1 25.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );

assertEquals( ""
+ "BUILD -1.00\n"
Expand Down Expand Up @@ -280,15 +295,18 @@ void listener() {
void height() {
new InheritanceHealth( 2, 6, 10, Assertions::assertEquals )
.expect( mdl( "root", "sppu" ),
"Actual | Optimal ",
"roots 24 | roots 12",
"edges 0 | edges 4",
"total 24 | total 16",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 1 100.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 8│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 24│roots 12|",
"|edges 0│edges 4|",
"|total 24│total 16|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 1 100.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );
}

/**
Expand All @@ -300,11 +318,14 @@ void plotRange() {
{
InheritanceHealth fit = new InheritanceHealth( 4, 4, 5, Assertions::assertEquals );
fit.expect( mdl,
"Actual | Optimal ",
"roots 24 | roots 12",
"edges 0 | edges 4",
"total 24 | total 16",
" 0 0.00% | 1 100.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 8│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 24│roots 12|",
"|edges 0│edges 4|",
"|total 24│total 16|",
"| 0 0.00%│ 1 100.00%|",
"└─────────────────┴─────────────────┘" );
}
{
InheritanceHealth low = new InheritanceHealth( 3, 3, 5, Assertions::assertEquals );
Expand Down Expand Up @@ -362,26 +383,29 @@ void customCost() {
value.applyAsInt( p ) - value.applyAsInt( c ) ) ) );

ih.expect( mdl,
"Actual | Optimal ",
"roots 1 | roots 1",
"edges 39 | edges 15",
"total 40 | total 16",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 1 25.00%",
" 0 0.00% | 1 25.00%",
" 0 0.00% | 0 0.00%",
" 1 25.00% | 1 25.00%",
" 0 0.00% | 0 0.00%",
" 1 25.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 1 25.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 0 0.00% | 0 0.00%",
" 1 25.00% | 0 0.00%",
" 1 25.00% | 0 0.00%" );
"┌───────────────────────────────────┐",
"│Total Debt : 24│",
"├─────Actual──────┬─────Optimal─────┤",
"|roots 1│roots 1|",
"|edges 39│edges 15|",
"|total 40│total 16|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 1 25.00%|",
"| 0 0.00%│ 1 25.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 1 25.00%│ 1 25.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 1 25.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 1 25.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 0 0.00%│ 0 0.00%|",
"| 1 25.00%│ 0 0.00%|",
"| 1 25.00%│ 0 0.00%|",
"└─────────────────┴─────────────────┘" );
}

private static final Pattern CHILD = Pattern.compile( "(.*)>(.*)" );
Expand Down

0 comments on commit 61c9d0d

Please sign in to comment.