Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Core/3.3/Attributes/CreatableAttribute.cs @ 16946

Last change on this file since 16946 was 16946, checked in by mkommend, 5 years ago

#2521: Merged trunk changes up to r16945 into branch.

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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;
25
26namespace HeuristicLab.Core {
27  [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
28  public sealed class CreatableAttribute : Attribute {
29    #region Predefined Categories
30    public static class Categories {
31      public const string SplitToken = "###";
32      public const string OrderToken = "$$$";
33
34      public const string Algorithms = "1" + OrderToken + "Algorithms";
35      public const string PopulationBasedAlgorithms = Algorithms + SplitToken + "1" + OrderToken + "Population Based";
36      public const string SingleSolutionAlgorithms = Algorithms + SplitToken + "2" + OrderToken + "Single Solution";
37      public const string ExactAlgorithms = Algorithms + SplitToken + "3" + OrderToken + "Exact";
38
39      public const string Problems = "2" + OrderToken + "Problems";
40      public const string CombinatorialProblems = Problems + SplitToken + "1" + OrderToken + "Combinatorial";
41      public const string GeneticProgrammingProblems = Problems + SplitToken + "2" + OrderToken + "Genetic Programming";
42      public const string ExternalEvaluationProblems = Problems + SplitToken + "3" + OrderToken + "External Evaluation";
43      public const string ExternalEvaluationProblemsSingleObjective = ExternalEvaluationProblems + SplitToken + "1" + OrderToken + "Single-Objective";
44      public const string ExternalEvaluationProblemsMultiObjective = ExternalEvaluationProblems + SplitToken + "2" + OrderToken + "Multi-Objective";
45      public const string ProgrammableProblems = Problems + SplitToken + "4" + OrderToken + "Programmable";
46      public const string ProgrammableProblemsSingleObjective = ProgrammableProblems + SplitToken + "1" + OrderToken + "Single-Objective";
47      public const string ProgrammableProblemsMultiObjective = ProgrammableProblems + SplitToken + "2" + OrderToken + "Multi-Objective";
48
49      public const string DataAnalysis = "3" + OrderToken + "Data Analysis";
50      public const string DataAnalysisRegression = DataAnalysis + SplitToken + "1" + OrderToken + "Regression";
51      public const string DataAnalysisClassification = DataAnalysis + SplitToken + "2" + OrderToken + "Classification";
52      public const string DataAnalysisEnsembles = DataAnalysis + SplitToken + "3" + OrderToken + "Ensembles";
53
54      public const string TestingAndAnalysis = "4" + OrderToken + "Testing & Analysis";
55      public const string TestingAndAnalysisOKB = TestingAndAnalysis + SplitToken + "1" + OrderToken + "OKB";
56
57      public const string Scripts = "5" + OrderToken + "Scripts";
58
59      public static string GetFullName(string rawName) {
60        return string.Join(SplitToken, GetTokens(rawName));
61      }
62      public static string GetName(string rawName) {
63        return GetTokens(rawName).Last();
64      }
65      public static IEnumerable<string> GetParentRawNames(string rawName) {
66        var tokens = GetTokensWithOrdering(rawName).ToList();
67        return tokens.Take(tokens.Count - 1);
68      }
69      private static IEnumerable<string> GetTokensWithOrdering(string rawName) {
70        return rawName.Split(new[] { SplitToken }, StringSplitOptions.RemoveEmptyEntries);
71      }
72      private static IEnumerable<string> GetTokens(string rawName) {
73        return GetTokensWithOrdering(rawName)
74          .Select(t => t.Split(new[] { OrderToken }, StringSplitOptions.RemoveEmptyEntries).Last());
75      }
76    }
77    #endregion
78
79    private string category;
80    public string Category {
81      get {
82        return category;
83      }
84      set {
85        if (value == null) throw new ArgumentNullException("Category", "CreataleAttribute.Category must not be null");
86        category = value;
87      }
88    }
89
90    public int Priority { get; set; }
91
92    public CreatableAttribute() {
93      Category = "Other Items";
94      Priority = int.MaxValue;
95    }
96    public CreatableAttribute(string category)
97      : this() {
98      Category = category;
99    }
100
101    public static bool IsCreatable(Type type) {
102      object[] attribs = type.GetCustomAttributes(typeof(CreatableAttribute), false);
103      return attribs.Length > 0;
104    }
105    public static string GetCategory(Type type) {
106      object[] attribs = type.GetCustomAttributes(typeof(CreatableAttribute), false);
107      if (attribs.Length > 0) return ((CreatableAttribute)attribs[0]).Category;
108      else return null;
109    }
110    public static int GetPriority(Type type) {
111      var attribs = type.GetCustomAttributes(typeof(CreatableAttribute), false);
112      if (attribs.Length > 0) return ((CreatableAttribute)attribs[0]).Priority;
113      else return int.MaxValue;
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.