Esempio n. 1
0
        /// <summary>
        /// 计算表达式
        /// 语法:    expression if expression else expression
        /// Example: $%提箱点编号% if str(%任务性质%) == "进口拆箱" else %装货地%$
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public object CalculateExpression(string expression, object entity)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return(null);
            }
            expression = PythonScript.TryRemoveExpressionQuotos(expression);

            bool?isEvalutionEngineExpression = IsEvaluationEngineExpression(expression);

            if (isEvalutionEngineExpression.HasValue && isEvalutionEngineExpression.Value)
            {
                string ownExpression = EntityHelper.ReplaceEntity(expression, entity);
                object result        = m_evaluationEngine.CalculateExpression(ownExpression, null);
                ExceptionProcess.ProcessWithResume(new NotSupportedException(string.Format("{0} 请使用Python表达式.", expression)));
                return(result);
            }

            try
            {
                try
                {
                    return(m_entityPython.CalculateExpression(expression, entity));
                }
                catch (Exception)
                {
                    string ownExpression = EntityHelper.ReplaceEntity(expression, entity);
                    ownExpression = ownExpression.Replace("\"\"", "None");
                    return(m_entityPython.CalculateExpression(ownExpression, entity));
                }
            }
            catch (Exception ex)
            {
                if (!isEvalutionEngineExpression.HasValue && ex.GetType().ToString().Contains("Microsoft.Scripting.SyntaxErrorException"))
                {
                    string ownExpression = EntityHelper.ReplaceEntity(expression, entity);
                    object result        = null;
                    try
                    {
                        result = m_evaluationEngine.CalculateExpression(ownExpression, null);
                        ExceptionProcess.ProcessWithResume(new NotSupportedException(string.Format("{0} 请使用Python表达式.", expression)));
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    return(result);
                }
                else
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// 计算简单表达式,无参数.
        /// 注意,iif不能嵌套。应该用中间变量,例如 $a := iif[%票.卸箱地编号% = "900125", "900005", %票.卸箱地编号%];iif[%票.卸箱地编号% = 900125", "900005", a]$
        /// 注意,最后不能加分号。
        /// 要计算的,前后加$, 要实体类中变量的,加%%
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="processParams"></param>
        /// <returns></returns>
        public object CalculateExpression(string expression, Dictionary <string, object> processParams)
        {
            expression = PythonScript.TryRemoveExpressionQuotos(expression);

            bool?isEvaluationEngineExp = IsEvaluationEngineExpression(expression);

            if (isEvaluationEngineExp.HasValue && isEvaluationEngineExp.Value)
            {
                ExceptionProcess.ProcessWithResume(new NotSupportedException(string.Format("{0} 请使用Python表达式.", expression)));
                return(m_evaluationEngine.CalculateExpression(expression, processParams));
            }
            else
            {
                // 尝试先以Python方式运行
                try
                {
                    if (expression.Contains("result"))
                    {
                        return(m_python.ExecuteStatement(expression, processParams));
                    }
                    else
                    {
                        return(m_python.CalculateExpression(expression, processParams));
                    }
                }
                catch (Exception ex)
                {
                    if (!isEvaluationEngineExp.HasValue && ex is Microsoft.Scripting.SyntaxErrorException || ex.InnerException is Microsoft.Scripting.SyntaxErrorException)
                    {
                        object result = null;
                        try
                        {
                            result = m_evaluationEngine.CalculateExpression(expression, processParams);
                            ExceptionProcess.ProcessWithResume(new NotSupportedException(string.Format("{0} 请使用Python表达式.", expression)));
                        }
                        catch (Exception)
                        {
                            throw ex;
                        }

                        return(result);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// 计算表达式
        /// 语法:    expression if expression else expression
        /// Example: $%提箱点编号% if str(%任务性质%) == "进口拆箱" else %装货地%$
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        private object CalculateExpressionInternal(string expression, object entity)
        {
            if (string.IsNullOrEmpty(expression))
            {
                return(null);
            }

            expression = PythonScript.TryRemoveExpressionQuotos(expression);

            object ret = null;

            if (entity is IEntity || (entity == null && expression.Contains("entity")))
            {
                expression = EntityHelper.ReplaceEntityParameters(expression, entity, true);

                ret = base.CalculateExpression(expression, new Dictionary <string, object> {
                    { "entity", entity }
                });
            }
#if !SILVERLIGHT
            else if (entity is System.Data.DataRowView || entity is IDictionary <string, object> ||
                     (entity == null && expression.Contains("row")))
            {
                expression = EntityHelper.ReplaceEntityParameters(expression, entity, false);
                ret        = base.CalculateExpression(expression, new Dictionary <string, object> {
                    { "row", entity }
                });
            }
#endif
            else if (entity == null)
            {
                if (expression.Contains("%"))
                {
                    expression = EntityHelper.ReplaceEntityParameters(expression, entity, true, true);
                    ret        = base.CalculateExpression(expression, new Dictionary <string, object> {
                    });
                }
                else
                {
                    return(base.CalculateExpression(expression, null));
                }
            }
            else
            {
                throw new NotSupportedException(string.Format("Now only IEntity or DataRowView is supported, {0} is not supported !", entity.GetType().ToString()));
            }
            return(ret);
        }
Esempio n. 4
0
        private void AppHelperForm_Load(object sender, EventArgs e)
        {
            //ServiceProvider.SetDefaultService<IExceptionProcess>(new WinFormExceptionProcess());

            //IPersistentCache c = new PersistentHashtableCache();
            //ServiceProvider.SetDefaultService<ICache>(c);
            //ServiceProvider.SetDefaultService<IPersistentCache>(c);

            Feng.DBDef def = new Feng.DBDef();
            ServiceProvider.SetDefaultService<IDefinition>(def);

            IDataBuffer buf = new Feng.DBDataBuffer();
            ServiceProvider.SetDefaultService<IDataBuffer>(buf);

            IDataBuffers bufs = new DataBuffers();
            bufs.AddDataBuffer(new Cache());
            bufs.AddDataBuffer(buf);
            bufs.AddDataBuffer(def);
            ServiceProvider.SetDefaultService<IDataBuffers>(bufs);

            ServiceProvider.SetDefaultService<IRepositoryFactory>(new Feng.NH.RepositoryFactory());
            ServiceProvider.SetDefaultService<Feng.NH.ISessionFactoryManager>(new Feng.NH.NHibernateSessionFactoryManager());

            //ServiceProvider.SetDefaultService<IEntityMetadataGenerator>(new NHDataEntityMetadataGenerator());

            //ServiceProvider.SetDefaultService<IMessageBox>(new Feng.Windows.Forms.MyMessageBox());

            IEntityScript script = new PythonScript();
            ServiceProvider.SetDefaultService<IScript>(script);
            ServiceProvider.SetDefaultService<IEntityScript>(script);

            IDataBuffers db = ServiceProvider.GetService<IDataBuffers>();
            if (db != null)
            {
                db.LoadData();
            }
        }