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);
 }
示例#2
0
    public bool ShowQuestionOverlay(string question, string button1, string button2, QuestionDelegate questionDelegate)
    {
        if (overlayCanvas != null)
        {
            return(false);
        }

        InstantiateCanvas(questionCanvas);
        overlayCanvas.GetComponent <QuestionCanvasBehaviour>().questionDelegate                     = questionDelegate;
        overlayCanvas.GetChild(0).GetChild(0).GetComponent <UnityEngine.UI.Text>().text             = question;
        overlayCanvas.GetChild(0).GetChild(1).GetChild(0).GetComponent <UnityEngine.UI.Text>().text = button1;
        if (button2 != null)
        {
            overlayCanvas.GetChild(0).GetChild(2).GetChild(0).GetComponent <UnityEngine.UI.Text>().text = button2;
        }
        else
        {
            overlayCanvas.GetChild(0).GetChild(1).localPosition =
                (overlayCanvas.GetChild(0).GetChild(1).localPosition + overlayCanvas.GetChild(0).GetChild(2).localPosition) / 2;
            Destroy(overlayCanvas.GetChild(0).GetChild(2).gameObject);
        }
        return(true);
    }
    private void Save()
    {
        GuiManager.Instance.messageCanvas.SetAndShow (MessageCanvasControl.LOADING_MESSAGE, false);
        Loom loom = Loom.Instance;
        loom.RunAsync (() =>
        {
            GameManager manager = GameManager.Instance;
            if (this.question != null && manager.UseDatabase) {
                Answer answer = new Answer (question, manager.ActiveGame, manager.ActivePlayer, hooks.Answer);
                manager.Entity.StoredAnswers.Add (answer);

                this.question = null;
            }

            loom.QueueOnMainThread (() =>
            {
                GuiManager.Instance.messageCanvas.Hide ();
                this.Hide ();

                if (OnEndQuestion != null) {
                    OnEndQuestion ();
                    OnEndQuestion = null;
                }
            });
        });
    }
 public QuestionNode(QuestionDelegate question, INode trueNode, INode falseNode)
 {
     _question  = question;
     _trueNode  = trueNode;
     _falseNode = falseNode;
 }
示例#5
0
 public QuestionNode(QuestionDelegate question, Node trueNode, Node falseNode)
 {
     this.question  = question;
     this.trueNode  = trueNode;
     this.falseNode = falseNode;
 }
        private static Parameter Evaluate(FormulaExpressionContext caller, ref ExpressionContext context, ref Formula formula, ref ParametersCollection parameters1, QuestionDelegate onQuestion, ref Model.Model model)
        {
            if (parameters1 is null)
            {
                throw new ArgumentNullException(nameof(parameters1));
            }

            IDynamicExpression e = null;

            if (!formula.IsSituational)
            {
                try
                {
                    e = context.CompileDynamic(formula.Functions[0].Expression);
                    var       result = e.Evaluate().Infer();
                    Parameter parameter;
                    parameter = new Parameter(formula.Name, result.Infer(), null, ref model);
                    parameter.IsCalculated = true;
                    parameters1.Add(parameter);
                    if (context.Variables.ContainsKey(parameter.Name))
                    {
                        context.Variables.Remove(parameter.Name);
                    }
                    context.Variables.Add(parameter.Name, parameter.Value.Infer());
                    return(parameter);
                }
                catch (ExpressionCompileException)
                {
                    // Function can not evaluate further, before a Question/Answer sequence is fullfilled by the client.
                    throw new UnresolvedException($"Function {formula.Functions[0].Expression} can not evaluate further, before a Question/Answer sequence is fullfilled by the client.");
                }
            }
            else
            {
                foreach (var function in formula.Functions)
                {
                    foreach (var item in parameters1)
                    {
                        if (item.Name == function.Situation && (bool)item.Value == true)
                        {
                            try
                            {
                                e = context.CompileDynamic(function.Expression);
                                var parameter = new Parameter(formula.Name, e.Evaluate().Infer(), null, ref model);
                                parameter.IsCalculated = true;
                                parameters1.Add(parameter);
                                if (context.Variables.ContainsKey(parameter.Name))
                                {
                                    context.Variables.Remove(parameter.Name);
                                }
                                context.Variables.Add(parameter.Name, parameter.Value.Infer());
                                return(parameter);
                            }
                            catch (ExpressionCompileException)
                            {
                                // Function can not evaluate further, before a Question/Answer sequence is fullfilled by the client.
                                throw new UnresolvedException($"Function {function.Expression} can not evaluate further, before a Question/Answer sequence is fullfilled by the client.");
                            }
                        }
                    }
                }
                StringBuilder situations = new StringBuilder();
                var           parameters = new ParametersCollection();
                foreach (var function in formula.Functions)
                {
                    situations.Append(function.Situation + ",");
                    var parameter = new Parameter(function.Situation, TypeInference.Infer(function).Type, null, ref model);
                    parameters.Add(parameter);
                }
                if (onQuestion == null)
                {
                    throw new Exception($"In order to evaluate variable one of the following situations:  {situations.ToString().Trim(',')}, you need to provide a delegate callback to the client for providing an answer");
                }
                onQuestion(caller, new QuestionArgs("", parameters));
                // situation has to be formulated as an input parameter by the client.
                throw new UnresolvedException($"Can't evaluate formula {formula.Name} for situation. Please specify one of the following situations: {situations.ToString().Trim(',')}.");
            }
        }