internal void CloneMember(ref FormulaTree member, CloneSpriteContext context)
 {
     if (member != null)
     {
         member = member.Clone(context);
     }
 }
 internal void CloneMember(ref FormulaTree member)
 {
     if (member != null)
     {
         member = member.Clone();
     }
 }
 private void ViewModelOnPropertyChanged(object sender, PropertyChangedEventArgs arg)
 {
     if (arg.PropertyName == "Formula")
     {
         Formula = ((FormulaEditorViewModel)sender).Formula;
     }
 }
 internal static FormulaTokenUnaryParameter CreateUnaryParameterToken(FormulaTree arg)
 {
     return(new FormulaTokenUnaryParameter
     {
         Parameter = arg,
     });
 }
 internal static FormulaTokenBinaryParameter CreateBinaryParameterToken(FormulaTree arg1, FormulaTree arg2)
 {
     return(new FormulaTokenBinaryParameter
     {
         FirstParameter = arg1,
         SecondParameter = arg2
     });
 }
Example #6
0
 public void TestEvaluate(double xVariableValue, double yVariableValue, double expectedResult)
 {
     FormulaTree formulaTree = new FormulaTree(_formulaRoot);
     formulaTree.Variables[0].Value = xVariableValue;
     formulaTree.Variables[1].Value = yVariableValue;
     double result = new FormulaTree(_formulaRoot).Evaluate();
     Assert.AreEqual(expectedResult, result);
 }
Example #7
0
        public static object Evaluate(FormulaTree formula)
        {
            if (formula == null)
            {
                return(null);
            }

            return(formula.IsNumber() ? (object)formula.EvaluateNumber() : formula.EvaluateLogic());
        }
        public static IEnumerable <IFormulaToken> Tokenize(FormulaTree formula)
        {
            if (formula == null)
            {
                return(null);
            }

            return(formula.Tokenize());
        }
Example #9
0
        public static string Serialize(FormulaTree formula)
        {
            if (formula == null)
            {
                return(string.Empty);
            }

            var sb = new StringBuilder();

            formula.Append(sb);
            return(sb.ToString());
        }
        private void InterpretTokens()
        {
            var interpretedFormula = FormulaInterpreter.Interpret(Tokens, out _parsingError);

            if (interpretedFormula != null)
            {
                _formula = interpretedFormula;
                RaisePropertyChanged(() => Formula);
            }
            RaisePropertyChanged(() => ParsingError);
            RaisePropertyChanged(() => HasError);
        }
 private static FormulaTree RemoveParentheses(FormulaTree node)
 {
     while (true)
     {
         var parenthesesNode = node as FormulaNodeParentheses;
         if (parenthesesNode == null)
         {
             return(node);
         }
         node = parenthesesNode.Child;
     }
 }
Example #12
0
        public static void Render(FormulaTree formulaTree, Range[] ranges, Size imageSize, ColorTransformation colorTransformation,
            bool reevaluateValues, int threadsCount, FormulaRenderResult formulaRenderResult)
        {
            using (ProgressReporter.CreateScope())
            {
                double evaluationSpan = 0;
                if (reevaluateValues)
                {
                    evaluationSpan = 0.93;
                    using (ProgressReporter.CreateScope(evaluationSpan))
                    {
                        EvaluateFormula(formulaTree, ranges, imageSize, threadsCount, formulaRenderResult.EvaluatedValuesBuffer);
                    }
                }

                using (ProgressReporter.CreateScope(1 - evaluationSpan))
                {
                    Render(colorTransformation, formulaRenderResult, threadsCount);
                }
            }
        }
