root/src/core/Network.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. node
  2. get_id
  3. hide
  4. show
  5. is_visible
  6. size
  7. add_neuron
  8. get_neuron
  9. remove_neuron
  10. set_colors

   1 #include "core/Network.hpp"
   2 
   3 Network::Network(const char * id) : id(id)
   4                                   , node(new osg::MatrixTransform())
   5 {
   6 }
   7 
   8 
   9 Network::~Network()
  10 {
  11     for(Neuron * neuron : neuron_seq)
  12     {
  13         neuron -> set_network(nullptr);
  14     }
  15 }
  16 
  17 const char *
  18 Network::get_id()
  19 {
  20     return id.c_str();
  21 }
  22 
  23 void
  24 Network::hide()
  25 {
  26     node -> setNodeMask(NODE_HIDE_MASK);
  27 }
  28 
  29 void
  30 Network::show()
  31 {
  32     node -> setNodeMask(NODE_SHOW_MASK);
  33 }
  34 
  35 bool
  36 Network::is_visible()
  37 {
  38     return (node -> getNodeMask() == NODE_SHOW_MASK ? true : false);
  39 }
  40 
  41 unsigned int
  42 Network::size()
  43 {
  44     return static_cast<unsigned int>(neuron_seq.size());
  45 }
  46 
  47 unsigned int
  48 Network::add_neuron(Neuron * neuron)
  49 {
  50     auto iter = neuron_map.find(neuron -> get_id());
  51     if(iter != neuron_map.end())
  52     {
  53         RECORD_WARNING("Neuron already exists!");
  54     }
  55     else
  56     {
  57         neuron_map[neuron -> get_id()] = neuron;
  58         neuron_seq.push_back(neuron);
  59         neuron -> set_network(this);
  60         node -> addChild(neuron -> node.get());
  61     }
  62     return static_cast<unsigned int>(neuron_seq.size());
  63 }
  64 
  65 Neuron *
  66 Network::get_neuron(const char * id)
  67 {
  68     auto iter = neuron_map.find(id);
  69     if(iter == neuron_map.end())
  70     {
  71         RECORD_WARNING("Neuron does not exist!");
  72         // TODO : raise exception instead of returning nullptr
  73         return nullptr;
  74     }
  75     return iter -> second;
  76 }
  77 
  78 
  79 unsigned int
  80 Network::remove_neuron(Neuron * neuron)
  81 {
  82     auto iter = neuron_map.find(neuron -> get_id());
  83     if(iter == neuron_map.end())
  84     {
  85         RECORD_WARNING("Neuron does not exist!");
  86     }
  87     else
  88     {
  89         neuron_map.erase(iter);
  90         neuron_seq.erase( std::find( neuron_seq.begin()
  91                                    , neuron_seq.end()
  92                                    , neuron
  93                                    )
  94                              );
  95         node -> removeChild(neuron -> node.get());
  96         neuron -> set_network(nullptr);
  97     }
  98     return static_cast<unsigned int>(neuron_seq.size());
  99 }
 100 
 101 bool
 102 Network::set_colors(PyObject * colors)
 103 {
 104     if(PySequence_Check(colors) != 1)
 105     {
 106         RECORD_ERROR("Invalid data structure provided for setting neuron colors.");
 107         Py_RETURN_FALSE;
 108     }
 109 
 110     unsigned int limit = std::min( static_cast<unsigned int>(PySequence_Length(colors))
 111                                  , static_cast<unsigned int>(neuron_seq.size())
 112                                  );
 113     unsigned int i;
 114     for(i = 0; i < limit;++i)
 115     {
 116         neuron_seq[i] -> set_color(PySequence_GetItem(colors, i));
 117     }
 118     Py_RETURN_TRUE;
 119 }

/* [<][>][^][v][top][bottom][index][help] */