COPASI API  4.16.103
CListOfLayouts.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 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) 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 #define USE_LAYOUT 1
18 #define USE_RENDER 1
19 
20 #include <sbml/packages/render/extension/RenderListOfLayoutsPlugin.h>
21 #include <sbml/packages/render/sbml/GlobalRenderInformation.h>
22 
23 #include <sbml/ListOf.h>
24 #include <sbml/packages/layout/extension/LayoutExtension.h>
25 #include <sbml/packages/layout/sbml/Layout.h>
26 
27 #include "CListOfLayouts.h"
28 #include "report/CKeyFactory.h"
30 #include "SBMLDocumentLoader.h"
31 
32 // the following is taken from libsbml 5.5 if a lower version is used then
33 // these defines will become active
34 #ifndef EXTENSION_CREATE_NS
35 #define EXTENSION_CREATE_NS(type,variable,sbmlns)\
36  type* variable;\
37  {\
38  XMLNamespaces* xmlns = sbmlns->getNamespaces();\
39  variable = dynamic_cast<type*>(sbmlns);\
40  if (variable == NULL)\
41  {\
42  variable = new type(sbmlns->getLevel(), sbmlns->getVersion());\
43  for (int i = 0; i < xmlns->getNumNamespaces(); i++)\
44  {\
45  if (!variable->getNamespaces()->hasURI(xmlns->getURI(i)))\
46  variable->getNamespaces()->add(xmlns->getURI(i), xmlns->getPrefix(i));\
47  }\
48  }\
49  }
50 #endif
51 
52 #ifndef LAYOUT_CREATE_NS
53 #define LAYOUT_CREATE_NS(variable,sbmlns)\
54  EXTENSION_CREATE_NS(LayoutPkgNamespaces,variable,sbmlns);
55 #endif
56 
57 CListOfLayouts::CListOfLayouts(const std::string & name,
58  const CCopasiContainer * pParent):
59  CCopasiVector<CLayout>(name, pParent),
60  mKey(CCopasiRootContainer::getKeyFactory()->add("Layout", this))
61  , mvGlobalRenderInformationObjects("ListOfGlobalRenderInformationObjects", this)
62 {}
63 
65 {
67 }
68 
69 const std::string& CListOfLayouts::getKey()
70 {
71  return mKey;
72 }
73 
74 void CListOfLayouts::addLayout(CLayout * layout, const std::map<std::string, std::string> & /* m */)
75 {
76  if (layout)
77  add(layout, true);
78 
79  //TODO: store map
80 }
81 
82 void CListOfLayouts::exportToSBML(ListOf * lol, std::map<const CCopasiObject*, SBase*> & copasimodelmap,
83  const std::map<std::string, const SBase*>& idMap, unsigned int level, unsigned int version) const
84 {
85  if (!lol) return;
86 
87  size_t i, imax;
88  // the global render information has to be handled first, because we might need
89  // some of the maps for the export of the local render information in the layout
90  ListOfLayouts* pLoL = dynamic_cast<ListOfLayouts*>(lol);
91  assert(pLoL != NULL);
93  GlobalRenderInformation* pGRI = NULL;
94  std::map<std::string, std::string> keyToIdMap;
95 
96  /*
97  std::map<std::string,std::string> colorKeyToIdMap;
98  std::map<std::string,std::string> gradientKeyToIdMap;
99  std::map<std::string,std::string> lineEndingKeyToIdMap;
100  std::map<std::string,std::map<std::string,std::string> > colorKeyToIdMapMap;
101  std::map<std::string,std::map<std::string,std::string> > gradientKeyToIdMapMap;
102  std::map<std::string,std::map<std::string,std::string> > lineEndingKeyToIdMapMap;
103  */
104 
105  RenderListOfLayoutsPlugin* rlolPlugin = (RenderListOfLayoutsPlugin*) lol->getPlugin("render");
106 
107  if (rlolPlugin == NULL)
108  {
109  // we do not yet have a render layout plugin so let us enable it
110  const std::string uri = (lol->getLevel() < 3 ? RenderExtension::getXmlnsL2() : RenderExtension::getXmlnsL3V1V1());
111  lol->enablePackage(uri, "render", true);
112  rlolPlugin = (RenderListOfLayoutsPlugin*) lol->getPlugin("render");
113 
114  if (lol->getLevel() > 2)
115  lol->getSBMLDocument()->setPackageRequired("render", false);
116  }
117 
118  assert(rlolPlugin != NULL);
119 
120  for (i = 0; i < imax; ++i)
121  {
122  //colorKeyToIdMap.clear();
123  //gradientKeyToIdMap.clear();
124  //lineEndingKeyToIdMap.clear();
125  //pGRI=this->mvGlobalRenderInformationObjects[i]->toSBML(colorKeyToIdMap,gradientKeyToIdMap,lineEndingKeyToIdMap);
126  pGRI = rlolPlugin->createGlobalRenderInformation();
127  this->mvGlobalRenderInformationObjects[i]->toSBML(pGRI, pLoL->getLevel(), pLoL->getVersion());
128  // add the id and key to the map
129  assert(pGRI != NULL);
130  keyToIdMap.insert(std::pair<std::string, std::string>(this->mvGlobalRenderInformationObjects[i]->getKey(), pGRI->getId()));
131  //colorKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pGRI->getId(),colorKeyToIdMap));
132  //gradientKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pGRI->getId(),gradientKeyToIdMap));
133  //lineEndingKeyToIdMapMap.insert(std::pair<std::string,std::map<std::string,std::string> >(pGRI->getId(),lineEndingKeyToIdMap));
134  }
135 
136  // fix the references
137  // we need to pass the ListOfGlobalRenderInformation objects as the first argument
138  SBMLDocumentLoader::convertRenderInformationReferencesKeys<GlobalRenderInformation>(*(rlolPlugin->getListOfGlobalRenderInformation()), keyToIdMap);
139  // fix the color ids, gradient ids and line ending ids.
140 
141  // we will generate sbml ids that are unique within the sbml file (although
142  // this may not be strictly necessary for the layouts). Therefore we will keep only
143  // one set of IDs:
144  std::map<std::string, const SBase*> sbmlIDs = idMap;
145 
146  //this will contain the SBML objects that were touched by this method.
147  std::set<SBase*> writtenToSBML;
148 
149  //some of the following code is currently useless: Layouts are never part of
150  //the copasimodelmap.
151 
152  //write all COPASI object to corresponding libsbml objects
153  imax = this->size();
154 
155  for (i = 0; i < imax; ++i)
156  {
157  CLayout * tmp = (*this)[i];
158 
159  //check if the layout exists in the libsbml data
160  std::map<const CCopasiObject*, SBase*>::const_iterator it;
161  it = copasimodelmap.find(tmp);
162 
163  Layout * pLayout;
164 
165  if (it == copasimodelmap.end()) //not found
166  {
167  //create new object and add to libsbml data structures
168  // the layout needs to be created with the correct level and version
169  // otherwise the render infromation is not correctly exported
170  // because newer version is libsbml set the level to 3 per default
171  //pLayout = new Layout(level, version);
172 
173  // the issue with the above is that the render package is not automatically
174  // instantiated (we really ought to simply call ->createLayout on the plugin object)
175  // until then we simply take all the namespaces from the parent element with us
176  LAYOUT_CREATE_NS(layoutns, lol->getSBMLNamespaces());
177  pLayout = new Layout(layoutns);
178 
179  lol->appendAndOwn(pLayout);
180 
181  //add object to map
182  //copasimodelmap[tmp] = pLayout; should not really be done in export
183  }
184  else
185  {
186  pLayout = dynamic_cast<Layout*>(it->second);
187  }
188 
189  //tmp->exportToSBML(pLayout, copasimodelmap, sbmlIDs,keyToIdMap,colorKeyToIdMapMap,gradientKeyToIdMapMap,lineEndingKeyToIdMapMap);
190  tmp->exportToSBML(pLayout, copasimodelmap, sbmlIDs, keyToIdMap);
191  writtenToSBML.insert(pLayout);
192  }
193 
194  //check if a something needs to be deleted from the SBML data structures
195  for (i = lol->size(); i > 0; --i)
196  {
197  SBase* object = lol->get((unsigned int) i - 1);
198 
199  if (writtenToSBML.find(object) == writtenToSBML.end())
200  {
201  lol->remove((unsigned int) i - 1);
202  pdelete(object);
203 
204  //TODO: delete from map
205  //the object and every object it contains need to be removed from the
206  //map.
207  //For now I do not implement this since layout object are not added to the
208  //map in the first place.
209  }
210  }
211 }
212 
214 {
215  if (pRenderInfo)
216  {
217  this->mvGlobalRenderInformationObjects.add(pRenderInfo, true); //true means vector takes ownership
218  }
219 }
220 
221 /**
222  * Returns a pointer to the global render information object with the given index.
223  * If the index is invalid, NULL is returned.
224  */
226 {
227  if (index < this->mvGlobalRenderInformationObjects.size())
228  {
229  return this->mvGlobalRenderInformationObjects[index];
230  }
231 
232  return NULL;
233 }
234 
235 /**
236  * Returns a const pointer to the global render information object with the given index.
237  * If the index is invalid, NULL is returned.
238  */
240 {
241  if (index < this->mvGlobalRenderInformationObjects.size())
242  {
243  return this->mvGlobalRenderInformationObjects[index];
244  }
245 
246  return NULL;
247 }
bool remove(const std::string &key)
std::string mKey
#define pdelete(p)
Definition: copasi.h:215
void exportToSBML(ListOf *lol, std::map< const CCopasiObject *, SBase * > &copasimodelmap, const std::map< std::string, const SBase * > &idMap, unsigned int level, unsigned int version) const
virtual size_t size() const
void addGlobalRenderInformation(CLGlobalRenderInformation *pRenderInfo)
virtual bool add(const CType &src)
CCopasiVector< CLGlobalRenderInformation > mvGlobalRenderInformationObjects
CListOfLayouts(const std::string &name="ListOfLayouts", const CCopasiContainer *pParent=NULL)
void addLayout(CLayout *layout, const std::map< std::string, std::string > &m)
#define LAYOUT_CREATE_NS(variable, sbmlns)
static CKeyFactory * getKeyFactory()
CLGlobalRenderInformation * getRenderInformation(size_t index)
const std::string & getKey()
void exportToSBML(Layout *layout, const std::map< const CCopasiObject *, SBase * > &copasimodelmap, std::map< std::string, const SBase * > &sbmlIDs, const std::map< std::string, std::string > &globalKeyToIdMap) const
Definition: CLayout.cpp:392