Free cookie consent management tool by TermsFeed Policy Generator

Changeset 14594


Ignore:
Timestamp:
01/20/17 11:16:42 (8 years ago)
Author:
jkarder
Message:

#2520: worked on persistence

Location:
branches/PersistenceOverhaul
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj

    r12616 r14594  
    239239      <Private>False</Private>
    240240    </ProjectReference>
     241    <ProjectReference Include="..\..\HeuristicLab.Persistence\4.0\HeuristicLab.Persistence-4.0.csproj">
     242      <Project>{B1663091-8156-4DEB-9E37-5B19810A4644}</Project>
     243      <Name>HeuristicLab.Persistence-4.0</Name>
     244    </ProjectReference>
    241245    <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj">
    242246      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
  • branches/PersistenceOverhaul/HeuristicLab.Core/3.3/PersistenceContentManager.cs

    r12012 r14594  
    2020#endregion
    2121
     22using System;
    2223using System.IO.Compression;
    2324using HeuristicLab.Common;
     25using HeuristicLab.Persistence;
    2426using HeuristicLab.Persistence.Default.Xml;
    2527
     
    2931
    3032    protected override IStorableContent LoadContent(string filename) {
    31       return XmlParser.Deserialize<IStorableContent>(filename);
     33      try {
     34        return XmlParser.Deserialize<IStorableContent>(filename);
     35      } catch (Exception) {
     36        var serializer = new ProtoBufSerializer();
     37        return (IStorableContent)serializer.Deserialize(filename);
     38      }
    3239    }
    3340
    3441    protected override void SaveContent(IStorableContent content, string filename, bool compressed) {
    35       XmlGenerator.Serialize(content, filename, compressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression);
     42      //XmlGenerator.Serialize(content, filename, compressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression);
     43      new ProtoBufSerializer().Serialize(content, filename);
    3644    }
    3745  }
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Mapper.cs

    r14549 r14594  
    139139      bundle.RootBoxId = mapper.GetBoxId(o);
    140140      bundle.AddRangeTransformerGuids(mapper.transformers.GetValues().Select(x => x.Guid).Select(x => ByteString.CopyFrom(x.ToByteArray())));
    141       bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => StaticCache.GetGuid(x)).Select(x => ByteString.CopyFromUtf8(x)));
     141      bundle.AddRangeTypeGuids(mapper.types.GetValues().Select(x => x.AssemblyQualifiedName ?? x.Name).Select(x => ByteString.CopyFromUtf8(x)));
    142142      bundle.AddRangeStrings(mapper.strings.GetValues());
    143143      bundle.AddRangeBoxes(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value));
     
    146146    public static object ToObject(Bundle bundle) {
    147147      var mapper = new Mapper();
    148       mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => x.ToStringUtf8()).Select(x => StaticCache.GetType(x)));
     148      mapper.types = new Index<Type>(bundle.TypeGuidsList.Select(x => x.ToStringUtf8()).Select(x => Type.GetType(x)));
    149149      mapper.strings = new Index<string>(bundle.StringsList);
    150150      mapper.boxId2Box = bundle.BoxesList.Select((b, i) => new { Box = b, Index = i }).ToDictionary(k => (uint)k.Index + 1, v => v.Box);
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs

    r14549 r14594  
    3535    private Dictionary<Guid, ITransformer> guid2Transformer;
    3636    private Dictionary<ITransformer, Guid> transformer2Guid;
    37     private Dictionary<string, Type> guid2Type;
    38     private Dictionary<Type, string> type2Guid;
     37    //private Dictionary<string, Type> guid2Type;
     38    //private Dictionary<Type, string> type2Guid;
    3939    private Dictionary<Type, TypeInfo> typeInfos;
    4040    private ExtensionRegistry extensionRegistry;
     
    4343      guid2Transformer = new Dictionary<Guid, ITransformer>();
    4444      transformer2Guid = new Dictionary<ITransformer, Guid>();
    45       guid2Type = new Dictionary<string, Type>();
    46       type2Guid = new Dictionary<Type, string>();
     45      //guid2Type = new Dictionary<string, Type>();
     46      //type2Guid = new Dictionary<Type, string>();
    4747      typeInfos = new Dictionary<Type, TypeInfo>();
    4848      extensionRegistry = ExtensionRegistry.CreateInstance();
     
    104104      //       RegisterType(new Guid("E92C35AD-32B1-4F37-B8D2-BE2F5FEB465B"), typeof(Dictionary<,>));
    105105
    106       RegisterTypeAndFullName(typeof(object));
    107       RegisterTypeAndFullName(typeof(bool));
    108       RegisterTypeAndFullName(typeof(byte));
    109       RegisterTypeAndFullName(typeof(sbyte));
    110       RegisterTypeAndFullName(typeof(short));
    111       RegisterTypeAndFullName(typeof(ushort));
    112       RegisterTypeAndFullName(typeof(char));
    113       RegisterTypeAndFullName(typeof(int));
    114       RegisterTypeAndFullName(typeof(uint));
    115       RegisterTypeAndFullName(typeof(long));
    116       RegisterTypeAndFullName(typeof(ulong));
    117       RegisterTypeAndFullName(typeof(float));
    118       RegisterTypeAndFullName(typeof(double));
    119       RegisterTypeAndFullName(typeof(decimal));
    120       RegisterTypeAndFullName(typeof(DateTime));
    121       RegisterTypeAndFullName(typeof(TimeSpan));
    122       RegisterTypeAndFullName(typeof(Font));
    123       RegisterTypeAndFullName(typeof(Color));
    124       RegisterTypeAndFullName(typeof(Bitmap));
    125       RegisterTypeAndFullName(typeof(Point));
    126       RegisterTypeAndFullName(typeof(KeyValuePair<,>));
    127       RegisterTypeAndFullName(typeof(Tuple<>));
    128       RegisterTypeAndFullName(typeof(Tuple<,>));
    129       RegisterTypeAndFullName(typeof(Tuple<,,>));
    130       RegisterTypeAndFullName(typeof(Tuple<,,,>));
    131       RegisterTypeAndFullName(typeof(Tuple<,,,,>));
    132       RegisterTypeAndFullName(typeof(Tuple<,,,,,>));
    133       RegisterTypeAndFullName(typeof(Tuple<,,,,,,>));
    134       RegisterTypeAndFullName(typeof(Tuple<,,,,,,,>));
    135       RegisterTypeAndFullName(typeof(Nullable<>));
    136       RegisterTypeAndFullName(typeof(string));
    137 
    138       RegisterTypeAndFullName(typeof(Array));
    139       RegisterTypeAndFullName(typeof(bool[]));
    140       RegisterTypeAndFullName(typeof(byte[]));
    141       RegisterTypeAndFullName(typeof(sbyte[]));
    142       RegisterTypeAndFullName(typeof(short[]));
    143       RegisterTypeAndFullName(typeof(ushort[]));
    144       RegisterTypeAndFullName(typeof(char[]));
    145       RegisterTypeAndFullName(typeof(int[]));
    146       RegisterTypeAndFullName(typeof(uint[]));
    147       RegisterTypeAndFullName(typeof(long[]));
    148       RegisterTypeAndFullName(typeof(ulong[]));
    149       RegisterTypeAndFullName(typeof(float[]));
    150       RegisterTypeAndFullName(typeof(double[]));
    151       RegisterTypeAndFullName(typeof(string[]));
    152       RegisterTypeAndFullName(typeof(List<>));
    153       RegisterTypeAndFullName(typeof(ArrayList));
    154       RegisterTypeAndFullName(typeof(HashSet<>));
    155       RegisterTypeAndFullName(typeof(Stack<>));
    156       RegisterTypeAndFullName(typeof(Stack));
    157       RegisterTypeAndFullName(typeof(Queue<>));
    158       RegisterTypeAndFullName(typeof(Queue));
    159       RegisterTypeAndFullName(typeof(Dictionary<,>));
    160 
    161       foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
    162         foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x) || x.IsValueType && !x.IsPrimitive && !x.IsEnum && x.IsSealed))
    163           RegisterTypeAndFullName(t);
    164       }
    165 
    166       var registeredTypes = type2Guid.Keys.ToArray();
    167       foreach (var t in registeredTypes) {
    168         AddBaseTypesRec(t);
    169       }
     106      //RegisterTypeAndFullName(typeof(object));
     107      //RegisterTypeAndFullName(typeof(bool));
     108      //RegisterTypeAndFullName(typeof(byte));
     109      //RegisterTypeAndFullName(typeof(sbyte));
     110      //RegisterTypeAndFullName(typeof(short));
     111      //RegisterTypeAndFullName(typeof(ushort));
     112      //RegisterTypeAndFullName(typeof(char));
     113      //RegisterTypeAndFullName(typeof(int));
     114      //RegisterTypeAndFullName(typeof(uint));
     115      //RegisterTypeAndFullName(typeof(long));
     116      //RegisterTypeAndFullName(typeof(ulong));
     117      //RegisterTypeAndFullName(typeof(float));
     118      //RegisterTypeAndFullName(typeof(double));
     119      //RegisterTypeAndFullName(typeof(decimal));
     120      //RegisterTypeAndFullName(typeof(DateTime));
     121      //RegisterTypeAndFullName(typeof(TimeSpan));
     122      //RegisterTypeAndFullName(typeof(Font));
     123      //RegisterTypeAndFullName(typeof(Color));
     124      //RegisterTypeAndFullName(typeof(Bitmap));
     125      //RegisterTypeAndFullName(typeof(Point));
     126      //RegisterTypeAndFullName(typeof(KeyValuePair<,>));
     127      //RegisterTypeAndFullName(typeof(Tuple<>));
     128      //RegisterTypeAndFullName(typeof(Tuple<,>));
     129      //RegisterTypeAndFullName(typeof(Tuple<,,>));
     130      //RegisterTypeAndFullName(typeof(Tuple<,,,>));
     131      //RegisterTypeAndFullName(typeof(Tuple<,,,,>));
     132      //RegisterTypeAndFullName(typeof(Tuple<,,,,,>));
     133      //RegisterTypeAndFullName(typeof(Tuple<,,,,,,>));
     134      //RegisterTypeAndFullName(typeof(Tuple<,,,,,,,>));
     135      //RegisterTypeAndFullName(typeof(Nullable<>));
     136      //RegisterTypeAndFullName(typeof(string));
     137
     138      //RegisterTypeAndFullName(typeof(Array));
     139      //RegisterTypeAndFullName(typeof(bool[]));
     140      //RegisterTypeAndFullName(typeof(byte[]));
     141      //RegisterTypeAndFullName(typeof(sbyte[]));
     142      //RegisterTypeAndFullName(typeof(short[]));
     143      //RegisterTypeAndFullName(typeof(ushort[]));
     144      //RegisterTypeAndFullName(typeof(char[]));
     145      //RegisterTypeAndFullName(typeof(int[]));
     146      //RegisterTypeAndFullName(typeof(uint[]));
     147      //RegisterTypeAndFullName(typeof(long[]));
     148      //RegisterTypeAndFullName(typeof(ulong[]));
     149      //RegisterTypeAndFullName(typeof(float[]));
     150      //RegisterTypeAndFullName(typeof(double[]));
     151      //RegisterTypeAndFullName(typeof(string[]));
     152      //RegisterTypeAndFullName(typeof(List<>));
     153      //RegisterTypeAndFullName(typeof(ArrayList));
     154      //RegisterTypeAndFullName(typeof(HashSet<>));
     155      //RegisterTypeAndFullName(typeof(Stack<>));
     156      //RegisterTypeAndFullName(typeof(Stack));
     157      //RegisterTypeAndFullName(typeof(Queue<>));
     158      //RegisterTypeAndFullName(typeof(Queue));
     159      //RegisterTypeAndFullName(typeof(Dictionary<,>));
     160
     161      //foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) {
     162      //  foreach (var t in asm.GetTypes().Where(x => StorableClassAttribute.IsStorableClass(x) // storable classes
     163      //                                           || x.IsValueType && !x.IsPrimitive && !x.IsEnum && x.IsSealed // structs
     164      //                                           || typeof(IEqualityComparer).IsAssignableFrom(x) // equality comparers
     165      //                                           || x.IsGenericType && typeof(IEqualityComparer<>).IsAssignableFrom(x.GetGenericTypeDefinition()))) // generic equality comparers
     166      //    RegisterTypeAndFullName(t);
     167      //}
     168
     169      //var registeredTypes = type2Guid.Keys.ToArray();
     170      //foreach (var t in registeredTypes) {
     171      //  AddBaseTypesRec(t);
     172      //}
    170173
    171174      RegisterExtension(BoolBox.Bool);
     
    195198    }
    196199
    197     private void AddBaseTypesRec(Type t) {
    198       foreach (var interfaceType in t.GetInterfaces()) {
    199         RegisterTypeAndFullName(interfaceType);
    200         AddBaseTypesRec(interfaceType);
    201       }
    202       if (t.BaseType != null) {
    203         RegisterTypeAndFullName(t.BaseType);
    204         AddBaseTypesRec(t.BaseType);
    205       }
    206     }
     200    //private void AddBaseTypesRec(Type t) {
     201    //  foreach (var interfaceType in t.GetInterfaces()) {
     202    //    RegisterTypeAndFullName(interfaceType);
     203    //    AddBaseTypesRec(interfaceType);
     204    //  }
     205    //  if (t.BaseType != null) {
     206    //    RegisterTypeAndFullName(t.BaseType);
     207    //    AddBaseTypesRec(t.BaseType);
     208    //  }
     209    //}
    207210
    208211    public void RegisterTransformer(ITransformer transformer) {
     
    218221    //      }
    219222    //    }
    220     public void RegisterTypeAndFullName(Type type) {
    221       lock (locker) {
    222         var key = type.AssemblyQualifiedName ?? type.Name;
    223         if (!guid2Type.ContainsKey(key)) {
    224           guid2Type.Add(key, type);
    225           type2Guid.Add(type, key);
    226         }
    227       }
    228     }
     223    //public void RegisterTypeAndFullName(Type type) {
     224    //  lock (locker) {
     225    //    var key = type.AssemblyQualifiedName ?? type.Name;
     226    //    if (!guid2Type.ContainsKey(key)) {
     227    //      guid2Type.Add(key, type);
     228    //      type2Guid.Add(type, key);
     229    //    }
     230    //  }
     231    //}
    229232    public void RegisterExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) {
    230233      lock (locker) {
     
    239242      return transformer2Guid[transformer];
    240243    }
    241     public Type GetType(string guid) {
    242       return guid2Type[guid];
    243     }
    244     public string GetGuid(Type type) {
    245       return type2Guid[type];
    246     }
     244    //public Type GetType(string guid) {
     245    //  return guid2Type[guid];
     246    //}
     247    //public string GetGuid(Type type) {
     248    //  return type2Guid[type];
     249    //}
    247250    public TypeInfo GetTypeInfo(Type type) {
    248251      lock (locker) {
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/StorableClassTransformer.cs

    r14549 r14594  
    3030  internal sealed class StorableClassBoxTransformer : BoxTransformer<object> {
    3131    public override bool CanTransformType(Type type) {
    32       return StorableClassAttribute.IsStorableClass(type);
     32      return StorableClassAttribute.IsStorableClass(type) || type.BaseType != null && CanTransformType(type.BaseType);
    3333    }
    3434
     
    7979        uint componentId;
    8080        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
    81         field.SetValue(obj, found ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue);
     81        if (found)
     82          field.SetValue(obj, mapper.GetObject(componentId));
     83        else if (componentInfo.StorableAttribute.DefaultValue != null)
     84          field.SetValue(obj, componentInfo.StorableAttribute.DefaultValue);
    8285      }
    8386
     
    8689        uint componentId;
    8790        bool found = components.TryGetValue(mapper.GetStringId(componentInfo.Name), out componentId);
    88         property.SetValue(obj, found ? mapper.GetObject(componentId) : componentInfo.StorableAttribute.DefaultValue, null);
     91        if (found)
     92          property.SetValue(obj, mapper.GetObject(componentId), null);
     93        else if (componentInfo.StorableAttribute.DefaultValue != null)
     94          property.SetValue(obj, componentInfo.StorableAttribute.DefaultValue, null);
    8995      }
    9096
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs

    r14549 r14594  
    807807    }
    808808    public override Box ToBox(object o, Mapper mapper) {
    809       Array arr = (Array)o;
    810       if (arr.GetLowerBound(0) != 0 || arr.Rank > 1) throw new NotSupportedException();
    811 
    812809      var box = Box.CreateBuilder();
    813810      box.TransformerId = mapper.GetTransformerId(this);
     
    816813    }
    817814    private void Populate(Box.Builder box, object value, Mapper mapper) {
     815      var type = value.GetType();
    818816      var array = (Array)value;
    819       var elementType = value.GetType().GetElementType();
     817      var rank = array.Rank;
    820818
    821819      var uIntArrayBox = UnsignedIntArrayBox.CreateBuilder();
    822       foreach (var item in (IEnumerable)array)
    823         uIntArrayBox.AddValues(mapper.GetBoxId(item));
    824 
    825       box.TypeId = mapper.GetBoxId(elementType);
     820      uIntArrayBox.AddValues(mapper.GetBoxId(rank));
     821
     822      int[] lengths = new int[rank];
     823      int[] lowerBounds = new int[rank];
     824      for (int i = 0; i < rank; i++) {
     825        lengths[i] = array.GetLength(i);
     826        lowerBounds[i] = array.GetLowerBound(i);
     827      }
     828
     829      uIntArrayBox.AddRangeValues(lengths.Select(x => mapper.GetBoxId(x)));
     830      uIntArrayBox.AddRangeValues(lowerBounds.Select(x => mapper.GetBoxId(x)));
     831
     832      int[] positions = (int[])lowerBounds.Clone();
     833      while (positions[rank - 1] < lengths[rank - 1] + lowerBounds[rank - 1]) {
     834        uIntArrayBox.AddValues(mapper.GetBoxId(array.GetValue(positions)));
     835        positions[0] += 1;
     836        for (int i = 0; i < rank - 1; i++) {
     837          if (positions[i] >= lowerBounds[i] + lengths[i]) {
     838            positions[i] = lowerBounds[i];
     839            positions[i + 1] += 1;
     840          } else {
     841            break;
     842          }
     843        }
     844      }
     845
     846      box.TypeId = mapper.GetBoxId(type);
    826847      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, uIntArrayBox.Build());
    827848    }
    828849    public override object ToObject(Box box, Mapper mapper) {
    829850      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
     851      var rank = (int)mapper.GetObject(uIntArrayBox.GetValues(0));
     852
     853      int[] lengths = new int[rank], lowerBounds = new int[rank];
     854      for (int i = 0; i < rank; i++)
     855        lengths[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1));
     856      for (int i = 0; i < rank; i++)
     857        lowerBounds[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1 + rank));
     858
    830859      var type = (Type)mapper.GetObject(box.TypeId);
    831       return Array.CreateInstance(type, uIntArrayBox.ValuesCount);
     860      return Array.CreateInstance(type.GetElementType(), lengths, lowerBounds);
    832861    }
    833862    public override void FillFromBox(object obj, Box box, Mapper mapper) {
     863      var array = (Array)obj;
    834864      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
    835       Array.Copy(uIntArrayBox.ValuesList.Select(x => mapper.GetObject(x)).ToArray(), (Array)obj, uIntArrayBox.ValuesCount);
     865      var rank = (int)mapper.GetObject(uIntArrayBox.GetValues(0));
     866
     867      int[] lengths = new int[rank], lowerBounds = new int[rank];
     868      for (int i = 0; i < rank; i++)
     869        lengths[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1));
     870      for (int i = 0; i < rank; i++)
     871        lowerBounds[i] = (int)mapper.GetObject(uIntArrayBox.GetValues(i + 1 + rank));
     872
     873      int[] positions = (int[])lowerBounds.Clone();
     874      var e = uIntArrayBox.ValuesList.Skip(1 + 2 * rank).GetEnumerator();
     875      while (e.MoveNext()) {
     876        int[] currentPositions = positions;
     877        array.SetValue(mapper.GetObject(e.Current), currentPositions);
     878        positions[0] += 1;
     879        for (int i = 0; i < rank - 1; i++) {
     880          if (positions[i] >= lengths[i] + lowerBounds[i]) {
     881            positions[i] = lowerBounds[i];
     882            positions[i + 1] += 1;
     883          } else {
     884            break;
     885          }
     886        }
     887      }
    836888    }
    837889  }
     
    857909    private void Populate(Box.Builder box, object value, Mapper mapper) {
    858910      var uIntArrayBox = UnsignedIntArrayBox.CreateBuilder();
     911
     912      var type = value.GetType();
     913      var propertyInfo = type.GetProperty("Comparer");
     914      if (propertyInfo != null) {
     915        // TODO: where to store id for comparer box? (atm: first element in int array ...)
     916        var comparer = propertyInfo.GetValue(value);
     917        var comparerType = comparer.GetType();
     918        if (Default.CompositeSerializers.Storable.StorableClassAttribute.IsStorableClass(comparerType))
     919          uIntArrayBox.AddValues(mapper.GetBoxId(comparer));
     920        else if (comparerType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Any())
     921          throw new NotSupportedException("Cannot serialize non-storable equality comparers with fields");
     922        else
     923          uIntArrayBox.AddValues(mapper.GetBoxId(comparerType));
     924      }
     925
    859926      foreach (var item in (IEnumerable)value)
    860927        uIntArrayBox.AddValues(mapper.GetBoxId(item));
    861       box.TypeId = mapper.GetBoxId(value.GetType());
     928
     929      box.TypeId = mapper.GetBoxId(type);
    862930      box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, uIntArrayBox.Build());
    863931    }
     
    865933      var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray);
    866934      var type = (Type)mapper.GetObject(box.TypeId);
    867       return Activator.CreateInstance(type, uIntArrayBox.ValuesCount);
     935      return Activator.CreateInstance(type);
    868936    }
    869937    public override void FillFromBox(object obj, Box box, Mapper mapper) {
     
    871939      var elements = uIntArrayBox.ValuesList.Select(mapper.GetObject);
    872940      var type = obj.GetType();
     941
    873942      string methodName = string.Empty;
    874943      if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Stack<>) || type == typeof(Stack)) {
     
    879948      } else {
    880949        methodName = "Add";
     950        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet<>)) {
     951          var fieldInfo = type.GetField("m_comparer", BindingFlags.NonPublic | BindingFlags.Instance);
     952          var comparerObj = mapper.GetObject(uIntArrayBox.GetValues(0));
     953          var comparer = comparerObj is Type ? Activator.CreateInstance((Type)comparerObj) : comparerObj;
     954          fieldInfo.SetValue(obj, comparer);
     955          elements = elements.Skip(1);
     956        }
    881957      }
    882958
     
    909985      var comparer = propertyInfo.GetValue(value);
    910986
    911       dictionaryBox.SetComparerId(mapper.GetBoxId(comparer));
    912       box.TypeId = mapper.GetBoxId(value.GetType());
     987      var comparerType = comparer.GetType();
     988      if (Default.CompositeSerializers.Storable.StorableClassAttribute.IsStorableClass(comparerType))
     989        dictionaryBox.SetComparerId(mapper.GetBoxId(comparer));
     990      else if (comparerType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy).Any())
     991        throw new NotSupportedException("Cannot serialize non-storable equality comparers with fields");
     992      else
     993        dictionaryBox.SetComparerId(mapper.GetBoxId(comparerType));
     994
     995      box.TypeId = mapper.GetBoxId(type);
    913996      box.SetExtension(DictionaryBox.Dictionary, dictionaryBox.Build());
    914997    }
     
    9201003      var type = obj.GetType();
    9211004      var dictionaryBox = box.GetExtension(DictionaryBox.Dictionary);
    922       var comparer = mapper.GetObject(dictionaryBox.ComparerId);
     1005      var comparerObj = mapper.GetObject(dictionaryBox.ComparerId);
     1006      var comparer = comparerObj is Type ? Activator.CreateInstance((Type)comparerObj) : comparerObj;
     1007
     1008      var fieldInfo = type.GetField("comparer", BindingFlags.NonPublic | BindingFlags.Instance);
     1009      fieldInfo.SetValue(obj, comparer);
     1010
     1011
    9231012      var addMethod = type.GetMethod("Add");
    9241013      for (int i = 0; i < dictionaryBox.KeyIdsCount; i++) {
  • branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs

    r14549 r14594  
    3333using HeuristicLab.Algorithms.GeneticAlgorithm;
    3434using HeuristicLab.Data;
     35using HeuristicLab.Persistence;
    3536using HeuristicLab.Persistence.Auxiliary;
    3637using HeuristicLab.Persistence.Core;
     
    3940using HeuristicLab.Persistence.Default.Xml;
    4041using HeuristicLab.Persistence.Tests;
     42using HeuristicLab.Problems.TestFunctions;
    4143using HeuristicLab.Tests;
    4244using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    401403
    402404    public string Profile(Func<object> GenerateDataFunc) {
    403       int nrOfRepetitions = 100;
     405      int nrOfRepetitions = 30;
    404406      StringBuilder report = new StringBuilder();
    405407      List<PerformanceData> dataList = new List<PerformanceData>();
     
    411413      report.Append("Performance Report for " + GenerateDataFunc.Method.Name + ": " + Environment.NewLine);
    412414      report.Append(Environment.NewLine);
    413       report.AppendFormat("Avg. old vs. new time for serializing a file: {0} / {1}; Factor: {2}",
     415      report.AppendFormat("Avg. old vs. new time for serializing a file; {0};{1};{2}",
    414416              dataList.Select(x => x.OldSerializingTime).Average(),
    415417              dataList.Select(x => x.NewSerializingTime).Average(),
     
    417419              );
    418420      report.Append(Environment.NewLine);
    419       report.AppendFormat("Avg. old vs. new time for deserializing a file: {0} / {1}; Factor: {2}",
     421      report.AppendFormat("Avg. old vs. new time for deserializing a file; {0};{1};{2}",
    420422              dataList.Select(x => x.OldDeserializingTime).Average(),
    421423              dataList.Select(x => x.NewDeserializingTime).Average(),
     
    423425              );
    424426      report.Append(Environment.NewLine);
    425       report.AppendFormat("Avg. old vs. new file size (in bytes): {0} / {1}; Factor: {2}",
     427      report.AppendFormat("Avg. old vs. new file size (in bytes); {0};{1};{2}",
    426428              dataList.Select(x => x.OldFileSize).Average(),
    427429              dataList.Select(x => x.NewFileSize).Average(),
     
    429431              );
    430432      report.Append(Environment.NewLine);
    431       report.AppendFormat("Avg. old vs. new memory consumption for serializing a file (in bytes): {0} / {1}; Factor: {2}",
     433      report.AppendFormat("Avg. old vs. new memory consumption for serializing a file (in bytes); {0};{1};{2}",
    432434              dataList.Select(x => x.OldSerializingMemoryConsumed).Average(),
    433435              dataList.Select(x => x.NewSerializingMemoryConsumed).Average(),
     
    435437              );
    436438      report.Append(Environment.NewLine);
    437       report.AppendFormat("Avg. old vs. new memory consumption for deserializing a file (in bytes): {0} / {1}; Factor: {2}",
     439      report.AppendFormat("Avg. old vs. new memory consumption for deserializing a file (in bytes); {0};{1};{2}",
    438440              dataList.Select(x => x.OldDeserializingMemoryConsumed).Average(),
    439441              dataList.Select(x => x.NewDeserializingMemoryConsumed).Average(),
     
    944946    #region Old persistence test methods
    945947    [TestMethod]
    946     [TestCategory("Persistence4-EXEPTION")]
     948    [TestCategory("Persistence4")]
    947949    [TestProperty("Time", "short")]
    948950    public void ComplexStorable() {
     
    10341036
    10351037    [TestMethod]
    1036     [TestCategory("Persistence4-EXEPTION")]
     1038    [TestCategory("Persistence4")]
    10371039    [TestProperty("Time", "short")]
    10381040    public void SelfReferences() {
     
    10621064
    10631065    [TestMethod]
    1064     [TestCategory("Persistence4-EXEPTION")]
     1066    [TestCategory("Persistence4")]
    10651067    [TestProperty("Time", "short")]
    10661068    public void ArrayCreation() {
     
    13521354
    13531355    [TestMethod]
    1354     [TestCategory("Persistence4-EXEPTION")]
     1356    [TestCategory("Persistence4")]
    13551357    [TestProperty("Time", "short")]
    13561358    public void InstantiateParentChainReference() {
     
    15161518
    15171519    [TestMethod]
    1518     [TestCategory("Persistence4-EXEPTION")]
     1520    [TestCategory("Persistence4")]
    15191521    [TestProperty("Time", "short")]
    15201522    public void TestStreaming() {
     
    20602062    public class A {
    20612063      [Storable]
    2062       public Tuple<B> B { get; set; }
     2064      public B B { get; set; }
     2065
     2066      [Storable] public int i;
    20632067    }
    20642068    [StorableClass("6075F1E8-948A-4AD8-8F5A-942B777852EC")]
     
    20662070      [Storable]
    20672071      public A A { get; set; }
     2072
     2073      [StorableHook(HookType.AfterDeserialization)]
     2074      void PostDeserializationHook() {
     2075        //Assert.AreEqual(3, A.i);
     2076      }
    20682077    }
    20692078
     
    20732082    public void TestCyclicReferencesWithTuple() {
    20742083      var test = new Func<A>(() => {
    2075         var a = new A { };
     2084        var a = new A { i = 4 };
    20762085        var b = new B { A = a };
    2077         a.B = Tuple.Create(b);
     2086        a.B = b;
    20782087        return a;
    20792088      });
    2080       ProtoBufSerializer serializer = new ProtoBufSerializer();
    2081       serializer.Serialize(test(), tempFile);
    2082       object o = serializer.Deserialize(tempFile);
    2083       A result = (A)o;
    2084 
    2085       Assert.AreEqual(result, result.B.Item1.A);
    2086       Assert.AreSame(result, result.B.Item1.A);
    2087 
    2088       string msg = Profile(test);
    2089       Console.WriteLine(msg);
     2089
     2090      //ProtoBufSerializer serializer = new ProtoBufSerializer();
     2091      //serializer.Serialize(test(), tempFile);
     2092      //object o = serializer.Deserialize(tempFile);
     2093      //A result = (A)o;
     2094
     2095      XmlGenerator.Serialize(test(), tempFile);
     2096      object o = XmlParser.Deserialize(tempFile);
     2097
     2098      string msg = Profile(test);
     2099      Console.WriteLine(msg);
     2100    }
     2101
     2102    [TestMethod]
     2103    [TestCategory("Persistence4")]
     2104    [TestProperty("Time", "short")]
     2105    public void TestGASerializeDeserializeExecute() {
     2106      var test = new Func<GeneticAlgorithm>(() => {
     2107        var ga = new GeneticAlgorithm();
     2108        ga.Problem = new SingleObjectiveTestFunctionProblem();
     2109        ga.MaximumGenerations.Value = 100;
     2110        ga.SetSeedRandomly.Value = false;
     2111        return ga;
     2112      });
     2113      ProtoBufSerializer serializer = new ProtoBufSerializer();
     2114      serializer.Serialize(test(), tempFile);
     2115      object o = serializer.Deserialize(tempFile);
     2116      GeneticAlgorithm result = (GeneticAlgorithm)o;
     2117      SamplesUtils.RunAlgorithm(result);
     2118      GeneticAlgorithm original = test();
     2119      SamplesUtils.RunAlgorithm(original);
     2120      //Assert.AreEqual(original.Results[""], result);
     2121
     2122      //string msg = Profile(test);
     2123      //Console.WriteLine(msg);
     2124    }
     2125    [TestMethod]
     2126    [TestCategory("Persistence4")]
     2127    [TestProperty("Time", "short")]
     2128    public void TestLoadingSamples() {
     2129      var path = @"D:\Dev\HL\branches\PersistenceOverhaul\HeuristicLab.Optimizer\3.3\Documents";
     2130      var serializer = new ProtoBufSerializer();
     2131      foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {
     2132        var original = XmlParser.Deserialize(fileName);
     2133        serializer.Serialize(original, fileName + ".proto");
     2134        // var newVersion = serializer.Deserialize(fileName + ".proto");
     2135        var p = Profile(() => original);
     2136        Console.WriteLine(p);
     2137      }
    20902138    }
    20912139  }
Note: See TracChangeset for help on using the changeset viewer.