public static bool Save(NeuronArray theNeuronArray, string fileName) { //Check for file access if (!CanWriteTo(fileName, out string message)) { MessageBox.Show("Could not save file because: " + message); return(false); } MainWindow.thisWindow.SetProgress(0, "Saving Network File"); string tempFile = System.IO.Path.GetTempFileName(); FileStream file = File.Create(tempFile); Type[] extraTypes = GetModuleTypes(); try { XmlSerializer writer = new XmlSerializer(typeof(NeuronArray), extraTypes); writer.Serialize(file, theNeuronArray); } catch (Exception e) { MessageBox.Show("Xml file write failed because: " + e.Message); return(false); } file.Position = 0;; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(file); XmlElement root = xmldoc.DocumentElement; XmlNode neuronsNode = xmldoc.CreateNode("element", "Neurons", ""); root.AppendChild(neuronsNode); for (int i = 0; i < theNeuronArray.arraySize; i++) { var progress = i / (float)theNeuronArray.arraySize; progress *= 100; if (MainWindow.thisWindow.SetProgress(progress, "")) { MainWindow.thisWindow.SetProgress(100, ""); return(false); } Neuron n = theNeuronArray.GetNeuronForDrawing(i); if (n.inUse || n.Label != "") { n = theNeuronArray.GetCompleteNeuron(i); string label = n.Label; //this is needed bacause inUse is true if any synapse points to this neuron--we don't need to bother with that if it's the only thing if (n.synapses.Count != 0 || label != "" || n.lastCharge != 0 || n.leakRate != 0.1f || n.model != Neuron.modelType.IF) { XmlNode neuronNode = xmldoc.CreateNode("element", "Neuron", ""); neuronsNode.AppendChild(neuronNode); XmlNode attrNode = xmldoc.CreateNode("element", "Id", ""); attrNode.InnerText = n.id.ToString(); neuronNode.AppendChild(attrNode); if (n.model != Neuron.modelType.IF) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = n.model.ToString(); neuronNode.AppendChild(attrNode); } if (n.model != Neuron.modelType.Color && n.lastCharge != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.lastCharge.ToString(); neuronNode.AppendChild(attrNode); } if (n.model == Neuron.modelType.Color && n.LastChargeInt != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.LastChargeInt.ToString(); neuronNode.AppendChild(attrNode); } if (n.leakRate != 0.1f) { attrNode = xmldoc.CreateNode("element", "LeakRate", ""); attrNode.InnerText = n.leakRate.ToString(); neuronNode.AppendChild(attrNode); } if (n.axonDelay != 0) { attrNode = xmldoc.CreateNode("element", "AxonDelay", ""); attrNode.InnerText = n.axonDelay.ToString(); neuronNode.AppendChild(attrNode); } if (label != "") { attrNode = xmldoc.CreateNode("element", "Label", ""); attrNode.InnerText = label; neuronNode.AppendChild(attrNode); } if (MainWindow.arrayView.IsShowingSnapses(n.id)) { attrNode = xmldoc.CreateNode("element", "ShowSynapses", ""); attrNode.InnerText = "True"; neuronNode.AppendChild(attrNode); } if (n.synapses.Count > 0) { XmlNode synapsesNode = xmldoc.CreateNode("element", "Synapses", ""); neuronNode.AppendChild(synapsesNode); foreach (Synapse s in n.synapses) { XmlNode synapseNode = xmldoc.CreateNode("element", "Synapse", ""); synapsesNode.AppendChild(synapseNode); if (s.weight != 1) { attrNode = xmldoc.CreateNode("element", "Weight", ""); attrNode.InnerText = s.weight.ToString(); synapseNode.AppendChild(attrNode); } attrNode = xmldoc.CreateNode("element", "TargetNeuron", ""); attrNode.InnerText = s.targetNeuron.ToString(); synapseNode.AppendChild(attrNode); if (s.model != Synapse.modelType.Fixed) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = s.model.ToString(); synapseNode.AppendChild(attrNode); } } } } } } //a way to get the xml as a string //using (var stringWriter = new StringWriter()) //using (var xmlTextWriter = XmlWriter.Create(stringWriter)) //{ // xmldoc.WriteTo(xmlTextWriter); // xmlTextWriter.Flush(); // string xxx = stringWriter.GetStringBuilder().ToString(); //} file.Position = 0; xmldoc.Save(file); file.Close(); try { File.Copy(tempFile, fileName, true); File.Delete(tempFile); } catch (Exception e) { MainWindow.thisWindow.SetProgress(100, ""); MessageBox.Show("Could not save file because: " + e.Message); return(false); } MainWindow.thisWindow.SetProgress(100, ""); return(true); }
//if you pass in the fileName 'ClipBoard', the save is to the windows clipboard public static bool Save(NeuronArray theNeuronArray, string fileName) { Stream file; string tempFile = ""; bool fromClipboard = fileName == "ClipBoard"; if (!fromClipboard) { //Check for file access if (!CanWriteTo(fileName, out string message)) { MessageBox.Show("Could not save file because: " + message); return(false); } MainWindow.thisWindow.SetProgress(0, "Saving Network File"); tempFile = System.IO.Path.GetTempFileName(); file = File.Create(tempFile); } else { file = new MemoryStream(); } Type[] extraTypes = GetModuleTypes(); try { XmlSerializer writer = new XmlSerializer(typeof(NeuronArray), extraTypes); writer.Serialize(file, theNeuronArray); } catch (Exception e) { if (e.InnerException != null) { MessageBox.Show("Xml file write failed because: " + e.InnerException.Message); } else { MessageBox.Show("Xml file write failed because: " + e.Message); } MainWindow.thisWindow.SetProgress(100, ""); return(false); } file.Position = 0;; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(file); XmlElement root = xmldoc.DocumentElement; XmlNode neuronsNode = xmldoc.CreateNode("element", "Neurons", ""); root.AppendChild(neuronsNode); for (int i = 0; i < theNeuronArray.arraySize; i++) { if (!fromClipboard) { var progress = i / (float)theNeuronArray.arraySize; progress *= 100; if (MainWindow.thisWindow.SetProgress(progress, "")) { MainWindow.thisWindow.SetProgress(100, ""); return(false); } } Neuron n = theNeuronArray.GetNeuronForDrawing(i); if (fromClipboard) { n.Owner = theNeuronArray; } if (n.inUse || n.Label != "" || fromClipboard) { n = theNeuronArray.GetCompleteNeuron(i, fromClipboard); n.Owner = theNeuronArray; string label = n.Label; if (n.ToolTip != "") { label += Neuron.toolTipSeparator + n.ToolTip; } //this is needed bacause inUse is true if any synapse points to this neuron-- //we don't need to bother with that if it's the only thing if (n.synapses.Count != 0 || label != "" || n.lastCharge != 0 || n.leakRate != 0.1f || n.model != Neuron.modelType.IF) { XmlNode neuronNode = xmldoc.CreateNode("element", "Neuron", ""); neuronsNode.AppendChild(neuronNode); XmlNode attrNode = xmldoc.CreateNode("element", "Id", ""); attrNode.InnerText = n.id.ToString(); neuronNode.AppendChild(attrNode); if (n.model != Neuron.modelType.IF) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = n.model.ToString(); neuronNode.AppendChild(attrNode); } if (n.model != Neuron.modelType.Color && n.lastCharge != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.lastCharge.ToString(); neuronNode.AppendChild(attrNode); } if (n.model == Neuron.modelType.Color && n.LastChargeInt != 0) { attrNode = xmldoc.CreateNode("element", "LastCharge", ""); attrNode.InnerText = n.LastChargeInt.ToString(); neuronNode.AppendChild(attrNode); } if (n.leakRate != 0.1f) { attrNode = xmldoc.CreateNode("element", "LeakRate", ""); attrNode.InnerText = n.leakRate.ToString(); neuronNode.AppendChild(attrNode); } if (n.axonDelay != 0) { attrNode = xmldoc.CreateNode("element", "AxonDelay", ""); attrNode.InnerText = n.axonDelay.ToString(); neuronNode.AppendChild(attrNode); } if (label != "") { attrNode = xmldoc.CreateNode("element", "Label", ""); attrNode.InnerText = label; neuronNode.AppendChild(attrNode); } if (n.ShowSynapses) { attrNode = xmldoc.CreateNode("element", "ShowSynapses", ""); attrNode.InnerText = "True"; neuronNode.AppendChild(attrNode); } if (n.RecordHistory && !fromClipboard) { attrNode = xmldoc.CreateNode("element", "RecordHistory", ""); attrNode.InnerText = "True"; neuronNode.AppendChild(attrNode); } if (n.synapses.Count > 0) { XmlNode synapsesNode = xmldoc.CreateNode("element", "Synapses", ""); neuronNode.AppendChild(synapsesNode); foreach (Synapse s in n.synapses) { XmlNode synapseNode = xmldoc.CreateNode("element", "Synapse", ""); synapsesNode.AppendChild(synapseNode); if (s.weight != 1) { attrNode = xmldoc.CreateNode("element", "Weight", ""); attrNode.InnerText = s.weight.ToString(); synapseNode.AppendChild(attrNode); } attrNode = xmldoc.CreateNode("element", "TargetNeuron", ""); attrNode.InnerText = s.targetNeuron.ToString(); synapseNode.AppendChild(attrNode); if (s.model != Synapse.modelType.Fixed) { attrNode = xmldoc.CreateNode("element", "Model", ""); attrNode.InnerText = s.model.ToString(); synapseNode.AppendChild(attrNode); } } } } } } file.Position = 0; xmldoc.Save(file); if (!fromClipboard) { file.Close(); try { File.Copy(tempFile, fileName, true); File.Delete(tempFile); MainWindow.thisWindow.SetProgress(100, ""); } catch (Exception e) { MainWindow.thisWindow.SetProgress(100, ""); MessageBox.Show("Could not save file because: " + e.Message); return(false); } } else { file.Position = 0; StreamReader str = new StreamReader(file); string temp = str.ReadToEnd(); Clipboard.SetText(temp); } return(true); }