Beispiel #1
0
		public void Test12()
		{
			

			Order order1 = new Order { Customer = Hans, ID = 302, Freight = 2001.99m, OrderDate = DateTime.Now.AddMonths(-20) };

			var assemblies = new Assembly[] { typeof(Order).Assembly, typeof(UnitTests).Assembly, typeof(ExpressionType).Assembly, typeof(IQueryable).Assembly };
			var resolver = new ExpressionSerializationTypeResolver(assemblies, new Type[] { typeof(Customer), typeof(Order), typeof(Product), typeof(Supplier), typeof(Shipper) });
			ExpressionSerializer serializer = new ExpressionSerializer(resolver);

			IEnumerable<Customer> customers = GetCustomers().ToArray();
            Expression<Func<int, IEnumerable<Order[]>>> e12 =
             n =>
                 from c in customers//instance == null : IEnumerable.Where/.Select
                 where c.ID < n
                 select c.Orders.ToArray();
			e12 = ( Expression<Func<int, IEnumerable<Order[]>>>)ObjectServices.Evaluator.PartialEval(e12);
			MethodCallExpression m1 = ((MethodCallExpression)e12.Body).Arguments[0] as MethodCallExpression;
			ConstantExpression cx = ((ConstantExpression)m1.Arguments[0]);
			LambdaExpression lambdaarg = ((LambdaExpression)m1.Arguments[1]);
			//Expression arg1 = ((MethodCallExpression)e12.Body).Arguments[1];
			XElement xml12 = serializer.Serialize(e12);
			
            Expression result12 = serializer.Deserialize( xml12);
            Assert.AreEqual( e12.ToString(), result12.ToString());
            Console.WriteLine(((result12 as Expression<Func<int, IEnumerable<Order[]>>>).Compile())(5));
		}
Beispiel #2
0
 public void ComplexTypeTest()
 {
     Expression<Func<Customer, bool>> addExpr = (customer) => string.IsNullOrEmpty(customer.CustomerID);
     var serializer = new ExpressionSerializer();
     XElement addXml = serializer.Serialize(addExpr);
     var addExpResult = serializer.Deserialize<Func<Customer, bool>>(addXml);
     Func<Customer, bool> addExpResultFunc = addExpResult.Compile();
     Assert.AreEqual(true, addExpResultFunc(new Customer() { CustomerID = "Allen" }));  // evaluates to 2
 }
        public static XElement SerializeQuery(this IQueryable query)
        {
			var resolver = new TypeResolver(assemblies: null,knownTypes: new Type[] { query.ElementType });
            ExpressionSerializer serializer = new ExpressionSerializer(resolver) 
			{ 
				Converters = { new DLinqCustomExpressionXmlConverter(null, resolver)} 
			};
            return serializer.Serialize(query.Expression);
        }
Beispiel #4
0
 public void PrimitiveTypeTest()
 {
     Expression<Func<int>> addExpr = () => 1 + 1;
     var serializer = new ExpressionSerializer();
     XElement addXml = serializer.Serialize(addExpr);
     Expression<Func<int>> addExpResult = serializer.Deserialize<Func<int>>(addXml);
     Func<int> addExpResultFunc = addExpResult.Compile();
     Assert.AreEqual(2, addExpResultFunc());  // evaluates to 2
 }
        public RemoteProvider(WebHttpClient <IQueryService> client)
        {
            this.client = client;
            visitor     = new StripQuoteVisitor();
            resolver    = new TypeResolver(null, client.knownTypes ?? new Type[0]);
            CustomExpressionXmlConverter queryconverter     = new QueryExpressionXmlConverter(null, resolver);
            CustomExpressionXmlConverter knowntypeconverter = new KnownTypeExpressionXmlConverter(resolver);

            serializer = new ExpressionSerializer(resolver, new[] { queryconverter, knowntypeconverter });
        }
Beispiel #6
0
        public static XElement SerializeQuery(this IQueryable query)
        {
            var resolver = new TypeResolver(assemblies: null, knownTypes: new Type[] { query.ElementType });
            ExpressionSerializer serializer = new ExpressionSerializer(resolver)
            {
                Converters = { new DLinqCustomExpressionXmlConverter(null, resolver) }
            };

            return(serializer.Serialize(query.Expression));
        }
