/*
    TCFbECsvOutputFormatter.cpp    June 6, 2004.

    Copyright (C) 2003-2005 CFbE Research Group,
    Software Engineering Laboratory,
    Graduate School of Information Science,
    Nara Institute of Science and Technology,
    All rights reserved.

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2, (at your option) or
    any later version.

    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GNU Emacs; see the file COPYING.  If not, write to the
    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/
//---------------------------------------------------------------------------
#pragma hdrstop

#include "TCFbECsvOutputFormatter.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
// コンストラクタ（argc と argv によってコマンドラインを与える
__fastcall TCFbECsvOutputFormatter::TCFbECsvOutputFormatter(TCFbEDocfbeCommandLineParser* Parser, TCFbEOutputCache* OutputCache, bool IsVerbose)
    : TCFbEOutputFormatter(Parser, OutputCache, IsVerbose)
{
}

//---------------------------------------------------------------------------
__fastcall TCFbECsvOutputFormatter::~TCFbECsvOutputFormatter()
{
}

//---------------------------------------------------------------------------
// ヘッダ部分を this->FOutputStringList に追加する．ヘッダが必要でない場合はすぐに return する．
void __fastcall TCFbECsvOutputFormatter::OutputHeader(void)
{
    // ラベルを出力
    AnsiString ALine = "Label";
    for (int j = 0; j < this->OutputCache->GetNumberOfColumns(); j++) {
        if (this->OutputCache->IsResultedColumn(j)) {
            ALine += "," + this->OutputCache->GetResultColumnLabel(j);
        }
    }

    // "値の分布" ラベルの出力
    if (this->Parser->DisplayDistribution) {
        ALine += ",Distributions of Similarities";
        for (int k = 1; k < 20; k++) {
            ALine += ",";
        }
    }

    // "Neighborのラベル" ラベルの出力
    if (this->Parser->DisplayNeighbors) {
        ALine += ",Neighbors' Labels";
        for (int k = 1; k < this->Parser->NeighborsSize; k++) {
            ALine += ",";
        }
    }

    // "Neighborの類似度" ラベルの出力
    if (this->Parser->DisplaySimilarities) {
        ALine += ",Neighbors' Similarities";
        for (int k = 1; k < this->Parser->NeighborsSize; k++) {
            ALine += ",";
        }
    }

    // "Neighborの値" ラベルの出力
    if (this->Parser->DisplayValues) {
        ALine += ",Neighbors' Values";
        for (int k = 1; k < this->Parser->NeighborsSize; k++) {
            ALine += ",";
        }
    }

    this->FOutputStringList->Add(ALine);
}

//---------------------------------------------------------------------------
// ボディ部分を this->FOutputStringList に追加する．
void __fastcall TCFbECsvOutputFormatter::OutputBody(int TargetRowIndex)
{
    // 行ラベルの出力
    AnsiString    ALine = this->OutputCache->GetResultRowLabel(TargetRowIndex);

    // 予測結果の値の出力
    for (int j = 0; j < this->OutputCache->GetNumberOfColumns(); j++) {
        if (this->OutputCache->IsResultedColumn(j)) {
            ALine += ",";
        }
        if (this->OutputCache->GetResultEnabled(TargetRowIndex, j)) {
            ALine += FloatToStr(this->OutputCache->GetResultValue(TargetRowIndex, j));
        } else if (this->OutputCache->GetResultFailed(TargetRowIndex, j)) {
            ALine += "FAILED!";
        }
    }

    // 類似度の分布の出力
    if (this->Parser->DisplayDistribution) {
        for (int k = 19; k >= 0; k--) {
            ALine += "," + IntToStr(this->OutputCache->GetDistribution(TargetRowIndex, k));
        }
    }

    // Neighbor のラベルの出力
    if (this->Parser->DisplayNeighbors) {
        for (int k = 0; k < this->OutputCache->GetNumberOfNeighbors(TargetRowIndex); k++) {
            ALine += "," + this->OutputCache->GetNeighbor(TargetRowIndex, k)->GetLabel();
        }
    }

    // Neighbor の類似度 の出力
    if (this->Parser->DisplaySimilarities) {
        for (int k = 0; k < this->OutputCache->GetNumberOfNeighbors(TargetRowIndex); k++) {
            ALine += "," + FloatToStr(this->OutputCache->GetNeighbor(TargetRowIndex, k)->GetSimilarity());
        }
    }

    // Neighbor の値の出力
    if (this->Parser->DisplayValues) {
        for (int k = 0; k < this->OutputCache->GetNumberOfNeighbors(TargetRowIndex); k++) {
            ALine += "," + FloatToStr(this->OutputCache->GetNeighbor(TargetRowIndex, k)->GetSourceValue());
        }
    }

    this->FOutputStringList->Add(ALine);
}

//---------------------------------------------------------------------------

