NeuroML C++ API  2.3.0
C++ API for NeuroML 2
NeuroML_custom.cxx
1 // Copyright 2017 University College London.
2 // See Licence.txt for details.
3 
4 #include "neuroml.hxx"
5 #include "internal.hxx"
6 
7 #include <cassert>
8 #include <cmath>
9 #include <cstdio>
10 #include <string>
11 #include <ios>
12 #include <iostream>
13 #include <fstream>
14 
15 namespace neuroml2
16 {
17 
18 // Global helper functions
19 
20 std::unique_ptr<NeuroMLDocument> parseFile(const std::string& filePath)
21 {
23  props.schema_location("http://www.neuroml.org/schema/neuroml2", getSchemaPath());
24  std::unique_ptr<NeuroMLDocument> model(neuroml(filePath, 0, props));
25  return model;
26 }
27 
29 {
30  std::string path;
31  if (FILE* fp = fopen(SOURCE_SCHEMA_PATH, "r"))
32  {
33  fclose(fp);
34  path = SOURCE_SCHEMA_PATH;
35  }
36  else if (FILE* fp = fopen(INSTALLED_SCHEMA_PATH, "r"))
37  {
38  fclose(fp);
39  path = INSTALLED_SCHEMA_PATH;
40  }
41  else
42  throw std::ios_base::failure("Unable to find NeuroML schema at " INSTALLED_SCHEMA_PATH);
43  return "file://" + path;
44 }
45 
46 // NeuroMLDocument methods
47 
48 void NeuroMLDocument::writeToFile(const std::string& pathToFile, std::ios_base::openmode mode) const
49 {
51  map[""].name = "http://www.neuroml.org/schema/neuroml2";
52  map[""].schema = getSchemaPath();
53  std::ofstream f(pathToFile, mode);
54  neuroml2::neuroml(f, *this, map);
55 }
56 
59 {
60 }
61 
62 NeuroMLDocument::NeuroMLDocument(const ::xercesc::DOMElement& e,
65  : NeuroMLDocument_base(e, f, c)
66 {
67 }
68 
72  : NeuroMLDocument_base(x, f, c)
73 {
74 }
75 
77  ::xml_schema::container* c) const
78 {
79  return new NeuroMLDocument(*this, f, c);
80 }
81 
83 {
84  if (this != &x)
85  {
86  static_cast<NeuroMLDocument_base&>(*this) = x;
87  }
88  return *this;
89 }
90 
92 {
93 }
94 
95 
96 // Morphology methods
97 
98 unsigned Morphology::num_segments() const
99 {
100  return segment().size();
101 }
102 
105 {
106 }
107 
108 Morphology::Morphology(const ::xercesc::DOMElement& e,
109  ::xml_schema::flags f,
111  : Morphology_base(e, f, c)
112 {
113 }
114 
116  ::xml_schema::flags f,
118  : Morphology_base(x, f, c)
119 {
120 }
121 
123  ::xml_schema::container* c) const
124 {
125  return new Morphology(*this, f, c);
126 }
127 
129 {
130  if (this != &x)
131  {
132  static_cast<Morphology_base&>(*this) = x;
133  }
134  return *this;
135 }
136 
138 {
139 }
140 
141 // Segment methods
142 
143 double Segment::length() const
144 {
145  assert(proximal().present());
146  double x = proximal()->x() - distal().x();
147  double y = proximal()->y() - distal().y();
148  double z = proximal()->z() - distal().z();
149 
150  return sqrt(x*x + y*y + z*z);
151 }
152 
153 double Segment::volume() const
154 {
155  assert(proximal().present());
156  double prox_rad = proximal()->diameter() / 2.0;
157  double dist_rad = distal().diameter() / 2.0;
158 
159  return (M_PI/3.0) * length() * (prox_rad*prox_rad + dist_rad*dist_rad + prox_rad*dist_rad);
160 }
161 
162 double Segment::surface_area() const
163 {
164  assert(proximal().present());
165  double prox_rad = proximal()->diameter() / 2.0;
166  double dist_rad = distal().diameter() / 2.0;
167  double rad_diff = prox_rad-dist_rad;
168  double len = length();
169 
170  return M_PI * (prox_rad + dist_rad) * sqrt(rad_diff*rad_diff + len*len);
171 }
172 
174  : Segment_base(id, d)
175 {
176 }
177 
178 Segment::Segment(const id_type& id, ::std::unique_ptr<distal_type> d)
179  : Segment_base(id, std::move(d))
180 {
181 }
182 
183 Segment::Segment(const ::xercesc::DOMElement& e, ::xml_schema::flags f, ::xml_schema::container* c)
184  : Segment_base(e, f, c)
185 {
186 }
187 
189  : Segment_base(x, f, c)
190 {
191 }
192 
194 {
195  return new Segment(*this, f, c);
196 }
197 
199 {
200  if (this != &x)
201  {
202  static_cast<Segment_base&>(*this) = x;
203  }
204  return *this;
205 }
206 
208 {
209 }
210 
211 // Connection methods
212 
213 unsigned Connection::_get_cell_id(const std::string& id_string) const
214 {
215  int id;
216  std::string::size_type pos = id_string.find('[');
217  if (pos != std::string::npos)
218  {
219  // The id is between square brackets
220  std::string::size_type end_pos = id_string.find(']', pos);
221  assert(end_pos != std::string::npos);
222  id = stoi(id_string.substr(pos+1, end_pos-pos-1));
223  }
224  else
225  {
226  // The id is after the second / (and before any third /, if present)
227  pos = id_string.find('/');
228  assert(pos != std::string::npos);
229  pos = id_string.find('/', pos+1);
230  assert(pos != std::string::npos);
231  std::string::size_type end_pos = id_string.find('/', pos+1);
232  if (end_pos == std::string::npos)
233  {
234  id = stoi(id_string.substr(pos+1));
235  }
236  else
237  {
238  id = stoi(id_string.substr(pos+1, end_pos-pos-1));
239  }
240  }
241  assert(id >= 0);
242  return static_cast<unsigned>(id);
243 }
244 
245 unsigned Connection::get_pre_cell_id() const
246 {
247  return _get_cell_id(preCellId());
248 }
249 
250 unsigned Connection::get_post_cell_id() const
251 {
252  return _get_cell_id(postCellId());
253 }
254 
256  : Connection_base(id, pre, post)
257 {
258 }
259 
260 Connection::Connection(const ::xercesc::DOMElement& e, ::xml_schema::flags f, ::xml_schema::container* c)
261  : Connection_base(e, f, c)
262 {
263 }
264 
266  : Connection_base(x, f, c)
267 {
268 }
269 
271 {
272  return new Connection(*this, f, c);
273 }
274 
276 {
277 }
278 
279 
280 // Streaming operators
281 
282 ::std::ostream&
283 operator<<(::std::ostream& o, const Connection& c)
284 {
285  o << ::std::endl << "Connection " << c.id() << ": "
286  << c.get_pre_cell_id() << " -> " << c.get_post_cell_id();
287  o << static_cast<const ::neuroml2::Connection_base&>(c);
288 
289  return o;
290 }
291 
292 
293 }
const preCellId_type & preCellId() const
Return a read-only (constant) reference to the attribute.
const postCellId_type & postCellId() const
Return a read-only (constant) reference to the attribute.
const id_type & id() const
Return a read-only (constant) reference to the attribute.
Class corresponding to the Connection schema type.
Class corresponding to the Connection schema type.
virtual Connection * _clone(::xml_schema::flags f=0, ::xml_schema::container *c=0) const
Copy the instance polymorphically.
virtual ~Connection()
Destructor.
Connection(const id_type &, const preCellId_type &, const postCellId_type &)
Create an instance from the ultimate base and initializers for required elements and attributes.
Class corresponding to the Morphology schema type.
const segment_sequence & segment() const
Return a read-only (constant) reference to the element sequence.
Class corresponding to the Morphology schema type.
virtual Morphology * _clone(::xml_schema::flags f=0, ::xml_schema::container *c=0) const
Copy the instance polymorphically.
unsigned num_segments() const
virtual ~Morphology()
Destructor.
Morphology(const id_type &)
Create an instance from the ultimate base and initializers for required elements and attributes.
Morphology & operator=(const Morphology &x)
Copy assignment operator.
Class corresponding to the NeuroMLDocument schema type.
Class corresponding to the NeuroMLDocument schema type.
NeuroMLDocument & operator=(const NeuroMLDocument &x)
Copy assignment operator.
NeuroMLDocument(const id_type &)
Create an instance from the ultimate base and initializers for required elements and attributes.
void writeToFile(const std::string &pathToFile, std::ios_base::openmode mode=std::ios_base::out) const
virtual NeuroMLDocument * _clone(::xml_schema::flags f=0, ::xml_schema::container *c=0) const
Copy the instance polymorphically.
virtual ~NeuroMLDocument()
Destructor.
Class corresponding to the Nml2PopulationReferencePath schema type.
Class corresponding to the NmlId schema type.
Class corresponding to the NonNegativeInteger schema type.
Class corresponding to the Point3DWithDiam schema type.
const x_type & x() const
Return a read-only (constant) reference to the attribute.
const z_type & z() const
Return a read-only (constant) reference to the attribute.
const y_type & y() const
Return a read-only (constant) reference to the attribute.
const diameter_type & diameter() const
Return a read-only (constant) reference to the attribute.
Class corresponding to the Segment schema type.
const distal_type & distal() const
Return a read-only (constant) reference to the element.
const proximal_optional & proximal() const
Return a read-only (constant) reference to the element container.
Class corresponding to the Segment schema type.
double volume() const
Segment & operator=(const Segment &x)
Copy assignment operator.
virtual ~Segment()
Destructor.
double length() const
double surface_area() const
Segment(const id_type &, const distal_type &)
Create an instance from the ultimate base and initializers for required elements and attributes.
virtual Segment * _clone(::xml_schema::flags f=0, ::xml_schema::container *c=0) const
Copy the instance polymorphically.
C++ namespace for the http://www.neuroml.org/schema/neuroml2 schema namespace.
::std::unique_ptr< ::neuroml2::NeuroMLDocument > neuroml(const ::std::string &u, ::xml_schema::flags f, const ::xml_schema::properties &p)
Parse a URI or a local file.
std::string getSchemaPath()
std::unique_ptr< NeuroMLDocument > parseFile(const std::string &filePath)
::xsd::cxx::tree::type container
Alias for the anyType type.
::xsd::cxx::tree::id< char, ncname > id
C++ type corresponding to the ID XML Schema built-in type.
::xsd::cxx::tree::flags flags
Parsing and serialization flags.
::xsd::cxx::tree::properties< char > properties
Parsing properties.
::xsd::cxx::xml::dom::namespace_infomap< char > namespace_infomap
Namespace serialization information map.
::xsd::cxx::tree::string< char, simple_type > string
C++ type corresponding to the string XML Schema built-in type.