Beispiel #7
0
        public static XElement SerializeQuery(this IQueryable query)
        {
            DLinqSerializationTypeResolver resolver   = new DLinqSerializationTypeResolver(null);
            ExpressionSerializer           serializer = new ExpressionSerializer(resolver)
            {
                Converters = { new DLinqCustomExpressionXmlConverter(null, resolver) }
            };

            return(serializer.Serialize(query.Expression));
        }
        //public static IQueryable DeserializeQuery(this DataContext dc, XElement rootXml)
		public static IQueryable DeserializeQuery(this ObjectContext dc, XElement rootXml)
        {
            DLinqSerializationTypeResolver resolver = new DLinqSerializationTypeResolver(dc);
            DLinqCustomExpressionXmlConverter customConverter = new DLinqCustomExpressionXmlConverter(dc, resolver);
            ExpressionSerializer serializer = new ExpressionSerializer(resolver) { Converters = { customConverter } };
            Expression queryExpr = serializer.Deserialize(rootXml);
            // Query kind is populated by the ResolveXmlFromExpression method
            if (customConverter.QueryKind == null)
                throw new Exception(string.Format("CAnnot deserialize into DLinq query for datacontext {0} - no Table found", dc));
            return customConverter.QueryKind.Provider.CreateQuery(queryExpr);
        }
        public static IQueryable DeserializeQuery(this DataContext dc, XElement rootXml)
        {
			var resolver = new TypeResolver(assemblies: null, knownTypes: GetKnownTypesFromTables(dc) );
            DLinqCustomExpressionXmlConverter customConverter = new DLinqCustomExpressionXmlConverter(dc, resolver);
			ExpressionSerializer serializer = new ExpressionSerializer(resolver, new List<CustomExpressionXmlConverter> { customConverter });
            Expression queryExpr = serializer.Deserialize(rootXml);
            // Query kind is populated by the ResolveXmlFromExpression method
            if (customConverter.QueryKind == null)
                throw new Exception(string.Format("CAnnot deserialize into DLinq query for datacontext {0} - no Table found", dc));
            return customConverter.QueryKind.Provider.CreateQuery(queryExpr);
        }
        public List<Character> GetCharacters(XElement query)
        {
            var serializer = new ExpressionSerializer();
              var expression = serializer.Deserialize<Func<Character, bool>>(query);
              if (expression == null)
            return null;

              using (var db = new RevolutionEntities1())
              {
            return db.Characters.Where(expression).ToList();
              }
        }
Beispiel #11
0
		static ExpressionSerializer CreateSerializer()
		{
			var assemblies = new Assembly[] { typeof(Customer).Assembly, typeof(ExpressionType).Assembly, typeof(IQueryable).Assembly };
			var resolver = new TypeResolver(assemblies, new Type[] 
			{ 
				typeof(Customer), typeof(Order), typeof(Product), 
				typeof(Supplier), typeof(Shipper) 
			});
			//var creator = new QueryCreator();
			CustomExpressionXmlConverter queryconverter = new QueryExpressionXmlConverter(creator: null, resolver: resolver);
			CustomExpressionXmlConverter knowntypeconverter = new KnownTypeExpressionXmlConverter(resolver);
			ExpressionSerializer serializer = new ExpressionSerializer(resolver, new CustomExpressionXmlConverter[] { queryconverter, knowntypeconverter });
			return serializer;
			//ExpressionSerializer serializer = new ExpressionSerializer()
		}
 public void TestGetCharacterByExpression()
 {
     // First, create the expression that we want the service to call.  In this case, we want to get any character with a first name of "progress"
       // Note: This expression MUST be of the type System.Linq.Expressions.Expression<Func<RevolutionDAL.Character, bool>> to succeed.
       System.Linq.Expressions.Expression<Func<RevolutionDAL.Character, bool>> exp = (c) => c.FirstName == "progress";
       // Next, create an ExpressionSerializer object
       var serializer = new ExpressionSerializer();
       // Now, serialize the expression...
       var query = serializer.Serialize(exp);
       // Now, we can send this to the DAL to be executed...
       List<Character> characters = null;
       Service<ICharacterService>.Use(client => characters = client.GetCharacters(query));
       Assert.IsNotNull(characters);
       Assert.IsNotEmpty(characters);
 }
        // Very simple serialization example
        public static void BasicExpressionSerialization()
        {
            Console.WriteLine("BASIC SAMPLE - Serialize/Deserialize Simple Expression:");

            Expression<Func<int, int, int>> addExpr = (x, y) => x + y;
            ExpressionSerializer serializer = new ExpressionSerializer();
            XElement addXml = serializer.Serialize(addExpr);
            Expression<Func<int,int,int>> addExpResult = serializer.Deserialize<Func<int,int,int>>(addXml);
            Func<int, int, int> addExpResultFunc = addExpResult.Compile();
            int result = addExpResultFunc(1, 2);  // evaluates to 3

            Console.WriteLine("Deserialized Expression Tree:");
            Console.WriteLine(" " + addExpResult.ToString());
            Console.WriteLine();
        }
