COPASI API  4.16.103
CReport.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2003 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "copasi.h"
16 
17 #include "CReportDefinition.h"
18 #include "CReport.h"
19 #include "CCopasiContainer.h"
20 #include "CCopasiTimer.h"
21 
25 #include "utilities/CDirEntry.h"
26 #include "utilities/utility.h"
28 
29 //////////////////////////////////////////////////
30 //
31 //class CReport
32 //
33 //////////////////////////////////////////////////
36  mpDataModel(NULL),
37  mpOstream(NULL),
38  mStreamOwner(false),
39  mpReportDef(NULL),
40  mTarget(""),
41  mAppend(true),
42  mConfirmOverwrite(true),
43  mFooterObjectList(),
44  mBodyObjectList(),
45  mHeaderObjectList(),
46  mpHeader(NULL),
47  mpBody(NULL),
48  mpFooter(NULL),
49  mState(Invalid)
50 {}
51 
53  COutputInterface(src),
54  mpDataModel(src.mpDataModel),
55  mpOstream(src.mpOstream),
56  mStreamOwner(false),
57  mpReportDef(src.mpReportDef),
58  mTarget(src.mTarget),
59  mAppend(src.mAppend),
60  mConfirmOverwrite(src.mConfirmOverwrite),
61  mFooterObjectList(src.mFooterObjectList),
62  mBodyObjectList(src.mBodyObjectList),
63  mHeaderObjectList(src.mHeaderObjectList),
64  mpHeader(src.mpHeader),
65  mpBody(src.mpBody),
66  mpFooter(src.mpFooter),
67  mState(Invalid)
68 {}
69 
71 {cleanup();}
72 
74 {
75  mHeaderObjectList.clear();
76  mBodyObjectList.clear();
77  mFooterObjectList.clear();
78 
79  finish();
80  close();
81 }
82 
84 {return mpReportDef;}
85 
87 {mpReportDef = reportDef;}
88 
89 const std::string & CReport::getTarget() const
90 {return mTarget;}
91 
92 void CReport::setTarget(const std::string & target)
93 {mTarget = target;}
94 
95 const bool & CReport::append() const
96 {return mAppend;}
97 
98 void CReport::setAppend(const bool & append)
99 {mAppend = append;}
100 
101 const bool & CReport::confirmOverwrite() const
102 {return mConfirmOverwrite;}
103 
104 void CReport::setConfirmOverwrite(const bool & confirmOverwrite)
106 
107 void CReport::output(const Activity & activity)
108 {
109  switch (activity)
110  {
112  printHeader();
113  break;
114 
116  printBody();
117  break;
118 
120  printFooter();
121  break;
122  }
123 }
124 
125 void CReport::separate(const Activity & /* activity */)
126 {
127  if (!mpOstream) return;
128 
129  (*mpOstream) << std::endl;
130 }
131 
133 {
135 
136  printFooter();
137 
138  pdelete(mpHeader);
139  pdelete(mpBody);
140  pdelete(mpFooter);
141 
142  mState = Invalid;
143 }
144 
146 {
148 
149  mpOstream = NULL;
150  mStreamOwner = false;
151 }
152 
154 {
155  if (!mpOstream) return;
156 
157  if (mpHeader)
158  switch (mState)
159  {
160  case Compiled:
163  return;
164 
165  case HeaderHeader:
166  mpHeader->printBody();
167  mState = HeaderBody;
168  return;
169 
170  case HeaderBody:
171  mpHeader->printBody();
172  return;
173 
174  case HeaderFooter:
176  return;
177 
178  default:
179  return;
180  }
181 
182  if (mState == HeaderFooter) return;
183 
185 
186  std::vector< CCopasiObject * >::iterator it = mHeaderObjectList.begin();
187  std::vector< CCopasiObject * >::iterator end = mHeaderObjectList.end();
188 
189  if (it == end) return;
190 
191  for (; it != end; ++it)(*it)->print(mpOstream);
192 
193  (*mpOstream) << std::endl;
194 }
195 
197 {
198  if (!mpOstream) return;
199 
200  // Close the header part
201  if (mState < HeaderFooter)
202  {
204 
205  if (mpHeader) mpHeader->printFooter();
206  }
207 
208  if (mpBody)
209  switch (mState)
210  {
211  case HeaderFooter:
212  mpBody->printHeader();
213  mState = BodyHeader;
214  return;
215 
216  case BodyHeader:
217  mpBody->printBody();
218  mState = BodyBody;
219  return;
220 
221  case BodyBody:
222  mpBody->printBody();
223  return;
224 
225  case BodyFooter:
226  mpBody->printFooter();
227  return;
228 
229  default:
230  return;
231  }
232 
233  if (mState == BodyFooter) return;
234 
235  mState = BodyBody;
236 
237  std::vector< CCopasiObject * >::iterator it = mBodyObjectList.begin();
238  std::vector< CCopasiObject * >::iterator end = mBodyObjectList.end();
239 
240  if (it == end) return;
241 
242  for (; it != end; ++it)(*it)->print(mpOstream);
243 
244  (*mpOstream) << std::endl;
245 }
246 
248 {
249  if (!mpOstream) return;
250 
251  // Close the body part
252  if (mState < BodyFooter)
253  {
254  mState = BodyFooter;
255 
256  if (mpBody) mpBody->printFooter();
257  }
258 
259  if (mpFooter)
260  switch (mState)
261  {
262  case BodyFooter:
265  return;
266 
267  case FooterHeader:
268  mpFooter->printBody();
269  mState = FooterBody;
270  return;
271 
272  case FooterBody:
273  mpFooter->printBody();
274  return;
275 
276  case FooterFooter:
278  return;
279 
280  default:
281  return;
282  }
283 
284  if (mState != FooterFooter) return;
285 
286  std::vector< CCopasiObject * >::iterator it = mFooterObjectList.begin();
287  std::vector< CCopasiObject * >::iterator end = mFooterObjectList.end();
288 
289  if (it == end) return;
290 
291  for (; it != end; ++it)(*it)->print(mpOstream);
292 
293  (*mpOstream) << std::endl;
294 }
295 
296 // Compile the List of Report Objects;
297 // Support Parellel
298 
299 bool CReport::compile(std::vector< CCopasiContainer * > listOfContainer,
300  const CCopasiDataModel* pDataModel)
301 {
302  assert(mpDataModel == pDataModel);
303 
304  bool success = true;
306 
307  // check if there is a Report Definition Defined
308  if (!mpReportDef) return false;
309 
310  if (mpReportDef->isTable())
311  if (!mpReportDef->preCompileTable(listOfContainer)) success = false;
312 
315 
316  if (mpHeader)
317  success &= compileChildReport(mpHeader, listOfContainer);
318 
321 
322  if (mpBody)
323  success &= compileChildReport(mpBody, listOfContainer);
324 
327 
328  if (mpFooter)
329  success &= compileChildReport(mpFooter, listOfContainer);
330 
331  mState = Compiled;
332 
333  return success;
334 }
335 
336 std::ostream * CReport::open(const CCopasiDataModel * pDataModel,
337  std::ostream * pOstream)
338 {
339  mpDataModel = pDataModel;
340  assert(mpDataModel != NULL);
341 
342  // If an ostream is given and it is the currently assigned one
343  // we do nothing.
344  if (pOstream != NULL &&
345  pOstream == mpOstream)
346  return mpOstream;
347 
348  if (mStreamOwner)
350 
351  mpOstream = pOstream;
352 
353  if (pOstream)
354  {
355  mStreamOwner = false;
356  }
357  else if (mTarget != "" && mpReportDef != NULL)
358  {
362 
363  mpOstream = new std::ofstream;
364  mStreamOwner = true;
365 
366  if (mAppend)
367  {
368  ((std::ofstream *) mpOstream)->
369  open(CLocaleString::fromUtf8(mTarget).c_str(), std::ios_base::out | std::ios_base::app);
370  }
371  else
372  {
373  ((std::ofstream *) mpOstream)->
374  open(CLocaleString::fromUtf8(mTarget).c_str(), std::ios_base::out);
375  }
376 
377  if (!((std::ofstream *) mpOstream)->is_open())
378  {
381  }
382 
383  if (mpOstream)
384  {
385  mpOstream->precision(mpReportDef->getPrecision());
386  }
387  }
388 
389  return mpOstream;
390 }
391 
392 std::ostream * CReport::getStream() const {return mpOstream;}
393 
394 // make to support parallel tasks
395 void CReport::generateObjectsFromName(const std::vector< CCopasiContainer * > * pListOfContainer,
396  std::vector<CCopasiObject*> & objectList,
397  CReport *& pReport,
398  const std::vector<CRegisteredObjectName>* nameVector)
399 {
400  objectList.clear();
401 
402  unsigned C_INT32 i;
403  CCopasiObject * pSelected;
404  CReportDefinition * pReportDefinition;
405 
406  for (i = 0; i < nameVector->size(); i++)
407  {
408  pSelected = mpDataModel->ObjectFromName(*pListOfContainer, (*nameVector)[i]);
409 
410  if (!pSelected)
411  {
412  CCopasiMessage(CCopasiMessage::WARNING, MCCopasiTask + 6, (*nameVector)[i].c_str());
413  continue;
414  }
415 
416  if (!i && (pReportDefinition = dynamic_cast< CReportDefinition * >(pSelected)) != NULL)
417  {
418  pReport = new CReport();
419  pReport->setReportDefinition(pReportDefinition);
420 
421  return;
422  }
423 
424  COutputInterface::mObjects.insert(pSelected);
425  objectList.push_back(pSelected);
426  }
427 }
428 
429 bool CReport::compileChildReport(CReport * pReport, std::vector< CCopasiContainer * > listOfContainer)
430 {
431  pReport->open(mpDataModel, mpOstream);
432  bool success = pReport->compile(listOfContainer, mpDataModel);
433 
434  const std::set< const CCopasiObject * > & Objects = pReport->COutputInterface::getObjects();
435  std::set< const CCopasiObject * >::const_iterator it = Objects.begin();
436  std::set< const CCopasiObject * >::const_iterator end = Objects.end();
437 
438  for (; it != end; ++it)
439  COutputInterface::mObjects.insert(*it);
440 
441  return success;
442 }
#define pdelete(p)
Definition: copasi.h:215
void printFooter()
Definition: CReport.cpp:247
virtual void finish()
Definition: CReport.cpp:132
const bool & confirmOverwrite() const
Definition: CReport.cpp:101
static bool isRelativePath(const std::string &path)
Definition: CDirEntry.cpp:414
virtual bool compile(std::vector< CCopasiContainer * > listOfContainer, const CCopasiDataModel *pDataModel)
Definition: CReport.cpp:299
bool mConfirmOverwrite
Definition: CReport.h:66
State mState
Definition: CReport.h:76
void cleanup()
Definition: CReport.cpp:73
static std::string fileName(const std::string &path)
Definition: CDirEntry.cpp:119
const std::string & getTarget() const
Definition: CReport.cpp:89
std::ostream * mpOstream
Definition: CReport.h:60
void setAppend(const bool &append)
Definition: CReport.cpp:98
void setConfirmOverwrite(const bool &confirmOverwrite)
Definition: CReport.cpp:104
void printBody()
Definition: CReport.cpp:196
#define C_INT32
Definition: copasi.h:90
std::vector< CCopasiObject * > mHeaderObjectList
Definition: CReport.h:70
const unsigned C_INT32 & getPrecision() const
CReport * mpFooter
Definition: CReport.h:74
const std::string & getFileName() const
std::vector< CCopasiObject * > mFooterObjectList
Definition: CReport.h:68
#define MCCopasiTask
const bool & append() const
Definition: CReport.cpp:95
std::string mTarget
Definition: CReport.h:64
std::ostream * getStream() const
Definition: CReport.cpp:392
std::vector< CRegisteredObjectName > * getFooterAddr()
bool compileChildReport(CReport *pReport, std::vector< CCopasiContainer * > listOfContainer)
Definition: CReport.cpp:429
std::vector< CCopasiObject * > mBodyObjectList
Definition: CReport.h:69
Header file of class CCopasiContainer.
#define MCDirEntry
std::set< const CCopasiObject * > mObjects
void setTarget(const std::string &target)
Definition: CReport.cpp:92
CReportDefinition * mpReportDef
Definition: CReport.h:63
bool mStreamOwner
Definition: CReport.h:61
~CReport()
Definition: CReport.cpp:70
std::vector< CRegisteredObjectName > * getBodyAddr()
CReport()
Definition: CReport.cpp:34
std::ostream * open(const CCopasiDataModel *pDataModel, std::ostream *pOstream=NULL)
Definition: CReport.cpp:336
CReport * mpBody
Definition: CReport.h:73
virtual void close()
Definition: CReport.cpp:145
static CLocaleString fromUtf8(const std::string &utf8)
CReport * mpHeader
Definition: CReport.h:72
const CCopasiDataModel * mpDataModel
Definition: CReport.h:59
CReportDefinition * getReportDefinition()
Definition: CReport.cpp:83
static bool makePathAbsolute(std::string &relativePath, const std::string &absoluteTo)
Definition: CDirEntry.cpp:481
void setReportDefinition(CReportDefinition *reportDef)
Definition: CReport.cpp:86
bool mAppend
Definition: CReport.h:65
void printHeader()
Definition: CReport.cpp:153
CCopasiObject * ObjectFromName(const std::vector< CCopasiContainer * > &listOfContainer, const CCopasiObjectName &CN) const
std::vector< CRegisteredObjectName > * getHeaderAddr()
virtual void separate(const Activity &)
Definition: CReport.cpp:125
bool preCompileTable(const std::vector< CCopasiContainer * > &listOfContainer=CCopasiContainer::EmptyList)
virtual void output(const Activity &activity)
Definition: CReport.cpp:107
void generateObjectsFromName(const std::vector< CCopasiContainer * > *pListOfContainer, std::vector< CCopasiObject * > &objectList, CReport *&pReport, const std::vector< CRegisteredObjectName > *nameVector)
Definition: CReport.cpp:395