private decimal CalcExpression(TimeSpan ts, string expression)
        {
            if (!string.IsNullOrEmpty(expression))
            {
                expression = expression.Replace("Days", Math.Round(ts.TotalDays, 2).ToString());
                expression = expression.Replace("Hours", Math.Round(ts.TotalHours, 2).ToString());
                expression = expression.Replace("Minutes", Math.Round(ts.TotalMinutes, 2).ToString());
                expression = expression.Replace("Price", this.Price.ToString());
                expression = expression.Replace("Day", ts.Days.ToString());
                expression = expression.Replace("Hour", ts.Hours.ToString());
                expression = expression.Replace("Minute", ts.Minutes.ToString());
                NHExt.Runtime.Util.RPN rpn = new NHExt.Runtime.Util.RPN();
                try
                {
                    rpn.Parse(expression);

                    return Convert.ToDecimal(rpn.Evaluate());
                }
                catch
                {
                    NHExt.Runtime.Logger.LoggerHelper.Error("��ʽ" + this.Device.Expression + "��ʽ����,������Ĺ�ʽΪ��" + expression);
                    throw new Exception("���ü��㹫ʽ����");
                }
            }
            return 0;
        }
示例#2
0
        public virtual decimal Evaluate(string expression)
        {
            if (string.IsNullOrEmpty(expression))
            {
                throw new Exception("���ʽ����Ϊ��");
            }
            string[] fields = expression.Split(new char[6] { '+', '-', '*', '/', '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
            if (fields.Count() > 0)
            {
                Regex r = new Regex(@"^[1-9]\d*");//ȥ������
                List<string> fieldList = new List<string>(fields);
                fieldList.Sort((a, b) =>
                {
                    if (a.Length > b.Length)
                    {
                        return -1;
                    }
                    else if (a.Length == b.Length)
                    {
                        return 0;
                    }
                    else
                    {
                        return 1;
                    }
                });
                fields = fieldList.ToArray();
                foreach (string field in fields)
                {
                    //��������־Ͳ���
                    if (r.IsMatch(field))
                    {
                        continue;
                    }
                    decimal objV = 0;
                    if (field.StartsWith("$."))
                    {
                        //���÷����ȡ����$.IWEHAVE.ERP.PubBP.Agent.GetYearDaysBPProxy(Month:12,Year:2015)
                        string newStr = field.Replace("$.", "");
                        string bpStr = newStr.Substring(0, newStr.IndexOf("{"));
                        string argStr = newStr.Substring(newStr.IndexOf("{") + 1, (newStr.LastIndexOf("}") - newStr.IndexOf("{") - 1));
                        NHExt.Runtime.Proxy.AgentInvoker invoker = new NHExt.Runtime.Proxy.AgentInvoker();
                        invoker.AssemblyName = bpStr;
                        invoker.DllName = bpStr.Substring(0, bpStr.IndexOf("Agent.")) + "Agent.dll";
                        string[] args = argStr.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        if (args.Count() > 0)
                        {
                            foreach (string arg in args)
                            {
                                //$.IWEHAVE.ERP.PubBP.Agent.GetYearDaysBPProxy(Month:#.PMerchantRent.Rent,Year:2015)
                                string fieldName = arg.Substring(0, arg.IndexOf(":"));
                                object fieldValue = new object();
                                if (!arg.Contains("@"))
                                {
                                    throw new Exception("���ʽ��������û�������Զ��庯����������");

                                }
                                string fieldArgV = arg.Substring(arg.IndexOf(":") + 1, (arg.IndexOf("@") - arg.IndexOf(":") - 1));//����ֵ
                                string fieldType = arg.Substring(arg.IndexOf("@") + 1);//��������
                                if (fieldArgV.StartsWith("#."))//�����Ļ���Ҫ�Ȼ�ȡ����
                                {
                                    fieldArgV = fieldArgV.Replace("#.", "");
                                    var objValue = this.GetData(fieldArgV);
                                    if (objValue != null)
                                    {
                                        fieldValue = this.GetValue(objValue, fieldType);
                                    }
                                }
                                else
                                {
                                    fieldValue = this.GetValue(fieldArgV, fieldType);
                                }
                                invoker.AppendField(new NHExt.Runtime.Proxy.PropertyField() { FieldName = fieldName, FieldValue = fieldValue });
                            }
                        }
                        Object obj = invoker.Do();
                        if (obj != null)
                        {
                            objV = Convert.ToDecimal(obj);
                        }
                    }
                    else
                    {
                        var obj = this.GetData(field);
                        if (obj != null)
                        {
                            objV = Convert.ToDecimal(obj);
                        }
                    }
                    expression = expression.Replace(field, objV.ToString());
                }
            }
            try
            {
                NHExt.Runtime.Util.RPN rpn = new NHExt.Runtime.Util.RPN();
                rpn.Parse(expression);
                return Convert.ToDecimal(rpn.Evaluate());
            }
            catch (Exception ex)
            {
                throw new Exception("���㹫ʽ��ʽ����");
            }
        }