Example #13
0
        private bool CheckSensorSupportOfBrickFormula(Brick brick, FormulaTree value)
        {
            if (!CheckAcceleration(brick, value) ||
                !CheckCompass(brick, value) ||
                !CheckInclinometer(brick, value) ||
                !CheckMicrophone(brick, value))
            {
                return(false);
            }

            foreach (FormulaTree child in value.Children)
            {
                if (!CheckAcceleration(brick, value) ||
                    !CheckCompass(brick, value) ||
                    !CheckInclinometer(brick, value) ||
                    !CheckMicrophone(brick, value))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #14
0
 public FormulaRenderArguments(FormulaTree formulaTree, Range[] ranges, ColorTransformation colorTransformation)
 {
     FormulaTree = formulaTree;
     Ranges = ranges;
     ColorTransformation = colorTransformation;
 }
Example #15
0
 public static string Serialize(FormulaTree formulaTree)
 {
     return TreeSerializer.Serialize(formulaTree.Root, op => op.Name);
 }
Example #16
0
 public static double EvaluateNumber(FormulaTree formula)
 {
     return(formula == null ? 0 : formula.EvaluateNumber());
 }
Example #17
0
 public SemanticsErrorException(FormulaTree node, string message) : base(message)
 {
     Node = node;
 }
Example #18
0
 public static bool EvaluateLogic(FormulaTree formula)
 {
     return(formula != null && formula.EvaluateLogic());
 }
Example #19
0
        private static void EvaluateFormula(FormulaTree formulaTree, Range[] ranges, Size areaSize, int threadsCount, float[] evaluatedValuesBuffer)
        {
            Stopwatch evaluationStopwatch = new Stopwatch();
            evaluationStopwatch.Start();

            if (evaluatedValuesBuffer.Length != areaSize.Square)
                throw new ArgumentException("Result buffer size isn't equal to ranges area size.", "evaluatedValuesBuffer");

            int xCount = areaSize.Width;
            int yCount = areaSize.Height;

            if (threadsCount < 1)
                threadsCount = 1;

            ranges = ranges.Select((r, i) => new Range(r.Start, r.End, i % 2 == 0 ? areaSize.Width : areaSize.Height)).ToArray();
            const int progressSteps = 100;
            using (ProgressReporter.CreateScope(progressSteps))
            {
                int steps = 0;
                double lastProgress = 0;
                double progress = 0;
                ProgressObserver progressObserver = new ProgressObserver(p => progress = p.Progress);

                Task[] tasks = new Task[threadsCount];
                int yStepCount = yCount/threadsCount;
                for (int i = 0; i < threadsCount; i++)
                {
                    int li = i;
                    tasks[i] = Task.Run(() =>
                    {
                        FormulaTree ft = li == 0 ? formulaTree : FormulaTreeSerializer.Deserialize(FormulaTreeSerializer.Serialize(formulaTree));
                        int yStart = li * yStepCount;
                        ProgressReporter.Subscribe(progressObserver);
                        ft.EvaluateRangesIn2DProjection(ranges, xCount, yStart, yStepCount,
                            evaluatedValuesBuffer);
                    });
                }

                while (tasks.Any(t => t.Status != TaskStatus.RanToCompletion && t.Status != TaskStatus.Faulted && t.Status != TaskStatus.Canceled))
                {
                    Task.WaitAny(tasks, 100);
                    double progressCopy = progress;
                    int inc = (int)((progressCopy - lastProgress) * progressSteps);
                    if (inc > 0)
                    {
                        for (int i = 0; i < inc && steps < progressSteps; i++)
                        {
                            ProgressReporter.Increase();
                            steps++;
                        }
                        lastProgress = progress;
                    }
                }
                Task.WaitAll();
            }

            //double x = 1;
            //double y = 2;
            //double z = 3;
            //double w = 4;
            //double x1 = -1;
            //double y1 = -2;
            //double z1 = -3;
            //double w1 = -4;
            //Stopwatch stopwatch2 = new Stopwatch();
            //stopwatch2.Start();
            //double[] arr = new double[Ranges[0].Count*Ranges[1].Count];
            //for (int i = 0; i < arr.Length; i++)
            //{
            //    arr[i] = Math.Sqrt((Math.Sin(x) * Math.Sin(y) + Math.Sin(z) * Math.Sin(w)) * (Math.Sin(x1) * Math.Sin(y1) + Math.Sin(z1) * Math.Sin(w1)));
            //    x += 0.1;
            //    y += 0.1;
            //    z += 0.1;
            //    w += 0.1;
            //    x1 += 0.3;
            //    y1 += 0.3;
            //    z1 += 0.3;
            //    w1 += 0.3;
            //}
            //stopwatch2.Stop();

            evaluationStopwatch.Stop();
        }
Example #20
0
 internal static IEnumerable <FormulaTree> AsEnumerable(this FormulaTree node)
 {
     return(node == null
         ? Enumerable.Empty <FormulaTree>()
         : Enumerable.Repeat(node, 1).Concat(node.Children.SelectMany(child => child.AsEnumerable())));
 }
 /// <remarks>Implemented by <see cref="IXmlObjectConvertible{XmlFormulaTree}" />.</remarks>
 public XmlFormula ConvertBack(FormulaTree formula)
 {
     return(formula == null ? null : formula.ToXmlObject());
 }