Changeset 12512


Ignore:
Timestamp:
06/25/15 15:02:26 (4 years ago)
Author:
pfleck
Message:

#2025

  • Added some helper for parsing the raw category string.
  • Fixed handling of creatable items with old category descriptions (without ordering).
  • Unified naming of variables added some comments.
Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/Attributes/CreatableAttribute.cs

    r12506 r12512  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Linq;
    2325
    2426namespace HeuristicLab.Core {
     
    4951      public const string Scripts = "5" + OrderToken + "Scripts";
    5052
     53      public static string GetFullName(string rawName) {
     54        return string.Join(SplitToken, GetTokens(rawName));
     55      }
     56      public static string GetName(string rawName) {
     57        return GetTokens(rawName).Last();
     58      }
     59      public static IEnumerable<string> GetParentRawNames(string rawName) {
     60        var tokens = GetTokensWithOrdering(rawName).ToList();
     61        return tokens.Take(tokens.Count - 1);
     62      }
    5163
     64      private static IEnumerable<string> GetTokensWithOrdering(string rawName) {
     65        return rawName.Split(new[] { SplitToken }, StringSplitOptions.RemoveEmptyEntries);
     66      }
     67      private static IEnumerable<string> GetTokens(string rawName) {
     68        return GetTokensWithOrdering(rawName)
     69          .Select(t => t.Split(new[] { OrderToken }, StringSplitOptions.RemoveEmptyEntries).Last());
     70      }
    5271    }
    5372    #endregion
  • trunk/sources/HeuristicLab.Optimizer/3.3/NewItemDialog.cs

    r12506 r12512  
    2626using System.Linq;
    2727using System.Windows.Forms;
     28using HeuristicLab.Common;
    2829using HeuristicLab.Core;
    2930using HeuristicLab.PluginInfrastructure;
     
    6364      if (isInitialized) return;
    6465
     66      // Sorted by hasOrdering to create category nodes first with concrete ordering.
     67      // Items with categoryname without ordering are inserted afterwards correctly
    6568      var categories =
    6669        from type in ApplicationManager.Manager.GetTypes(typeof(IItem))
     70        where CreatableAttribute.IsCreatable(type)
    6771        let category = CreatableAttribute.GetCategory(type)
     72        let hasOrdering = category.Contains(CreatableAttribute.Categories.OrderToken)
    6873        let name = ItemAttribute.GetName(type)
    6974        let priority = CreatableAttribute.GetPriority(type)
    7075        let version = ItemAttribute.GetVersion(type)
    71         where CreatableAttribute.IsCreatable(type)
    72         orderby category, priority, name, version ascending
     76        orderby category, hasOrdering descending, priority, name, version ascending
    7377        group type by category into categoryGroup
    7478        select categoryGroup;
     
    9498      var rootNode = new TreeNode();
    9599
     100      // CategoryNode
     101      // Tag: raw string, used for sorting, e.g. 1$$$Algorithms###2$$$Single Solution
     102      // Name: full name = combined category name with parent categories, used for finding nodes in tree, e.g. Algorithms###Single Solution
     103      // Text: category name, used for displaying on node itself, e.g. Single Solution
     104
    96105      foreach (var category in categories) {
    97         var fullName = category.Key;
    98         var tokensWithOrdering = fullName.Split(new[] { CreatableAttribute.Categories.SplitToken }, StringSplitOptions.RemoveEmptyEntries);
    99         var tokens = tokensWithOrdering.Select(t => t.Split(new[] { CreatableAttribute.Categories.OrderToken }, StringSplitOptions.RemoveEmptyEntries).Last()).ToList();
    100         var name = tokens.Last();
    101         var parents = tokensWithOrdering.Take(tokens.Count - 1);
     106        var rawName = category.Key;
     107        string fullName = CreatableAttribute.Categories.GetFullName(rawName);
     108        string name = CreatableAttribute.Categories.GetName(rawName);
     109
     110        // Skip categories with same full name because the raw name can still be different (missing order)
     111        if (rootNode.Nodes.Find(fullName, searchAllChildren: true).Length > 0)
     112          continue;
    102113
    103114        var categoryNode = new TreeNode(name, imageIndex: 1, selectedImageIndex: 1) {
    104115          Name = fullName,
    105           Tag = fullName
     116          Tag = rawName
    106117        };
    107118
     119        var parents = CreatableAttribute.Categories.GetParentRawNames(rawName);
    108120        var parentNode = FindOrCreateParentNode(rootNode, parents);
    109121        if (parentNode != null)
     
    115127      return rootNode;
    116128    }
    117     private TreeNode FindOrCreateParentNode(TreeNode node, IEnumerable<string> parentCategories) {
     129    private TreeNode FindOrCreateParentNode(TreeNode node, IEnumerable<string> rawParentNames) {
    118130      TreeNode parentNode = null;
    119       string fullName = null;
    120       foreach (string parentCategory in parentCategories) {
    121         fullName = fullName == null ? parentCategory : fullName + CreatableAttribute.Categories.SplitToken + parentCategory;
     131      string rawName = null;
     132      foreach (string rawParentName in rawParentNames) {
     133        rawName = rawName == null ? rawParentName : rawName + CreatableAttribute.Categories.SplitToken + rawParentName;
     134        var fullName = CreatableAttribute.Categories.GetFullName(rawName);
    122135        parentNode = node.Nodes.Find(fullName, searchAllChildren: false).SingleOrDefault();
    123136        if (parentNode == null) {
    124           parentNode = new TreeNode(parentCategory, imageIndex: 1, selectedImageIndex: 1) {
     137          var name = CreatableAttribute.Categories.GetName(rawName);
     138          parentNode = new TreeNode(name, imageIndex: 1, selectedImageIndex: 1) {
    125139            Name = fullName,
    126             Tag = fullName
     140            Tag = rawName
    127141          };
    128142          node.Nodes.Add(parentNode);
     
    134148    private void CreateItemNodes(TreeNode node, IEnumerable<IGrouping<string, Type>> categories) {
    135149      foreach (var category in categories) {
    136         var categoryNode = node.Nodes.Find(category.Key, searchAllChildren: true).Single();
     150        var fullName = CreatableAttribute.Categories.GetFullName(category.Key);
     151        var categoryNode = node.Nodes.Find(fullName, searchAllChildren: true).Single();
    137152        foreach (var creatable in category) {
    138153          var itemNode = CreateItemNode(creatable);
    139           itemNode.Name = itemNode.Name + ":" + category.Key;
     154          itemNode.Name = itemNode.Name + ":" + fullName;
    140155          categoryNode.Nodes.Add(itemNode);
    141156        }
     
    257272
    258273      if (typesTreeView.SelectedNode != null) {
    259         string category = typesTreeView.SelectedNode.Tag as string;
     274        var node = typesTreeView.SelectedNode;
     275        string category = node.Tag as string;
    260276        if (category != null) {
    261           itemDescriptionTextBox.Text = category;
    262         }
    263         Type type = typesTreeView.SelectedNode.Tag as Type;
     277          itemDescriptionTextBox.Text = string.Join(" - ", node.Name.Split(new[] { CreatableAttribute.Categories.SplitToken }, StringSplitOptions.RemoveEmptyEntries));
     278        }
     279        Type type = node.Tag as Type;
    264280        if (type != null) {
    265281          string description = ItemAttribute.GetDescription(type);
     
    433449
    434450    private class ItemTreeNodeComparer : IComparer {
     451      private static readonly IComparer<string> Comparer = new NaturalStringComparer();
    435452      public int Compare(object x, object y) {
    436453        var lhs = (TreeNode)x;
     
    438455
    439456        if (lhs.Tag is string && rhs.Tag is string) {
    440           return lhs.Name.CompareTo(rhs.Name);
     457          return Comparer.Compare((string)lhs.Tag, (string)rhs.Tag);
    441458        } else if (lhs.Tag is string) {
    442459          return -1;
Note: See TracChangeset for help on using the changeset viewer.