Beispiel #14
0
        public static IQueryable DeserializeQuery(this DataContext dc, XElement rootXml)
        {
            var resolver = new TypeResolver(assemblies: null, knownTypes: GetKnownTypesFromTables(dc));
            DLinqCustomExpressionXmlConverter customConverter = new DLinqCustomExpressionXmlConverter(dc, resolver);
            ExpressionSerializer serializer = new ExpressionSerializer(resolver, new List <CustomExpressionXmlConverter> {
                customConverter
            });
            Expression queryExpr = serializer.Deserialize(rootXml);

            // Query kind is populated by the ResolveXmlFromExpression method
            if (customConverter.QueryKind == null)
            {
                throw new Exception(string.Format("CAnnot deserialize into DLinq query for datacontext {0} - no Table found", dc));
            }
            return(customConverter.QueryKind.Provider.CreateQuery(queryExpr));
        }
Beispiel #15
0
        //public static IQueryable DeserializeQuery(this DataContext dc, XElement rootXml)
        public static IQueryable DeserializeQuery(this ObjectContext dc, XElement rootXml)
        {
            DLinqSerializationTypeResolver    resolver        = new DLinqSerializationTypeResolver(dc);
            DLinqCustomExpressionXmlConverter customConverter = new DLinqCustomExpressionXmlConverter(dc, resolver);
            ExpressionSerializer serializer = new ExpressionSerializer(resolver)
            {
                Converters = { customConverter }
            };
            Expression queryExpr = serializer.Deserialize(rootXml);

            // Query kind is populated by the ResolveXmlFromExpression method
            if (customConverter.QueryKind == null)
            {
                throw new Exception(string.Format("CAnnot deserialize into DLinq query for datacontext {0} - no Table found", dc));
            }
            return(customConverter.QueryKind.Provider.CreateQuery(queryExpr));
        }
Beispiel #16
0
        private static ExpressionSerializer CreateSerializer(IEnumerable <Type> knownTypes = null)
        {
            if (knownTypes == null || !knownTypes.Any())
            {
                return(new ExpressionSerializer());
            }
            var assemblies = new List <Assembly> {
                typeof(ExpressionType).Assembly, typeof(IQueryable).Assembly
            };

            knownTypes.ToList().ForEach(type => assemblies.Add(type.Assembly));
            var resolver           = new TypeResolver(assemblies, knownTypes);
            var knownTypeConverter = new KnownTypeExpressionXmlConverter(resolver);
            var serializer         = new ExpressionSerializer(resolver, new CustomExpressionXmlConverter[] { knownTypeConverter });

            return(serializer);
        }
        // Serializing an expression tree representing a query expression
        public static void ComplexExpressionSerializationSamples()
        {
            Console.WriteLine("COMPLEX SAMPLE - Serialize/Deserialize In-Memory Query Expression:");

            Expression<Func<IEnumerable<int>>> queryExp = () => from i in Enumerable.Range(1, 10)
                                                                where i % 2 == 0
                                                                select i * i;

            ExpressionSerializer serializer = new ExpressionSerializer();
            XElement queryXml = serializer.Serialize(queryExp);
            Expression<Func<IEnumerable<int>>> queryExpResult = serializer.Deserialize<Func<IEnumerable<int>>>(queryXml);

            // Print out the expression tree: "(x, y) => x + y"
            Console.WriteLine("Deserialized Expression Tree:");
            Console.WriteLine(" " + queryExpResult.ToString());

            // Call it
            Func<IEnumerable<int>> f = queryExpResult.Compile();
            IEnumerable<int> result = f();
            Console.WriteLine("\nResults: ");
            result.ToList().ForEach(n => Console.WriteLine(" " + n));
            Console.WriteLine();
        }
