Exemple #1
0
        /// <summary>
        /// Pattern for Coordinate of the point,
        /// such as "Y=3", "x=4.0", "x+1=2", -5.0
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="?"></param>
        /// <param name="coord"></param>
        /// <returns></returns>
        public static bool IsCoordinateTerm(this starPadSDK.MathExpr.Expr expr,
                                            out object coord)
        {
            coord = null;
            if (expr.IsNumeric(out coord))
            {
                return(true);
            }

            object ll;

            if (expr.IsLabel(out ll))
            {
                coord = ll;
                return(true);
            }

            if (expr.IsTerm(out coord))
            {
                return(true);
            }

            //TODO ""x+1=2""
            //ContainLabel

            return(false);
        }
Exemple #2
0
        /// <summary>
        /// Pattern for Coordinate, such as "Y=3", "x=4.0"
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="coord"></param>
        /// <returns></returns>
        public static bool IsTerm(this starPadSDK.MathExpr.Expr expr,
                                  out object coord)
        {
            coord = null;
            var compExpr = expr as CompositeExpr;

            if (compExpr != null &&
                compExpr.Head.Equals(WellKnownSym.equals) &&
                compExpr.Args.Count() == 2)
            {
                var expr1 = compExpr.Args[0] as starPadSDK.MathExpr.Expr;
                var expr2 = compExpr.Args[1] as starPadSDK.MathExpr.Expr;

                object label;
                object number;
                if (expr1.IsLabel(out label) && expr2.IsNumeric(out number))
                {
                    //coord = new KeyValuePair<object, object>(label, number);
                    //var goal = new EqGoal();
                    //coord = new AGPropertyExpr(expr, new EqGoal(new Var(label), number));
                    coord = new EqGoal(new Var(label), number);
                    return(true);
                }
                else if (expr1.IsNumeric(out number) && expr2.IsLabel(out label))
                {
                    //coord = new KeyValuePair<object, object>(label,number);
                    //coord = new EqGoal(new Var(label), number);\
                    coord = new EqGoal(new Var(label), number);
                    return(true);
                }
            }

            return(false);
        }
