COPASI API  4.16.103
COutputHandler.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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) 2004 - 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 "COutputHandler.h"
18 #include "CCopasiTask.h"
19 
20 #include "report/CCopasiTimer.h"
23 #include "model/CModel.h"
24 #include "trajectory/CTimeSeries.h"
25 
28  mInterfaces(),
29  mpMaster(NULL),
30  mObjectRefreshes()
31 {}
32 
34  COutputInterface(src),
35  mInterfaces(src.mInterfaces),
36  mpMaster(src.mpMaster),
37  mObjectRefreshes(src.mObjectRefreshes)
38 {}
39 
41 
42 std::set<COutputInterface *> COutputHandler::getInterfaces() const
43 {
44  return mInterfaces;
45 }
46 
47 bool COutputHandler::compile(std::vector< CCopasiContainer * > listOfContainer, const CCopasiDataModel* pDataModel)
48 {
49  bool success = true;
50  mObjects.clear();
51 
52  std::set< COutputInterface *>::iterator it = mInterfaces.begin();
53  std::set< COutputInterface *>::iterator end = mInterfaces.end();
54 
55  std::set< const CCopasiObject * >::const_iterator itObj;
56  std::set< const CCopasiObject * >::const_iterator endObj;
57 
58  for (; it != end; ++it)
59  {
60  success &= (*it)->compile(listOfContainer, pDataModel);
61 
62  // Assure that this is the only one master.
63  COutputHandler * pHandler = dynamic_cast< COutputHandler * >(*it);
64 
65  if (pHandler != NULL) pHandler->setMaster(this);
66 
67  // Collect the list of objects
68  const std::set< const CCopasiObject * > & Objects = (*it)->getObjects();
69 
70  for (itObj = Objects.begin(), endObj = Objects.end(); itObj != endObj; ++itObj)
71  mObjects.insert(*itObj);
72  }
73 
74  if (mpMaster == NULL)
75  success &= compileRefresh(listOfContainer, pDataModel);
76 
77  return success;
78 }
79 
80 void COutputHandler::output(const Activity & activity)
81 {
82  if (mpMaster == NULL)
83  refresh();
84 
85  std::set< COutputInterface *>::iterator it = mInterfaces.begin();
86  std::set< COutputInterface *>::iterator end = mInterfaces.end();
87 
88  for (; it != end; ++it)
89  (*it)->output(activity);
90 
91  return;
92 }
93 
94 void COutputHandler::separate(const Activity & activity)
95 {
96  std::set< COutputInterface *>::iterator it = mInterfaces.begin();
97  std::set< COutputInterface *>::iterator end = mInterfaces.end();
98 
99  for (; it != end; ++it)
100  (*it)->separate(activity);
101 
102  return;
103 }
104 
106 {
107  std::set< COutputInterface *>::iterator it = mInterfaces.begin();
108  std::set< COutputInterface *>::iterator end = mInterfaces.end();
109 
110  // This hack is necessary as the reverse iterator behaves strangely
111  // under Visual C++ 6.0, i.e., removing an object advances the iterator.
112  std::vector< COutputInterface * > ToBeRemoved;
113 
114  // Closing a stream is separated from finishing the output
115  // since subtask may still need to finish their output.
116  for (; it != end; ++it)
117  {
118  (*it)->finish();
119  }
120 
121  for (it = mInterfaces.begin(); it != end; ++it)
122  {
123  (*it)->close();
124 
125  // CTimesSeries and CReport are only used once.
126  if (dynamic_cast< CReport * >(*it) != NULL ||
127  dynamic_cast< CTimeSeries * >(*it) != NULL)
128  ToBeRemoved.push_back(*it);
129  }
130 
131  std::vector< COutputInterface * >::iterator itRemove = ToBeRemoved.begin();
132  std::vector< COutputInterface * >::iterator endRemove = ToBeRemoved.end();
133 
134  for (; itRemove != endRemove; ++itRemove)
135  removeInterface(*itRemove);
136 
137  return;
138 }
139 
141 {
142  mInterfaces.insert(pInterface);
143 
144  // Assure that this is the only one master.
145  COutputHandler * pHandler = dynamic_cast< COutputHandler * >(pInterface);
146 
147  if (pHandler != NULL) pHandler->setMaster(this);
148 }
149 
151 {
152  mInterfaces.erase(pInterface);
153 
154  // Assure that the removed handler is its own master.
155  COutputHandler * pHandler = dynamic_cast< COutputHandler * >(pInterface);
156 
157  if (pHandler != NULL) pHandler->setMaster(NULL);
158 }
159 
161 {mpMaster = pMaster;}
162 
164 {return (mpMaster == NULL);}
165 
167 {
168  std::vector< Refresh * >::iterator it = mObjectRefreshes.begin();
169  std::vector< Refresh * >::iterator end = mObjectRefreshes.end();
170 
171  for (; it != end; ++it)(**it)();
172 }
173 
174 bool COutputHandler::compileRefresh(const std::vector< CCopasiContainer * > & listOfContainer, const CCopasiDataModel* pDataModel)
175 {
176  const CModel * pModel =
177  dynamic_cast<const CModel * >(pDataModel->ObjectFromName(listOfContainer, pDataModel->getModel()->getCN()));
178 
180 
181  std::set< const CCopasiObject * >::const_iterator it = mObjects.begin();
182  std::set< const CCopasiObject * >::const_iterator end = mObjects.end();
183 
184  // Timers are treated differently they are started during compilation.
185  for (; it != end; ++it)
186  if (dynamic_cast< const CCopasiTimer * >(*it))
187  const_cast< CCopasiTimer * >(static_cast< const CCopasiTimer * >(*it))->start();
188 
189  return true;
190 }
virtual void separate(const Activity &activity)
std::vector< Refresh * > mObjectRefreshes
bool isMaster() const
virtual void removeInterface(COutputInterface *pInterface)
virtual CCopasiObjectName getCN() const
const std::set< const CCopasiObject * > & getUptoDateObjects() const
Definition: CModel.cpp:1178
COutputHandler * mpMaster
void setMaster(COutputHandler *pMaster)
virtual void addInterface(COutputInterface *pInterface)
virtual ~COutputHandler()
std::set< COutputInterface * > getInterfaces() const
bool compileRefresh(const std::vector< CCopasiContainer * > &listOfContainer, const CCopasiDataModel *pDataModel)
std::set< const CCopasiObject * > mObjects
virtual bool compile(std::vector< CCopasiContainer * > listOfContainer, const CCopasiDataModel *pDataModel)
virtual void output(const Activity &activity)
std::set< COutputInterface * > mInterfaces
static std::vector< Refresh * > buildUpdateSequence(const DataObjectSet &objects, const DataObjectSet &uptoDateObjects, const DataObjectSet &context=DataObjectSet())
virtual void finish()
Definition: CModel.h:50
CCopasiObject * ObjectFromName(const std::vector< CCopasiContainer * > &listOfContainer, const CCopasiObjectName &CN) const