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

Universal Framework for Science and Engineering - Part 6: Determination of Orbits of Artificial Satellites

Rate me:
Please Sign up or sign in to vote.
4.88/5 (28 votes)
8 Jul 2011CPOL19 min read 82.5K   6.6K   82  
An article on framework applications to determine the orbits of artificial satellites
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CategoryTheory
{
    /// <summary>
    /// Extensions methods
    /// </summary>
    public static class StaticExtension
    {
        /// <summary>
        /// Exception message
        /// </summary>
        public static readonly string TargetSourceArrow = "Target of first arrow does not coincide with source of next arrow";

        /// <summary>
        /// Gets interface of object
        /// </summary>
        /// <param name="obj">object</param>
        /// <param name="interfaceName">Interface name</param>
        /// <returns>The interface</returns>
        static object GetInterface(this IAssociatedObject obj, string interfaceName)
        {
            Type t = obj.GetType();
            Type ti = t.GetInterface(interfaceName);
            if (ti != null)
            {
                return obj;
            }
            if (obj is IChildrenObject)
            {
                IChildrenObject co = obj as IChildrenObject;
                IAssociatedObject[] ch = co.Children;
                if (ch != null)
                {
                    foreach (IAssociatedObject ob in ch)
                    {
                        if (ob != null)
                        {
                            object o = GetInterface(ob, interfaceName);
                            if (o != null)
                            {
                                return o;
                            }
                        }
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Gets object of predefined type
        /// </summary>
        /// <typeparam name="T">The type</typeparam>
        /// <param name="obj">The prototype</param>
        /// <returns>The object of predefined type</returns>
        public static T GetObject<T>(this IAssociatedObject obj) where T : class
        {
            // If o is subtype of t
            if (obj is T)
            {
                // Returns o as T
                return obj as T;
            }
            // Search in childen
            // If o is IChildrenObject
            if (obj is IChildrenObject)
            {
                IChildrenObject co = obj as IChildrenObject;
                // Gets children
                IAssociatedObject[] ch = co.Children;
                if (ch != null)
                {
                    // Recursive search among children
                    foreach (IAssociatedObject ob in ch)
                    {
                        T a = GetObject<T>(ob);
                        // If chid object is found
                        if (a != null)
                        {
                            // Returns the object
                            return a;
                        }
                    }
                }
            }
            return null;
        }


        /// <summary>
        /// Try to get object of predefined type
        /// </summary>
        /// <typeparam name="T">The type</typeparam>
        /// <param name="obj">The prototype</param>
        /// <param name="message">The exception message</param>
        /// <returns>The object of predefined type</returns>
        public static T GetObject<T>(this IAssociatedObject obj, string message) where T : class
        {
            T a = GetObject<T>(obj);
            if (a != null)
            {
                return a;
            }
            throw new Exception(message);
        }

 

        /// <summary>
        /// Try to get source of the arrow
        /// </summary>
        /// <typeparam name="T">Source type</typeparam>
        /// <param name="obj">The prototype</param>
        /// <returns>The source</returns>
        public static T GetSource<T>(this IAssociatedObject obj) where T : class
        {
            return GetObject<T>(obj, CategoryException.IllegalSource);
        }

        /// <summary>
        /// Try to get target of arrow
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj">Target type</param>
        /// <returns>The target</returns>
        public static T GetTarget<T>(this IAssociatedObject obj) where T : class
        {
            return GetObject<T>(obj, CategoryException.IllegalTarget);
        }

        /// <summary>
        /// Gets simple object
        /// </summary>
        /// <typeparam name="T">Type of object</typeparam>
        /// <param name="o">Initial object</param>
        /// <returns>The object</returns>
        public static T GetSimpleObject<T>(this object o) where T : class
        {
            if (o is T)
            {
                return o as T;
            }
            return null;
        }


        /// <summary>
        /// Gets simple object
        /// </summary>
        /// <typeparam name="T">Type of object</typeparam>
        /// <param name="o">Initial object</param>
        /// <param name="message">Exception message</param>
        /// <returns>The object</returns>
        public static T GetSimpleObject<T>(this object o, string message) where T : class
        {
            T t = GetSimpleObject<T>(o);
            if (t != null)
            {
                return t;
            }
            throw new Exception(message);
        }


        /// <summary>
        /// Sets associated object to object and all its children;
        /// </summary>
        /// <param name="ao">The associated object</param>
        /// <param name="obj">The object to set</param>
        static public void SetAssociatedObject(this IAssociatedObject ao, object obj)
        {
            ao.Object = obj;
            if (ao is IChildrenObject)
            {
                IChildrenObject co = ao as IChildrenObject;
                IAssociatedObject[] ch = co.Children;
                if (ch != null)
                {
                    foreach (IAssociatedObject o in ch)
                    {
                        if (o != null)
                        {
                            SetAssociatedObject(o, obj);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Checks connection between first and next arrow
        /// </summary>
        /// <param name="first">The first arrow</param>
        /// <param name="next">The next arrow</param>
        static public void CheckArrowConnection(this ICategoryArrow first, ICategoryArrow next)
        {
            if (first.Source != next.Target)
            {
                throw new CategoryException(TargetSourceArrow);
            }
        }
    }
}

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