Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Services.OKB.AttributeSelection/3.3/ExpressionTools.cs @ 6810

Last change on this file since 6810 was 4279, checked in by swagner, 14 years ago

Integrated OKB services (#1166)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Linq.Expressions;
26using System.Reflection;
27using HeuristicLab.Services.OKB.DataAccess;
28
29namespace HeuristicLab.Services.OKB.AttributeSelection {
30
31  public static class ExpressionTools {
32
33    /// <summary>
34    /// p => f(g(p)) where p : T
35    /// </summary>
36    public static LambdaExpression CallAfter<T>(Expression f, Expression g) {
37      ParameterExpression p = Expression.Parameter(typeof(T), "p");
38      return Expression.Lambda(Expression.Invoke(f, Expression.Invoke(g, p)), p);
39    }
40
41    public static Selector GetSelector<T, D>(Expression<Func<Run, T>> tableSelector, Expression<Func<T, D>> fieldSelector) {
42      Expression member = fieldSelector.Body;
43      while (member is UnaryExpression)
44        member = ((UnaryExpression)member).Operand;
45      if (!(member is MemberExpression))
46        throw new ArgumentException("cannot determine field name");
47      string fieldName = ((MemberExpression)member).Member.Name;
48      return new Selector() {
49        Table = tableSelector.Body.Type,
50        Field = fieldName,
51        Expression = ExpressionTools.CallAfter<Run>(fieldSelector, tableSelector),
52      };
53    }
54
55    public static readonly List<Type> SupportedTypes = new List<Type>() {
56      typeof(int), typeof(double), typeof(DateTime), typeof(Guid) };
57
58    public static BinaryExpression GetLessThanOrEqualExpression<T>(Expression left, Expression right) {
59      if (SupportedTypes.Contains(typeof(T)))
60        return Expression.LessThanOrEqual(left, right);
61      else if (typeof(T) == typeof(string))
62        return Expression.LessThanOrEqual(
63          GetStringComparisonExpression(left, right), Expression.Constant(0));
64      else throw new ArgumentException("Unsupported comparison for types of " + typeof(T));
65    }
66
67    public static BinaryExpression GetGreaterOrEqualExpression<T>(Expression left, Expression right) {
68      if (SupportedTypes.Contains(typeof(T)))
69        return Expression.GreaterThanOrEqual(left, right);
70      else if (typeof(T) == typeof(string))
71        return Expression.GreaterThanOrEqual(
72          GetStringComparisonExpression(left, right), Expression.Constant(0));
73      else throw new ArgumentException("Unsupported comparison for types of " + typeof(T));
74    }
75
76    public static MethodCallExpression GetStringComparisonExpression(Expression left, Expression right) {
77      return Expression.Call(null, String_Compare, left, right);
78    }
79
80    public static readonly MethodInfo String_Compare =
81      typeof(string).GetMethod("Compare", new[] { typeof(string), typeof(string) });
82
83    public static readonly MethodInfo Enumerable_Contains =
84      typeof(Enumerable).GetMethods().Single(mi => mi.Name == "Contains" && mi.GetParameters().Count() == 2);
85
86    public static readonly MethodInfo Enumerable_Single =
87      typeof(Enumerable).GetMethods().Single(mi => mi.Name == "Single" && mi.GetParameters().Count() == 2);
88
89    public static readonly MethodInfo Enumerable_Where =
90      (from mi in typeof(Enumerable).GetMethods()
91       where mi.Name == "Where"
92       let p = mi.GetParameters()
93       where p.Count() == 2
94       let pt = p[1].ParameterType
95       where pt.IsGenericType
96       where pt.GetGenericArguments().Count() == 2
97       select mi).Single();
98
99    public static readonly MethodInfo Enumerable_Cast = typeof(Enumerable).GetMethod("Cast");
100
101    public static readonly MethodInfo Enumerable_OrderBy =
102      typeof(Enumerable).GetMethods().Single(mi => mi.Name == "OrderBy" && mi.GetParameters().Count() == 2);
103
104    public static readonly MethodInfo Enumerable_Count =
105      typeof(Enumerable).GetMethods().Single(mi => mi.Name == "Count" && mi.GetParameters().Count() == 1);
106  }
107}
Note: See TracBrowser for help on using the repository browser.