📄 netloader.cpp
字号:
void NetLoader::ParseNeuron(const char **attrs){ for (unsigned int i = 0; attrs[i] != NULL; i++) { string name(attrs[i]); string value(attrs[++i]); if ( name == "id" ){ currNId = atoi(value.c_str()); } else if ( name == "type" ) { currNType = value; } } if(pass == 1) { // here we create only the neuronProperties. Neuron is created at end tag NFactory * nf = dynamic_cast<NFactory*> (FactoryBase::GetRegistry().GetFactory(currNType)); if (nf == NULL) throw runtime_error("No neuron type " + currNType + " registered!"); nProps = nf->MakeNeuronProperties(true); } else if (pass == 2){ // get the neuron reference back to allow creating synapses and adding I/O currNeuron = Network::GetNetworkRef()->GetNeuron(currNId); if(spikeInput){ InputNeuron * in = dynamic_cast <InputNeuron*> (currNeuron); if (in) spikeInput->AddNeuron(in); else cerr << "Warning: Cannot add neuron " << currNeuron->GetId() << " because it's not an InputNeuron" << endl; } else if (outputGroup){ OutputManager::AddNeuronToGroup(currNeuron, outputGroup); } }}void NetLoader::ParseProperties(const char **attrs){ string key(attrs[1]); string value(attrs[3]); if(pass == 1 && nProps && !sProps){ // Neuron Properties nProps->SetProperty(key, value); } else if (pass == 2 && sProps){ // Synapse Properties sProps->SetProperty(key, value); }}void NetLoader::ParseTopology(const char **attrs){ string topName; string topType; for (unsigned int i = 0; attrs[i] != NULL; i++) { string name(attrs[i]); string value(attrs[++i]); if ( name == "name" ){ topName = value; } else if ( name == "type" ) { topType = value; } } if(pass == 1) { TFactory * tf = dynamic_cast<TFactory*> (FactoryBase::GetRegistry().GetFactory(topType)); if (tf == NULL) throw runtime_error("No topology type " + topType + " registered!"); currTop = tf->MakeTopology(topName); Network::GetNetworkRef()->AddTopology(currTop); } else if (pass == 2){ currTop = Network::GetNetworkRef()->GetTopology(topName); }}//////////////////////// End SAX callback stuff //////////////////////7void NetLoader::LoadXML(string filename, bool _loadPhysProps){ loadPhysProps = _loadPhysProps; LOGGER(2, "Starting SAX Pass 1 from file " + filename); pass = 1; SAXPass(filename); LOGGER(2, "Starting SAX Pass 2 from file " + filename); pass = 2; SAXPass(filename);}void NetLoader::WriteProperties(FILE * netFile, const Properties * props, const char * indent){ try{ map<string, string> propMap = props->GetPropertyMap(); for(map<string, string>::const_iterator pPropIt = propMap.begin(); pPropIt != propMap.end(); pPropIt++){ fprintf(netFile, "%s<property name=\"%s\" value=\"%s\"/>\n", indent, pPropIt->first.c_str(), pPropIt->second.c_str()); } } catch (runtime_error e) { cerr << "Warning: not saving Properties: "; cerr << e.what() << endl; }}void NetLoader::SaveXML(string filename, bool compress){ string cmd; if (compress) { cmd = "gzip -c > " + filename; } else { cmd = "cat > " + filename; } FILE *netFile = popen(cmd.c_str(), "w"); if(netFile == NULL) throw runtime_error("cannot open: " + filename); fputs ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n", netFile); fputs ("<!DOCTYPE network PUBLIC \"amygdala.dtd\" \"dtd/amygdala.dtd\">\n\n", netFile); fputs ("<network>\n", netFile); Network * net = Network::GetNetworkRef(); for (Network::const_iterator topIt = net->begin(); topIt != net->end(); topIt++){ Topology * topology = topIt->second; fprintf(netFile, " <topology name=\"%s\" type=\"%s\">\n", topology->GetName().c_str(), topology->GetClassName().c_str()); nProps = topology->GetDefaultNeuronProperties(); if(nProps != NULL) WriteProperties(netFile, nProps, " "); for(NeuronGroup::const_iterator nIt = topology->begin(); nIt != topology->end(); nIt++) { Neuron * n = *nIt; fprintf(netFile, " <neuron type=\"%s\" id=\"%d\">\n", n->GetClassName().c_str(), n->GetId()); nProps = n->Properties(); // reuse pointer from above if(nProps != NULL) WriteProperties(netFile, nProps, " "); Axon * axon = n->GetAxon(); for (Axon::iterator aIt = axon->begin(); aIt != axon->end(); aIt++){ AxonNode * aNode = *aIt; AxonNodeIterator anIt = aNode->Begin(); Synapse *syn; while ((syn = anIt++)){ fprintf(netFile, " <synapse type=\"%s\" postneuron=\"%d\" delay=\"%d\">\n", syn->GetClassName().c_str(), syn->GetDendrite()->GetPostNeuron()->GetId(), aNode->GetDelay()); sProps = syn->Properties(); // the delay is 0, we need to update this to the value obtained from the AxonNode sProps->SetProperty("delay", Utilities::itostr(aNode->GetDelay())); if(sProps != NULL) WriteProperties(netFile, sProps, " "); fputs(" </synapse>\n", netFile); } } fputs(" </neuron>\n", netFile); } fputs(" </topology>\n", netFile); for(Network::spikeinput_iterator siIt = net->SpikeInput_begin(); siIt != net->SpikeInput_end(); siIt++){ SpikeInput *si = *siIt; fprintf(netFile, " <spikeinput name=\"%s\" type=\"%s\">\n", si->GetName().c_str(), si->GetClassName().c_str()); for(NeuronGroup::const_iterator nIt = si->begin(); nIt != si->end(); nIt++) { Neuron * n = *nIt; fprintf(netFile, " <neuron id=\"%d\"/>\n", n->GetId()); } fputs(" </spikeinput>\n", netFile); } fputs(" <output>\n", netFile); for(OutputManager::spikeoutput_iterator soIt = OutputManager::SpikeOutput_begin(); soIt != OutputManager::SpikeOutput_end(); soIt++){ SpikeOutput *so = soIt->second; fprintf(netFile, " <spikeoutput name=\"%s\" type=\"%s\">\n", so->GetName().c_str(), so->GetClassName().c_str()); for(NeuronGroup::const_iterator nIt = so->begin(); nIt != so->end(); nIt++) { Neuron * n = *nIt; fprintf(netFile, " <neuron id=\"%d\"/>\n", n->GetId()); } fputs(" </spikeoutput>\n", netFile); } for (OutputManager::output_group_iterator ogIt = OutputManager::OutputGroup_begin(); ogIt != OutputManager::OutputGroup_end(); ogIt++) { AmGroupInt groupId = ogIt->first; vector<Neuron*> nVec = ogIt->second; fprintf(netFile, " <outputgroup id=\"%d\" enabled=\"%d\">\n", groupId, Neuron::CaptureOutput(groupId)); for (unsigned int j=0; j<nVec.size(); ++j) { fprintf(netFile, " <neuron id=\"%d\"/>\n", nVec[j]->GetId()); } fputs(" </outputgroup>\n", netFile); } fputs(" </output>\n", netFile); } fputs("</network>\n", netFile); fclose(netFile);}void NetLoader::SAXPass(string filename){ xmlSAXHandler saxHandler; memset (&saxHandler, 0, sizeof(saxHandler)); currTop = NULL; currNId = 0; currNeuron = NULL; sProps = NULL; saxErrors = 0; spikeInput = NULL; spikeOutput = NULL; outputGroup = 0; xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt(filename.c_str()); if (ctxt == NULL) throw runtime_error("can't create a parser context: " + filename); saxHandler.warning = (warningSAXFunc)__SAXError; saxHandler.error = (errorSAXFunc)__SAXError; saxHandler.fatalError = (fatalErrorSAXFunc)__SAXError; switch(pass){ case 1: saxHandler.startElement = (startElementSAXFunc)__SAXStartElement1; saxHandler.endElement = (endElementSAXFunc)__SAXEndElement1; break; case 2: saxHandler.startElement = (startElementSAXFunc)__SAXStartElement2; saxHandler.endElement = (endElementSAXFunc)__SAXEndElement2; break; default: throw runtime_error("Pass must be either 1 or 2"); break; } ctxt->sax = &saxHandler; ctxt->userData = this; xmlParseDocument(ctxt); ctxt->sax = NULL; xmlFreeParserCtxt(ctxt); if(saxErrors > 0 )throw runtime_error ("Errors during loading...");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -