private Comparator < VarBindingDef > byUsage ( final VarTupleSet varTupleSet ) { return new Comparator < VarBindingDef > ( ) { public int compare ( VarBindingDef binding1 , VarBindingDef binding2 ) { // Compare by usage score: higher score is preferred. int resultScore = getScore ( binding2 ) . compareTo ( getScore ( binding1 ) ) ; return // If equal usage score... resultScore == 0 // ...then compare lexigraphically ? varBindingDefSorter_ . compare ( binding1 , binding2 ) : resultScore ; } private Integer getScore ( VarBindingDef binding ) { Integer score = bindingScores_ . get ( binding ) ; if ( score == null ) { // Preferred higher "unused-ness" and lower "used-ness" (especially among once-only tuples) int maxScore = 1000 ; int unusedScore = ( int ) ( varTupleSet . getUnusedScore ( binding ) * maxScore ) ; int usedScore = ( int ) ( ( 1.0 - varTupleSet . getUsedScore ( binding ) ) * ( maxScore - 1 ) ) ; int usedOnceScore = ( int ) ( ( 1.0 - varTupleSet . getUsedOnceScore ( binding ) ) * ( maxScore - 1 ) ) ; score = ( ( unusedScore * maxScore ) + usedOnceScore ) * maxScore + usedScore ; bindingScores_ . put ( binding , score ) ; } return score ; } private Map < VarBindingDef , Integer > bindingScores_ = new HashMap < VarBindingDef , Integer > ( ) ; } ; }
Returns a comparator that orders bindings by decreasing preference preferring bindings that are less used .