Changeset 15986 for branches/PersistenceReintegration
- Timestamp:
- 07/05/18 17:22:34 (6 years ago)
- Location:
- branches/PersistenceReintegration
- Files:
-
- 1 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Index.cs
r15509 r15986 59 59 } 60 60 61 public bool TryGetValue(uint index, out T value) { 62 return values.TryGetValue(index, out value); 63 } 64 61 65 public IEnumerable<T> GetValues() { 62 66 return values.Values; -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/Mapper.cs
r15529 r15986 93 93 public Type GetType(uint typeId) { 94 94 return types.GetValue(typeId); 95 } 96 97 public bool TryGetType(uint typeId, out Type type) { 98 return types.TryGetValue(typeId, out type); 95 99 } 96 100 #endregion … … 132 136 o = transformer.ToObject(box, this); 133 137 boxId2Object.Add(boxId, o); 134 transformer.FillFromBox(o, box, this);138 if (o != null) transformer.FillFromBox(o, box, this); 135 139 } 136 140 … … 176 180 mapper.strings = new Index<string>(bundle.Strings); 177 181 178 return mapper.GetObject(bundle.RootBoxId); 182 var root = mapper.GetObject(bundle.RootBoxId); 183 184 ExecuteAfterDeserializationHooks(mapper.boxId2Object.Values.ToArray()); 185 186 return root; 187 } 188 189 private static void ExecuteAfterDeserializationHooks(object[] objects) { 190 var emptyArgs = new object[0]; 191 192 foreach (var obj in objects) { 193 if (!StorableTypeAttribute.IsStorableType(obj.GetType())) continue; // TODO: performance 194 195 var typeStack = new Stack<Type>(); 196 for (var type = obj.GetType(); type != null; type = type.BaseType) { 197 typeStack.Push(type); 198 } 199 200 while (typeStack.Any()) { 201 var type = typeStack.Pop(); 202 if (!StorableTypeAttribute.IsStorableType(type)) continue; // object <- mytype (has hooks) or object <- valuetype <- mystruct 203 204 var typeInfo = staticCache.GetTypeInfo(type); 205 foreach (var hook in typeInfo.AfterDeserializationHooks) { 206 hook.Invoke(obj, emptyArgs); 207 } 208 } 209 } 179 210 } 180 211 } -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StaticCache.cs
r15857 r15986 68 68 RegisterType(new Guid("724A2D49-7E7B-455B-BBA9-4214C64E8A21"), typeof(TimeSpan)); 69 69 RegisterType(new Guid("ECC12A57-DA8D-43D9-9EC7-FCAC878A4D69"), typeof(Font)); 70 RegisterType(new Guid("494C1352-A1C3-47A3-8754-A0B19B8F1567"), typeof(FontStyle)); 71 RegisterType(new Guid("41147B67-4C7A-4907-9F6C-509568395EDB"), typeof(GraphicsUnit)); 70 72 RegisterType(new Guid("E8348C94-9817-4164-9C98-377689F83F30"), typeof(Bitmap)); 71 73 RegisterType(new Guid("05551382-E894-4218-B860-FEE1D92CA07D"), typeof(Nullable<>)); … … 111 113 if (StorableTypeAttribute.IsStorableType(t)) { 112 114 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(t).Guid, t); 113 }114 foreach (var mi in t.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)) {115 if (StorableConversionAttribute.IsStorableConversionMethod(mi)) {116 RegisterStorableConversion(StorableConversionAttribute.GetStorableConversionAttribute(mi).Guid, mi);117 }118 115 } 119 116 } … … 194 191 } 195 192 } 196 197 public IEnumerable<MethodInfo> GetStorableConversions(Guid guid) {198 List<MethodInfo> conversionMethods;199 if (guid2ConversionMethods.TryGetValue(guid, out conversionMethods)) return conversionMethods;200 return Enumerable.Empty<MethodInfo>();201 }202 203 public IEnumerable<MethodInfo> GetStorableConversions(IList<Tuple<Guid, uint>> typeChain) {204 foreach (var type in typeChain) {205 var conversionMethod = (from methodInfo in GetStorableConversions(type.Item1)206 let attrib = StorableConversionAttribute.GetStorableConversionAttribute(methodInfo)207 where attrib.Version == type.Item2 && (attrib.Dependencies == null ||208 attrib.Dependencies.All(x => typeChain.Any(y => y.Item1 == Guid.Parse(x.Key) && y.Item2 == x.Value)))209 select methodInfo).SingleOrDefault();210 if (conversionMethod != null)211 yield return conversionMethod;212 }213 }214 193 } 215 194 } -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/StorableTypeAttribute.cs
r15034 r15986 37 37 38 38 public Guid Guid { get; private set; } 39 public uint Version { get; private set; }40 39 41 40 /// <summary> … … 43 42 /// </summary> 44 43 /// <param name="memberSelection">The storable class memberSelection.</param> 45 public StorableTypeAttribute(StorableMemberSelection memberSelection, string guid , uint version = 1) {44 public StorableTypeAttribute(StorableMemberSelection memberSelection, string guid) { 46 45 MemberSelection = memberSelection; 47 46 Guid = new Guid(guid); 48 Version = version;49 47 } 50 48 51 public StorableTypeAttribute(string guid , uint version = 1) {49 public StorableTypeAttribute(string guid) { 52 50 Guid = new Guid(guid); 53 Version = version;54 51 } 55 52 -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Core/TypeInfo.cs
r15529 r15986 70 70 71 71 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllProperties) { 72 var fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 72 // TODO: improved performance for static fields 73 var fieldInfos = type.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 73 74 .Where(x => !x.Name.StartsWith("<") && !x.Name.EndsWith("k__BackingField")); // exclude backing fields 74 75 … … 84 85 85 86 if (StorableTypeAttribute.MemberSelection != StorableMemberSelection.AllFields) { 86 var propertyInfos = type.GetProperties(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 87 // TODO: improved performance for static properties 88 var propertyInfos = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic) 87 89 .Where(x => !x.GetIndexParameters().Any()); // exclude indexed properties 88 90 -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/HeuristicLab.Persistence-4.0.csproj
r15509 r15986 65 65 <Compile Include="Core\StorableTypeAttribute.cs" /> 66 66 <Compile Include="Core\Transformer.cs" /> 67 <Compile Include="Core\StorableConversionAttribute.cs" />68 67 <Compile Include="Core\TransformerAttribute.cs" /> 69 68 <Compile Include="Core\TypeInfo.cs" /> -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Protos/PersistenceMessages.proto
r15529 r15986 17 17 18 18 uint32 type_id = 10; 19 uint32 type_version = 11; 20 repeated uint32 generic_type_box_ids = 12; 19 repeated uint32 generic_type_box_ids = 11; 21 20 22 21 bool bool = 20; -
branches/PersistenceReintegration/HeuristicLab.Persistence/4.0/Transformers/Transformers.cs
r15857 r15986 48 48 49 49 public override object ToObject(Box box, Mapper mapper) { 50 return Extract(box, (Type)mapper.GetObject(box.TypeBoxId), mapper); 50 var type = (Type)mapper.GetObject(box.TypeBoxId); 51 return type == null ? default(T) : Extract(box, type, mapper); 51 52 } 52 53 … … 80 81 box.TypeId = mapper.GetTypeId(type); 81 82 } 82 83 if (StorableTypeAttribute.IsStorableType(type))84 box.TypeVersion = StorableTypeAttribute.GetStorableTypeAttribute(type).Version;85 83 } 86 84 87 85 public override object ToObject(Box box, Mapper mapper) { 88 return Extract(box, mapper.GetType(box.TypeId), mapper); 86 Type type; 87 return mapper.TryGetType(box.TypeId, out type) ? Extract(box, type, mapper) : null; 89 88 } 90 89 91 90 private object Extract(Box box, Type type, Mapper mapper) { 92 91 if (type.IsGenericType) { 93 return type.MakeGenericType(box.GenericTypeBoxIds.Select(x => (Type)mapper.GetObject(x)).ToArray()); 92 var genericArgumentTypes = box.GenericTypeBoxIds.Select(x => (Type)mapper.GetObject(x)).ToArray(); 93 return genericArgumentTypes.Any(x => x == null) ? null : type.MakeGenericType(genericArgumentTypes); 94 94 } else if (type == typeof(Array)) { 95 return ((Type)mapper.GetObject(box.GenericTypeBoxIds[0])).MakeArrayType(); 95 var arrayType = (Type)mapper.GetObject(box.GenericTypeBoxIds[0]); 96 return arrayType?.MakeArrayType(); 96 97 } else { 97 98 return type; … … 526 527 var box = new Box { 527 528 TransformerId = mapper.GetTransformerId(this), 528 Type Id = mapper.GetStringId(o.GetType().AssemblyQualifiedName),529 UInt = mapper.GetStringId(Enum.Format(o.GetType(), o, "G")) ,529 TypeBoxId = mapper.GetBoxId(o.GetType()), 530 UInt = mapper.GetStringId(Enum.Format(o.GetType(), o, "G")) // TODO: introduce old names for enum values to enable refactoring 530 531 }; 531 532 return box; … … 533 534 534 535 public override object ToObject(Box box, Mapper mapper) { 535 return Enum.Parse(Type.GetType(mapper.GetString(box.TypeId)), mapper.GetString(box.UInt)); 536 var type = (Type)mapper.GetObject(box.TypeBoxId); 537 return type == null ? null : Enum.Parse(type, mapper.GetString(box.UInt)); 536 538 } 537 539 } … … 784 786 protected override void Populate(Box box, object value, Mapper mapper) { 785 787 var kvps = box.KeyValuePairs; 786 var parentTypeVersions = box.UInts;787 788 var emptyArgs = new object[0]; 788 789 … … 815 816 } 816 817 817 if (type != originalType) {818 parentTypeVersions.AddRange(new[] { mapper.GetStringId(guid.ToString()), attribute.Version });819 }820 821 818 type = type.BaseType; 822 819 } … … 829 826 public override void FillFromBox(object obj, Box box, Mapper mapper) { 830 827 var kvps = box.KeyValuePairs; 831 var parentTypeVersions = box.UInts;832 828 var emptyArgs = new object[0]; 833 829 … … 840 836 841 837 var type = (Type)mapper.GetObject(box.TypeBoxId); 842 var typeBox = mapper.GetBox(box.TypeBoxId);843 838 var typeInfo = Mapper.StaticCache.GetTypeInfo(type); 844 845 var typeVersions = new List<Tuple<Guid, uint>>();846 typeVersions.Add(Tuple.Create(typeInfo.StorableTypeAttribute.Guid, typeBox.TypeVersion));847 for (int i = 0; i < parentTypeVersions.Count; i += 2)848 typeVersions.Add(Tuple.Create(Guid.Parse(mapper.GetString(parentTypeVersions[i])), parentTypeVersions[i + 1]));849 850 while (true) {851 var applicableConversion = Mapper.StaticCache.GetStorableConversions(typeVersions).FirstOrDefault();852 if (applicableConversion == null) break;853 854 Dictionary<string, object> newDict;855 List<Tuple<string, uint>> typeChain = null;856 857 var conversionParameters = applicableConversion.GetParameters();858 if (conversionParameters.Length == 1) {859 newDict = (Dictionary<string, object>)applicableConversion.Invoke(null, new object[] { dict });860 } else if (conversionParameters.Length == 2) {861 var parameters = new object[] { dict, typeChain };862 newDict = (Dictionary<string, object>)applicableConversion.Invoke(null, parameters);863 typeChain = (List<Tuple<string, uint>>)parameters[1];864 } else throw new PersistenceException("Conversion method has an invalid signature.");865 866 foreach (var kvp in newDict) dict[kvp.Key] = kvp.Value;867 868 var convertedType = StorableConversionAttribute.GetGuid(applicableConversion);869 var convertedVersion = StorableConversionAttribute.GetVersion(applicableConversion);870 871 var index = typeVersions.FindIndex(x => x.Item1 == convertedType);872 typeVersions.RemoveAt(index);873 typeVersions.Insert(index, Tuple.Create(convertedType, convertedVersion + 1));874 875 if (typeChain != null && typeChain.Any()) {876 typeVersions.RemoveRange(index + 1, typeVersions.Count - (index + 1));877 typeVersions.AddRange(typeChain.Select(x => Tuple.Create(Guid.Parse(x.Item1), x.Item2)));878 }879 }880 881 839 var typeStack = new Stack<Tuple<Type, TypeInfo>>(); 882 840 … … 905 863 } 906 864 907 // set all members as generated by conversion method chain865 // set all members 908 866 foreach (var kvp in dict) { 909 867 string key = kvp.Key; … … 940 898 // typeInfo.StorableTypeAttribute.Version, 941 899 // string.Join(", ", undefinedMembers.Select(x => x.Key)))); 942 943 foreach (var hook in typeInfo.AfterDeserializationHooks) {944 hook.Invoke(obj, emptyArgs);945 }946 900 } 947 901 } -
branches/PersistenceReintegration/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs
r15878 r15986 40 40 using HeuristicLab.Persistence.Default.Xml; 41 41 using HeuristicLab.Persistence.Tests; 42 using HeuristicLab.Problems.TestFunctions;43 42 using HeuristicLab.Tests; 44 43 using Microsoft.VisualStudio.TestTools.UnitTesting; … … 338 337 } 339 338 340 public static void RemoveTypeInfo(Type type) {341 typeInfos.Remove(typeof(ConversionA));342 }339 //public static void RemoveTypeInfo(Type type) { 340 // typeInfos.Remove(typeof(ConversionA)); 341 //} 343 342 344 343 public static Guid GetTypeGuid(Type type) { … … 2150 2149 #region conversion 2151 2150 2152 [StorableType("F9F51075-490C-48E3-BF64-14514A210149", 1)]2153 private class OldBaseType {2154 [Storable]2155 public ItemCollection<IItem> items;2156 }2157 [StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)]2158 private class V1 : OldBaseType {2159 [Storable]2160 public IntValue a;2161 2162 [Storable]2163 public ItemList<IntValue> vals;2164 2165 [Storable]2166 public V1 mySelf;2167 2168 [Storable]2169 public Tuple<int, int> tup;2170 2171 [Storable]2172 public int x;2173 [Storable]2174 public int y;2175 2176 [Storable]2177 public string s;2178 }2179 2180 2181 [StorableType("B72C58C8-3321-4706-AA94-578F57337070")]2182 private class NewBaseType {2183 [Storable]2184 public DoubleValue[] items;2185 }2186 [StorableType("00000000-0000-0000-0000-BADCAFFEE001", 2)] // for testing (version 2)2187 private class V2 : NewBaseType {2188 [Storable]2189 public int a;2190 2191 [Storable]2192 public int[] val;2193 2194 [Storable]2195 public V2 mySelf;2196 2197 [Storable]2198 public int TupItem1;2199 2200 [Storable]2201 public int TupItem2;2202 2203 [Storable]2204 public Point coords;2205 2206 [Storable(Name = "s")]2207 public string StorableString { get; set; }2208 }2209 2210 [StorableType("00000000-0000-0000-0000-BADCAFFEE002", 3)] // for testing (version 3)2211 private class V3 : NewBaseType {2212 [Storable]2213 public int a;2214 2215 [Storable]2216 public int[] val;2217 2218 [Storable]2219 public V3 mySelf;2220 2221 [Storable]2222 public Tuple<int, int> tup;2223 2224 [Storable]2225 public Point coords;2226 2227 [Storable(Name = "s", AllowOneWay = true)]2228 public string StorableString { set { PublicString = value; } }2229 public string PublicString { get; set; }2230 }2231 2232 private static class Conversions {2233 [StorableConversion("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)]2234 private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) {2235 var newValues = new Dictionary<string, object>();2236 var items = (ItemCollection<IItem>)values["F9F51075-490C-48E3-BF64-14514A210149.items"];2237 newValues["B72C58C8-3321-4706-AA94-578F57337070.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray();2238 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"] = ((IntValue)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"]).Value;2239 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.val"] = ((ItemList<IntValue>)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.vals"]).Select(iv => iv.Value).ToArray();2240 2241 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"]; // myself type will be mapped correctly2242 2243 var tup = (Tuple<int, int>)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.tup"];2244 if (tup != null) {2245 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem1"] = tup.Item1;2246 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem2"] = tup.Item2;2247 }2248 2249 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.coords"] = new Point((int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.x"], (int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.y"]);2250 2251 return newValues;2252 }2253 2254 [StorableConversion("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 2)]2255 private static Dictionary<string, object> ConvertV2(Dictionary<string, object> values) {2256 var newValues = new Dictionary<string, object>();2257 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"];2258 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.val"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.val"];2259 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"];2260 2261 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.tup"] = Tuple.Create((int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem1"], (int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem2"]);2262 newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.coords"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.coords"];2263 2264 return newValues;2265 }2266 }2267 2268 [TestMethod]2269 [TestCategory("PersistenceNew")]2270 [TestProperty("Time", "short")]2271 public void TestConversion() {2272 var test = new Func<V1>(() => {2273 var p = new V1();2274 p.a = new IntValue(1);2275 p.mySelf = p;2276 p.vals = new ItemList<IntValue>(new IntValue[] { p.a, new IntValue(2), new IntValue(3) });2277 p.tup = Tuple.Create(17, 4);2278 var dv = new DoubleValue(1.0);2279 p.items = new ItemCollection<IItem>(new IItem[] { dv, dv, p.a });2280 p.s = "123";2281 return p;2282 });2283 2284 ProtoBufSerializer serializer = new ProtoBufSerializer();2285 var old = test();2286 serializer.Serialize(old, tempFile);2287 2288 DeregisterType(typeof(V1));2289 DeregisterType(typeof(V2));2290 DeregisterType(typeof(V3));2291 2292 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid, typeof(V3));2293 SetTypeGuid(typeof(V3), GetTypeGuid(typeof(V1)));2294 RemoveTypeInfo(typeof(V1));2295 2296 object o = serializer.Deserialize(tempFile);2297 var restored = (V3)o;2298 Assert.AreEqual(restored.a, old.a.Value);2299 Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value)));2300 Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value)));2301 // Assert.AreSame(restored.items[0], restored.items[1]);2302 Assert.AreSame(restored, restored.mySelf);2303 Assert.AreEqual(restored.PublicString, old.s);2304 }2305 2306 #endregion2307 2308 [TestMethod]2309 [TestCategory("PersistenceNew")]2310 [TestProperty("Time", "short")]2311 public void TestGASerializeDeserializeExecute() {2312 var test = new Func<GeneticAlgorithm>(() => {2313 var ga = new GeneticAlgorithm();2314 ga.Problem = new SingleObjectiveTestFunctionProblem();2315 ga.MaximumGenerations.Value = 100;2316 ga.SetSeedRandomly.Value = false;2317 return ga;2318 });2319 ProtoBufSerializer serializer = new ProtoBufSerializer();2320 GeneticAlgorithm original = test();2321 serializer.Serialize(original, tempFile);2322 object o = serializer.Deserialize(tempFile);2323 2324 // this fails because old persistence didn't handle all IComparer ?2325 // Assert.AreEqual(DebugStringGenerator.Serialize(original),DebugStringGenerator.Serialize(o));2326 2327 GeneticAlgorithm result = (GeneticAlgorithm)o;2328 SamplesUtils.RunAlgorithm(result);2329 SamplesUtils.RunAlgorithm(original);2330 Assert.AreEqual(((DoubleValue)result.Results["BestQuality"].Value).Value,2331 ((DoubleValue)original.Results["BestQuality"].Value).Value);2332 2333 // Assert.AreEqual(DebugStringGenerator.Serialize(result), DebugStringGenerator.Serialize(result2));2334 }2335 2336 [TestMethod]2337 [TestCategory("PersistenceNew")]2338 [TestProperty("Time", "short")]2339 public void TestLoadingSamples() {2340 var path = @"D:\Dev\Software_HL\branches\PersistenceReintegration\HeuristicLab.Optimizer\3.3\Documents";2341 var serializer = new ProtoBufSerializer();2342 foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) {2343 var original = XmlParser.Deserialize(fileName);2344 var ok = true;2345 // foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) {2346 // if (2347 // t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)2348 // .Any(ctor => StorableConstructorAttribute.IsStorableConstructor(ctor))) {2349 // try {2350 // if (t.IsGenericType) {2351 // var g = Mapper.StaticCache.GetGuid(t.GetGenericTypeDefinition());2352 // } else {2353 // var g = Mapper.StaticCache.GetGuid(t);2354 // }2355 // } catch (Exception e) {2356 // Console.WriteLine(t.FullName);2357 // ok = false;2358 // }2359 // }2360 // }2361 if (ok) {2362 serializer.Serialize(original, fileName + ".proto");2363 // var newVersion = serializer.Deserialize(fileName + ".proto");2364 //Console.WriteLine("File: " + fileName);2365 var p = Profile(() => original, Path.GetFileName(fileName));2366 Console.WriteLine(p);2367 }2368 }2369 }2370 2371 [StorableType("3B48A44F-93E2-4ECC-A9B9-56E425EF96F8")]2372 private class ConversionA {2373 [Storable]2374 public int f;2375 }2376 2377 [StorableType("00000000-0000-0000-0000-000000000002", 2)]2378 private class ConversionANew {2379 [Storable]2380 public int g;2381 }2382 2383 [StorableType("7CBF2251-CF9F-4D9E-AA7B-C65495AE078F")]2384 private class ConversionB : ConversionA {2385 [Storable]2386 public int f;2387 public int BaseF { get { return base.f; } }2388 2389 public ConversionB() {2390 2391 }2392 2393 public ConversionB(int myF, int baseF) {2394 this.f = myF;2395 base.f = baseF;2396 }2397 }2398 2399 [StorableType("00000000-0000-0000-0000-000000000001", 2)]2400 private class ConversionBNew : ConversionANew {2401 [Storable]2402 public int g;2403 2404 public int BaseG { get { return base.g; } }2405 }2406 2407 private static class NewConversions {2408 [StorableConversion("7CBF2251-CF9F-4D9E-AA7B-C65495AE078F", 1)]2409 private static Dictionary<string, object> ConvertB(Dictionary<string, object> values) {2410 var newValues = new Dictionary<string, object>();2411 newValues["7CBF2251-CF9F-4D9E-AA7B-C65495AE078F.g"] = values["7CBF2251-CF9F-4D9E-AA7B-C65495AE078F.f"];2412 return newValues;2413 }2414 2415 [StorableConversion("3B48A44F-93E2-4ECC-A9B9-56E425EF96F8", 1)]2416 private static Dictionary<string, object> ConvertA(Dictionary<string, object> values) {2417 var newValues = new Dictionary<string, object>();2418 newValues["3B48A44F-93E2-4ECC-A9B9-56E425EF96F8.g"] = values["3B48A44F-93E2-4ECC-A9B9-56E425EF96F8.f"];2419 return newValues;2420 }2421 }2422 2423 [TestMethod]2424 [TestCategory("PersistenceNew")]2425 [TestProperty("Time", "short")]2426 public void TestConversionCase1() {2427 var test = new Func<ConversionB>(() => {2428 return new ConversionB(1, 2);2429 });2430 2431 ProtoBufSerializer serializer = new ProtoBufSerializer();2432 var old = test();2433 serializer.Serialize(old, tempFile);2434 2435 DeregisterType(typeof(ConversionB));2436 DeregisterType(typeof(ConversionBNew));2437 DeregisterType(typeof(ConversionA));2438 DeregisterType(typeof(ConversionANew));2439 2440 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(ConversionA)).Guid, typeof(ConversionANew));2441 SetTypeGuid(typeof(ConversionANew), GetTypeGuid(typeof(ConversionA)));2442 RemoveTypeInfo(typeof(ConversionA));2443 2444 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(ConversionB)).Guid, typeof(ConversionBNew));2445 SetTypeGuid(typeof(ConversionBNew), GetTypeGuid(typeof(ConversionB)));2446 RemoveTypeInfo(typeof(ConversionB));2447 2448 object o = serializer.Deserialize(tempFile);2449 var restored = (ConversionBNew)o;2450 Assert.AreEqual(restored.g, old.f);2451 Assert.AreEqual(restored.BaseG, old.BaseF);2452 }2453 2454 [StorableType("90470973-4559-428E-AF28-DD95866B0A84")]2455 private class A0 {2456 [Storable]2457 public int x;2458 }2459 2460 [StorableType("00000000-0000-0000-0000-0000000000A1", 2)]2461 private class A1 {2462 [Storable]2463 public int y;2464 }2465 2466 [StorableType("847DD840-3EE3-4A01-AC60-FD7E71ED05F8")]2467 private class B0 {2468 [Storable]2469 public int x;2470 }2471 2472 [StorableType("00000000-0000-0000-0000-0000000000B1", 2)]2473 private class B1 : A0 { }2474 2475 [StorableType("00000000-0000-0000-0000-0000000000B2", 3)]2476 private class B2 {2477 [Storable]2478 public int x;2479 }2480 2481 private static class ConversionsA2A_B2B {2482 [StorableConversion("847DD840-3EE3-4A01-AC60-FD7E71ED05F8", 1, baseGuid: "90470973-4559-428E-AF28-DD95866B0A84", baseVersion: 1)]2483 private static Dictionary<string, object> ConvertB0_B1(Dictionary<string, object> values) {2484 var newValues = new Dictionary<string, object>();2485 newValues["90470973-4559-428E-AF28-DD95866B0A84.x"] = values["847DD840-3EE3-4A01-AC60-FD7E71ED05F8.x"];2486 return newValues;2487 }2488 2489 [StorableConversion("90470973-4559-428E-AF28-DD95866B0A84", 1)]2490 private static Dictionary<string, object> ConvertA0_A1(Dictionary<string, object> values) {2491 var newValues = new Dictionary<string, object>();2492 newValues["90470973-4559-428E-AF28-DD95866B0A84.y"] = values["90470973-4559-428E-AF28-DD95866B0A84.x"];2493 return newValues;2494 }2495 2496 [StorableConversion("847DD840-3EE3-4A01-AC60-FD7E71ED05F8", 2)]2497 private static Dictionary<string, object> ConvertB1_B2(Dictionary<string, object> values) {2498 var newValues = new Dictionary<string, object>();2499 newValues["847DD840-3EE3-4A01-AC60-FD7E71ED05F8.x"] = values["90470973-4559-428E-AF28-DD95866B0A84.y"];2500 return newValues;2501 }2502 }2503 2504 [TestMethod]2505 [TestCategory("PersistenceNew")]2506 [TestProperty("Time", "short")]2507 public void TestConversionCase2() {2508 var test = new Func<B0>(() => {2509 return new B0() { x = 17 };2510 });2511 2512 ProtoBufSerializer serializer = new ProtoBufSerializer();2513 var old = test();2514 serializer.Serialize(old, tempFile);2515 2516 DeregisterType(typeof(B0));2517 DeregisterType(typeof(B1));2518 DeregisterType(typeof(B2));2519 DeregisterType(typeof(A0));2520 DeregisterType(typeof(A1));2521 2522 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(B0)).Guid, typeof(B2));2523 SetTypeGuid(typeof(B2), GetTypeGuid(typeof(B0)));2524 RemoveTypeInfo(typeof(B0));2525 2526 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(A0)).Guid, typeof(A1));2527 SetTypeGuid(typeof(A1), GetTypeGuid(typeof(A0)));2528 RemoveTypeInfo(typeof(A0));2529 2530 object o = serializer.Deserialize(tempFile);2531 var restored = (B2)o;2532 Assert.AreEqual(restored.x, old.x);2533 }2534 2535 #region Inheritance Chain Test2536 [StorableType("C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95")]2537 private class InheritanceA0 {2538 [Storable]2539 public int x;2540 }2541 2542 [StorableType("00000000-0000-0000-0000-0000000001A1", 2)]2543 private class InheritanceA1 {2544 [Storable]2545 public int y;2546 }2547 2548 [StorableType("28A5F6B8-49AF-4C6A-AF0E-F92EB4511722", 1)]2549 private class InheritanceC0 {2550 [Storable]2551 public int x;2552 }2553 2554 [StorableType("00000000-0000-0000-0000-0000000001C1", 2)]2555 private class InheritanceC1 {2556 [Storable]2557 public int x;2558 }2559 2560 [StorableType("41108958-227D-43C4-B049-80AD0D3DB7F6")]2561 private class InheritanceB0 : InheritanceA0 {2562 [Storable]2563 public int x;2564 2565 [StorableConstructor]2566 private InheritanceB0(StorableConstructorFlag flag) { }2567 public InheritanceB0(int x) {2568 this.x = x;2569 base.x = x;2570 }2571 }2572 2573 [StorableType("00000000-0000-0000-0000-0000000001B1", 2)]2574 private class InheritanceB1 : InheritanceC1 { }2575 2576 private static class InheritanceConversionsA2A_B2B {2577 [StorableConversion("C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95", 1)]2578 private static Dictionary<string, object> ConvertA0_A1(Dictionary<string, object> values) {2579 var newValues = new Dictionary<string, object>();2580 newValues["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.y"] = values["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.x"];2581 return newValues;2582 }2583 2584 [StorableConversion("41108958-227D-43C4-B049-80AD0D3DB7F6", 1, baseGuid: "C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95", baseVersion: 2)]2585 private static Dictionary<string, object> ConvertB0_B1(Dictionary<string, object> values, out List<Tuple<string, uint>> typeChain) {2586 typeChain = new List<Tuple<string, uint>> {2587 Tuple.Create("28A5F6B8-49AF-4C6A-AF0E-F92EB4511722", 1u)2588 };2589 2590 var newValues = new Dictionary<string, object>();2591 newValues["41108958-227D-43C4-B049-80AD0D3DB7F6.x"] = values["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.y"];2592 newValues["28A5F6B8-49AF-4C6A-AF0E-F92EB4511722.x"] = values["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.y"];2593 return newValues;2594 }2595 2596 [StorableConversion("28A5F6B8-49AF-4C6A-AF0E-F92EB4511722", 1)]2597 private static Dictionary<string, object> ConvertC0_C1(Dictionary<string, object> values) {2598 var newValues = new Dictionary<string, object>();2599 newValues["28A5F6B8-49AF-4C6A-AF0E-F92EB4511722.x"] = (int)values["28A5F6B8-49AF-4C6A-AF0E-F92EB4511722.x"] * 2;2600 return newValues;2601 }2602 }2603 2604 private static class InheritanceConversionsWithNamesDemo {2605 [StorableConversion("A", 1)]2606 private static Dictionary<string, object> ConvertA1_A2(Dictionary<string, object> values) {2607 var newValues = new Dictionary<string, object>();2608 newValues["y"] = values["x"];2609 return newValues;2610 }2611 2612 [StorableConversion("B", 1, baseGuid: "A", baseVersion: 2)]2613 private static Dictionary<string, object> ConvertB1_B2(Dictionary<string, object> values, out List<Tuple<string, uint>> typeChain) {2614 typeChain = new List<Tuple<string, uint>> {2615 Tuple.Create("C", 1u)2616 };2617 2618 var newValues = new Dictionary<string, object>();2619 newValues["x"] = values["A.y"];2620 newValues["C.x"] = values["A.y"];2621 return newValues;2622 }2623 2624 [StorableConversion("C", 1)]2625 private static Dictionary<string, object> ConvertC1_C2(Dictionary<string, object> values) {2626 var newValues = new Dictionary<string, object>();2627 newValues["x"] = (int)values["x"] * 2;2628 return newValues;2629 }2630 2631 [StorableConversion("C", 1, "D", 1)]2632 2633 private static string Rename() { return "D"; }2634 }2635 2636 [TestMethod]2637 [TestCategory("PersistenceNew")]2638 [TestProperty("Time", "short")]2639 public void TestInheritanceConversionCase1() {2640 var test = new Func<InheritanceB0>(() => {2641 return new InheritanceB0(17);2642 });2643 2644 ProtoBufSerializer serializer = new ProtoBufSerializer();2645 var old = test();2646 serializer.Serialize(old, tempFile);2647 2648 DeregisterType(typeof(InheritanceB0));2649 DeregisterType(typeof(InheritanceB1));2650 DeregisterType(typeof(InheritanceA0));2651 DeregisterType(typeof(InheritanceA1));2652 DeregisterType(typeof(InheritanceC0));2653 DeregisterType(typeof(InheritanceC1));2654 2655 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(InheritanceA0)).Guid, typeof(InheritanceA1));2656 SetTypeGuid(typeof(InheritanceA1), GetTypeGuid(typeof(InheritanceA0)));2657 RemoveTypeInfo(typeof(InheritanceA0));2658 2659 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(InheritanceB0)).Guid, typeof(InheritanceB1));2660 SetTypeGuid(typeof(InheritanceB1), GetTypeGuid(typeof(InheritanceB0)));2661 RemoveTypeInfo(typeof(InheritanceB0));2662 2663 RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(InheritanceC0)).Guid, typeof(InheritanceC1));2664 SetTypeGuid(typeof(InheritanceC1), GetTypeGuid(typeof(InheritanceC0)));2665 RemoveTypeInfo(typeof(InheritanceC0));2666 2667 object o = serializer.Deserialize(tempFile);2668 var restoredC1 = (InheritanceC1)o;2669 var restoredB1 = (InheritanceB1)o;2670 Assert.AreEqual(old.x * 2, restoredC1.x);2671 }2151 //[StorableType("F9F51075-490C-48E3-BF64-14514A210149", 1)] 2152 //private class OldBaseType { 2153 // [Storable] 2154 // public ItemCollection<IItem> items; 2155 //} 2156 //[StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)] 2157 //private class V1 : OldBaseType { 2158 // [Storable] 2159 // public IntValue a; 2160 2161 // [Storable] 2162 // public ItemList<IntValue> vals; 2163 2164 // [Storable] 2165 // public V1 mySelf; 2166 2167 // [Storable] 2168 // public Tuple<int, int> tup; 2169 2170 // [Storable] 2171 // public int x; 2172 // [Storable] 2173 // public int y; 2174 2175 // [Storable] 2176 // public string s; 2177 //} 2178 2179 2180 //[StorableType("B72C58C8-3321-4706-AA94-578F57337070")] 2181 //private class NewBaseType { 2182 // [Storable] 2183 // public DoubleValue[] items; 2184 //} 2185 //[StorableType("00000000-0000-0000-0000-BADCAFFEE001", 2)] // for testing (version 2) 2186 //private class V2 : NewBaseType { 2187 // [Storable] 2188 // public int a; 2189 2190 // [Storable] 2191 // public int[] val; 2192 2193 // [Storable] 2194 // public V2 mySelf; 2195 2196 // [Storable] 2197 // public int TupItem1; 2198 2199 // [Storable] 2200 // public int TupItem2; 2201 2202 // [Storable] 2203 // public Point coords; 2204 2205 // [Storable(Name = "s")] 2206 // public string StorableString { get; set; } 2207 //} 2208 2209 //[StorableType("00000000-0000-0000-0000-BADCAFFEE002", 3)] // for testing (version 3) 2210 //private class V3 : NewBaseType { 2211 // [Storable] 2212 // public int a; 2213 2214 // [Storable] 2215 // public int[] val; 2216 2217 // [Storable] 2218 // public V3 mySelf; 2219 2220 // [Storable] 2221 // public Tuple<int, int> tup; 2222 2223 // [Storable] 2224 // public Point coords; 2225 2226 // [Storable(Name = "s", AllowOneWay = true)] 2227 // public string StorableString { set { PublicString = value; } } 2228 // public string PublicString { get; set; } 2229 //} 2230 2231 //private static class Conversions { 2232 // [StorableConversion("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)] 2233 // private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) { 2234 // var newValues = new Dictionary<string, object>(); 2235 // var items = (ItemCollection<IItem>)values["F9F51075-490C-48E3-BF64-14514A210149.items"]; 2236 // newValues["B72C58C8-3321-4706-AA94-578F57337070.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray(); 2237 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"] = ((IntValue)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"]).Value; 2238 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.val"] = ((ItemList<IntValue>)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.vals"]).Select(iv => iv.Value).ToArray(); 2239 2240 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"]; // myself type will be mapped correctly 2241 2242 // var tup = (Tuple<int, int>)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.tup"]; 2243 // if (tup != null) { 2244 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem1"] = tup.Item1; 2245 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem2"] = tup.Item2; 2246 // } 2247 2248 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.coords"] = new Point((int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.x"], (int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.y"]); 2249 2250 // return newValues; 2251 // } 2252 2253 // [StorableConversion("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 2)] 2254 // private static Dictionary<string, object> ConvertV2(Dictionary<string, object> values) { 2255 // var newValues = new Dictionary<string, object>(); 2256 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.a"]; 2257 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.val"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.val"]; 2258 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.mySelf"]; 2259 2260 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.tup"] = Tuple.Create((int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem1"], (int)values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.TupItem2"]); 2261 // newValues["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.coords"] = values["D211A828-6440-4E72-A8C7-AA4F9B4FFA75.coords"]; 2262 2263 // return newValues; 2264 // } 2265 //} 2266 2267 //[TestMethod] 2268 //[TestCategory("PersistenceNew")] 2269 //[TestProperty("Time", "short")] 2270 //public void TestConversion() { 2271 // var test = new Func<V1>(() => { 2272 // var p = new V1(); 2273 // p.a = new IntValue(1); 2274 // p.mySelf = p; 2275 // p.vals = new ItemList<IntValue>(new IntValue[] { p.a, new IntValue(2), new IntValue(3) }); 2276 // p.tup = Tuple.Create(17, 4); 2277 // var dv = new DoubleValue(1.0); 2278 // p.items = new ItemCollection<IItem>(new IItem[] { dv, dv, p.a }); 2279 // p.s = "123"; 2280 // return p; 2281 // }); 2282 2283 // ProtoBufSerializer serializer = new ProtoBufSerializer(); 2284 // var old = test(); 2285 // serializer.Serialize(old, tempFile); 2286 2287 // DeregisterType(typeof(V1)); 2288 // DeregisterType(typeof(V2)); 2289 // DeregisterType(typeof(V3)); 2290 2291 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid, typeof(V3)); 2292 // SetTypeGuid(typeof(V3), GetTypeGuid(typeof(V1))); 2293 // RemoveTypeInfo(typeof(V1)); 2294 2295 // object o = serializer.Deserialize(tempFile); 2296 // var restored = (V3)o; 2297 // Assert.AreEqual(restored.a, old.a.Value); 2298 // Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value))); 2299 // Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value))); 2300 // // Assert.AreSame(restored.items[0], restored.items[1]); 2301 // Assert.AreSame(restored, restored.mySelf); 2302 // Assert.AreEqual(restored.PublicString, old.s); 2303 //} 2304 2305 //#endregion 2306 2307 //[TestMethod] 2308 //[TestCategory("PersistenceNew")] 2309 //[TestProperty("Time", "short")] 2310 //public void TestGASerializeDeserializeExecute() { 2311 // var test = new Func<GeneticAlgorithm>(() => { 2312 // var ga = new GeneticAlgorithm(); 2313 // ga.Problem = new SingleObjectiveTestFunctionProblem(); 2314 // ga.MaximumGenerations.Value = 100; 2315 // ga.SetSeedRandomly.Value = false; 2316 // return ga; 2317 // }); 2318 // ProtoBufSerializer serializer = new ProtoBufSerializer(); 2319 // GeneticAlgorithm original = test(); 2320 // serializer.Serialize(original, tempFile); 2321 // object o = serializer.Deserialize(tempFile); 2322 2323 // // this fails because old persistence didn't handle all IComparer ? 2324 // // Assert.AreEqual(DebugStringGenerator.Serialize(original),DebugStringGenerator.Serialize(o)); 2325 2326 // GeneticAlgorithm result = (GeneticAlgorithm)o; 2327 // SamplesUtils.RunAlgorithm(result); 2328 // SamplesUtils.RunAlgorithm(original); 2329 // Assert.AreEqual(((DoubleValue)result.Results["BestQuality"].Value).Value, 2330 // ((DoubleValue)original.Results["BestQuality"].Value).Value); 2331 2332 // // Assert.AreEqual(DebugStringGenerator.Serialize(result), DebugStringGenerator.Serialize(result2)); 2333 //} 2334 2335 //[TestMethod] 2336 //[TestCategory("PersistenceNew")] 2337 //[TestProperty("Time", "short")] 2338 //public void TestLoadingSamples() { 2339 // var path = @"C:\Dev\Software_HL\branches\PersistenceReintegration\HeuristicLab.Optimizer\3.3\Documents"; 2340 // var serializer = new ProtoBufSerializer(); 2341 // foreach (var fileName in Directory.EnumerateFiles(path, "*.hl")) { 2342 // var original = XmlParser.Deserialize(fileName); 2343 // var ok = true; 2344 // // foreach (var t in original.GetObjectGraphObjects().Select(o => o.GetType())) { 2345 // // if ( 2346 // // t.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public) 2347 // // .Any(ctor => StorableConstructorAttribute.IsStorableConstructor(ctor))) { 2348 // // try { 2349 // // if (t.IsGenericType) { 2350 // // var g = Mapper.StaticCache.GetGuid(t.GetGenericTypeDefinition()); 2351 // // } else { 2352 // // var g = Mapper.StaticCache.GetGuid(t); 2353 // // } 2354 // // } catch (Exception e) { 2355 // // Console.WriteLine(t.FullName); 2356 // // ok = false; 2357 // // } 2358 // // } 2359 // // } 2360 // if (ok) { 2361 // serializer.Serialize(original, fileName + ".proto"); 2362 // // var newVersion = serializer.Deserialize(fileName + ".proto"); 2363 // //Console.WriteLine("File: " + fileName); 2364 // var p = Profile(() => original, Path.GetFileName(fileName)); 2365 // Console.WriteLine(p); 2366 // } 2367 // } 2368 //} 2369 2370 //[StorableType("3B48A44F-93E2-4ECC-A9B9-56E425EF96F8")] 2371 //private class ConversionA { 2372 // [Storable] 2373 // public int f; 2374 //} 2375 2376 //[StorableType("00000000-0000-0000-0000-000000000002", 2)] 2377 //private class ConversionANew { 2378 // [Storable] 2379 // public int g; 2380 //} 2381 2382 //[StorableType("7CBF2251-CF9F-4D9E-AA7B-C65495AE078F")] 2383 //private class ConversionB : ConversionA { 2384 // [Storable] 2385 // public int f; 2386 // public int BaseF { get { return base.f; } } 2387 2388 // public ConversionB() { 2389 2390 // } 2391 2392 // public ConversionB(int myF, int baseF) { 2393 // this.f = myF; 2394 // base.f = baseF; 2395 // } 2396 //} 2397 2398 //[StorableType("00000000-0000-0000-0000-000000000001", 2)] 2399 //private class ConversionBNew : ConversionANew { 2400 // [Storable] 2401 // public int g; 2402 2403 // public int BaseG { get { return base.g; } } 2404 //} 2405 2406 //private static class NewConversions { 2407 // [StorableConversion("7CBF2251-CF9F-4D9E-AA7B-C65495AE078F", 1)] 2408 // private static Dictionary<string, object> ConvertB(Dictionary<string, object> values) { 2409 // var newValues = new Dictionary<string, object>(); 2410 // newValues["7CBF2251-CF9F-4D9E-AA7B-C65495AE078F.g"] = values["7CBF2251-CF9F-4D9E-AA7B-C65495AE078F.f"]; 2411 // return newValues; 2412 // } 2413 2414 // [StorableConversion("3B48A44F-93E2-4ECC-A9B9-56E425EF96F8", 1)] 2415 // private static Dictionary<string, object> ConvertA(Dictionary<string, object> values) { 2416 // var newValues = new Dictionary<string, object>(); 2417 // newValues["3B48A44F-93E2-4ECC-A9B9-56E425EF96F8.g"] = values["3B48A44F-93E2-4ECC-A9B9-56E425EF96F8.f"]; 2418 // return newValues; 2419 // } 2420 //} 2421 2422 //[TestMethod] 2423 //[TestCategory("PersistenceNew")] 2424 //[TestProperty("Time", "short")] 2425 //public void TestConversionCase1() { 2426 // var test = new Func<ConversionB>(() => { 2427 // return new ConversionB(1, 2); 2428 // }); 2429 2430 // ProtoBufSerializer serializer = new ProtoBufSerializer(); 2431 // var old = test(); 2432 // serializer.Serialize(old, tempFile); 2433 2434 // DeregisterType(typeof(ConversionB)); 2435 // DeregisterType(typeof(ConversionBNew)); 2436 // DeregisterType(typeof(ConversionA)); 2437 // DeregisterType(typeof(ConversionANew)); 2438 2439 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(ConversionA)).Guid, typeof(ConversionANew)); 2440 // SetTypeGuid(typeof(ConversionANew), GetTypeGuid(typeof(ConversionA))); 2441 // RemoveTypeInfo(typeof(ConversionA)); 2442 2443 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(ConversionB)).Guid, typeof(ConversionBNew)); 2444 // SetTypeGuid(typeof(ConversionBNew), GetTypeGuid(typeof(ConversionB))); 2445 // RemoveTypeInfo(typeof(ConversionB)); 2446 2447 // object o = serializer.Deserialize(tempFile); 2448 // var restored = (ConversionBNew)o; 2449 // Assert.AreEqual(restored.g, old.f); 2450 // Assert.AreEqual(restored.BaseG, old.BaseF); 2451 //} 2452 2453 //[StorableType("90470973-4559-428E-AF28-DD95866B0A84")] 2454 //private class A0 { 2455 // [Storable] 2456 // public int x; 2457 //} 2458 2459 //[StorableType("00000000-0000-0000-0000-0000000000A1", 2)] 2460 //private class A1 { 2461 // [Storable] 2462 // public int y; 2463 //} 2464 2465 //[StorableType("847DD840-3EE3-4A01-AC60-FD7E71ED05F8")] 2466 //private class B0 { 2467 // [Storable] 2468 // public int x; 2469 //} 2470 2471 //[StorableType("00000000-0000-0000-0000-0000000000B1", 2)] 2472 //private class B1 : A0 { } 2473 2474 //[StorableType("00000000-0000-0000-0000-0000000000B2", 3)] 2475 //private class B2 { 2476 // [Storable] 2477 // public int x; 2478 //} 2479 2480 //private static class ConversionsA2A_B2B { 2481 // [StorableConversion("847DD840-3EE3-4A01-AC60-FD7E71ED05F8", 1, baseGuid: "90470973-4559-428E-AF28-DD95866B0A84", baseVersion: 1)] 2482 // private static Dictionary<string, object> ConvertB0_B1(Dictionary<string, object> values) { 2483 // var newValues = new Dictionary<string, object>(); 2484 // newValues["90470973-4559-428E-AF28-DD95866B0A84.x"] = values["847DD840-3EE3-4A01-AC60-FD7E71ED05F8.x"]; 2485 // return newValues; 2486 // } 2487 2488 // [StorableConversion("90470973-4559-428E-AF28-DD95866B0A84", 1)] 2489 // private static Dictionary<string, object> ConvertA0_A1(Dictionary<string, object> values) { 2490 // var newValues = new Dictionary<string, object>(); 2491 // newValues["90470973-4559-428E-AF28-DD95866B0A84.y"] = values["90470973-4559-428E-AF28-DD95866B0A84.x"]; 2492 // return newValues; 2493 // } 2494 2495 // [StorableConversion("847DD840-3EE3-4A01-AC60-FD7E71ED05F8", 2)] 2496 // private static Dictionary<string, object> ConvertB1_B2(Dictionary<string, object> values) { 2497 // var newValues = new Dictionary<string, object>(); 2498 // newValues["847DD840-3EE3-4A01-AC60-FD7E71ED05F8.x"] = values["90470973-4559-428E-AF28-DD95866B0A84.y"]; 2499 // return newValues; 2500 // } 2501 //} 2502 2503 //[TestMethod] 2504 //[TestCategory("PersistenceNew")] 2505 //[TestProperty("Time", "short")] 2506 //public void TestConversionCase2() { 2507 // var test = new Func<B0>(() => { 2508 // return new B0() { x = 17 }; 2509 // }); 2510 2511 // ProtoBufSerializer serializer = new ProtoBufSerializer(); 2512 // var old = test(); 2513 // serializer.Serialize(old, tempFile); 2514 2515 // DeregisterType(typeof(B0)); 2516 // DeregisterType(typeof(B1)); 2517 // DeregisterType(typeof(B2)); 2518 // DeregisterType(typeof(A0)); 2519 // DeregisterType(typeof(A1)); 2520 2521 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(B0)).Guid, typeof(B2)); 2522 // SetTypeGuid(typeof(B2), GetTypeGuid(typeof(B0))); 2523 // RemoveTypeInfo(typeof(B0)); 2524 2525 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(A0)).Guid, typeof(A1)); 2526 // SetTypeGuid(typeof(A1), GetTypeGuid(typeof(A0))); 2527 // RemoveTypeInfo(typeof(A0)); 2528 2529 // object o = serializer.Deserialize(tempFile); 2530 // var restored = (B2)o; 2531 // Assert.AreEqual(restored.x, old.x); 2532 //} 2533 2534 //#region Inheritance Chain Test 2535 //[StorableType("C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95")] 2536 //private class InheritanceA0 { 2537 // [Storable] 2538 // public int x; 2539 //} 2540 2541 //[StorableType("00000000-0000-0000-0000-0000000001A1", 2)] 2542 //private class InheritanceA1 { 2543 // [Storable] 2544 // public int y; 2545 //} 2546 2547 //[StorableType("28A5F6B8-49AF-4C6A-AF0E-F92EB4511722", 1)] 2548 //private class InheritanceC0 { 2549 // [Storable] 2550 // public int x; 2551 //} 2552 2553 //[StorableType("00000000-0000-0000-0000-0000000001C1", 2)] 2554 //private class InheritanceC1 { 2555 // [Storable] 2556 // public int x; 2557 //} 2558 2559 //[StorableType("41108958-227D-43C4-B049-80AD0D3DB7F6")] 2560 //private class InheritanceB0 : InheritanceA0 { 2561 // [Storable] 2562 // public int x; 2563 2564 // [StorableConstructor] 2565 // private InheritanceB0(StorableConstructorFlag flag) { } 2566 // public InheritanceB0(int x) { 2567 // this.x = x; 2568 // base.x = x; 2569 // } 2570 //} 2571 2572 //[StorableType("00000000-0000-0000-0000-0000000001B1", 2)] 2573 //private class InheritanceB1 : InheritanceC1 { } 2574 2575 //private static class InheritanceConversionsA2A_B2B { 2576 // [StorableConversion("C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95", 1)] 2577 // private static Dictionary<string, object> ConvertA0_A1(Dictionary<string, object> values) { 2578 // var newValues = new Dictionary<string, object>(); 2579 // newValues["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.y"] = values["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.x"]; 2580 // return newValues; 2581 // } 2582 2583 // [StorableConversion("41108958-227D-43C4-B049-80AD0D3DB7F6", 1, baseGuid: "C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95", baseVersion: 2)] 2584 // private static Dictionary<string, object> ConvertB0_B1(Dictionary<string, object> values, out List<Tuple<string, uint>> typeChain) { 2585 // typeChain = new List<Tuple<string, uint>> { 2586 // Tuple.Create("28A5F6B8-49AF-4C6A-AF0E-F92EB4511722", 1u) 2587 // }; 2588 2589 // var newValues = new Dictionary<string, object>(); 2590 // newValues["41108958-227D-43C4-B049-80AD0D3DB7F6.x"] = values["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.y"]; 2591 // newValues["28A5F6B8-49AF-4C6A-AF0E-F92EB4511722.x"] = values["C1BFC249-89F6-45E2-8DFB-DA95E1BE9B95.y"]; 2592 // return newValues; 2593 // } 2594 2595 // [StorableConversion("28A5F6B8-49AF-4C6A-AF0E-F92EB4511722", 1)] 2596 // private static Dictionary<string, object> ConvertC0_C1(Dictionary<string, object> values) { 2597 // var newValues = new Dictionary<string, object>(); 2598 // newValues["28A5F6B8-49AF-4C6A-AF0E-F92EB4511722.x"] = (int)values["28A5F6B8-49AF-4C6A-AF0E-F92EB4511722.x"] * 2; 2599 // return newValues; 2600 // } 2601 //} 2602 2603 ////private static class InheritanceConversionsWithNamesDemo { 2604 //// [StorableConversion("A", 1)] 2605 //// private static Dictionary<string, object> ConvertA1_A2(Dictionary<string, object> values) { 2606 //// var newValues = new Dictionary<string, object>(); 2607 //// newValues["y"] = values["x"]; 2608 //// return newValues; 2609 //// } 2610 2611 //// [StorableConversion("B", 1, baseGuid: "A", baseVersion: 2)] 2612 //// private static Dictionary<string, object> ConvertB1_B2(Dictionary<string, object> values, out List<Tuple<string, uint>> typeChain) { 2613 //// typeChain = new List<Tuple<string, uint>> { 2614 //// Tuple.Create("C", 1u) 2615 //// }; 2616 2617 //// var newValues = new Dictionary<string, object>(); 2618 //// newValues["x"] = values["A.y"]; 2619 //// newValues["C.x"] = values["A.y"]; 2620 //// return newValues; 2621 //// } 2622 2623 //// [StorableConversion("C", 1)] 2624 //// private static Dictionary<string, object> ConvertC1_C2(Dictionary<string, object> values) { 2625 //// var newValues = new Dictionary<string, object>(); 2626 //// newValues["x"] = (int)values["x"] * 2; 2627 //// return newValues; 2628 //// } 2629 2630 //// [StorableConversion("C", 1, "D", 1)] 2631 //// //[StorableConversion("D", 1, "E", 1)] 2632 //// private static string Rename() { return "D"; } 2633 ////} 2634 2635 //[TestMethod] 2636 //[TestCategory("PersistenceNew")] 2637 //[TestProperty("Time", "short")] 2638 //public void TestInheritanceConversionCase1() { 2639 // var test = new Func<InheritanceB0>(() => { 2640 // return new InheritanceB0(17); 2641 // }); 2642 2643 // ProtoBufSerializer serializer = new ProtoBufSerializer(); 2644 // var old = test(); 2645 // serializer.Serialize(old, tempFile); 2646 2647 // DeregisterType(typeof(InheritanceB0)); 2648 // DeregisterType(typeof(InheritanceB1)); 2649 // DeregisterType(typeof(InheritanceA0)); 2650 // DeregisterType(typeof(InheritanceA1)); 2651 // DeregisterType(typeof(InheritanceC0)); 2652 // DeregisterType(typeof(InheritanceC1)); 2653 2654 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(InheritanceA0)).Guid, typeof(InheritanceA1)); 2655 // SetTypeGuid(typeof(InheritanceA1), GetTypeGuid(typeof(InheritanceA0))); 2656 // RemoveTypeInfo(typeof(InheritanceA0)); 2657 2658 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(InheritanceB0)).Guid, typeof(InheritanceB1)); 2659 // SetTypeGuid(typeof(InheritanceB1), GetTypeGuid(typeof(InheritanceB0))); 2660 // RemoveTypeInfo(typeof(InheritanceB0)); 2661 2662 // RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(InheritanceC0)).Guid, typeof(InheritanceC1)); 2663 // SetTypeGuid(typeof(InheritanceC1), GetTypeGuid(typeof(InheritanceC0))); 2664 // RemoveTypeInfo(typeof(InheritanceC0)); 2665 2666 // object o = serializer.Deserialize(tempFile); 2667 // var restoredC1 = (InheritanceC1)o; 2668 // var restoredB1 = (InheritanceB1)o; 2669 // Assert.AreEqual(old.x * 2, restoredC1.x); 2670 //} 2672 2671 #endregion 2673 2672 #endregion
Note: See TracChangeset
for help on using the changeset viewer.