Example #1
0
        private string ReplaceCommonVariablesInSentence(IList <StageVariable> variables, CultureInfo cultureInfo, string sentence)
        {
            //logger.Info("Inicio Reemplazar Variables en Sentencia");
            var             regex            = new Regex(@"[A-Za-z]+[A-Za-z0-9]*");
            MatchCollection matches          = regex.Matches(sentence);
            int             previousPosition = 0;

            foreach (Match match in matches)
            {
                StageVariable variableAux = variables.FirstOrDefault(x => x.Name == match.Value);
                if (variableAux != null)
                {
                    string actualValue     = variableAux.ActualValue >= 0 ? variableAux.ActualValue.ToString("F6", cultureInfo).TrimEnd('0') : string.Format("({0})", variableAux.ActualValue.ToString("F6", cultureInfo).TrimEnd('0'));
                    int    indexOfComma    = actualValue.IndexOf('.');
                    string valueAfterComma = actualValue.Substring(indexOfComma);
                    if (valueAfterComma != null && valueAfterComma.Length == 1 && valueAfterComma == ".")
                    {
                        actualValue = actualValue.Remove(indexOfComma, 1);
                    }

                    sentence          = sentence.Remove(match.Index - previousPosition, match.Length).Insert(match.Index - previousPosition, actualValue);
                    previousPosition += match.Length - actualValue.Length;
                }
            }

            //logger.Info("Fin Reemplazar Variables en Sentencia");
            return(sentence);
        }
Example #2
0
        public override Node Execute(IList <StageVariable> variables, Delegate NotifyUIMethod)
        {
            try
            {
                Debug.Debug.instance().execute(this, NotifyUIMethod, variables);

                //logger.Info("Inicio Nodo Sentencia");
                var cultureInfo = new CultureInfo("en-US");
                int indexEqual  = this.Code.IndexOf("=");

                string sentence = this.Code.Substring(indexEqual + 1).Trim();
                sentence = this.GetSentenceToEvaluate(variables, cultureInfo, sentence);

                string        variableStr = this.Code.Substring(0, indexEqual).Trim();
                StageVariable variable    = this.GetVariable(variables, cultureInfo, variableStr);

                ExpressionResolver.Resolve(variable, sentence, new Random().NextDouble());

                return(base.Execute(variables, NotifyUIMethod));
            }
            catch (Exception exception)
            {
                //logger.Error("Error Nodo Sentencia: " + exception.Message);
                logger.Error(exception.Source + " - " + exception.Message + ": " + exception.StackTrace);
                throw new Exception("Nodo sentencia", exception);
            }
        }
Example #3
0
        private StageVariable GetVariable(IList <StageVariable> variables, CultureInfo cultureInfo, string variableStr)
        {
            //logger.Info("Inicio Obtener Variable");
            StageVariable variable = null;

            if (variableStr.Contains("(") && variableStr.Contains(")"))
            {
                MatchCollection match         = new Regex(@"(?<=\().+?(?=\))").Matches(this.Code);
                string          vectorIndex   = match[0].Value;
                var             variableIndex = variables.FirstOrDefault(v => v.Name == vectorIndex);
                if (variableIndex != null)
                {
                    variableStr = variableStr.Remove(match[0].Index, match[0].Length).Insert(match[0].Index, variableIndex.ActualValue.ToString(cultureInfo));
                }

                foreach (StageVariableArray varArray in variables.Where(v => v is StageVariableArray).OrderByDescending(x => x.Name.Length))
                {
                    variable = varArray.Variables.FirstOrDefault(x => x.Name == variableStr);
                    if (variable != null)
                    {
                        break;
                    }
                }
            }
            else
            {
                variable = variables.First(x => x.Name == variableStr);
            }

            //logger.Info("Fin Obtener Variable");
            return(variable);
        }
Example #4
0
        private string ReplaceArraysVariablesInSentence(IList <StageVariable> variables, CultureInfo cultureInfo, string sentence)
        {
            //logger.Info("Inicio Reemplazar variables en array");

            var             regex            = new Regex(@"[A-Z0-9a-z]+[(][0-9]+[)]");
            MatchCollection matches          = regex.Matches(sentence);
            int             previousPosition = 0;

            foreach (Match match in matches)
            {
                foreach (StageVariableArray varArray in variables.Where(v => v is StageVariableArray))
                {
                    StageVariable variable = varArray.Variables.FirstOrDefault(x => x.Name == match.Value);
                    if (variable != null)
                    {
                        string actualValue = variable.ActualValue.ToString(cultureInfo);
                        sentence          = sentence.Remove(match.Index - previousPosition, match.Length).Insert(match.Index - previousPosition, actualValue);
                        previousPosition += match.Length - actualValue.Length;
                        break;
                    }
                }
            }
            //logger.Info("Fin Reemplazar variables en array");
            return(sentence);
        }
        //el parametro R se utiliza unicamente en casos en los que se  haya definido una FDP para el dato que se va a ejecutar ya que se calcula a partir del objeto y no por el parser
        public static void Resolve(StageVariable variable, string expression, double R = 1)
        {
            expression = expression.Replace(',', '.');
            var formulaParser = new FormulaParser.FormulaParser(expression);

            variable.ActualValue = formulaParser.GetValor();
        }
        public static void Resolve(StageVariable variable, string expression)
        {
            try
            {
                var formulaParser = new FormulaParser.FormulaParser(expression);

                variable.ActualValue = formulaParser.GetValor();
                //logger.Info("Fin Resolver");
            } catch (Exception ex)
            {
                logger.Error("Error Resolver:" + ex.Message);
                throw ex;
            }
        }
Example #7
0
        private void InicializarVariableIteradora(IList <StageVariable> variables)
        {
            //logger.Info("Inicio Inicializar Variable Iteradora");
            var variable = variables.FirstOrDefault(v => v.Name == this.VariableName);

            if (!string.IsNullOrEmpty(this.VariableName) && variable != null)
            {
                this.variableIteradora = variable;
            }
            else
            {
                this.variableIteradora = new StageVariable();
            }

            this.variableIteradora.InitialValue = this.ValorInicial;
            this.variableIteradora.ActualValue  = this.ValorInicial;
            //logger.Info("Fin Incializar Variable Iteradora");
        }
        private static string ReplaceCommonVariablesInSentence(IList <StageVariable> variables, CultureInfo cultureInfo, string sentence)
        {
            var             regex            = new Regex(@"[A-Za-z]+[A-Za-z0-9]*f{0,1}");
            MatchCollection matches          = regex.Matches(sentence);
            int             previousPosition = 0;

            foreach (Match match in matches)
            {
                StageVariable variable = variables.FirstOrDefault(x => x.Name == match.Value);
                if (variable != null)
                {
                    string actualValue = variable.ActualValue.ToString(cultureInfo);
                    sentence          = sentence.Remove(match.Index - previousPosition, match.Length).Insert(match.Index - previousPosition, actualValue);
                    previousPosition += match.Length - actualValue.Length;
                }
            }

            return(sentence);
        }