<commit_before>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* 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 of the License, or (at your option)   *
* 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 this program; if not, write to the Free Software Foundation, Inc., 51  *
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
*******************************************************************************
*                            SOFA :: Applications                             *
*                                                                             *
* Authors: <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>,*
* <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>,  *
* <NAME>, <NAME>, J-P. de la Plata Alcade, <NAME> and <NAME>          *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
#include "GraphVisitor.h"

#include <sstream>

#include <tinyxml.cpp>
#include <tinyxmlerror.cpp>
#include <tinystr.cpp>
#include <tinyxmlparser.cpp>

#include <algorithm>

#include <sofa/helper/system/thread/CTime.h>

#ifndef SOFA_QT4
#include <qsplitter.h>
#include <qvaluelist.h>
typedef QListViewItem Q3ListViewItem;
#else
#include <QSplitter>
#include <QList>
#endif
namespace sofa
{

namespace gui
{

namespace qt
{

typedef sofa::helper::system::thread::CTime CTime;

bool cmpTime(const dataTime &a, const dataTime &b) { return a.time > b.time;};
bool GraphVisitor::load(std::string &file)
{
    //Open it using TinyXML
    TiXmlDocument doc;
    doc.Parse(file.c_str());

//         std::cerr << "GRAPH:"<< std::endl << file << std::endl;

    TiXmlHandle hDoc(&doc);
    TiXmlNode* pElem;
    //Getting the root of the file
    pElem=hDoc.FirstChildElement().Element();

    // should always have a valid root but handle gracefully if it does
    if (!pElem) return false;

    totalTime = getTotalTime(pElem);

    componentsTime.clear();
    visitorsTime.clear();


    openNode( pElem, NULL, NULL);


    std::sort(componentsTime.begin(),componentsTime.end(),cmpTime);
    std::sort(visitorsTime.begin(),visitorsTime.end(),cmpTime);

    std::sort(componentsTimeTotal.begin(),componentsTimeTotal.end(),cmpTime);
    std::sort(visitorsTimeTotal.begin(),visitorsTimeTotal.end(),cmpTime);


    if (totalTimeMax<totalTime)
    {
        totalTimeMax=totalTime;
        componentsTimeMax=componentsTime;
        visitorsTimeMax=visitorsTime;
    }

    //        window->pieChart->setChart(visitorsTime, visitorsTime.size());
    window->setCharts(componentsTime,componentsTimeMax,componentsTimeTotal,
            visitorsTime,visitorsTimeMax,visitorsTimeTotal);

    if (!initSize)
    {
        const int sizeLeft = window->graphView->columnWidth(0)+window->graphView->columnWidth(1)+7;
#ifdef SOFA_QT4
        QList< int > listSize;
#else
        QValueList< int > listSize;
#endif
        listSize << sizeLeft
                << window->statsWidget->width()-(sizeLeft-window->graphView->width());
        window->splitterStats->setSizes(listSize);
        initSize=true;
    }
    return true;
}


void GraphVisitor::openAttribute      ( TiXmlElement* element, Q3ListViewItem* item)
{
    if (!element) return;
    TiXmlAttribute* attribute=element->FirstAttribute();
    while (attribute)
    {
        std::string nameOfAttribute(attribute->Name());
        std::string valueOfAttribute(attribute->Value());

        addInformation(item, nameOfAttribute, valueOfAttribute);
        attribute=attribute->Next();
    }

}



void GraphVisitor::openTime      ( TiXmlNode* node, Q3ListViewItem* item)
{
    TiXmlElement* element=node->ToElement();
    if (!element) return;
    TiXmlAttribute* attribute=element->FirstAttribute();
    double timeSec= getTime(attribute);
    double time = 100.0*timeSec/totalTime;
    std::ostringstream s;
    s.setf(std::ios::fixed, std::ios::floatfield);
    s.precision(3);

    s << time << "%";

    TiXmlNode* parent = node->Parent();
    if (parent)
    {
        std::string nodeType = parent->Value();
        if (nodeType == "Component")
        {
            TiXmlAttribute* attribute=parent->ToElement()->FirstAttribute();
            std::string componentName, componentType, componentPtr;
            while (attribute)
            {
                std::string nameOfAttribute(attribute->Name());
                std::string valueOfAttribute(attribute->Value());
                if (nameOfAttribute=="name")
                    componentName=valueOfAttribute;
                else if (nameOfAttribute=="type")
                    componentType=valueOfAttribute;
                else if (nameOfAttribute=="ptr")
                    componentPtr=valueOfAttribute;
                attribute=attribute->Next();
            }
            if (std::find(visitedNode.begin(), visitedNode.end(), componentName) == visitedNode.end())
            {
                dataTime t(timeSec-timeComponentsBelow.back()
                        , componentType, componentName, componentPtr);
                std::vector< dataTime >::iterator it=std::find(componentsTime.begin(),componentsTime.end(),t);
                if (it != componentsTime.end()) it->time += t.time;
                else componentsTime.push_back(t);


                it=std::find(componentsTimeTotal.begin(),componentsTimeTotal.end(),t);
                if (it != componentsTimeTotal.end()) it->time += t.time;
                else componentsTimeTotal.push_back(t);


                visitedNode.push_back(componentName);
            }
        }
        else
        {
            if (nodeType != "Node" && std::find(visitedNode.begin(), visitedNode.end(),nodeType) == visitedNode.end())
            {
                dataTime t(timeSec, nodeType);
                std::vector< dataTime >::iterator it=std::find(visitorsTime.begin(),visitorsTime.end(),t);
                if (it != visitorsTime.end()) it->time += timeSec;
                else visitorsTime.push_back(t);

                it=std::find(visitorsTimeTotal.begin(),visitorsTimeTotal.end(),t);<commit_msg>r5567/sofa-dev : FIX: minor change in the profiler<commit_after>/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4      *
*                (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS                    *
*                                                                             *
* 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 of the License, or (at your option)   *
* 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 this program; if not, write to the Free Software Foundation, Inc., 51  *
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
*******************************************************************************
*                            SOFA :: Applications                             *
*                                                                             *
* Authors: <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>,*
* <NAME>, <NAME>, <NAME>, <NAME>, <NAME>, <NAME>,  *
* <NAME>, <NAME>, <NAME>, <NAME> and <NAME>          *
*                                                                             *
* Contact information: <EMAIL>                             *
******************************************************************************/
#include "GraphVisitor.h"

#include <sstream>

#include <tinyxml.cpp>
#include <tinyxmlerror.cpp>
#include <tinystr.cpp>
#include <tinyxmlparser.cpp>

#include <algorithm>

#include <sofa/helper/system/thread/CTime.h>

#ifndef SOFA_QT4
#include <qsplitter.h>
#include <qvaluelist.h>
typedef QListViewItem Q3ListViewItem;
#else
#include <QSplitter>
#include <QList>
#endif
namespace sofa
{

namespace gui
{

namespace qt
{

typedef sofa::helper::system::thread::CTime CTime;

bool cmpTime(const dataTime &a, const dataTime &b) { return a.time > b.time;};
bool GraphVisitor::load(std::string &file)
{
    //Open it using TinyXML
    TiXmlDocument doc;
    doc.Parse(file.c_str());

//         std::cerr << "GRAPH:"<< std::endl << file << std::endl;

    TiXmlHandle hDoc(&doc);
    TiXmlNode* pElem;
    //Getting the root of the file
    pElem=hDoc.FirstChildElement().Element();

    // should always have a valid root but handle gracefully if it does
    if (!pElem) return false;

    totalTime = getTotalTime(pElem);

    componentsTime.clear();
    visitorsTime.clear();


    openNode( pElem, NULL, NULL);


    std::sort(componentsTime.begin(),componentsTime.end(),cmpTime);
    std::sort(visitorsTime.begin(),visitorsTime.end(),cmpTime);

    std::sort(componentsTimeTotal.begin(),componentsTimeTotal.end(),cmpTime);
    std::sort(visitorsTimeTotal.begin(),visitorsTimeTotal.end(),cmpTime);


    if (totalTimeMax<totalTime)
    {
        totalTimeMax=totalTime;
        componentsTimeMax=componentsTime;
        visitorsTimeMax=visitorsTime;
    }

    //        window->pieChart->setChart(visitorsTime, visitorsTime.size());
    window->setCharts(componentsTime,componentsTimeMax,componentsTimeTotal,
            visitorsTime,visitorsTimeMax,visitorsTimeTotal);

    if (!initSize)
    {
        const int sizeLeft = window->graphView->columnWidth(0)+window->graphView->columnWidth(1)+7;
#ifdef SOFA_QT4
        QList< int > listSize;
#else
        QValueList< int > listSize;
#endif
        listSize << sizeLeft
                << window->statsWidget->width()-(sizeLeft-window->graphView->width());
        window->splitterStats->setSizes(listSize);
        initSize=true;
    }
    return true;
}


void GraphVisitor::openAttribute      ( TiXmlElement* element, Q3ListViewItem* item)
{
    if (!element) return;
    TiXmlAttribute* attribute=element->FirstAttribute();
    while (attribute)
    {
        std::string nameOfAttribute(attribute->Name());
        std::string valueOfAttribute(attribute->Value());

        addInformation(item, nameOfAttribute, valueOfAttribute);
        attribute=attribute->Next();
    }

}



void GraphVisitor::openTime      ( TiXmlNode* node, Q3ListViewItem* item)
{
    TiXmlElement* element=node->ToElement();
    if (!element) return;
    TiXmlAttribute* attribute=element->FirstAttribute();
    double timeSec= getTime(attribute);
    double time = 100.0*timeSec/totalTime;
    std::ostringstream s;
    s.setf(std::ios::fixed, std::ios::floatfield);
    s.precision(3);

    s << time << "%";

    TiXmlNode* parent = node->Parent();
    if (parent)
    {
        std::string nodeType = parent->Value();
        if (nodeType == "Component")
        {
            TiXmlAttribute* attribute=parent->ToElement()->FirstAttribute();
            std::string componentName, componentType, componentPtr;
            while (attribute)
            {
                std::string nameOfAttribute(attribute->Name());
                std::string valueOfAttribute(attribute->Value());
                if (nameOfAttribute=="name")
                    componentName=valueOfAttribute;
                else if (nameOfAttribute=="type")
                    componentType=valueOfAttribute;
                else if (nameOfAttribute=="ptr")
                    componentPtr=valueOfAttribute;
                attribute=attribute->Next();
            }
            if (std::find(visitedNode.begin(), visitedNode.end(), componentName) == visitedNode.end())
            {
                dataTime t(timeSec-timeComponentsBelow.back()
                        , componentType, componentName, componentPtr);
                std::vector< dataTime >::iterator it=std::find(componentsTime.begin(),componentsTime.end(),t);
                if (it != componentsTime.end()) it->time += t.time;
                else componentsTime.push_back(t);


                it=std::find(componentsTimeTotal.begin(),componentsTimeTotal.end(),t);
                if (it != componentsTimeTotal.end()) it->time += t.time;
                else componentsTimeTotal.push_back(t);


                visitedNode.push_back(componentName);
            }
        }
        else
        {
            if (nodeType != "Node" && nodeType != "Input" && nodeType != "Output" && nodeType != "Vector" &&
                std::find(visitedNode.begin(), visitedNode.end(),nodeType) == visitedNode.end())
            {
                dataTime t(timeSec, nodeType);
                std::vector< dataTime >::iterator it=std::find(visitorsTime.begin(),visitorsTime.end(),t);
                if (it != visitorsTime.end()) it->time += timeSec;
                else visitorsTime.push_back(t);

                it=std::find(visitorsTimeTotal.begin(),visitorsTimeTotal.end(),t);
{'fraction_non_alphanumeric': 0.148975791433892, 'fraction_numerical': 0.005241740809711014, 'mean_word_length': 2.2108060230292295, 'pattern_counts': {'":': 0, '<': 102, '<?xml version=': 0, '>': 128, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 38, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3300461', 'n_tokens_mistral': 3854, 'n_tokens_neox': 3648, 'n_words': 861}