public FormulaInfoDTO MapFrom(IFormula formula)
 {
     return(new FormulaInfoDTO {
         Name = formula.Name, Type = formula.GetType()
     });
 }
示例#2
0
        public bool FormulasAreTheSame(IFormula firstFormula, IFormula secondFormula)
        {
            if (firstFormula == null && secondFormula == null)
            {
                return(true);
            }

            if (firstFormula == null || secondFormula == null)
            {
                return(false);
            }

            var firstType  = firstFormula.GetType();
            var secondType = secondFormula.GetType();

            if (firstType != secondType)
            {
                return(false);
            }

            //nothing more to check for distributed formula or black box formula
            if (firstFormula.IsDistributed() || firstFormula.IsBlackBox() || firstFormula.IsDynamic())
            {
                return(true);
            }

            if (firstFormula.IsConstant())
            {
                var firstConstFormula  = firstFormula.DowncastTo <ConstantFormula>();
                var secondConstFormula = secondFormula.DowncastTo <ConstantFormula>();
                return(firstConstFormula.Value == secondConstFormula.Value);
            }

            if (firstFormula.IsExplicit())
            {
                var firstExplicit  = firstFormula.DowncastTo <ExplicitFormula>();
                var secondExplicit = secondFormula.DowncastTo <ExplicitFormula>();

                if (!string.Equals(firstExplicit.FormulaString, secondExplicit.FormulaString))
                {
                    return(false);
                }

                //check that formula have the same references using the same alias
                var firstObjectPathCache = new Cache <string, IFormulaUsablePath>(x => x.Alias);
                firstObjectPathCache.AddRange(firstExplicit.ObjectPaths);
                var secondObjectPathCache = new Cache <string, IFormulaUsablePath>(x => x.Alias);
                secondObjectPathCache.AddRange(secondExplicit.ObjectPaths);


                if (firstObjectPathCache.Count() != secondObjectPathCache.Count())
                {
                    return(false);
                }

                foreach (var keyValue in firstObjectPathCache.KeyValues)
                {
                    if (!secondObjectPathCache.Contains(keyValue.Key))
                    {
                        return(false);
                    }
                    var path = secondObjectPathCache[keyValue.Key];
                    if (!path.Equals(keyValue.Value))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
 public DistributionFormulaType MapFrom(IFormula formula)
 {
     if (formula == null)
     {
         return(DistributionFormulaType.DiscreteDistribution);
     }
     if (formula.IsAnImplementationOf <DiscreteDistributionFormula>() || formula.IsConstant())
     {
         return(DistributionFormulaType.DiscreteDistribution);
     }
     if (formula.IsAnImplementationOf <UniformDistributionFormula>())
     {
         return(DistributionFormulaType.UniformDistribution);
     }
     if (formula.IsAnImplementationOf <NormalDistributionFormula>())
     {
         return(DistributionFormulaType.NormalDistribution);
     }
     if (formula.IsAnImplementationOf <LogNormalDistributionFormula>())
     {
         return(DistributionFormulaType.LogNormalDistribution);
     }
     throw new MoBiException(AppConstants.Exceptions.UnknownDistributedFormula(formula.GetType()));
 }
示例#4
0
 public IEditTypedFormulaPresenter PresenterFor(IFormula formula)
 {
     return(PresenterFor(formula.GetType()));
 }
示例#5
0
        public static void Main(string[] args)
        {
            SaveDir = Application.StartupPath + "/save/";
            if (!Directory.Exists(SaveDir))
            {
                Directory.CreateDirectory(SaveDir);
            }


            RuntimeSetting setting = new RuntimeSetting();
            RuntimeData    data    = new Runtime.RuntimeData(setting);
            IFormula       formula = null;

            Analyzer.Analyzer analyzer = null;

            Console.WriteLine("============================");
            Console.WriteLine("    Func Calc ");
            Console.WriteLine("============================");
            for (;;)
            {
                Console.WriteLine(" 1. New Formula");
                Console.WriteLine(" 2. Show Functions");
                Console.WriteLine(" 3. Show Operators");
                if (formula != null)
                {
                    Console.WriteLine(" 4. Get Result");
                    Console.WriteLine(" 5. Run Addition Formula");
                    Console.WriteLine(" 6. Show DebugInformation");
                    Console.WriteLine(" 7. Show Expression Tree");
                    Console.WriteLine(" 8. Enabled DebugMode");
                }
                Console.WriteLine(" 9. Quit");
                char c = Console.ReadKey(true).KeyChar;
                Console.Clear();

                switch (c)
                {
                case '1':
                    setting = new RuntimeSetting();
                    data    = new RuntimeData(setting);
                    Console.WriteLine("Initialized FormulaRuntime");
                    Console.WriteLine("Input formula");
                    Console.Write("> ");
                    try {
                        analyzer = new Analyzer.Analyzer(Console.ReadLine());
                        Stopwatch sw1 = Stopwatch.StartNew();
                        formula = analyzer.GetResult() as IFormula;
                        sw1.Stop();
                        Console.WriteLine("Success! Time : " + sw1.Elapsed.ToString());
                    }
                    catch (SyntaxException ex) {
                        Console.WriteLine("構文エラーが発生しました。計算式の書式のミスを確認してください。");
                        Console.WriteLine("エラー : " + ex.Message);
                        Console.WriteLine("トークン : {0}", ex.Token);
                        Console.Write("場所 : ");
                        ConsoleColor cc = Console.BackgroundColor;
                        if (analyzer.Tokens == null)
                        {
                            Console.WriteLine("トークン情報がありませんでした");
                        }
                        else
                        {
                            foreach (var t in analyzer.Tokens)
                            {
                                if (t != ex.Token)
                                {
                                    Console.BackgroundColor = cc;
                                }
                                else
                                {
                                    Console.BackgroundColor = ConsoleColor.Red;
                                }
                                Console.Write(t.Text);
                            }
                        }
                        Console.BackgroundColor = cc; Console.WriteLine();
                        Console.WriteLine("詳細: \n{0}", ex.ToString());
                    }
                    catch (RuntimeException ex) {
                        Console.WriteLine("実行エラーが発生しました。");
                        Console.WriteLine("エラー : " + ex.Message);
                        Console.WriteLine("トークン : {0}", ex.Token);
                        Console.WriteLine("詳細: \n{0}", ex.ToString());
                    }
                    break;

                case '2':
                    foreach (var func in data.Functions)
                    {
                        StringBuilder param = new StringBuilder();
                        foreach (var p in func.Value.Parameter)
                        {
                            if (param.Length != 0)
                            {
                                param.Append(", ");
                            }
                            param.Append(p.ToString());
                        }
                        Console.WriteLine("{0}({1}) - {2}",
                                          func.Value.Name, param, func.Value.Description);
                    }
                    break;

                case '3':
                    Console.WriteLine("Prior. L  Op   R - Description");
                    foreach (var op in setting.Spec.Operations.OrderBy(d => d.Value).Reverse())
                    {
                        Console.WriteLine("{0,6} {1} {2,5} {3} - {4}",
                                          op.Value,
                                          op.Key.RequireLeftParameter ? "#" : " ",
                                          op.Key.Text,
                                          op.Key.RequireRightParameter ? "#" : " ",
                                          op.Key.Name);
                    }
                    break;

                case '4':
                    if (formula == null)
                    {
                        continue;
                    }
                    RuntimeData dd  = data.Clone() as RuntimeData;
                    Stopwatch   sw  = Stopwatch.StartNew();
                    var         res = formula.Eval(dd);
                    sw.Stop();
                    Console.WriteLine("Input  : " + analyzer.Line);
                    Console.WriteLine("Formula: " + formula.ToString());
                    Console.WriteLine("Result : " + (res == null ? "戻り値はありませんでした" : res.ToString()));
                    Console.WriteLine("Mathjax: " + (res == null ? "戻り値はありませんでした" : res.Output(OutputType.Mathjax)));
                    Console.WriteLine("Time   : " + sw.Elapsed.ToString());
                    Console.WriteLine("Variables:");
                    foreach (var block in data.Blocks)
                    {
                        foreach (var item in block.Variables)
                        {
                            Console.WriteLine("{0:10} : {1}", item.Key, item.Value);
                        }
                    }
                    break;

                case '5':
                    if (formula == null)
                    {
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Input Addition Formula:");
                        Console.Write(" >");
                        Analyzer.Analyzer anal = new Analyzer.Analyzer(
                            Console.ReadLine(), setting);
                        Console.WriteLine("Success!");
                        var d    = anal.GetResult();
                        var dres = d.Eval(data);
                        Console.WriteLine("Done. (ReturnVal: " +
                                          (dres == null ? "null" : dres.ToString()) + ")");
                    }
                    break;

                case '6':
                    if (formula == null)
                    {
                        continue;
                    }
                    data.OutputDebug();
                    break;

                case '7':
                    if (formula == null)
                    {
                        continue;
                    }
                    Console.WriteLine("Input  : " + analyzer.Line);
                    Console.WriteLine("Output Expression: " + formula.GetType().Name);
                    OutputFormula(data, formula, 0);
                    break;

                case '8':
                    if (formula == null)
                    {
                        continue;
                    }
                    if (!setting.IsDebug)
                    {
                        setting.IsDebug = true;
                        Console.WriteLine("Debug Enabled!");
                    }
                    else
                    {
                        setting.IsDebug = false;
                        Console.WriteLine("Debug Disabled!");
                    }
                    break;


                case '9': return;

                case 'w':
                    ToWritingMode(data);
                    break;

                default:
                    continue;
                }
                Console.WriteLine();
            }
        }