Click here to Skip to main content
15,879,239 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.6K   5.9K   38  
Import of Simulink files
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;

using CategoryTheory;

namespace MathGraph
{
    /// <summary>
    /// Path of digraph
    /// </summary>
    public class DigraphPath
    {
        /// <summary>
        /// Edges of path
        /// </summary>
        private ArrayList edges = new ArrayList();

        /// <summary>
        /// Auxiliary edge
        /// </summary>
        private DigraphEdge prev = null;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="edges">Array of edges</param>
        /// <param name="inverse">The "inverse" sign</param>
        public DigraphPath(ArrayList edges, bool inverse)
        {
            if (inverse)
            {
                for (int i = edges.Count - 1; i >= 0; i--)
                {
                    object o = edges[i];
                    if (!(o is DigraphEdge))
                    {
                        throw new Exception("Path should contains digraph edges only");
                    }
                    DigraphEdge e = o as DigraphEdge;
                    if (prev != null)
                    {
                        if (prev.Target != e.Source)
                        {
                            throw new Exception("Path edges are not connected");
                        }
                    }
                    prev = e;
                    this.edges.Add(e);
                }
            }
            else
            {
                for (int i = 0; i < edges.Count; i++)
                {
                    object o = edges[i];
                    if (!(o is DigraphEdge))
                    {
                        throw new Exception("Path should contains digraph edges only");
                    }
                    DigraphEdge e = o as DigraphEdge;
                    if (prev != null)
                    {
                        if (prev.Target != e.Source)
                        {
                            throw new Exception("Path edges are not connected");
                        }
                    }
                    prev = e;
                    this.edges.Add(e);
                }
            }
        }

        /// <summary>
        /// Count of edges
        /// </summary>
        public int Count
        {
            get
            {
                return edges.Count;
            }
        }


        /// <summary>
        /// Access to i - th edge
        /// </summary>
        public DigraphEdge this[int i]
        {
            get
            {
                return edges[i] as DigraphEdge;
            }
        }

        /// <summary>
        /// Overriden "GetHashCode" function
        /// </summary>
        /// <returns>Hash code</returns>
        public override int GetHashCode()
        {
            return edges.Count;
        }


        /// <summary>
        /// Checks whether this path contains the edge
        /// </summary>
        /// <param name="edge">The edge to check</param>
        /// <returns>True if path contains edge and false otherwise</returns>
        public bool Contains(DigraphEdge edge)
        {
            return edges.Contains(edge);
        }

        /// <summary>
        /// Checks wheter path contains associated object
        /// </summary>
        /// <param name="o">The object</param>
        /// <returns>True if path contains object and false otherwise</returns>
        public bool ContainsObject(object o)
        {
            foreach (DigraphEdge edge in edges)
            {
                if (edge.Object == o)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Overriden "Equals" function
        /// </summary>
        /// <param name="o">Object to compare</param>
        /// <returns>True if o equals this object and false otherwise</returns>
        public override bool Equals(object o)
        {
            if (!(o is DigraphPath))
            {
                throw new Exception("You cannot compare digraph with another type object");
            }
            DigraphPath p = o as DigraphPath;
            if (p.edges.Count != edges.Count)
            {
                return false;
            }
            for (int i = 0; i < edges.Count; i++)
            {
                if (p.edges[i] != edges[i])
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Arrow of path
        /// </summary>
        public ICategoryArrow Arrow
        {
            get
            {
                int n = Count;
                ICategoryArrow arrow = this[n - 1].Object as ICategoryArrow;
                for (int i = n - 2; i >= 0; i++)
                {
                    ICategoryArrow a = this[i].Object as ICategoryArrow;
                    arrow = a.Compose(null, arrow);
                }
                return arrow;
            }
        }

        /// <summary>
        /// Gets arrow of this path
        /// </summary>
        /// <param name="category">Arrow category</param>
        /// <returns>The arrow</returns>
        public ICategoryArrow GetArrow(ICategory category)
        {
            int n = Count;
            ICategoryArrow arrow = this[n - 1].Object as ICategoryArrow;
            for (int i = n - 2; i >= 0; i++)
            {
                ICategoryArrow a = this[i].Object as ICategoryArrow;
                arrow = a.Compose(category, arrow);
            }
            return arrow;
        }

        /// <summary>
        /// Path source
        /// </summary>
        public DigraphVertex Source
        {
            get
            {
                DigraphEdge e = edges[0] as DigraphEdge;
                return e.Source;
            }
        }

        /// <summary>
        /// Path target
        /// </summary>
        public DigraphVertex Target
        {
            get
            {
                DigraphEdge e = edges[edges.Count - 1] as DigraphEdge;
                return e.Target;
            }
        }
    }
}

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