Beispiel #18
0
 public void TestExpresstion([FromBody] XElement s)
 {
     //XElement x = JsonConvert.DeserializeObject<XElement>(s);
     var serializer = new ExpressionSerializer();
     var newPredicate = serializer.Deserialize<Func<string, bool>>(s);
 }
		public void Tests1To11()
		{
			Order order1 = new Order { Customer = Hans, ID = 302, Freight = 2001.99m, OrderDate = DateTime.Now.AddMonths(-20) };

			var assemblies = new Assembly[] { typeof(Order).Assembly, typeof(BaseTests).Assembly, typeof(ExpressionType).Assembly, typeof(IQueryable).Assembly };
			var resolver = new TypeResolver(assemblies, new Type[] { typeof(Customer), typeof(Order), typeof(Product), typeof(Supplier), typeof(Shipper) });
			ExpressionSerializer serializer = new ExpressionSerializer(resolver);

			Console.WriteLine("\nTEST - 2");
			Expression<Func<int>> e2 = () => 1;
			XElement xml2 = serializer.Serialize(e2.Body);
			Expression result2 = serializer.Deserialize(xml2);
			Assert.AreEqual(e2.Body.ToString(), result2.ToString());

			Console.WriteLine("\nTEST - 3");
			Expression<Func<ExpressionType>> e3 = () => ExpressionType.Add;
			XElement xml3 = serializer.Serialize(e3.Body);
			Expression result3 = serializer.Deserialize(xml3);
			Assert.AreEqual(e3.Body.ToString(), result3.ToString());

			Console.WriteLine("\nTEST - 4");
			Expression<Func<bool>> e4 = () => true;
			XElement xml4 = serializer.Serialize(e4.Body);
			Expression result4 = serializer.Deserialize(xml4);
			Assert.AreEqual(e4.Body.ToString(), result4.ToString());

			Console.WriteLine("\nTEST - 5");
			Expression<Func<decimal, decimal>> e5 = d => d + 1m;
			XElement xml5 = serializer.Serialize(e5.Body);
			Expression result5 = serializer.Deserialize(xml5);
			Assert.AreEqual(e5.Body.ToString(), result5.ToString());

			Console.WriteLine("\nTEST - 6");
			Expression<Func<decimal, decimal>> e6 = d => d + 1m;
			XElement xml6 = serializer.Serialize(e6);
			Expression result6 = serializer.Deserialize(xml6);
			Assert.AreEqual(e6.ToString(), result6.ToString());
			Console.WriteLine(((result6 as Expression<Func<decimal, decimal>>).Compile())(3));

			Console.WriteLine("\nTEST - 7");
			Expression<Func<string, int>> e7 = s => int.Parse(s);
			XElement xml7 = serializer.Serialize(e7);
			Expression result7 = serializer.Deserialize(xml7);
			Assert.AreEqual(e7.ToString(), result7.ToString());
			Console.WriteLine(((result7 as Expression<Func<string, int>>).Compile())("1234"));

			Console.WriteLine("\nTEST - 8");
			Expression<Func<string, string>> e8 = s => s.PadLeft(4);
			XElement xml8 = serializer.Serialize(e8);
			Expression result8 = serializer.Deserialize(xml8);
			Assert.AreEqual(e8.ToString(), result8.ToString());
			Console.WriteLine(((result8 as Expression<Func<string, string>>).Compile())("1"));

			Console.WriteLine("\nTEST - 9");
			Expression<Func<string, int>> e9 = s => Foo<string, int>(s, 1);
			XElement xml9 = serializer.Serialize(e9);
			Expression result9 = serializer.Deserialize(xml9);
			Assert.AreEqual(e9.ToString(), result9.ToString());
			Console.WriteLine(((result9 as Expression<Func<string, int>>).Compile())("abcdac"));

			Console.WriteLine("\nTEST - 10");
			Expression<Func<string, char[]>> e10 = s => s.Where(c => c != 'a').ToArray();
			XElement xml10 = serializer.Serialize(e10);
			Expression result10 = serializer.Deserialize(xml10);
			Assert.AreEqual(e10.ToString(), result10.ToString());
			Console.WriteLine(((result10 as Expression<Func<string, char[]>>).Compile())("abcdac"));

			Console.WriteLine("\nTEST - 11");
			Expression<Func<string, char[]>> e11 =
				s =>
					(from c in s
					 where c != 'a'
					 select (char)(c + 1)).ToArray();
			XElement xml11 = serializer.Serialize(e11);
			Expression result11 = serializer.Deserialize(xml11);
			Assert.AreEqual(e11.ToString(), result11.ToString());
			Console.WriteLine(((result11 as Expression<Func<string, char[]>>).Compile())("abcdac"));



			Console.WriteLine("\nTEST - 19");
			Expression<Func<object, string>> e19 = o => o as string;
			XElement xml19 = serializer.Serialize(e19);
			Expression result19 = serializer.Deserialize(xml19);
			Assert.AreEqual(e19.ToString(), result19.ToString());
			Console.WriteLine(((result19 as Expression<Func<object, string>>).Compile())(7));

			Console.WriteLine("\nTEST - 20");
			Expression<Func<object, bool>> e20 = o => o is string;
			XElement xml20 = serializer.Serialize(e20);
			Expression result20 = serializer.Deserialize(xml20);
			Assert.AreEqual(e20.ToString(), result20.ToString());
			Console.WriteLine(((result20 as Expression<Func<object, bool>>).Compile())(7));

			Console.WriteLine("\nTEST - 21");
			Expression<Func<IEnumerable<string>>> e21 = () => from m in typeof(string).GetMethods()
															  where !m.IsStatic
															  group m by m.Name into g
															  select g.Key + g.Count().ToString();

			XElement xml21 = serializer.Serialize(e21);
			Expression result21 = serializer.Deserialize(xml21);
			Assert.AreEqual(e21.ToString(), result21.ToString());
			Console.WriteLine(((result21 as Expression<Func<IEnumerable<string>>>).Compile())());

			Console.WriteLine("\nTEST - 22 (may take a while)");
			Expression<Func<IEnumerable<int>>> e22 = () => from a in Enumerable.Range(1, 13)
														   join b in Enumerable.Range(1, 13) on 4 * a equals b
														   from c in Enumerable.Range(1, 13)
														   join d in Enumerable.Range(1, 13) on 5 * c equals d
														   from e in Enumerable.Range(1, 13)
														   join f in Enumerable.Range(1, 13) on 3 * e equals 2 * f
														   join g in Enumerable.Range(1, 13) on 2 * (c + d) equals 3 * g
														   from h in Enumerable.Range(1, 13)
														   join i in Enumerable.Range(1, 13) on 3 * h - 2 * (e + f) equals 3 * i
														   from j in Enumerable.Range(1, 13)
														   join k in Enumerable.Range(1, 13) on 3 * (a + b) + 2 * j - 2 * (g + c + d) equals k
														   from l in Enumerable.Range(1, 13)
														   join m in Enumerable.Range(1, 13) on (h + i + e + f) - l equals 4 * m
														   where (4 * (l + m + h + i + e + f) == 3 * (j + k + g + a + b + c + d))
														   select a + b + c + d + e + f + g + h + i + j + k + l + m;
			XElement xml22 = serializer.Serialize(e22);
			Expression result22 = serializer.Deserialize(xml22);
			Assert.AreEqual(e22.ToString(), result22.ToString());
			Console.WriteLine(((result22 as Expression<Func<IEnumerable<int>>>).Compile())().FirstOrDefault());

			Console.WriteLine("\nTEST - 23");
			Expression<Func<int, int>> e23 = n => ((Func<int, int>)(x => x + 1))(n);
			XElement xml23 = serializer.Serialize(e23);
			Expression result23 = serializer.Deserialize(xml23);
			Assert.AreEqual(e23.ToString(), result23.ToString());
			Console.WriteLine(((result23 as Expression<Func<int, int>>).Compile())(7));


			Console.WriteLine("\nTEST - 24");
			Expression<Func<IEnumerable<int>>> e24 = () => from x in Enumerable.Range(1, 10)
														   from y in Enumerable.Range(1, 10)
														   where x < y
														   select x * y;
			XElement xml24 = serializer.Serialize(e24);
			Expression result24 = serializer.Deserialize(xml24);
			Assert.AreEqual(e24.ToString(), result24.ToString());
			Console.WriteLine(((result24 as Expression<Func<IEnumerable<int>>>).Compile())());

			Console.WriteLine("\nTEST - 25");
			Expression<Func<DateTime>> e25 = () => new DateTime(10000);
			XElement xml25 = serializer.Serialize(e25);
			Expression result25 = serializer.Deserialize(xml25);
			Assert.AreEqual(e25.ToString(), result25.ToString());
			Console.WriteLine(((result25 as Expression<Func<DateTime>>).Compile())());

		}
