Exemplo n.º 1
0
        /// <summary>
        /// Write an object.
        /// </summary>
        /// <param name="obj">The object to write.</param>
        public void WriteObject(IEncogPersistedObject obj)
        {
            IPersistor persistor = obj.CreatePersistor();

            if (persistor == null)
            {
                String str = "Can't find a persistor for object of type "
                             + obj.GetType().Name;
#if logging
                if (this.logger.IsErrorEnabled)
                {
                    this.logger.Error(str);
                }
#endif
                throw new PersistError(str);
            }
            persistor.Save(obj, this.xmlOut);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a persistor object.  These objects know how to persist
        /// certain types of classes.
        /// </summary>
        /// <param name="className">The name of the class to create a persistor for.</param>
        /// <returns>The persistor for the specified class.</returns>
        public static IPersistor CreatePersistor(String className)
        {
            // handle any hard coded ones
            if (className.Equals("TrainingData"))
            {
                return(new BasicNeuralDataSetPersistor());
            }

            String     name      = "Encog.Persist.Persistors." + className + "Persistor";
            IPersistor persistor = (IPersistor)Assembly.GetExecutingAssembly().CreateInstance(name);

            // try another way
            if (persistor == null)
            {
                String type = ReflectionUtil.ResolveEncogClass(className);
                IEncogPersistedObject temp =
                    (IEncogPersistedObject)ReflectionUtil.LoadObject(type);

                persistor = temp.CreatePersistor();
            }

            return(persistor);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Perform a deep copy.
        /// Silverlight version.
        /// </summary>
        /// <param name="oldObj">The old object.</param>
        /// <returns>The new object.</returns>
        static public IEncogPersistedObject DeepCopy(IEncogPersistedObject oldObj)
        {
            bool replacedName = false;

            // encog objects won't save without a name
            if (oldObj.Name == null)
            {
                replacedName = true;
                oldObj.Name  = "temp";
            }

            // now make the copy
            MemoryStream mstream   = new MemoryStream();
            WriteXML     xmlOut    = new WriteXML(mstream);
            IPersistor   persistor = oldObj.CreatePersistor();

            xmlOut.BeginDocument();
            persistor.Save(oldObj, xmlOut);
            xmlOut.EndDocument();
            // now read it back
            mstream.Position = 0;
            ReadXML xmlIn = new ReadXML(mstream);

            xmlIn.ReadToTag();
            IEncogPersistedObject result = persistor.Load(xmlIn);

            mstream.Close();

            // put the name back to null if we changed it
            if (replacedName)
            {
                oldObj.Name = null;
                result.Name = null;
            }
            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Write an object.
        /// </summary>
        /// <param name="obj">The object to write.</param>
        public void WriteObject(IEncogPersistedObject obj)
        {
            IPersistor persistor = obj.CreatePersistor();
            if (persistor == null)
            {
                String str = "Can't find a persistor for object of type "
                       + obj.GetType().Name;
#if logging
                if (this.logger.IsErrorEnabled)
                {
                    this.logger.Error(str);
                }
#endif
                throw new PersistError(str);
            }
            persistor.Save(obj, this.xmlOut);
        }
Exemplo n.º 5
0
    /// <summary>
    /// Perform a deep copy.
    /// Silverlight version.
    /// </summary>
    /// <param name="oldObj">The old object.</param>
    /// <returns>The new object.</returns>
        static public IEncogPersistedObject DeepCopy(IEncogPersistedObject oldObj)
        {
            bool replacedName = false;

            // encog objects won't save without a name
            if (oldObj.Name == null)
            {
                replacedName = true;
                oldObj.Name = "temp";
            }

            // now make the copy
            MemoryStream mstream = new MemoryStream();
            WriteXML xmlOut = new WriteXML(mstream);
            IPersistor persistor = oldObj.CreatePersistor();
            xmlOut.BeginDocument();
            persistor.Save(oldObj, xmlOut);
            xmlOut.EndDocument();
            // now read it back
            mstream.Position = 0;
            ReadXML xmlIn = new ReadXML(mstream);
            xmlIn.ReadToTag();
            IEncogPersistedObject result = persistor.Load(xmlIn);
            mstream.Close();

            // put the name back to null if we changed it
            if (replacedName)
            {
                oldObj.Name = null;
                result.Name = null;
            }
            return result;        }