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

using FormulaEditor.Interfaces;

namespace FormulaEditor
{

    /// <summary>
    /// Conditional operation
    /// </summary>
    public class OptionalOperation : IObjectOperation, ICloneable, IFormulaCreatorOperation
    {

        /// <summary>
        /// Type
        /// </summary>
        public object type;


        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type">Type</param>
        public OptionalOperation(object type)
        {
            this.type = type;
        }

        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Operation tree</param>
        /// <param name="level">Formula level</param>
        /// <param name="sizes">Sizes of symbols</param>
        /// <returns>The formula</returns>
        public MathFormula CreateFormula(ObjectFormulaTree tree, byte level, int[] sizes)
        {
            return CreateFormula(this, tree, level, sizes, "?:");
        }



        /// <summary>
        /// Operation priority
        /// </summary>
        public int OperationPriority
        {
            get
            {
                return (int)ElementaryOperationPriorities.Optional;
            }
        }



        /// <summary>
        /// Clones itself
        /// </summary>
        /// <returns>A clone</returns>
        public object Clone()
        {
            return new OptionalOperation(type);
        }

        /// <summary>
        /// Arity of this operation
        /// </summary>
        public int Arity
        {
            get
            {
                return 3;
            }
        }

        /// <summary>
        /// Calculates result of this operation
        /// </summary>
        public object this[object[] x]
        {
            get
            {
                bool b = (bool)x[0];
                return b ? x[1] : x[2];
            }
        }

        /// <summary>
        /// Return type
        /// </summary>
        public object ReturnType
        {
            get
            {
                return type;
            }
        }

        /// <summary>
        /// The "is powered" sign
        /// </summary>
        public bool IsPowered
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// Creates formula
        /// </summary>
        /// <param name="tree">Tree</param>
        /// <param name="level">Level</param>
        /// <param name="sizes">Sizes</param>
        /// <param name="str">Str of binrary</param>
        /// <returns>The formuls</returns>
        public static MathFormula CreateFormula(IFormulaCreatorOperation operation, ObjectFormulaTree tree, byte level, int[] sizes, string str)
        {
            MathFormula form = new MathFormula(level, sizes);
            for (int i = 0; i < str.Length + 1; i++)
            {
                IFormulaCreatorOperation op = tree[i].Operation as IFormulaCreatorOperation;
                MathFormula f = op.CreateFormula(tree[i], level, sizes);
                MathFormula fp = null;
                if (op.OperationPriority < operation.OperationPriority)
                {
                    fp = new MathFormula(level, sizes);
                    MathSymbol s = new BracketsSymbol();
                    s.Append(fp);
                    fp.First[0] = f;
                }
                else
                {
                    fp = f;
                }
                form.Add(fp);
                if (i < str.Length)
                {
                    MathSymbol s = new BinarySymbol(str[i]);
                    s.Append(form);
                }
            }
            return form;
        }
    }
}

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