예제 #1
0
        public ParametersCollection GetFunctionTree(YamlScriptController controller, string expression = null)
        {
            localContext = new ExpressionContext(controller);
            localContext.Options.ParseCulture = CultureInfo.InvariantCulture;
            // Allow the expression to use all static public methods of System.Math
            localContext.Imports.AddType(typeof(Math));
            // Allow the expression to use all static overload public methods our CustomFunctions class
            localContext.Imports.AddType(typeof(CustomFunctions));
            var variables  = new System.Collections.Generic.Dictionary <string, Type>();
            var parameters = new ParametersCollection();

            localContext.Variables.ResolveVariableType += (object sender, ResolveVariableTypeEventArgs e) =>
            {
                parameters.Add(new Parameter(e.VariableName, 0, null, ref _model));
                variables.Add(e.VariableName, typeof(object));
                e.VariableType = typeof(object);
            };

            if (expression != null)
            {
                try
                {
                    IGenericExpression <object> eDynamic = localContext.CompileGeneric <object>(expression);
                }
                catch
                {
                }
                return(parameters);
            }

            foreach (var formula in _model.Formulas)
            {
                try
                {
                    IGenericExpression <object> eDynamic = localContext.CompileGeneric <object>(formula.Functions[0].Expression);
                }
                catch
                {
                }
            }
            return(parameters);
        }
 public FormulaExpressionContext(ref Model.Model model, ref IParametersCollection parameters, Formula formula, QuestionDelegate onQuestion, YamlScriptController controller)
 {
     _parameters = parameters as ParametersCollection ?? throw new ArgumentNullException(nameof(parameters));
     _formula    = formula ?? throw new ArgumentNullException(nameof(formula));
     OnQuestion  = onQuestion ?? throw new ArgumentNullException(nameof(onQuestion));
     _model      = model ?? throw new ArgumentNullException(nameof(model));
     // Define the context of our expression
     _context = new ExpressionContext(controller);
     _context.Options.ParseCulture = CultureInfo.InvariantCulture;
     // Allow the expression to use all static public methods of System.Math
     _context.Imports.AddType(typeof(Math));
     // Allow the expression to use all static overload public methods our CustomFunctions class
     _context.Imports.AddType(typeof(CustomFunctions));
     // Financial formulas
     _context.Imports.AddType(typeof(BankingFormulas));
     _context.Imports.AddType(typeof(CorporateFormulas));
     _context.Imports.AddType(typeof(FinancialFormulas));
     _context.Imports.AddType(typeof(FinancialMarketsFormulas));
     _context.Imports.AddType(typeof(GeneralFinanceFormulas));
     _context.Imports.AddType(typeof(StocksBondsFormulas));
     // this will visit ResolveVariableType
     _context.Variables.ResolveVariableType += ResolveVariableType;
     // this will visit ResolveVariableValue
     _context.Variables.ResolveVariableValue += ResolveVariableValue;
     Map(ref _parameters, _context.Variables);
 }