Esempio n. 1
0
 protected override void BuildConverterFunction(Expression from, Type fromType, Type toType)
 {
     if (toType == Types.Boolean && !hasEnterBoolConvert)
     {
         //把有符号数转换为无符号数,然后进行转换
         if (fromType == Types.SByte ||
             fromType == Types.Int16 ||
             fromType == Types.Int32 ||
             fromType == Types.Int64)
         {
             hasEnterBoolConvert = true;
             from = Expression.Call(typeof(Math).GetMethod("Abs", new Type[] { fromType }), from);
             ConvertTo(from, toType);
             hasEnterBoolConvert = false;
             return;
         }
         if (fromType == Types.String)
         {
             hasEnterBoolConvert = true;
             from = Expression.Call(typeof(Math).GetMethod("Abs", new Type[] { Types.Int32 })
                                    , Expression.Call(MethodRepository.GetConvertMethod(Types.String, Types.Int32), from));
             ConvertTo(from, toType);
             hasEnterBoolConvert = false;
             return;
         }
     }
     if (toType == Types.DateTime)
     {
         sb.Append("CONVERT(DATETIME,");
         Visit(from);
         sb.Append(",20)");
         return;
     }
     if (toType == Types.String)
     {
         if (fromType == Types.DateTime)
         {
             //参考:http://www.cnblogs.com/liushanshan/archive/2011/06/30/2094387.html
             sb.Append("CONVERT(NVARCHAR(200),");
             Visit(from);
             sb.Append(",20)");
             return;
         }
         if (fromType == Types.Guid)
         {
             sb.Append("CAST(");
             Visit(from);
             sb.Append(" AS NVARCHAR(72))");
             return;
         }
     }
     base.BuildConverterFunction(from, fromType, toType);
 }
Esempio n. 2
0
 protected override Expression VisitUnary(UnaryExpression u)
 {
     u = base.VisitUnary(u) as UnaryExpression;
     switch (u.NodeType)
     {
     case ExpressionType.TypeAs:
     case ExpressionType.Convert:
     case ExpressionType.ConvertChecked:
         return(Expression.Call(MethodRepository.GetConvertMethod(u.Operand.Type, u.Type), u.Operand));
     }
     return(u);
 }
Esempio n. 3
0
        public Simulation()
        {
            _worldStateService = new WorldStateService();
            _workItemDecomp = new SimpleWorkItemDecomposition();
            _goalService = new GoalService(_worldStateService);

            // Set up database classes
            _databaseProvider = new MySql.Data.MySqlClient.MySqlClientFactory();
            _stateDataAccess = new WorldStateDataAccess(_databaseProvider);
            _knowledgeDataAccess = new WorldKnowledgeDataAccess(_databaseProvider);

            _worldStateRepos = new WorldStateRepository(_stateDataAccess);
            _accessRecordRepos = new AccessRecordRepository(_stateDataAccess);
            _assetServiceRepos = new AssetServiceRoutineRepository(_stateDataAccess);
            _methodRepository = new MethodRepository(_knowledgeDataAccess);

            _activityMethodService = new ActivityMethodService(_methodRepository);

            // Set up service invocation classes
            _serviceRoutineService = new ServiceRoutineService(_worldStateService, _assetServiceRepos);
            _abortHandler = new AbortHandler();
            _serviceRoutineService.AddServiceInvocationHandler(_abortHandler);
        }
Esempio n. 4
0
        protected override void BuildConverterFunction(Expression from, Type fromType, Type toType)
        {
            if (fromType == Types.Boolean && !hasEnterBoolConvert)
            {
                //Access 的bool值的True=-1, False=0,所以需要首先把bool转化为整形然后取绝对值,这样bool的True才能转化为1
                hasEnterBoolConvert = true;
                from = Expression.Call(typeof(Math).GetMethod("Abs", new Type[] { Types.Int32 }), Expression.Call(MethodRepository.GetConvertMethod(Types.Boolean, Types.Int32), from));
                ConvertTo(from, toType);
                hasEnterBoolConvert = false;
                return;
            }

            if (toType == Types.String)
            {
                //if (fromType == Types.Guid)
                //{
                //    Visit(from);
                //    return;
                //}
                if (fromType == Types.DateTime)
                {
                    sb.Append("FORMAT(");
                    Visit(from);
                    sb.Append(",'yyyy-MM-dd HH:mm:ss')");
                    return;
                }
            }
            if (fromType == Types.String)
            {
                if (toType == Types.Guid)
                {
                    from = Expression.Call(MethodRepository.ToLower, from);
                    Visit(from);
                    return;
                }
            }

            DBType code;

            if (!SqlType.TypeMap.TryGetValue(toType.TypeHandle.Value.GetHashCode(), out code))
            {
                throw new InvalidCastException(string.Format("Cann't convert from '{0}' to db type.", fromType.FullName));
            }

            var fnView = CastFunctions[code];

            fnView.Render(this, from);
        }