Beispiel #20
0
        private void GetData(ExternalServiceClient client)
        {
            InvokeOnFormThread(() =>
            {
                this.chart2.Series.Clear();
                var timeseries = this.chart2.Series.Add("timeseries");
                timeseries.XValueType = ChartValueType.Date;

                textBox2.Text = "Working...";

                label4.Text = "Working...";
            });

            string query = textBox1.Text;

            //object[] parameters = new object[] { textBox1.Text };

            //Expression<Func<Mention, object[], bool>> query = (x,p) => true;

            //if (!string.IsNullOrWhiteSpace(textBox1.Text))
            //{
            //    query = (x, p) => x.Name.Contains((string)p[0]) || x.Description.Contains((string)p[0]);
            //}

            //var query2 = EvoQLBuilder.GetLambda(textBox1.Text);

            //Expression<Func<IEnumerable<ThriftMention>, IEnumerable<ThriftDatapoint>>> mapreduce = mentions =>
            //    from mention in mentions
            //    from tag in mention.Tags
            //    group mention by new { mention.OccurredOn.Year, mention.OccurredOn.Month, mention.OccurredOn.Day, TagId = tag } into g
            //    select new ThriftDatapoint { Count = g.Count(), EntityId = g.Key.TagId, Value = g.Average(x => x.Sentiment), Timestamp = new DateTime(g.Key.Year, g.Key.Month, g.Key.Day).Ticks };

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce1 = collection =>
            //    from mention in collection
            //    from tag in mention.Tags
            //    from datasource in mention.Datasources
            //    group mention by new MentionGroup{  Timestamp = mention.OccurredOnTicks, Id = tag, Id2 = datasource } into g
            //    select new Datapoint { EntityId = g.Key.Id, EntityId2 = g.Key.Id2, Timestamp = g.Key.Timestamp };

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = collection =>
            //    collection.SelectMany(m => m.Tags, (m, t) => new MentionMetadata { Mention = m, Id = t }).SelectMany(x => x.Mention.Datasources, (md, ds) => new MentionMetadata2 { Child = md, Id = ds }).GroupBy(md => new MentionGroup { Timestamp = md.Child.Mention.OccurredOnTicks, Id = md.Child.Id, Id2 = md.Id }).Select(x => new Datapoint { Timestamp = x.Key.Timestamp, EntityId = x.Key.Id, EntityId2 = x.Key.Id2 });

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = x => x.GroupBy(y => y.OccurredOnDayTicks).Select(g => new Datapoint { Count = g.Count(), Timestamp = g.Key });
            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = x => x.GroupBy(y => new MentionGroup{ Timestamp = y.OccurredOnTicks - (y.OccurredOnTicks % 864000000000) }).Select(g => new Datapoint { Count = g.Count(), Value = g.Average(y => y.Sentiment), Timestamp = g.Key.Timestamp });

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>> mapreduce = null; // ReduceExpressionGeneration.MakeExpression(null, SelectedSelect, SelectedGroupBy, SelectedGroupBy2);
            
            //Expression<Func<IEnumerable<Datapoint>, double>> merge = x => x.Sum(y => y.Value);
            //if (SelectedSelect == SelectTypes.Count) merge = x => x.Sum(y => (double)y.Count);

            var mapreduce = textBox9.Text;

            //Expression<Func<IEnumerable<Mention>, IEnumerable<Mention>>> paging = c => c.OrderByDescending(x => x.OccurredOnTicks).Take(25);

            ExpressionSerializer serializer = new ExpressionSerializer();

            //InvokeOnFormThread(() =>
            //{

            //    //ExpressionSerializer serializer = new ExpressionSerializer();
            //    //textBox6.Text = serializer.Serialize(mapreduce).ToString();
            //});

            var domain = textBox3.Text;
            //var minDate = dateTimePicker1.Value;
            //var maxDate = dateTimePicker2.Value;

            string command = textBox7.Text;

            Stopwatch sw = new Stopwatch();
            sw.Start();
            var result = client.GetData(domain, "__ql__" + query, "__ql__" + mapreduce, "__default__", null, command);
            sw.Stop();

            result.Metadata.OperationTime = sw.Elapsed;

            InvokeOnFormThread(() =>
            {
                var area = chart2.ChartAreas.First();
                var oldseries = chart2.Series.ToArray();
                string separator = "\r\n################################################################################################################################\r\n";

                HardcodedBermudaDatapoint[] datapoints = new HardcodedBermudaDatapoint[0];
                string jsonError = null;

                if (result.Data != null)
                {
                    try
                    {
                        datapoints = new JavaScriptSerializer().Deserialize<HardcodedBermudaDatapoint[]>(result.Data);
                        label4.Text = "Retrieved " + datapoints.Count() + "\r\nin " + sw.Elapsed;
                    }
                    catch (Exception ex)
                    {
                        jsonError = ex.ToString() + separator;
                    }
                }
                else
                {
                    datapoints = new HardcodedBermudaDatapoint[0];
                }


                StringBuilder sb = new StringBuilder();

                if (result.Metadata != null) ConvertStatsToString(sb, result.Metadata);
                textBox2.Text = result.CacheKey + separator + jsonError + result.Error + separator + sb.ToString() + separator + result.Data;

                var groups = datapoints.GroupBy(x => new { x.Id, x.Text } );
                
                chart2.SuspendLayout();

                foreach (var s in chart2.Series.Skip(1).ToArray())
                {
                    chart2.Series.Remove(s);
                }

                int i = 0;
                foreach (var g in groups)
                {
                    string name = g.Key.Text ?? g.Key.Id.ToString();
                    var timeseries = i == 0 ? chart2.Series.FirstOrDefault() : chart2.Series.Add(name);
                    timeseries.Points.Clear();
                    timeseries.Name = name;

                    timeseries.XValueType = g.Any(p => new DateTime(p.Id2).Year > 2000) ? ChartValueType.Date : ChartValueType.Int64;

                    foreach (var d in g)
                    {
                        timeseries.Points.Add(timeseries.XValueType == ChartValueType.Date ? new DataPoint(new DateTime(d.Id2).ToOADate(), d.Value) : new DataPoint(d.Id2, d.Value));
                    }

                    i++;
                }
                
                chart2.ResumeLayout(true);
                
                
                //textBox2.Text = sb.ToString().Trim() + separator + string.Join("\r\n", datapoints.Datapoints.Select(x => x.Id.ToString().PadLeft(32) + x.Id2.ToString().PadLeft(32) + ((int)x.Value).ToString().PadLeft(16) + x.Count.ToString().PadLeft(16) ));
                
                //var sb2 = new StringBuilder();
                //ConvertStatsToString(sb2, mentions.Metadata);
                //textBox5.Text = sb2.ToString().Trim() + separator + string.Join(separator, mentions.Mentions.Select(x => DateTime.FromBinary( x.OccurredOnTicks ) + " :: " + x.Name + "\r\n" + x.Description));

                //label4.Text = "Received " + (datapoints.Datapoints.Count()) + " datapoints\r\nin " + sw.Elapsed;
            });

            //return datapoints.Datapoints;
        }
