Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Services.OKB.AttributeSelector/3.3/AttributeSelector.cs @ 4279

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

Integrated OKB services (#1166)

File size: 6.5 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.Data.Linq;
25using System.Linq;
26using System.Linq.Expressions;
27using System.Text;
28using HeuristicLab.Services.OKB.DataAccess;
29
30namespace HeuristicLab.Services.OKB.AttributeSelection {
31
32  public class AttributeSelector : IAttributeSelector {
33
34    public string TableName { get; private set; }
35    public string FieldName { get; private set; }
36    public bool IsHidden { get; set; }
37    public object MinValue { get; set; }
38    public object MaxValue { get; set; }
39    public ICollection<object> AllowedValues { get; set; }
40
41    public Type TableType { get; private set; }
42    public bool IsDynamic { get; private set; }
43    public Type DataType { get; private set; }
44    public int ParentId { get; private set; }
45
46    protected static readonly List<string> staticTables = new List<string>() {
47      typeof(AlgorithmClass).Name,
48      typeof(Algorithm).Name,
49      typeof(ProblemClass).Name,
50      typeof(Problem).Name,
51      typeof(Experiment).Name,
52      typeof(Platform).Name,
53      typeof(SolutionRepresentation).Name,
54      typeof(Project).Name,
55      typeof(Run).Name,
56      typeof(Client).Name,
57      typeof(User).Name,
58      typeof(ExperimentCreator).Name,
59    };
60
61    protected static readonly List<string> dynamicTables = new List<string>() {
62      typeof(ProblemCharacteristic).Name,
63      typeof(Parameter).Name,
64      typeof(Result).Name,
65    };
66
67    protected AttributeSelector(Type tableType, string fieldName, bool isDynamic, Type dataType, int parentId) {
68      TableName = tableType.Name;
69      FieldName = fieldName;
70      TableType = tableType;
71      IsDynamic = isDynamic;
72      DataType = dataType;
73      ParentId = parentId;
74    }
75
76    public AttributeSelector(OKBDataContext okb, string tableName, string fieldName) {
77      TableName = tableName;
78      FieldName = fieldName;
79      if (tableName == typeof(ExperimentCreator).Name)
80        tableName = typeof(User).Name;
81      TableType = GetTableType(tableName);
82      ITable Table = GetTable(okb, tableName);
83      if (staticTables.Contains(tableName)) {
84        IsDynamic = false;
85        try {
86          DataType = TableType.GetProperty(fieldName).PropertyType;
87        }
88        catch (Exception x) {
89          throw new ArgumentException(String.Format(
90            "Invalid field name {0} in table {1}", fieldName, tableName),
91            x);
92        }
93      } else if (dynamicTables.Contains(tableName)) {
94        IsDynamic = true;
95        try {
96          IDynamicParent parent =
97            Table
98            .Cast<IDynamicParent>()
99            .Single(t => t.Name == fieldName);
100          ParentId = parent.Id;
101          DataType = parent.DataType.Type;
102        }
103        catch (Exception x) {
104          throw new ArgumentException(String.Format(
105            "Invalid field name {0} in table {1}", fieldName, tableName),
106            x);
107        }
108      } else {
109        throw new ArgumentException(String.Format("Invalid table name {0}", tableName));
110      }
111    }
112
113    protected static Type GetTableType(string name) {
114      try {
115        return typeof(Run).Assembly.GetType(typeof(Run).Namespace + "." + name, true);
116      }
117      catch {
118        return null;
119      }
120    }
121
122    protected static ITable GetTable(OKBDataContext okb, string name) {
123      return okb.GetTable(GetTableType(name));
124    }
125
126    public string FullName {
127      get {
128        return new StringBuilder().Append(TableName).Append('_').Append(FieldName).ToString();
129      }
130    }
131
132    protected static void LoadWith(DataLoadOptions dlo, Type parentType, Type type) {
133      ParameterExpression parent = Expression.Parameter(parentType, parentType.Name.ToLower());
134      LambdaExpression expr = Expression.Lambda(Expression.PropertyOrField(parent, type.Name), parent);
135      try {
136        dlo.LoadWith(expr);
137      }
138      catch (ArgumentException) {
139        Console.WriteLine(String.Format("Info: dlo.LoadWith<{0}>({1}) has already been added", parent.Type.Name, expr));
140      }
141    }
142
143    protected LambdaExpression GetDynamicSelector<T>(
144      Type masterType,
145      Expression<Func<Run, T>> parentSelector,
146      Expression<Func<T, int>> intSelector,
147      Expression<Func<T, double>> doubleSelector,
148      Expression<Func<T, string>> stringSelector,
149      Expression<Func<T, object>> operatorNameSelector) {
150      if (TableType != masterType)
151        return null;
152      ParameterExpression run = Expression.Parameter(typeof(Run), "run");
153      switch (Type.GetTypeCode(DataType)) {
154        case TypeCode.Int32:
155          return ExpressionTools.CallAfter<Run>(intSelector, parentSelector);
156        case TypeCode.Double:
157          return ExpressionTools.CallAfter<Run>(doubleSelector, parentSelector);
158        case TypeCode.String:
159          return ExpressionTools.CallAfter<Run>(stringSelector, parentSelector);
160        case TypeCode.Object:
161          return ExpressionTools.CallAfter<Run>(operatorNameSelector, parentSelector);
162      };
163      return null;
164    }
165
166    public override string ToString() {
167      StringBuilder sb = new StringBuilder()
168        .Append(TableName)
169        .Append('.')
170        .Append(FieldName);
171      if (MinValue != null)
172        sb.Append(" >").Append(MinValue);
173      if (MaxValue != null)
174        sb.Append(" <").Append(MaxValue);
175      if (AllowedValues != null)
176        sb.Append(" in [")
177          .Append(string.Join(", ",
178                    AllowedValues
179                    .Select(v => v.ToString())
180                    .ToArray()))
181          .Append(']');
182      if (IsHidden)
183        return String.Format("({0})", sb.ToString());
184      else
185        return sb.ToString();
186    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.