Click here to Skip to main content
15,888,984 members
Articles / Programming Languages / C#

Grandiose Projects 3. Compatibility with Simulink

Rate me:
Please Sign up or sign in to vote.
4.27/5 (11 votes)
8 Feb 2010CPOL23 min read 47.7K   5.9K   38  
Import of Simulink files
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;


using CategoryTheory;
using DataPerformer;
using DataPerformer.Interfaces;

namespace Regression
{
    /// <summary>
    /// Combined selection
    /// </summary>
    [Serializable()]
    public class CombinedSelection :CategoryObject, ISerializable, IStructuredSelection, 
        IStructuredSelectionConsumer, IPostSetArrow, IStructuredSelectionCollection
    {

        #region Fields

        /// <summary>
        /// Selection
        /// </summary>
        protected IStructuredSelection selection;

        /// <summary>
        /// Weights
        /// </summary>
        protected IStructuredSelection weights;

        /// <summary>
        /// All selections
        /// </summary>
        protected List<IStructuredSelectionCollection> selections = new List<IStructuredSelectionCollection>();

        /// <summary>
        /// Numbers
        /// </summary>
        protected int[,] num = new int[2, 2];
        
        #endregion

        #region Ctor

        /// <summary>
        /// Default constructor
        /// </summary>
        public CombinedSelection()
        {
        }

        /// <summary>
        /// Deserializable cinstructor
        /// </summary>
        /// <param name="info">Serialization Info</param>
        /// <param name="context">Streaming Context</param>
        protected CombinedSelection(SerializationInfo info, StreamingContext context)
        {
            num = info.GetValue("Num", num.GetType()) as int[,];
        }

        #endregion

        #region ISerializable Members

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Num", num, num.GetType());
        }

        #endregion

        #region IStructuredSelection Members

        int IStructuredSelection.DataDimension
        {
            get { return selection.DataDimension; }
        }

        double? IStructuredSelection.this[int n]
        {
            get { return selection[n]; }
        }

        double IStructuredSelection.GetWeight(int n)
        {
            if (weights.DataDimension <= n)
            {
                return 1;
            }
            double? a = weights[n];
            if (a == null)
            {
                return 1;
            }
            return (double)a;
        }

        double IStructuredSelection.GetApriorWeight(int n)
        {
            return selection.GetApriorWeight(n);
        }

        int IStructuredSelection.GetTolerance(int n)
        {
            return selection.GetTolerance(n);
        }

        void IStructuredSelection.SetTolerance(int n, int tolerance)
        {
           selection.SetTolerance(n, tolerance);
        }

        bool IStructuredSelection.HasFixedAmount
        {
            get { return selection.HasFixedAmount; }
        }

        string IStructuredSelection.Name
        {
            get { return selection.Name + "_" + weights.Name; }
        }

        #endregion

        #region IStructuredSelectionConsumer Members

        void IStructuredSelectionConsumer.Add(IStructuredSelectionCollection selection)
        {
            selections.Add(selection);
        }

        void IStructuredSelectionConsumer.Remove(IStructuredSelectionCollection selection)
        {
            selections.Remove(selection);
        }

        #endregion

        #region IPostSetArrow Members

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

        #endregion

        #region IStructuredSelectionCollection Members

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

        IStructuredSelection IStructuredSelectionCollection.this[int i]
        {
            get { return this; }
        }

        #endregion

        #region Specific Members


        public int[,] Numbers
        {
            get
            {
                return num;
            }
        }

        public IList<IStructuredSelectionCollection> Selections
        {
            get
            {
                return selections;
            }
        }
/*
        public void Set(IStructuredSelectionCollection selection, IStructuredSelection weight, int nSel, int nWeght)
        {
            num[0, 0] = selections.IndexOf(selection);
            num[0, 1] = nSel;
            num[1, 0] = selections.IndexOf(weight);
            num[1, 1] = nWeght;
            post();
        }
*/

        public void Set(IStructuredSelectionCollection selection, int nSel, bool b)
        {
            int k = b ? 0 : 1;
            num[k, 0] = selections.IndexOf(selection);
            num[k, 1] = nSel;
            post();
        }


        private void post()
        {
            if (num[0, 0] < selections.Count)
            {
                if (num[0, 1] < selections[num[0, 0]].Count)
                {
                    selection = selections[num[0, 0]][num[0, 1]];
                }
            }
            if (num[1, 0] < selections.Count)
            {
                if (num[1, 1] < selections[num[1, 0]].Count)
                {
                    weights = selections[num[1, 0]][num[1, 1]];
                }
            }
        }

        #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