Beispiel #21
0
        private static object GetMergeFunc(string str, string mapreduce, Type itemType)
        {
            if (str == null) return null;

            object result = null;

            //if (mergeFuncCache.TryGetValue(str, out result)) return result;

            //if (str == DefaultToken)
            //{
            //    Expression<Func<IEnumerable<Datapoint>, double>> merge = x => x.Sum(y => y.Value);
            //    result = merge.Compile();
            //}
            //else if (str == "__average__")
            //{
            //    Expression<Func<IEnumerable<Datapoint>, double>> merge = x => x.Sum(y => y.Value * (double)y.Count) / x.Sum(y => y.Count);
            //    result = merge.Compile();
            //}
            if (str == DefaultToken)
            {
                if (!mapreduce.StartsWith(QlHeader)) throw new Exception("QL based mapreduce expression must be used with __default__ merge specification");
                var expr = EvoQLBuilder.GetMergeExpression(mapreduce.Substring(QlHeader.Length), itemType);
                var type = expr == null ? null : expr.GetType();
                var compileMethod = expr == null ? null : type.GetMethods().FirstOrDefault(x => x.Name == "Compile" && x.GetParameters().Length == 0);
                result = compileMethod.Invoke(expr, new object[0]);
            }
            else
            {
                var serializer = new ExpressionSerializer(new TypeResolver(new Assembly[] { Assembly.GetAssembly(typeof(Entities.Thrift.ThriftMention)) }));
                var expr = serializer.Deserialize<Func<IEnumerable<Datapoint>, double>>(XElement.Parse(str));
                result = expr.Compile();
            }

            mergeFuncCache[str] = result;

            return result;
        }
