Ignore:
Timestamp:
07/03/14 16:36:10 (5 years ago)
Author:
ascheibe
Message:

#2117 merged r10130, r10150, r10154, r10170, r11079 into stable

Location:
stable
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Clients.Hive/3.3/Tasks/ItemTask.cs

    r9456 r11083  
    2222using System;
    2323using System.Drawing;
     24using System.Linq;
    2425using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Hive;
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     29using HeuristicLab.PluginInfrastructure;
    2830
    2931namespace HeuristicLab.Clients.Hive {
     
    3133  [StorableClass]
    3234  public abstract class ItemTask : NamedItem, ITask {
     35    public virtual HiveTask CreateHiveTask() {
     36      return new HiveTask(this, true);
     37    }
     38
    3339    public virtual bool IsParallelizable {
    3440      get { return true; }
     41      set { }
    3542    }
    3643
     
    7178      this.Item = cloner.Clone(original.Item);
    7279    }
     80    public ItemTask(IItem item) {
     81      Item = item;
     82    }
    7383
    7484    [StorableHook(HookType.AfterDeserialization)]
     
    211221      return Name;
    212222    }
     223
     224    #region Helpers
     225    public static bool IsTypeSupported(Type itemType) {
     226      var supportedHiveTaskTypes = ApplicationManager.Manager.GetTypes(typeof(ItemTask))
     227          .Select(t => t.GetProperties().Single(x => x.Name == "Item" && x.PropertyType != typeof(IItem)).PropertyType);
     228      return supportedHiveTaskTypes.Any(x => x.IsAssignableFrom(itemType));
     229    }
     230
     231    public static Type GetHiveTaskType(Type itemType) {
     232      if (!IsTypeSupported(itemType)) throw new Exception("Item " + itemType + " is not supported for Hive.");
     233
     234      var typeHiveTaskMap = ApplicationManager.Manager.GetTypes(typeof(ItemTask))
     235          .Select(t => new Tuple<Type, Type>(t.GetProperties().Single(x => x.Name == "Item" && x.PropertyType != typeof(IItem)).PropertyType, t));
     236
     237      return typeHiveTaskMap.Single(x => x.Item1.IsAssignableFrom(itemType)).Item2;
     238    }
     239
     240    public static ItemTask GetItemTaskForItem(IItem item) {
     241      Type itemType = item.GetType();
     242      Type hiveTaskType = GetHiveTaskType(itemType);
     243      return Activator.CreateInstance(hiveTaskType, new object[] { item }) as ItemTask;
     244    }
     245    #endregion
    213246  }
    214247}
Note: See TracChangeset for help on using the changeset viewer.