// transpose values in fields by natural sort order
pipe = new Each( pipe, new SortElements( new Fields( "name1", "rate1" ), new Fields( "name2", "rate2" ) ) );
// unique the pipe
pipe = new GroupBy( pipe, Fields.ALL );
pipe = new Every( pipe, Fields.ALL, new First(), Fields.RESULTS );
// calculate square of diff
Function sqDiff = new Identity( new Fields( "score" ) )
{
public void operate( FlowProcess flowProcess, FunctionCall functionCall )
{
TupleEntry input = functionCall.getArguments();
functionCall.getOutputCollector().add( new Tuple( Math.pow( input.getTuple().getDouble( 0 ) - input.getTuple().getDouble( 1 ), 2 ) ) );
}
};
// out: movie, name1, rate1, name2, rate2, score
pipe = new Each( pipe, new Fields( "rate1", "rate2" ), sqDiff, Fields.ALL );
// sum and sqr for each name pair
pipe = new GroupBy( pipe, new Fields( "name1", "name2" ) );
Sum distance = new Sum( new Fields( "distance" ) )
{
public void complete( FlowProcess flowProcess, AggregatorCall aggregatorCall )
{
Tuple tuple = super.getResult( aggregatorCall );
aggregatorCall.getOutputCollector().add( new Tuple( 1 / ( 1 + tuple.getDouble( 0 ) ) ) );
}
};
pipe = new Every( pipe, new Fields( "score" ), distance, new Fields( "name1", "name2", "distance" ) );
Flow flow = getPlatform().getFlowConnector().connect( source, sink, pipe );
flow.complete();