Beispiel #22
0
        private static object GetMapReduceFunc(string str, Type itemType, out Type resultType)
        {
            if (str == null)
            {
                resultType = null;
                return null;
            }

            object result = null;

            //if (reduceFuncCache.TryGetValue(str, out result)) return result;

            if (str.StartsWith(QlHeader))
            {
                var expr = EvoQLBuilder.GetReduceExpression(str.Substring(QlHeader.Length), itemType);
                var type = expr == null ? null : expr.GetType();
                resultType = expr == null ? itemType : ReduceExpressionGeneration.GetTypeOfEnumerable( type.GetProperty("ReturnType").GetValue(expr, null) as Type );
                var compileMethod = expr == null ? null : type.GetMethods().FirstOrDefault(x => x.Name == "Compile" && x.GetParameters().Length == 0);
                result = compileMethod.Invoke(expr, new object[0]);
            }
            else
            {
                var serializer = new ExpressionSerializer(new TypeResolver(new Assembly[] { Assembly.GetAssembly(typeof(Mention)) }));
                var expr = serializer.Deserialize<Func<IEnumerable<Mention>, IEnumerable<Datapoint>>>(XElement.Parse(str));
                resultType = typeof(Datapoint);
                result = expr.Compile();
            }

            reduceFuncCache[str] = result;

            return result;
        }
