Changeset 14594
- Timestamp:
- 01/20/17 11:16:42 (8 years ago)
- Location:
- branches/PersistenceOverhaul
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceOverhaul/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj
r12616 r14594 239 239 <Private>False</Private> 240 240 </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> 241 245 <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\3.3\HeuristicLab.PluginInfrastructure-3.3.csproj"> 242 246 <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project> -
branches/PersistenceOverhaul/HeuristicLab.Core/3.3/PersistenceContentManager.cs
r12012 r14594 20 20 #endregion 21 21 22 using System; 22 23 using System.IO.Compression; 23 24 using HeuristicLab.Common; 25 using HeuristicLab.Persistence; 24 26 using HeuristicLab.Persistence.Default.Xml; 25 27 … … 29 31 30 32 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 } 32 39 } 33 40 34 41 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); 36 44 } 37 45 } -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r14549 r14594 139 139 bundle.RootBoxId = mapper.GetBoxId(o); 140 140 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))); 142 142 bundle.AddRangeStrings(mapper.strings.GetValues()); 143 143 bundle.AddRangeBoxes(mapper.boxId2Box.OrderBy(x => x.Key).Select(x => x.Value)); … … 146 146 public static object ToObject(Bundle bundle) { 147 147 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))); 149 149 mapper.strings = new Index<string>(bundle.StringsList); 150 150 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 35 35 private Dictionary<Guid, ITransformer> guid2Transformer; 36 36 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; 39 39 private Dictionary<Type, TypeInfo> typeInfos; 40 40 private ExtensionRegistry extensionRegistry; … … 43 43 guid2Transformer = new Dictionary<Guid, ITransformer>(); 44 44 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>(); 47 47 typeInfos = new Dictionary<Type, TypeInfo>(); 48 48 extensionRegistry = ExtensionRegistry.CreateInstance(); … … 104 104 // RegisterType(new Guid("E92C35AD-32B1-4F37-B8D2-BE2F5FEB465B"), typeof(Dictionary<,>)); 105 105 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 //} 170 173 171 174 RegisterExtension(BoolBox.Bool); … … 195 198 } 196 199 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 //} 207 210 208 211 public void RegisterTransformer(ITransformer transformer) { … … 218 221 // } 219 222 // } 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 //} 229 232 public void RegisterExtension<TExtension>(GeneratedExtensionBase<TExtension> extension) { 230 233 lock (locker) { … … 239 242 return transformer2Guid[transformer]; 240 243 } 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 //} 247 250 public TypeInfo GetTypeInfo(Type type) { 248 251 lock (locker) { -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/StorableClassTransformer.cs
r14549 r14594 30 30 internal sealed class StorableClassBoxTransformer : BoxTransformer<object> { 31 31 public override bool CanTransformType(Type type) { 32 return StorableClassAttribute.IsStorableClass(type) ;32 return StorableClassAttribute.IsStorableClass(type) || type.BaseType != null && CanTransformType(type.BaseType); 33 33 } 34 34 … … 79 79 uint componentId; 80 80 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); 82 85 } 83 86 … … 86 89 uint componentId; 87 90 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); 89 95 } 90 96 -
branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
r14549 r14594 807 807 } 808 808 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 812 809 var box = Box.CreateBuilder(); 813 810 box.TransformerId = mapper.GetTransformerId(this); … … 816 813 } 817 814 private void Populate(Box.Builder box, object value, Mapper mapper) { 815 var type = value.GetType(); 818 816 var array = (Array)value; 819 var elementType = value.GetType().GetElementType();817 var rank = array.Rank; 820 818 821 819 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); 826 847 box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, uIntArrayBox.Build()); 827 848 } 828 849 public override object ToObject(Box box, Mapper mapper) { 829 850 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 830 859 var type = (Type)mapper.GetObject(box.TypeId); 831 return Array.CreateInstance(type , uIntArrayBox.ValuesCount);860 return Array.CreateInstance(type.GetElementType(), lengths, lowerBounds); 832 861 } 833 862 public override void FillFromBox(object obj, Box box, Mapper mapper) { 863 var array = (Array)obj; 834 864 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 } 836 888 } 837 889 } … … 857 909 private void Populate(Box.Builder box, object value, Mapper mapper) { 858 910 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 859 926 foreach (var item in (IEnumerable)value) 860 927 uIntArrayBox.AddValues(mapper.GetBoxId(item)); 861 box.TypeId = mapper.GetBoxId(value.GetType()); 928 929 box.TypeId = mapper.GetBoxId(type); 862 930 box.SetExtension(UnsignedIntArrayBox.UnsignedIntArray, uIntArrayBox.Build()); 863 931 } … … 865 933 var uIntArrayBox = box.GetExtension(UnsignedIntArrayBox.UnsignedIntArray); 866 934 var type = (Type)mapper.GetObject(box.TypeId); 867 return Activator.CreateInstance(type , uIntArrayBox.ValuesCount);935 return Activator.CreateInstance(type); 868 936 } 869 937 public override void FillFromBox(object obj, Box box, Mapper mapper) { … … 871 939 var elements = uIntArrayBox.ValuesList.Select(mapper.GetObject); 872 940 var type = obj.GetType(); 941 873 942 string methodName = string.Empty; 874 943 if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Stack<>) || type == typeof(Stack)) { … … 879 948 } else { 880 949 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 } 881 957 } 882 958 … … 909 985 var comparer = propertyInfo.GetValue(value); 910 986 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); 913 996 box.SetExtension(DictionaryBox.Dictionary, dictionaryBox.Build()); 914 997 } … … 920 1003 var type = obj.GetType(); 921 1004 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 923 1012 var addMethod = type.GetMethod("Add"); 924 1013 for (int i = 0; i < dictionaryBox.KeyIdsCount; i++) { -
branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs
r14549 r14594 33 33 using HeuristicLab.Algorithms.GeneticAlgorithm; 34 34 using HeuristicLab.Data; 35 using HeuristicLab.Persistence; 35 36 using HeuristicLab.Persistence.Auxiliary; 36 37 using HeuristicLab.Persistence.Core; … … 39 40 using HeuristicLab.Persistence.Default.Xml; 40 41 using HeuristicLab.Persistence.Tests; 42 using HeuristicLab.Problems.TestFunctions; 41 43 using HeuristicLab.Tests; 42 44 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 401 403 402 404 public string Profile(Func<object> GenerateDataFunc) { 403 int nrOfRepetitions = 100;405 int nrOfRepetitions = 30; 404 406 StringBuilder report = new StringBuilder(); 405 407 List<PerformanceData> dataList = new List<PerformanceData>(); … … 411 413 report.Append("Performance Report for " + GenerateDataFunc.Method.Name + ": " + Environment.NewLine); 412 414 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}", 414 416 dataList.Select(x => x.OldSerializingTime).Average(), 415 417 dataList.Select(x => x.NewSerializingTime).Average(), … … 417 419 ); 418 420 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}", 420 422 dataList.Select(x => x.OldDeserializingTime).Average(), 421 423 dataList.Select(x => x.NewDeserializingTime).Average(), … … 423 425 ); 424 426 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}", 426 428 dataList.Select(x => x.OldFileSize).Average(), 427 429 dataList.Select(x => x.NewFileSize).Average(), … … 429 431 ); 430 432 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}", 432 434 dataList.Select(x => x.OldSerializingMemoryConsumed).Average(), 433 435 dataList.Select(x => x.NewSerializingMemoryConsumed).Average(), … … 435 437 ); 436 438 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}", 438 440 dataList.Select(x => x.OldDeserializingMemoryConsumed).Average(), 439 441 dataList.Select(x => x.NewDeserializingMemoryConsumed).Average(), … … 944 946 #region Old persistence test methods 945 947 [TestMethod] 946 [TestCategory("Persistence4 -EXEPTION")]948 [TestCategory("Persistence4")] 947 949 [TestProperty("Time", "short")] 948 950 public void ComplexStorable() { … … 1034 1036 1035 1037 [TestMethod] 1036 [TestCategory("Persistence4 -EXEPTION")]1038 [TestCategory("Persistence4")] 1037 1039 [TestProperty("Time", "short")] 1038 1040 public void SelfReferences() { … … 1062 1064 1063 1065 [TestMethod] 1064 [TestCategory("Persistence4 -EXEPTION")]1066 [TestCategory("Persistence4")] 1065 1067 [TestProperty("Time", "short")] 1066 1068 public void ArrayCreation() { … … 1352 1354 1353 1355 [TestMethod] 1354 [TestCategory("Persistence4 -EXEPTION")]1356 [TestCategory("Persistence4")] 1355 1357 [TestProperty("Time", "short")] 1356 1358 public void InstantiateParentChainReference() { … … 1516 1518 1517 1519 [TestMethod] 1518 [TestCategory("Persistence4 -EXEPTION")]1520 [TestCategory("Persistence4")] 1519 1521 [TestProperty("Time", "short")] 1520 1522 public void TestStreaming() { … … 2060 2062 public class A { 2061 2063 [Storable] 2062 public Tuple<B> B { get; set; } 2064 public B B { get; set; } 2065 2066 [Storable] public int i; 2063 2067 } 2064 2068 [StorableClass("6075F1E8-948A-4AD8-8F5A-942B777852EC")] … … 2066 2070 [Storable] 2067 2071 public A A { get; set; } 2072 2073 [StorableHook(HookType.AfterDeserialization)] 2074 void PostDeserializationHook() { 2075 //Assert.AreEqual(3, A.i); 2076 } 2068 2077 } 2069 2078 … … 2073 2082 public void TestCyclicReferencesWithTuple() { 2074 2083 var test = new Func<A>(() => { 2075 var a = new A { };2084 var a = new A { i = 4 }; 2076 2085 var b = new B { A = a }; 2077 a.B = Tuple.Create(b);2086 a.B = b; 2078 2087 return a; 2079 2088 }); 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 } 2090 2138 } 2091 2139 }
Note: See TracChangeset
for help on using the changeset viewer.