Exemple #1
0
        public void DoStep(string stepName, int times)
        {
            var step = Steps.FirstOrDefault(m => m.SourceExpression.Title == stepName);

            if (step.State == StepState.Unready)
            {
                _DoStep.State = StepState.Unready;
                return;
            }
            if (step == null)
            {
                throw new Exception("No such step,please check stepName");
            }
            else
            {
                for (int i = 0; i < times; i++)
                {
                    step.Evaluate(_vc);
                }
            }
            foreach (string key in step.OutVariables)
            {
                SetValueAcrossSteps(key, _vc.GetValue(key));
            }
        }
Exemple #2
0
        public static object Evaluate(Expression[] expressions, Dictionary <string, object> variables)
        {
            VariableContext c = Funcs.CreateVariableContext(variables);

            object[] result =
                expressions
                .Select(i => new Mathy.Language.MathyLanguageService().CreateEvaluator().Evaluate(i, c))
                .ToArray();


            variables.Clear();
            foreach (string variableName in c.GetAllVariables())
            {
                variables.Add(variableName, c.GetValue(variableName));
            }


            return(result.Length == 1 ? result[0] : result);
        }
Exemple #3
0
        public void Update()
        {
            VariableContext vc = CreateVariableContext();

            foreach (Step step in Steps)
            {
                UpdateStepState(step);
                _DoStep = step;
                if (step.State == StepState.Ready && step.Conditions.Length > 0)
                {
                    try
                    {
                        if (!step.EvaluateConditions(vc))
                        {
                            step.State = StepState.Skipped;
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.InnerException == null)
                        {
                            throw;
                        }
                        else
                        {
                            throw ex.InnerException;
                        }
                    }
                }

                if (step.State == StepState.Ready)
                {
                    try
                    {
                        step.Evaluate(vc);
                        this._vc = vc;
                        foreach (string key in step.OutVariables)
                        {
                            SetValueAcrossSteps(key, _vc.GetValue(key));
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.InnerException == null)
                        {
                            throw;
                        }
                        else
                        {
                            throw ex.InnerException;
                        }
                    }
                }
            }


            variables.Clear();

            foreach (string variable in vc.GetAllVariables())
            {
                SetValue(variable, vc.GetValue(variable));
            }
        }
        private object EvaluteFunctionCallExpression(FunctionCallExpression expression, VariableContext context)
        {
            List <object> parameters = new List <object>();

            for (int i = 0; i <= expression.Parameters.Length - 1; i++)
            {
                object p = Evaluate(expression.Parameters[i], context);

                parameters.Add(p);
            }

            if (expression.Method.GetParameters().Length > 0 && expression.Method.GetParameters().Last().ParameterType == typeof(VariableContext))
            {
                parameters.Add(context);
            }

            if (expression.MethodName == "Loop" || expression.MethodName.Contains("MCM") || expression.MethodName.Contains("Draw_"))
            {
                parameters.Add(context.GetValue("_EvaluationContext"));
            }

            if (expression.IsCustomFunc)
            {
                List <CoefficientDetail> data;
                if (context.HasVariable(expression.MethodName))
                {
                    data = (List <CoefficientDetail>)context.GetValue(expression.MethodName);
                }
                else
                {
                    var dal = new CoefficientDAL();
                    data = dal.GetCoefficientDetail(expression.MethodName);
                }
                //var a = Evaluate(expression.Parameters[0], context);
                var colName  = Convert.ToInt32(Evaluate(expression.Parameters[0], context));
                var rowIndex = Convert.ToInt32(Evaluate(expression.Parameters[1], context));
                return(Convert.ToDouble(data.FirstOrDefault(m => m.CoefficientDetailRow == rowIndex && m.CoefficientDetailIndex == colName).CoefficientDetailValue));
            }

            try
            {
                //可变参数处理
                if (expression.Parameters.Count() > 1 && parameters[0].GetType() != typeof(double[]))
                {
                    var p = new List <object>();
                    var o = new List <double[]>();
                    foreach (var n in parameters)
                    {
                        if (n.GetType() != typeof(double[]))
                        {
                            p.Add(n);
                        }
                        else
                        {
                            o.Add(n as double[]);
                        }
                    }
                    if (o.Count > 0)
                    {
                        p.Add(o.ToArray());
                        parameters = p;
                    }
                }
                return(expression.Method.Invoke(this, parameters.ToArray()));
            }
            catch (Exception ex)
            {
                throw ex;
                // throw new Exception(expression.MethodName + "\r\n" + (ex.InnerException == null ? ex.Message : ex.InnerException.Message));
            }
        }
 private object EvaluateVariableExpression(VariableExpression expression, VariableContext context)
 {
     return(context.GetValue(expression.VariableName));
 }
Exemple #6
0
        private static RenderBranch Create(double x, double y, int angle, TreeBranch branch, VariableContext variables, Graphics g, Font font)
        {
            double x2 = x - branch.Position * Math.Cos(angle * Math.PI / 180);
            double y2 = y + branch.Position * Math.Sin(angle * Math.PI / 180);

            double x1 = x2 + branch.Length * Math.Cos((angle + branch.Angle) * Math.PI / 180);
            double y1 = y2 - branch.Length * Math.Sin((angle + branch.Angle) * Math.PI / 180);

            double a = Funcs.Atan(x1, y1, x2, y2);

            x2 = x1 + (branch.Length - 2) * Math.Cos(a);
            y2 = y1 - (branch.Length - 2) * Math.Sin(a);

            RenderBranch[] branches = branch.Branches.Select(i => Create(x2, y2, angle + branch.Angle - 180, i, variables, g, font)).ToArray();


            string description;

            double imageX          = 0;
            double imageY          = 0;
            Bitmap expressionImage = null;

            if (branch is ExpressionBranch)
            {
                string[] preVariables = variables.GetAllVariables();

                Expression[] expressions = new MathyLanguageService().Compile((branch as ExpressionBranch).Expression, variables);
                foreach (Expression expression in expressions)
                {
                    new MathyLanguageService().CreateEvaluator().Evaluate(expression, variables);
                }


                StringBuilder b = new StringBuilder();

                foreach (string variableName in variables.GetAllVariables().Where(i => !preVariables.Contains(i)))
                {
                    b.AppendFormat("{0}={1}", variableName, variables.GetValue(variableName));
                }


                description = branch.Description + "\r\n" + b.ToString();


                double xt = x2 + (branch as ExpressionBranch).ImageX * Math.Cos(a + Math.PI);
                double yt = y2 - (branch as ExpressionBranch).ImageX * Math.Sin(a + Math.PI);

                double d = (branch as ExpressionBranch).ImageY;

                imageX = xt + d * Math.Cos(a + Math.PI / 2);
                imageY = yt - d * Math.Sin(a + Math.PI / 2);

                expressionImage = new NodeVisualizer(expressions.Select(i => new NodeConverter().Convert(i)).ToArray()).VisulizeAsBitmap();
            }
            else
            {
                string variableName = (branch as VariableBranch).VariableName;
                object value        = variables.GetValue(variableName);
                description = string.Format("{0}\r\n{1}={2}", branch.Description, variableName, value);
            }


            double dx;
            double dy;

            EvaluateDescriptionPosition(branch, description, x1, y1, x2, y2, g, font, out dx, out dy);


            return(new RenderBranch()
            {
                X1 = x1,
                Y1 = y1,
                X2 = x2,
                Y2 = y2,
                IsVariable = branch is VariableBranch,
                ImageX = imageX,
                ImageY = imageY,
                Image = expressionImage,
                Branches = branches,
                DescriptionX = dx,
                DescriptionY = dy,
                Description = description
            });
        }