Result.cc revision 10447:a465576671d4
1#include "util/Result.h"
2
3#include <iostream>
4
5#include "libutil/Log.h"
6#include "libutil/Assert.h"
7
8namespace DSENT
9{
10    using std::ostream;
11    using std::endl;
12
13    Result::SubResult::SubResult(const Result* sub_result_, const String& producer_, double num_results_)
14        : m_result_(sub_result_), m_producer_(producer_), m_num_results_(num_results_)
15    {
16        // Check if the result is not a null pointer
17        ASSERT((sub_result_ != NULL), "Internal error: sub_result_ is null");
18
19        // Check if the number of results greater than 0
20        ASSERT((num_results_ >= 0), "Internal error: num_results_ (" + String(num_results_) + ") is less than 0");
21    }
22
23    Result::SubResult::~SubResult()
24    {}
25
26    const Result* Result::SubResult::getResult() const
27    {
28        return m_result_;
29    }
30
31    const String& Result::SubResult::getProducer() const
32    {
33        return m_producer_;
34    }
35
36    double Result::SubResult::getNumResults() const
37    {
38        return m_num_results_;
39    }
40
41    Result::SubResult* Result::SubResult::clone() const
42    {
43        return new SubResult(*this);
44    }
45
46    Result::SubResult::SubResult(const SubResult& sub_result_)
47        : m_result_(sub_result_.m_result_), m_producer_(sub_result_.m_producer_), m_num_results_(sub_result_.m_num_results_)
48    {}
49
50    Result::Result()
51    {}
52
53    Result::Result(const String& result_name_)
54        : m_result_name_(result_name_)
55    {}
56
57    Result::~Result()
58    {
59        // Clear all sub results
60        for(vector<SubResult*>::iterator it = m_sub_results_.begin();
61            it != m_sub_results_.end(); ++it)
62        {
63            SubResult* sub_result = (*it);
64            delete sub_result;
65        }
66    }
67
68    const String& Result::getName() const
69    {
70        return m_result_name_;
71    }
72
73    void Result::setValue(double /* value_ */)
74    {
75        throw LibUtil::Exception("[Error] " + getName() + " -> Cannot set the value of a non-atomic result!");
76        return;
77    }
78
79    void Result::addValue(double /* value_ */)
80    {
81        throw LibUtil::Exception("[Error] " + getName() +
82                " -> Cannot add the value of a non-atomic result");
83        return;
84    }
85
86    double Result::getValue() const
87    {
88        throw LibUtil::Exception("[Error] " + getName() + " -> Cannot get the value of a non-atomic result!");
89        return 0.0;
90    }
91
92    void Result::addSubResult(const Result* sub_result_, const String& result_producer_, double num_results_)
93    {
94        SubResult* new_sub_result = new SubResult(sub_result_, result_producer_, num_results_);
95        m_sub_results_.push_back(new_sub_result);
96        return;
97    }
98
99    void Result::removeAllSubResults()
100    {
101        // Clear all sub results
102        for(vector<SubResult*>::iterator it = m_sub_results_.begin();
103            it != m_sub_results_.end(); ++it)
104        {
105            SubResult* sub_result = (*it);
106            delete sub_result;
107        }
108        m_sub_results_.clear();
109        return;
110    }
111
112    double Result::calculateSum() const
113    {
114        double sum = 0.0;
115
116        // Loop through all sub results and calculate the sum
117        for(vector<SubResult*>::const_iterator it = m_sub_results_.begin();
118            it != m_sub_results_.end(); ++it)
119        {
120            const SubResult* temp_sub_result = (*it);
121            const Result* temp_result = temp_sub_result->getResult();
122            double num_results = temp_sub_result->getNumResults();
123            sum += temp_result->calculateSum()*num_results;
124        }
125        return sum;
126    }
127
128    void Result::print(const String& prepend_str_, int detail_level_, ostream& ost_) const
129    {
130        print(prepend_str_, 1.0, detail_level_, ost_);
131        return;
132    }
133
134    Result* Result::clone() const
135    {
136        return new Result(*this);
137    }
138
139    Result::Result(const Result& result_)
140    {
141        // Copy the result name
142        m_result_name_ = result_.m_result_name_;
143
144        // Clone all sub results
145        for(vector<SubResult*>::const_iterator it = m_sub_results_.begin();
146            it != m_sub_results_.end(); ++it)
147        {
148            const SubResult* temp_sub_result = (*it);
149            SubResult* new_sub_result = temp_sub_result->clone();
150            m_sub_results_.push_back(new_sub_result);
151        }
152    }
153
154    void Result::print(const String& prepend_str_, double num_results_, int detail_level_, ostream& ost_) const
155    {
156        // Go down to lower level if detail_level_ > 0, else print the sthe sthe sthe sum
157        if(detail_level_ > 0)
158        {
159            for(vector<SubResult*>::const_iterator it = m_sub_results_.begin();
160                    it != m_sub_results_.end(); ++it)
161            {
162                const SubResult* temp_sub_result = (*it);
163                const Result* temp_result = temp_sub_result->getResult();
164                const String& temp_producer = temp_sub_result->getProducer();
165                const String& temp_result_name = temp_result->getName();
166                double temp_num_results = temp_sub_result->getNumResults();
167                String temp_prepend_str = prepend_str_ + "->" + temp_producer;
168
169                if(!temp_result_name.empty())
170                {
171                    temp_prepend_str += ":" + temp_result_name;
172                }
173                temp_result->print(temp_prepend_str, num_results_*temp_num_results, detail_level_ - 1, ost_);
174            }
175        }
176        else
177        {
178            ost_ << prepend_str_ << " = " << calculateSum()*num_results_;
179            ost_ << " (" << calculateSum() << " * " << num_results_ << ")" << endl;
180        }
181        return;
182    }
183
184    void Result::printHierarchy(const String& prepend_str_, int detail_level_, ostream& ost_) const
185    {
186        if(detail_level_ > 0)
187        {
188            for(vector<SubResult*>::const_iterator it = m_sub_results_.begin(); it != m_sub_results_.end(); ++it)
189            {
190                const SubResult* temp_sub_result = (*it);
191                const Result* temp_result = temp_sub_result->getResult();
192                const String& temp_producer = temp_sub_result->getProducer();
193                const String& temp_result_name = temp_result->getName();
194                String temp_prepend_str = prepend_str_ + "    ";
195
196                ost_ << prepend_str_ << " |--" << temp_producer << "->" << temp_result_name << endl;
197
198                temp_result->printHierarchy(temp_prepend_str, detail_level_ - 1, ost_);
199            }
200        }
201        return;
202    }
203
204    AtomicResult::AtomicResult(const String& result_name_, double value_)
205        : Result(result_name_), m_value_(value_)
206    {}
207
208    AtomicResult::~AtomicResult()
209    {}
210
211    void AtomicResult::setValue(double value_)
212    {
213        m_value_ = value_;
214        return;
215    }
216
217    void AtomicResult::addValue(double value_)
218    {
219        m_value_ += value_;
220        return;
221    }
222
223    double AtomicResult::getValue() const
224    {
225        return m_value_;
226    }
227
228    double AtomicResult::calculateSum() const
229    {
230        return m_value_;
231    }
232
233    AtomicResult* AtomicResult::clone() const
234    {
235        return new AtomicResult(*this);
236    }
237
238    AtomicResult::AtomicResult(const AtomicResult& atomic_result_)
239        : Result(atomic_result_), m_value_(atomic_result_.m_value_)
240    {}
241
242    void AtomicResult::print(const String& prepend_str_, double num_results_, int /* detail_level_ */, ostream& ost_) const
243    {
244        ost_ << prepend_str_ << " = " << m_value_*num_results_;
245        ost_ << " (" << m_value_ << " * " << num_results_ << ")" << endl;
246        return;
247    }
248} // namespace DSENT
249
250