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); }
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 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 }); }