Click here to Skip to main content
15,898,134 members
Articles / Desktop Programming / WPF

The Time Machine

Rate me:
Please Sign up or sign in to vote.
4.91/5 (13 votes)
9 May 2012CPOL6 min read 34.3K   1.2K   32  
Long time strategy of software design and development
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

using CategoryTheory;
using DiagramUI;
using BaseTypes;

using FormulaEditor;



using DataPerformer.Interfaces;

namespace DataPerformer
{
    /// <summary>
    /// Object which assemblies variables to matrix
    /// </summary>
    [Serializable()]
    public class MatrixAssembly : CategoryObject,  ISerializable, IDataConsumer, IMeasure, IPostSetArrow, IMeasurements
    {

        #region Fields

        /// <summary>
        /// Change input event
        /// </summary>
        private event Action onChangeInput = () => { };
        
        /// <summary>
        /// Names of scalars
        /// </summary>
        protected string[,] names;

        /// <summary>
        /// Measures of scalars
        /// </summary>
        protected IMeasure[,] measures;

        /// <summary>
        /// All measurements
        /// </summary>
        protected IList<IMeasurements> measurements = new List<IMeasurements>();

        /// <summary>
        /// Return type
        /// </summary>
        protected ArrayReturnType type;

        /// <summary>
        /// The "is updated" sign
        /// </summary>
        protected bool isUpdated;

        /// <summary>
        /// Values
        /// </summary>
        protected double[,] val;

        /// <summary>
        /// Parameter
        /// </summary>
        protected Func<object> parameter;

        const Double a = 0;
  

        #endregion

        #region Ctor


        /// <summary>
        /// Default constructor
        /// </summary>
        public MatrixAssembly()
        {
            parameter = GetValue;
        }

        /// <summary>
        /// Deserialization constructor
        /// </summary>
        /// <param name="info">Serialization info</param>
        /// <param name="context">Streaming context</param>
        protected MatrixAssembly(SerializationInfo info, StreamingContext context)
            : this()
        {
            try
            {
                names = info.GetValue("Names", typeof(string[,])) as string[,];
            }
            catch (Exception ex)
            {
                ex.ShowError(10);
            }
        }

        #endregion

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            if (names == null)
            {
                return;
            }
            info.AddValue("Names", names, typeof(string[,]));
        }

        #endregion

        #region IPostSetArrow Members

        void IPostSetArrow.PostSetArrow()
        {
            Post();
        }

        #endregion

        #region IDataConsumer Members

        void IDataConsumer.Add(IMeasurements measurements)
        {
            this.measurements.Add(measurements);
        }

        void IDataConsumer.Remove(IMeasurements measurements)
        {
            this.measurements.Remove(measurements); ;
        }

        void IDataConsumer.UpdateChildrenData()
        {
            foreach (IMeasurements m in measurements)
            {
                m.UpdateMeasurements();
            }
        }

        int IDataConsumer.Count
        {
            get { return measurements.Count; }
        }

        IMeasurements IDataConsumer.this[int n]
        {
            get { return measurements[n]; }
        }

        void IDataConsumer.Reset()
        {
            this.FullReset();
        }

        event Action IDataConsumer.OnChangeInput
        {
            add { onChangeInput += value; }
            remove { onChangeInput -= value; }
        }

        #endregion

        #region IMeasure Members

        Func<object> IMeasure.Parameter
        {
            get { return parameter; }
        }

        string IMeasure.Name
        {
            get { return "Matrix"; }
        }

        object IMeasure.Type
        {
            get { return type; }
        }

        #endregion

        #region IMeasurements Members

        int IMeasurements.Count
        {
            get { return 1; }
        }

        IMeasure IMeasurements.this[int n]
        {
            get { return this; }
        }

        void IMeasurements.UpdateMeasurements()
        {
            if (isUpdated)
            {
                return;
            }
            IDataConsumer c = this;
            c.UpdateChildrenData();
            for (int i = 0; i < val.GetLength(0); i++)
            {
                for (int j = 0; j < val.GetLength(1); j++)
                {
                    val[i, j] = (double)measures[i, j].Parameter();
                }
            }
            isUpdated = true;
        }

        bool IMeasurements.IsUpdated
        {
            get
            {
                return isUpdated;
            }
            set
            {
                isUpdated = value;
            }
        }

        #endregion

        #region Specific Members

        /// <summary>
        /// Names of scalar variables
        /// </summary>
        public string[,] Names
        {
            get
            {
                return names;
            }
            set
            {
                names = value;
                Post();
            }
        }

        /// <summary>
        /// Gets value
        /// </summary>
        /// <returns>Value</returns>
        protected object GetValue()
        {
            return val;
        }


        private void Post()
        {
            if (names == null)
            {
                return;
            }
            measures = new IMeasure[names.GetLength(0), names.GetLength(1)];
            for (int i = 0; i < names.GetLength(0); i++)
            {
                for (int j = 0; j < names.GetLength(1); j++)
                {
                    measures[i, j] = this.FindMeasure(names[i, j], false);
                }
            }
            val = new double[measures.GetLength(0), measures.GetLength(1)];
            type = new ArrayReturnType(a, new int[] { measures.GetLength(0), measures.GetLength(1) }, false);
        }


        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions