Changeset 1357
- Timestamp:
- 03/18/09 18:13:40 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 11 added
- 4 deleted
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence.sln
r1338 r1357 2 2 Microsoft Visual Studio Solution File, Format Version 10.00 3 3 # Visual Studio 2008 4 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = " Persistence", "Persistence\Persistence.csproj", "{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}"4 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Persistence", "Persistence\HeuristicLab.Persistence.csproj", "{102BC7D3-0EF9-439C-8F6D-96FF0FDB8E1B}" 5 5 EndProject 6 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{E2245078-6F40-449E-A370-01A97A7C5E6A}" 6 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Persistence.Test", "Test\HeuristicLab.Persistence.Test.csproj", "{E2245078-6F40-449E-A370-01A97A7C5E6A}" 7 EndProject 8 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.PluginInfrastructure", "..\..\..\trunk\sources\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj", "{94186A6A-5176-4402-AE83-886557B53CCA}" 7 9 EndProject 8 10 Global … … 26 28 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Release|Any CPU.Build.0 = Release|Any CPU 27 29 {E2245078-6F40-449E-A370-01A97A7C5E6A}.Release|x86.ActiveCfg = Release|Any CPU 30 {94186A6A-5176-4402-AE83-886557B53CCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 {94186A6A-5176-4402-AE83-886557B53CCA}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 {94186A6A-5176-4402-AE83-886557B53CCA}.Debug|x86.ActiveCfg = Debug|x86 33 {94186A6A-5176-4402-AE83-886557B53CCA}.Debug|x86.Build.0 = Debug|x86 34 {94186A6A-5176-4402-AE83-886557B53CCA}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 {94186A6A-5176-4402-AE83-886557B53CCA}.Release|Any CPU.Build.0 = Release|Any CPU 36 {94186A6A-5176-4402-AE83-886557B53CCA}.Release|x86.ActiveCfg = Release|x86 37 {94186A6A-5176-4402-AE83-886557B53CCA}.Release|x86.Build.0 = Release|x86 28 38 EndGlobalSection 29 39 GlobalSection(SolutionProperties) = preSolution -
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1356 r1357 1 1 using System.Collections.Generic; 2 2 using System; 3 using HeuristicLab.Persistence.Interfaces; 3 4 4 namespace Persistence { 5 namespace HeuristicLab.Persistence { 6 7 8 struct ParentReference { } 9 delegate void Setter(object value); 10 11 12 interface IAccessibleObject { 13 object Obj { get; } 14 Setter GetSetter(string name); 15 } 16 17 18 class CustomObject : IAccessibleObject { 19 20 public object Obj { get; private set; } 21 public readonly List<object> customValues; 22 23 public CustomObject(object obj) { 24 Obj = obj; 25 customValues = new List<object>(); 26 } 27 28 public void AddValue(object value) { 29 customValues.Add(value); 30 } 31 32 public Setter GetSetter(string name) { 33 int index = customValues.Count - 1; 34 return value => customValues[index] = value; 35 } 36 } 37 38 39 class CompositeObject : IAccessibleObject { 40 41 public object Obj { get; private set; } 42 public readonly Dictionary<string, DataMemberAccessor> accessorDict; 43 44 public CompositeObject(object obj, Dictionary<string, DataMemberAccessor> accessorDict) { 45 Obj = obj; 46 this.accessorDict = new Dictionary<string, DataMemberAccessor>(); 47 foreach (KeyValuePair<string, DataMemberAccessor> pair in accessorDict) { 48 this.accessorDict.Add( 49 pair.Value.Name, 50 pair.Value); 51 } 52 } 53 54 public void SetValue(string name, object value) { 55 accessorDict[name].Set(value); 56 accessorDict.Remove(name); 57 } 58 59 public Setter GetSetter(string name) { 60 return value => accessorDict[name].Set(value); 61 } 62 63 public void PopulateDefaultValues() { 64 foreach (var pair in accessorDict) { 65 pair.Value.Set(pair.Value.DefaultValue); 66 } 67 } 68 } 69 5 70 6 71 public class DeSerializer { 7 72 8 struct ParentReference { }9 10 delegate void Setter(object value);11 12 interface IAccessibleObject {13 object Obj { get; }14 Setter GetSetter(string name);15 }16 17 class CustomObject : IAccessibleObject {18 public object Obj { get; private set; }19 public readonly List<object> customValues;20 public CustomObject(object obj) {21 Obj = obj;22 customValues = new List<object>();23 }24 public void AddValue(object value) {25 customValues.Add(value);26 }27 public Setter GetSetter(string name) {28 int index = customValues.Count-1;29 return value => customValues[index] = value;30 }31 }32 33 class CompositeObject : IAccessibleObject {34 public object Obj { get; private set; }35 public readonly Dictionary<string, DataMemberAccessor> accessorDict;36 public CompositeObject(object obj, Dictionary<string, DataMemberAccessor> accessorDict) {37 Obj = obj;38 this.accessorDict = new Dictionary<string, DataMemberAccessor>();39 foreach (KeyValuePair<string, DataMemberAccessor> pair in accessorDict) {40 this.accessorDict.Add(41 pair.Value.Name,42 pair.Value);43 }44 }45 public void SetValue(string name, object value) {46 accessorDict[name].Set(value);47 accessorDict.Remove(name);48 }49 public Setter GetSetter(string name) {50 return value => accessorDict[name].Set(value);51 }52 public void PopulateDefaultValues() {53 foreach (var pair in accessorDict) {54 pair.Value.Set(pair.Value.DefaultValue);55 }56 }57 }58 59 73 private delegate void Handler(ISerializationToken token); 74 private delegate void Thunk(); 60 75 61 76 private readonly Dictionary<int, object> id2obj; … … 63 78 private readonly Stack<IAccessibleObject> compositeStack; 64 79 private readonly PersistenceConfiguration persistenceConfiguration; 65 private readonly Dictionary<int, Type> typeIds; 66 67 delegate void Thunk(); 80 private readonly Dictionary<int, Type> typeIds; 68 81 private List<Thunk> finalFixes; 69 82 … … 115 128 id2obj.Add((int)start.Id, instance); 116 129 } 130 117 131 private void CompositeEndHandler(ISerializationToken token) { 118 132 EndToken end = (EndToken)token; … … 132 146 } 133 147 } 148 134 149 private void PrimitiveHandler(ISerializationToken token) { 135 150 PrimitiveToken primitive = (PrimitiveToken)token; … … 142 157 SetValue(primitive.Name, value); 143 158 } 159 144 160 private void ReferenceHandler(ISerializationToken token) { 145 161 ReferenceToken reference = (ReferenceToken)token; … … 152 168 } 153 169 } 170 154 171 private void NullHandler(ISerializationToken token) { 155 172 NullReferenceToken nil = (NullReferenceToken)token; 156 173 SetValue(nil.Name, null); 157 174 } 175 158 176 private void SetValue(string name, object value) { 159 177 if (compositeStack.Count == 0) { -
branches/New Persistence Exploration/Persistence/Persistence/Decomposers.cs
r1354 r1357 3 3 using System.Reflection; 4 4 using System.Collections.Generic; 5 namespace Persistence { 5 using HeuristicLab.Persistence.Interfaces; 6 7 namespace HeuristicLab.Persistence { 6 8 7 8 public interface IDecomposer {9 bool CanSerialize(Type type);10 IEnumerable Serialize(object obj);11 object DeSerialize(IEnumerable objects, Type type);12 }13 14 15 9 public class EnumerableDecomposer : IDecomposer { 16 10 -
branches/New Persistence Exploration/Persistence/Persistence/PersistenceConfiguration.cs
r1349 r1357 2 2 using System.Collections.Generic; 3 3 using System.Reflection; 4 using HeuristicLab.Persistence.Interfaces; 4 5 5 namespace Persistence {6 namespace HeuristicLab.Persistence { 6 7 7 8 public class PersistenceConfiguration { -
branches/New Persistence Exploration/Persistence/Persistence/PrimitiveFormatters.cs
r1348 r1357 1 1 using System; 2 using System.Collections;3 using System.Collections.Generic;4 2 using System.Text; 5 3 using System.Globalization; 6 namespace Persistence { 4 using HeuristicLab.Persistence.Interfaces; 7 5 8 public interface IFormat { 9 string Name { get; } 10 } 11 12 public interface IFormatter { 13 Type Type { get; } 14 IFormat Format { get; } 15 object Serialize(object o); 16 object DeSerialize(object o); 17 } 6 namespace HeuristicLab.Persistence { 18 7 19 8 public class XmlFormat : IFormat { … … 85 74 } 86 75 87 public abstract class NumberArray2XmlFormatter : IFormatter { 88 public abstract Type Type { get; } 89 public IFormat Format { get { return XmlFormat.Instance; } } 90 protected virtual string Separator { get { return ";"; } } 91 protected abstract string formatValue(object o); 92 protected abstract object parseValue(string o); 93 public object Serialize(object obj) { 94 Array a = (Array)obj; 95 StringBuilder sb = new StringBuilder(); 96 sb.Append(a.Rank); 97 for (int i = 0; i < a.Rank; i++) { 98 sb.Append(Separator); 99 sb.Append(a.GetLength(i)); 100 } 101 foreach (object o in a) { 102 sb.Append(Separator); 103 sb.Append(formatValue(o)); 104 } 105 return sb.ToString(); 106 } 107 public object DeSerialize(object o) { 108 IEnumerator values = 109 ((string) o) 110 .Split(new[] {Separator}, 111 StringSplitOptions.RemoveEmptyEntries).GetEnumerator(); 112 values.MoveNext(); 113 int rank = int.Parse((string)values.Current); 114 int[] lengths = new int[rank]; 115 for ( int i = 0; i<rank; i++ ) { 116 values.MoveNext(); 117 lengths[i] = int.Parse((string)values.Current); 118 } 119 Array a = Array.CreateInstance(this.Type.GetElementType(), lengths); 120 int[] positions = new int[rank]; 121 while ( values.MoveNext() ) { 122 a.SetValue(parseValue((string)values.Current), positions); 123 positions[0] += 1; 124 for ( int i = 0; i<rank-1; i++) { 125 if (positions[i] >= lengths[i]) { 126 positions[i] = 0; 127 positions[i + 1] += 1; 128 } else { 129 break; 130 } 131 } 132 } 133 return a; 134 } 135 } 136 public class IntArray2XmlFormatter : NumberArray2XmlFormatter { 137 public override Type Type { get { return typeof(int[]); } } 138 protected override string formatValue(object o) { return o.ToString(); } 139 protected override object parseValue(string o) { return int.Parse(o); } 140 } 141 public class Int2DArray2XmlFormatter : IntArray2XmlFormatter { 142 public override Type Type { get { return typeof (int[,]); } } 143 } 144 public class Int3DArray2XmlFormatter : IntArray2XmlFormatter { 145 public override Type Type { get { return typeof(int[,,]); } } 146 } 147 public class DoubleArray2XmlFormatter : NumberArray2XmlFormatter { 148 public override Type Type { get { return typeof(double[]); } } 149 protected override string formatValue(object o) { return ((double) o).ToString("r"); } 150 protected override object parseValue(string o) { return double.Parse(o); } 151 } 152 public class Double2DArray2XmlFormatter : DoubleArray2XmlFormatter { 153 public override Type Type { get { return typeof(double[,]); } } 154 } 155 public class Double3DArray2XmlFormatter : DoubleArray2XmlFormatter { 156 public override Type Type { get { return typeof(double[,,]); } } 157 } 158 159 public abstract class NumberEnumeration2XmlFormatter : IFormatter { 160 public abstract Type Type { get; } 161 public IFormat Format { get { return XmlFormat.Instance; } } 162 protected virtual string Separator { get { return ";"; } } 163 protected abstract void Add(IEnumerable enumeration, object o); 164 protected abstract object Instantiate(); 165 protected abstract string formatValue(object o); 166 protected abstract object parseValue(string o); 167 public object Serialize(object o) { 168 StringBuilder sb = new StringBuilder(); 169 foreach (var value in (IEnumerable) o) { 170 sb.Append(formatValue(value)); 171 sb.Append(Separator); 172 } 173 return sb.ToString(); 174 } 175 public object DeSerialize(object o) { 176 IEnumerable enumeration = (IEnumerable) Instantiate(); 177 string[] values = ((string) o).Split(new[] {Separator}, StringSplitOptions.RemoveEmptyEntries); 178 foreach ( var value in values ) { 179 Add(enumeration, parseValue(value)); 180 } 181 return enumeration; 182 } 183 } 184 public class IntList2XmlFormatter : NumberEnumeration2XmlFormatter { 185 public override Type Type { get { return typeof(List<int>); } } 186 protected override void Add(IEnumerable enumeration, object o) { ((List<int>)enumeration).Add((int)o); } 187 protected override object Instantiate() { return new List<int>(); } 188 protected override string formatValue(object o) { return o.ToString(); } 189 protected override object parseValue(string o) { return int.Parse(o); } 190 } 191 public class DoubleList2XmlFormatter : NumberEnumeration2XmlFormatter { 192 public override Type Type { get { return typeof(List<double>); } } 193 protected override void Add(IEnumerable enumeration, object o) { ((List<double>)enumeration).Add((int)o); } 194 protected override object Instantiate() { return new List<double>(); } 195 protected override string formatValue(object o) { return ((double) o).ToString("r"); } 196 protected override object parseValue(string o) { return double.Parse(o); } 197 } 76 198 77 } -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1356 r1357 3 3 using System; 4 4 using System.Linq; 5 using HeuristicLab.Persistence.Interfaces; 5 6 6 namespace Persistence {7 namespace HeuristicLab.Persistence { 7 8 8 9 public class Serializer : IEnumerable<ISerializationToken> { … … 46 47 } 47 48 49 48 50 private IEnumerator<ISerializationToken> Serialize(DataMemberAccessor accessor) { 49 50 51 object value = accessor.Get(); 51 52 if (value == null) { 53 yield return new NullReferenceToken(accessor.Name); 54 yield break; 55 } 56 57 if (obj2id.ContainsKey(value)) { 58 yield return new ReferenceToken(accessor.Name, obj2id[value]); 59 yield break; 60 } 61 52 if (value == null) 53 return NullReferenceEnumeration(accessor.Name); 54 if (obj2id.ContainsKey(value)) 55 return ReferenceTokenEnumeration(accessor.Name, obj2id[value]); 62 56 if ( ! typeCache.ContainsKey(value.GetType())) 63 57 typeCache.Add(value.GetType(), typeCache.Count); 64 58 int typeId = typeCache[value.GetType()]; 65 66 59 int? id = null; 67 60 if ( ! value.GetType().IsValueType) { … … 69 62 obj2id.Add(value, (int)id); 70 63 } 64 IFormatter formatter = persistenceConfiguration.GetFormatter(XmlFormat.Instance, value.GetType()); 65 if (formatter != null) 66 return PrimitiveEnumeration(accessor.Name, typeId, formatter.Serialize(value), id); 67 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType()); 68 if (decomposer != null) 69 return CompositeEnumeration(accessor.Name, decomposer.Serialize(value), id, typeId); 70 return StorableEnumeration(accessor.Name, value, id, typeId); 71 } 71 72 72 IFormatter formatter = persistenceConfiguration.GetFormatter(XmlFormat.Instance, value.GetType()); 73 if (formatter != null) { 74 yield return new PrimitiveToken( 75 accessor.Name, 76 typeId, 77 formatter.Serialize(value), 78 id); 79 yield break; 80 } 81 82 yield return new BeginToken(accessor.Name, typeId, id); 83 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType()); 73 private IEnumerator<ISerializationToken> NullReferenceEnumeration(string name) { 74 yield return new NullReferenceToken(name); 75 } 84 76 85 if (decomposer != null) { 86 foreach (object o in decomposer.Serialize(value)) { 77 private IEnumerator<ISerializationToken> ReferenceTokenEnumeration(string name, int id) { 78 yield return new ReferenceToken(name, id); 79 } 80 81 private IEnumerator<ISerializationToken> PrimitiveEnumeration(string name, int typeId, object serializedValue, int? id) { 82 yield return new PrimitiveToken(name, typeId, serializedValue, id); 83 } 84 85 private IEnumerator<ISerializationToken> CompositeEnumeration(string name, IEnumerable values, int? id, int typeId) { 86 yield return new BeginToken(name, typeId, id); 87 foreach (object o in values) { 87 88 IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o)); 88 89 while (iterator.MoveNext()) 89 90 yield return iterator.Current; 90 91 } 91 yield return new EndToken(accessor.Name, typeId, id); 92 yield break; 93 } 92 yield return new EndToken(name, typeId, id); 93 } 94 94 95 private IEnumerator<ISerializationToken> StorableEnumeration(string name, object value, int? id, int typeId) { 96 yield return new BeginToken(name, typeId, id); 95 97 int nSubComponents = 0; 96 98 foreach (KeyValuePair<string, DataMemberAccessor> mapping in … … 107 109 value.GetType().FullName)); 108 110 } 109 yield return new EndToken( accessor.Name, typeId, id);111 yield return new EndToken(name, typeId, id); 110 112 } 111 112 113 } 114 115 113 116 } -
branches/New Persistence Exploration/Persistence/Persistence/StorableAttribute.cs
r1348 r1357 3 3 using System.Reflection; 4 4 using System.Linq; 5 6 namespace Persistence { 5 using HeuristicLab.Persistence.Interfaces; 6 7 namespace HeuristicLab.Persistence { 7 8 8 9 [AttributeUsage( -
branches/New Persistence Exploration/Persistence/Persistence/Util.cs
r1329 r1357 4 4 using System.Reflection; 5 5 6 namespace Persistence { 6 namespace HeuristicLab.Persistence { 7 7 8 public class InterfaceInstantiatior { 8 9 public static List<T> InstantiateAll<T>() { -
branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs
r1355 r1357 2 2 using System; 3 3 using System.Text; 4 namespace Persistence { 4 using HeuristicLab.Persistence.Interfaces; 5 6 namespace HeuristicLab.Persistence { 5 7 public class XmlFormatter { 6 8 -
branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs
r1356 r1357 4 4 using System.Collections; 5 5 using System.IO; 6 using HeuristicLab.Persistence.Interfaces; 6 7 7 namespace Persistence {8 namespace HeuristicLab.Persistence { 8 9 9 10 -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1356 r1357 1 1 using System; 2 using System.Collections; 2 3 using System.Collections.Generic; 3 4 using System.IO; 4 5 namespace Persistence.Test { 5 using HeuristicLab.Persistence.Interfaces; 6 7 namespace HeuristicLab.Persistence.Test { 8 9 public class RootBase { 10 [Storable] 11 private string baseString = "Serial"; 12 } 13 14 public class Root : RootBase { 15 [Storable] 16 public int[] i = new[] { 3, 4, 5, 6 }; 17 [Storable] 18 public string s; 19 [Storable] 20 public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 }); 21 [Storable] 22 public List<int> intList = new List<int>(new[] { 321, 312, 321 }); 23 [Storable] 24 public Custom c; 25 [Storable] 26 public List<Root> selfReferences; 27 [Storable] 28 public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; 29 [Storable] 30 public bool boolean = true; 31 [Storable] 32 public DateTime dateTime; 33 [Storable] 34 public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("Serial", 123); 35 [Storable] 36 public Dictionary<string, int> dict = new Dictionary<string, int>(); 37 [Storable(DefaultValue = "default")] 38 public string uninitialized; 39 } 40 41 public class Custom { 42 [Storable] 43 public int i; 44 [Storable] 45 public Root r; 46 [Storable] 47 public string name = "Serial"; 48 } 49 50 public class CloneableRoot { 51 public int[] i = new[] { 3, 4, 5, 6 }; 52 public string s; 53 public ArrayList intArray = new ArrayList(new[] { 1, 2, 3 }); 54 public List<int> intList = new List<int>(new[] { 321, 312, 321 }); 55 public CloneableCustom c; 56 public List<CloneableRoot> selfReferences; 57 public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; 58 public bool boolean = true; 59 public DateTime dateTime; 60 public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123); 61 public Dictionary<string, int> dict = new Dictionary<string, int>(); 62 public object Clone(Dictionary<object, object> twins) { 63 if (twins.ContainsKey(this)) 64 return twins[this]; 65 CloneableRoot cr = new CloneableRoot(); 66 twins.Add(this, cr); 67 cr.i = i; 68 cr.s = s; 69 cr.intArray = new ArrayList(intArray); 70 cr.intList = new List<int>(intList); 71 cr.c = (CloneableCustom)c.Clone(twins); 72 cr.selfReferences = new List<CloneableRoot>(); 73 for (int j = 0; j < selfReferences.Count; j++) { 74 cr.selfReferences.Add(this); 75 } 76 cr.multiDimArray = (double[,])multiDimArray.Clone(); 77 cr.dateTime = new DateTime(dateTime.Ticks); 78 cr.kvp = new KeyValuePair<string, int>(kvp.Key, kvp.Value); 79 cr.dict = new Dictionary<string, int>(dict); 80 return cr; 81 } 82 } 83 84 public class CloneableCustom { 85 public int i; 86 public CloneableRoot r; 87 public string name = "Serial"; 88 public object Clone(Dictionary<object, object> twins) { 89 if (twins.ContainsKey(this)) 90 return twins[this]; 91 CloneableCustom cc = new CloneableCustom(); 92 twins.Add(this, cc); 93 cc.i = i; 94 cc.r = (CloneableRoot)r.Clone(twins); 95 cc.name = name; 96 return cc; 97 } 98 } 99 100 public class Manager { 101 102 private DateTime lastLoadTime; 103 [Storable] 104 private DateTime lastLoadTimePersistence { 105 get { return lastLoadTime; } 106 // ReSharper disable ValueParameterNotUsed 107 set { lastLoadTime = DateTime.Now; } 108 // ReSharper restore ValueParameterNotUsed 109 } 110 [Storable] 111 private double? dbl; 112 } 113 114 public class StorableObject { 115 116 [Storable] 117 Dictionary<int, string> dict; 118 119 public void Init() { 120 dict = new Dictionary<int, string>(); 121 for (int i = 0; i < 1000000; i++) { 122 dict.Add(i, i.ToString()); 123 } 124 } 125 } 126 127 public class CloneableObject : ICloneable { 128 129 Dictionary<int, string> dict; 130 131 public void Init() { 132 dict = new Dictionary<int, string>(); 133 for (int i = 0; i < 1000000; i++) { 134 dict.Add(i, i.ToString()); 135 } 136 } 137 public object Clone() { 138 CloneableObject clone = new CloneableObject { 139 dict = new Dictionary<int, string>(dict) 140 }; 141 return clone; 142 } 143 } 6 144 7 145 public class NewSerializationTest {
Note: See TracChangeset
for help on using the changeset viewer.