Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Services.OKB.AttributeSelection/3.3/DataSetBuilder.cs @ 7634

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

Integrated OKB services (#1166)

File size: 5.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;
24using System.Collections.Generic;
25using System.Data;
26using System.Data.Linq;
27using System.Linq;
28using System.Linq.Expressions;
29using HeuristicLab.Services.OKB.DataAccess;
30
31
32namespace HeuristicLab.Services.OKB.AttributeSelection {
33  public class DataSetBuilder : IEnumerable<DataRow> {
34
35    public static DataSet GetDataSet(OKBDataContext okb, IEnumerable<RunAttributeSelector> selectors) {
36      DataSetBuilder builder = new DataSetBuilder(okb, selectors);
37      DataTable runs = new DataTable("Runs");
38      foreach (DataRow row in builder) {
39        runs.Rows.Add(row);
40      }
41      DataSet dataSet = new DataSet();
42      dataSet.Tables.Add(runs);
43      return dataSet;
44    }
45
46    private IQueryable<Run> query;
47    public DataTable Runs { get; private set; }
48    private IEnumerable<RunAttributeSelector> selectors;
49
50    public DataSetBuilder(OKBDataContext okb, IEnumerable<RunAttributeSelector> selectors) {
51      DataLoadOptions dlo = new DataLoadOptions();
52      query = okb.Runs.AsQueryable();
53      Runs = new DataTable("Runs");
54      this.selectors = selectors;
55
56      foreach (var selector in selectors) {
57        query = query.Where(selector.GetWhereExpression());
58        selector.ConfigureDataLoadOptions(dlo);
59        if (!selector.IsHidden) {
60          AddColumn(selector);
61        }
62      }
63
64      okb.LoadOptions = dlo;
65      Console.WriteLine(query);
66    }
67
68    private void AddColumn(RunAttributeSelector selector) {
69      Type type = selector.DataType;
70      if (type == typeof(object))
71        type = typeof(string);
72      if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
73        type = type.GetGenericArguments()[0];
74      int i = 0;
75      string columnName = selector.FullName;
76      while (Runs.Columns.Contains(columnName)) {
77        i++;
78        columnName = string.Format("{0}({1})", selector.FullName, i);
79      }
80      Runs.Columns.Add(columnName, type);
81    }
82
83    public IEnumerator<DataRow> GetEnumerator() {
84      foreach (var run in query) {
85        DataRow row = Runs.NewRow();
86        int columnNr = 0;
87        foreach (var selector in selectors.Where(s => !s.IsHidden)) {
88          object value = selector.CompiledSelector(run);
89          row[columnNr] = selector.CompiledSelector(run) ?? DBNull.Value;
90          columnNr++;
91        }
92        yield return row;
93      }
94    }
95
96    IEnumerator IEnumerable.GetEnumerator() {
97      return GetEnumerator();
98    }
99
100
101    public int GetCount() {
102      return query.Count();
103    }
104
105    public static IEnumerable<object> GetEntities(OKBDataContext okb, Type tableType, IEnumerable<int> ids) {
106      ITable table = okb.GetTable(tableType);
107
108      // idFilter = element => ids.Contains(element.Id)
109      ParameterExpression param = Expression.Parameter(table.ElementType, "element");
110      LambdaExpression idFilter = Expression.Lambda(
111        Expression.Call(
112          null,
113          ExpressionTools.Enumerable_Contains.MakeGenericMethod(typeof(int)),
114          Expression.Constant(ids),
115          Expression.Property(param, "Id")), param);
116
117      // idSelector = element => element.Id
118      ParameterExpression param2 = Expression.Parameter(table.ElementType, "element");
119      LambdaExpression idSelector = Expression.Lambda(
120        Expression.Property(param2, "Id"), param2);
121
122      // selector = () => okb.<tableType>.Where(idFilter).OrderBy(idSelector).Cast<object>()
123      LambdaExpression selector =
124        Expression.Lambda(
125          Expression.Call(
126            null,
127            ExpressionTools.Enumerable_Cast.MakeGenericMethod(typeof(object)),
128            Expression.Call(
129              null,
130              ExpressionTools.Enumerable_OrderBy.MakeGenericMethod(table.ElementType, typeof(int)),
131              Expression.Call(
132                null,
133                ExpressionTools.Enumerable_Where.MakeGenericMethod(table.ElementType),
134                table.Expression,
135                idFilter),
136             idSelector)));
137      return (IEnumerable<object>)selector.Compile().DynamicInvoke();
138    }
139
140    public static int CountEntities(OKBDataContext okb, Type tableType) {
141      ITable table = okb.GetTable(tableType);
142      // okb.<tableType>.Count();
143      LambdaExpression counter =
144        Expression.Lambda(
145          Expression.Call(
146            null,
147            ExpressionTools.Enumerable_Count.MakeGenericMethod(table.ElementType),
148            table.Expression));
149      return (int)counter.Compile().DynamicInvoke();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.