Beispiel #23
0
		public void BasicExpressionSerialization()
		{
			Type funcT1T2T3 = typeof(Func<>).Assembly.GetType("System.Func`3");

			Debug.WriteLine("BASIC SAMPLE - Serialize/Deserialize Simple Expression:");
			XElement addXml;
			Expression<Func<int, int, int>> addExpr = (x, y) => x + y;
			ExpressionSerializer serializer = new ExpressionSerializer();
			Expression simplifiedAddExpr = Evaluator.PartialEval(addExpr);
			//addXml = serializer.Serialize(simplifiedAddExpr);	//does not seem necessary
			addXml = serializer.Serialize(addExpr);
			Expression<Func<int, int, int>> addExpResult = serializer.Deserialize<Func<int, int, int>>(addXml);
			Func<int, int, int> addExpResultFunc = addExpResult.Compile();
			int result = addExpResultFunc(1, 2);  // evaluates to 3

			Debug.WriteLine("Deserialized Expression Tree:");
			Debug.WriteLine(" " + addExpResult.ToString());
		}
        protected System.Tuple<Expression, Type> DeserializeExpression(XElement rootXml)
        {
            //move to ctor
            BoxSerializationTypeResolver resolver = new BoxSerializationTypeResolver(this);
            BoxCustomExpressionXmlConverter customConverter = new BoxCustomExpressionXmlConverter(resolver, RepoFinder.Mappings);
            ExpressionSerializer serializer = new ExpressionSerializer(resolver) { Converters = { customConverter } };

            return new System.Tuple<Expression, Type>(serializer.Deserialize(rootXml), customConverter.QueryKind.ElementType);
        }
 private static ExpressionSerializer GetSerializer()
 {
     var tr = new TypeResolver(new Assembly[] { Assembly.GetAssembly(typeof(ExpressionSerializationTest)) },
                               new Type[] { typeof(DTO), typeof(Entity), typeof(BaseClass) });
     var serializer = new ExpressionSerializer(tr, new List<CustomExpressionXmlConverter>());
     return serializer;
 }
Beispiel #26
0
        private static object GetFilterFunc(string str, Type itemType)
        {
            if (str == null) return null;

            object result = null;

            if (filterFuncCache.TryGetValue(str, out result)) return result;

            if (str.StartsWith(QlHeader))
            {
                var expr = EvoQLBuilder.GetWhereExpression(str.Substring(QlHeader.Length), itemType);
                var type = expr == null ? null : expr.GetType();
                var compileMethod = expr == null ? null : type.GetMethods().FirstOrDefault(x => x.Name == "Compile" && x.GetParameters().Length == 0);
                result = compileMethod.Invoke(expr, new object[0]);
            }
            else
            {
                var serializer = new ExpressionSerializer(new TypeResolver(new Assembly[] { Assembly.GetAssembly(typeof(Mention)) }));
                var expr = serializer.Deserialize<Func<Mention, object[], bool>>(XElement.Parse(str));
                result = expr.Compile();
            }

            filterFuncCache[str] = result;

            return result;
        }
Beispiel #27
0
 public static XElement SerializeQuery(this IQueryable query)
 {
     DLinqSerializationTypeResolver resolver = new DLinqSerializationTypeResolver(null);
     ExpressionSerializer serializer = new ExpressionSerializer(resolver) { Converters = { new DLinqCustomExpressionXmlConverter(null, resolver) } };
     return serializer.Serialize(query.Expression);
 }