Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4435 was 4298, checked in by swagner, 14 years ago

Worked on OKB user authentication (#1167)

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