Esempio n. 5
0
        protected override Expression VisitMethodCall(MethodCallExpression m)
        {
            m = base.VisitMethodCall(m) as MethodCallExpression;

            var lambda = MethodMapping.ConvertMember(m.Method);

            if (lambda != null)
            {
                var ef    = lambda.Body.Unwrap();
                var parms = new Dictionary <string, ParameterMapping>(lambda.Parameters.Count);
                var pn    = m.Method.IsStatic ? 0 : -1;

                foreach (var p in lambda.Parameters)
                {
                    parms.Add(p.Name, new ParameterMapping(pn++, p.Type));
                }

                var pie = ef.Convert(wpi =>
                {
                    if (wpi.NodeType == ExpressionType.Parameter)
                    {
                        ParameterMapping n;
                        if (parms.TryGetValue(((ParameterExpression)wpi).Name, out n))
                        {
                            var tmp = n.Index < 0 ? m.Object : m.Arguments[n.Index];
                            if (tmp.Type != n.Type)
                            {
                                return(Expression.Convert(tmp, n.Type));
                            }
                            return(tmp);
                        }
                    }

                    return(wpi);
                });

                if (m.Method.ReturnType != pie.Type)
                {
                    pie = Expression.Convert(pie, m.Method.ReturnType);
                }

                return(Visit(pie));
            }



            var methodName    = m.Method.Name;
            var declaringType = m.Method.DeclaringType;

            if (declaringType == typeof(System.Convert))
            {
                Expression operand = null;
                Type       toType  = null;
                if (m.Method.Name.StartsWith("To"))
                {
                    toType  = ClassLoader.Load("System." + m.Method.Name.Replace("To", ""));
                    operand = m.Arguments[0];
                }

                if (operand != null && toType != null && toType != Types.Object)
                {
                    return(Expression.Call(MethodRepository.GetConvertMethod(operand.Type, toType), operand));
                }
            }

            if (typeof(TypeConverter).IsAssignableFrom(declaringType))
            {
                Expression operand = null;
                Type       toType  = null;

                if (methodName.StartsWith("ConvertFrom"))
                {
                    var  c             = m.Object as ConstantExpression;
                    Type converterType = null;
                    if (c != null)
                    {
                        converterType = (m.Object as ConstantExpression).Value.GetType();
                    }
                    else
                    {
                        var ma = m.Object as MemberExpression;
                        if (ma != null)
                        {
                            c = ma.Expression as ConstantExpression;
                            if (c == null)
                            {
                                throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name));
                            }

                            converterType = ma.Member.GetGetter()(c.Value).GetType();
                        }
                    }
                    toType = ClassLoader.Load("System." + converterType.Name.Replace("Converter", ""));
                    if (toType == null)
                    {
                        throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name));
                    }

                    if (methodName == "ConvertFrom" && m.Arguments.Count == 1)
                    {
                        operand = m.Arguments[0];
                    }
                    else if (methodName == "ConvertFromString" && m.Arguments.Count == 1)
                    {
                        operand = m.Arguments[0];
                    }
                    else if (methodName == "ConvertFromInvariantString" && m.Arguments.Count == 1)
                    {
                        operand = m.Arguments[0];
                    }
                }
                else if (methodName == "ConvertTo" && m.Arguments.Count == 2)
                {
                    operand = m.Arguments[0];
                    toType  = (m.Arguments[1] as ConstantExpression).Value as Type;
                }
                else if (methodName == "ConvertToInvariantString" && m.Arguments.Count == 1)
                {
                    operand = m.Arguments[0];
                    toType  = Types.String;
                }
                else if (methodName == "ConvertToString" && m.Arguments.Count == 1)
                {
                    operand = m.Arguments[0];
                    toType  = Types.String;
                }

                if (operand != null && toType != null && toType != Types.Object)
                {
                    return(Expression.Call(MethodRepository.GetConvertMethod(operand.Type, toType), operand));
                }
                throw new NotSupportedException(string.Format("The method '{0}' is not supported", methodName));
            }

            if (methodName == "Parse" &&
                m.Method.IsStatic &&
                (declaringType.IsValueType || declaringType.IsNullable()) &&
                m.Arguments.Count == 1 &&
                m.Method.ReturnType == m.Type)
            {
                Expression operand = m.Arguments[0];
                Type       toType  = declaringType.IsNullable() ? Nullable.GetUnderlyingType(declaringType) : declaringType;
                return(Expression.Call(MethodRepository.GetConvertMethod(operand.Type, toType), operand));
            }
            if (declaringType == Types.String)
            {
                if (methodName == "Concat")
                {
                    return(BindConcat(m.Arguments.ToArray()));
                }
                if (methodName == "Join")
                {
                    return(BindJoin(m));
                }
            }

            if (methodName == "ContainsValue" && IsSameOrParent(typeof(IDictionary <,>), declaringType))
            {
                var args = GetGenericArguments(declaringType, typeof(IDictionary <,>));
                var minf = EnumerableMethods
                           .First(s => s.Name == "Contains" && s.GetParameters().Length == 2)
                           .MakeGenericMethod(args[1]);

                return(Expression.Call(
                           minf,
                           Expression.PropertyOrField(m.Object, "Values"),
                           m.Arguments[0]));
            }
            if (methodName == "ContainsKey" && IsSameOrParent(typeof(IDictionary <,>), declaringType))
            {
                var args = GetGenericArguments(declaringType, typeof(IDictionary <,>));
                var minf = EnumerableMethods
                           .First(s => s.Name == "Contains" && s.GetParameters().Length == 2)
                           .MakeGenericMethod(args[1]);

                return(Expression.Call(
                           minf,
                           Expression.PropertyOrField(m.Object, "Keys"),
                           m.Arguments[0]));
            }


            if (declaringType.FullName == DLinq.StrSqlMethhodsType && declaringType.Assembly.GetName().Name == DLinq.StrAssemblyName)
            {
                DLinq.Init(declaringType.Assembly);
                return(Visit(m));
            }
            //if (methodName == "Like" && declaringType == typeof(SqlFunctions) && m.Arguments.Count == 5)
            //{
            //    return BindLike(
            //        m.Arguments[0]
            //        , m.Arguments[1]
            //        , (bool)(m.Arguments[2] as ConstantExpression).Value
            //        , (bool)(m.Arguments[3] as ConstantExpression).Value
            //        , (bool)(m.Arguments[4] as ConstantExpression).Value);
            //}

            if (typeof(Queryable).IsAssignableFrom(declaringType) || typeof(Enumerable).IsAssignableFrom(declaringType))
            {
                var elementType = NLite.Data.Linq.Internal.ReflectionHelper.GetElementType(m.Arguments[0].Type);
                switch (methodName)
                {
                case "Contains":
                    EntityMapping mapping;
                    if (DbContext.dbConfiguration.mappings.TryGetValue(elementType.TypeHandle.Value, out mapping))
                    {
                        // Expression left =
                    }
                    break;
                    //case "Aggregate":
                    //    {
                    //        var type = NLite.Reflection.TypeHelper.GetElementType(m.Arguments[0].Type);
                    //        if (type.IsNullable())
                    //            type = Nullable.GetUnderlyingType(type);
                    //        if (type.IsClass && type != Types.String)
                    //            throw new NotSupportedException("Not support 'Aggregate' function for complex type.");
                    //        break;
                    //    }
                    //case "ElementAt":
                    //    var index = m.Arguments[1];
                    //    var elementType = NLite.Reflection.TypeHelper.GetElementType(m.Arguments[0].Type);
                    //    var c = index as ConstantExpression;
                    //    if (c != null)
                    //    {
                    //        if((int)c.Value == 0)
                    //            return Expression.Call(typeof(Enumerable), "Take", new Type[] { elementType }, m.Arguments[0], Expression.Constant(1, Types.Int32));
                    //        index = Expression.Constant((int)c.Value + 1, Types.Int32);
                    //    }
                    //    else
                    //        index = Expression.Add(index, Expression.Constant(1, Types.Int32));
                    //    var s = Expression.Call(typeof(Enumerable), "Skip", new Type[] { elementType },m.Arguments[0], index);
                    //    return Expression.Call(typeof(Enumerable), "Take", new Type[] { elementType },s, Expression.Constant(1,Types.Int32));
                }
            }

            if (IsSameOrParent(typeof(IEnumerable <>), declaringType) &&
                !declaringType.IsArray &&
                m.Object != null &&
                m.Object.NodeType == ExpressionType.Constant &&
                !typeof(IQueryable).IsAssignableFrom(declaringType))
            {
                switch (methodName)
                {
                case "Contains":
                    var elementType = NLite.Data.Linq.Internal.ReflectionHelper.GetElementType(declaringType);
                    if (!DbContext.dbConfiguration.HasClass(elementType) && elementType != Types.Char)
                    {
                        var lst = (m.Object as ConstantExpression).Value as IEnumerable;
                        if (lst != null)
                        {
                            var items = lst.Cast <object>().ToArray();
                            var arry  = Array.CreateInstance(elementType, items.Length);
                            for (int i = 0; i < items.Length; i++)
                            {
                                arry.SetValue(items[i], i);
                            }
                            Expression array = Expression.Constant(arry);

                            var containsMethod = EnumerableMethods
                                                 .First(s => s.Name == "Contains" && s.GetParameters().Length == 2)
                                                 .MakeGenericMethod(elementType);
                            m = Expression.Call(containsMethod, array, m.Arguments[0]);
                        }
                    }
                    break;
                }
            }
            return(m);
        }
Esempio n. 6
0
 public ActivityMethodService(MethodRepository methodRepos)
 {
     _methodRepos = methodRepos;
 }