Exemple #3
0
        public static bool IsLineRel(this starPadSDK.MathExpr.Expr expr, out LineSymbol ls)
        {
            ls = null;
            var compExpr = expr as CompositeExpr;

            if (compExpr == null)
            {
                return(false);
            }

            if (compExpr.Head.Equals(WellKnownSym.times) &&
                compExpr.Args.Count() == 2)
            {
                var underExpr = compExpr.Args[1] as CompositeExpr;
                if (underExpr == null)
                {
                    return(false);
                }
                if (underExpr.Head.Equals(WellKnownSym.divide) &&
                    underExpr.Args.Count() == 1)
                {
                    var expr1 = underExpr.Args[0] as CompositeExpr;
                    if (expr1 == null)
                    {
                        return(false);
                    }

                    if (expr1.Head.Equals(WellKnownSym.times) &&
                        expr1.Args.Count() == 2)
                    {
                        object obj1, obj2;
                        var    result1 = expr1.Args[0].IsLabel(out obj1);
                        var    result2 = expr1.Args[1].IsLabel(out obj2);

                        if (result1 && result2)
                        {
                            var str1 = obj1 as string;
                            var str2 = obj2 as string;
                            Debug.Assert(str1 != null);
                            Debug.Assert(str2 != null);
                            var label = str1 + str2;
                            var line  = new Line(label);
                            ls = new LineSymbol(line);
                            return(true);
                        }
                        return(false);
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(false);
            }
            return(false);
        }
Exemple #4
0
        public static bool IsNumeric(this starPadSDK.MathExpr.Expr expr,
                                     out object number)
        {
            number = null;

            var integerExpr = expr as IntegerNumber;

            if (integerExpr != null)
            {
                number = int.Parse(integerExpr.Num.ToString());
                return(true);
            }

            var doubleExpr = expr as DoubleNumber;

            if (doubleExpr != null)
            {
                number = double.Parse(doubleExpr.Num.ToString());
                return(true);
            }

            Expr tempExpr;

            if (IsNegativeTerm(expr, out tempExpr))
            {
                if (tempExpr.IsNumeric(out number))
                {
                    int n;
                    if (Utils.IsInt(number, out n))
                    {
                        number = -1 * n;
                        return(true);
                    }

                    double d;
                    if (Utils.IsDouble(number, out d))
                    {
                        number = -1 * d;
                        return(true);
                    }
                }
            }

            return(false);
        }
Exemple #5
0
        /// <summary>
        /// A(3.0,-4.0), (-3.0, x), B(x+1=9, 9), C(x,y)
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        public static bool IsPoint(this starPadSDK.MathExpr.Expr expr, out object point)
        {
            point = null;
            object label;

            var composite = expr as CompositeExpr;

            if (composite == null)
            {
                return(false);
            }

            var headExpr = composite.Head;
            var hasLabel = headExpr.IsLabel(out label);

            if (!hasLabel)
            {
                var wordSymbol      = composite.Head as WordSym;
                var wellKnownSymbol = composite.Head as WellKnownSym;
                if (wordSymbol != null)
                {
                    if (wordSymbol.Word.Equals(""))
                    {
                        if (composite.Args.Count() == 1)
                        {
                            return(IsPoint(composite.Args[0], out point));
                        }
                    }

                    /*if (!wordSymbol.Word.Equals("comma"))
                     * {
                     *  return false;
                     * }*/
                }
                else if (wellKnownSymbol != null)
                {
                    if (!wellKnownSymbol.ID.Equals(WKSID.comma))
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }

            if (composite.Args.Length != 2)
            {
                return(false);
            }

            var expr1 = composite.Args[0];
            var expr2 = composite.Args[1];

            object coord1 = null;
            object coord2 = null;

            bool isPointForm =
                expr1.IsCoordinateTerm(out coord1) &&
                expr2.IsCoordinateTerm(out coord2);

            if (isPointForm)
            {
                if (hasLabel)
                {
                    var temp = CreatePointSymbol((string)label, coord1, coord2);
                    //point = new AGShapeExpr(expr, temp);
                    point = temp;
                }
                else
                {
                    var temp = CreatePointSymbol(coord1, coord2);
                    //point = new AGShapeExpr(expr, temp);
                    point = temp;
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemple #6
0
        public static bool IsQuery(this starPadSDK.MathExpr.Expr expr, out object property)
        {
            property = null;
            if (!(expr is CompositeExpr))
            {
                return(false);
            }
            var composite = expr as CompositeExpr;

            if (!composite.Head.Equals(WellKnownSym.equals))
            {
                return(false);
            }

            if (composite.Args.Length == 1)
            {
                var expr1 = composite.Args[0];

                object obj;
                var    result = expr1.IsLabel(out obj);
                if (result)
                {
                    //property = new KeyValuePair<string, object>("Label", new Var(obj));
                    property = new Query(new Var(obj));
                    return(true);
                }
                result = expr1.IsExpression(out obj);
                if (result)
                {
                    //TODO
                    //property = new KeyValuePair<string, object>("Term", obj);
                    property = new Query(obj);
                    return(true);
                }
                return(false);
            }
            else if (composite.Args.Length == 2)
            {
                var expr1 = composite.Args[0];
                var expr2 = composite.Args[1];

                if (expr2 is ErrorExpr)
                {
                    object obj;
                    var    result = expr1.IsLabel(out obj);
                    if (result)
                    {
                        //property = new KeyValuePair<string, object>("Label", new Var(obj));
                        property = new Query(new Var(obj));
                        return(true);
                    }

                    result = expr1.IsExpression(out obj);
                    if (result)
                    {
                        //property = new KeyValuePair<string, object>("Term", obj);
                        property = new Query(obj, null);
                        return(true);
                    }
                    return(false);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }
Exemple #7
0
        /// <summary>
        /// Pattern for label: such as "A", "c", "XT","c12","c_1"
        /// False: 2A, 12mm,2m1,
        /// </summary>
        /// <param name="expr"></param>
        /// <param name="label"></param>
        /// <returns></returns>
        public static bool IsLabel(this starPadSDK.MathExpr.Expr expr,
                                   out object label)
        {
            label = null;
            if (expr is LetterSym)
            {
                var letter = expr as LetterSym;
                label = letter.Letter.ToString();
                return(true);
            }
            else if (expr is WellKnownSym)
            {
                var comma = expr as WellKnownSym;
                if (comma.Equals(WellKnownSym.comma))
                {
                    return(false);
                }
            }
            else if (expr is WordSym)
            {
                var word = expr as WordSym;
                if (word.Word.Equals("comma") || word.Word.Equals(""))
                {
                    return(false);
                }
                label = word.Word;
                return(true);
            }
            else if (expr is CompositeExpr) // merge labels
            {
                var composite = expr as CompositeExpr;
                if (composite.Head.Equals(WellKnownSym.times))
                {
                    var builder = new StringBuilder();

                    if (composite.Args.Count() == 2)
                    {
                        object tempObj0;
                        var    result = composite.Args[0].IsLabel(out tempObj0);
                        if (result)
                        {
                            object tempObj1;
                            bool   result2 = composite.Args[1].IsNumeric(out tempObj1);
                            if (result2)
                            {
                                builder.Append(tempObj0);
                                builder.Append(tempObj1);
                                label = builder.ToString();
                                return(true);
                            }
                        }
                    }

                    foreach (var tempExpr in composite.Args)
                    {
                        object tempObj;
                        var    result = tempExpr.IsLabel(out tempObj);
                        if (!result)
                        {
                            return(false);
                        }
                        builder.Append(tempObj);
                    }
                    label = builder.ToString();
                    return(true);
                }
            }
            return(false);
        }