Changeset 1419


Ignore:
Timestamp:
03/25/09 17:16:32 (11 years ago)
Author:
epitzer
Message:

Implement persistence of storables as decomposer. (#506)

Location:
branches/New Persistence Exploration/Persistence/Persistence
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Core/DataMemberAccessor.cs

    r1360 r1419  
    1212    public readonly Getter Get;
    1313    public readonly Setter Set;
    14     public readonly string Name;
    15     public readonly Type Type;
     14    public readonly string Name;   
    1615    public readonly object DefaultValue;
    1716
     
    2322        FieldInfo fieldInfo = (FieldInfo)memberInfo;
    2423        Get = () => fieldInfo.GetValue(obj);
    25         Set = value => fieldInfo.SetValue(obj, value);
    26         Type = fieldInfo.FieldType;
     24        Set = value => fieldInfo.SetValue(obj, value);       
    2725      } else if (memberInfo.MemberType == MemberTypes.Property) {
    2826        PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
     
    3230        }
    3331        Get = () => propertyInfo.GetValue(obj, null);
    34         Set = value => propertyInfo.SetValue(obj, value, null);
    35         Type = propertyInfo.PropertyType;
     32        Set = value => propertyInfo.SetValue(obj, value, null);       
    3633      } else {
    3734        throw new NotSupportedException(
     
    4340
    4441    public DataMemberAccessor(
    45         string name, Type type, object defaultValue,
     42        string name, object defaultValue,
    4643        Getter getter, Setter setter) {
    47       Name = name;
    48       Type = type;
     44      Name = name;     
    4945      DefaultValue = defaultValue;
    5046      Get = getter;
     
    5349
    5450    public DataMemberAccessor(object o) {
    55       Name = null;
    56       Type = o.GetType();
     51      Name = null;     
    5752      DefaultValue = null;
    5853      Get = () => o;
     
    6055    }
    6156
     57    public DataMemberAccessor(object o, string name) {
     58      Name = name;     
     59      DefaultValue = null;
     60      Get = () => o;
     61      Set = null;
     62    }
     63
     64
    6265    public override string ToString() {
    63       return String.Format("DataMember({0}, {1}, {2}, {3}, {4})",
     66      return String.Format("DataMember({0}, {2}, {3}, {4})",
    6467        Name,
    65         Type == null ? "<null>" : Type.FullName,
    6668        DefaultValue ?? "<null>",
    6769        Get.Method, Set.Method);
  • branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs

    r1362 r1419  
    33using HeuristicLab.Persistence.Interfaces;
    44
    5 namespace HeuristicLab.Persistence.Core {
    6 
     5namespace HeuristicLab.Persistence.Core { 
    76
    87  struct ParentReference { }
     
    1918
    2019    public object Obj { get; private set; }
    21     public readonly List<object> customValues;
     20    public List<Tag> customValues;
    2221
    2322    public CustomComposite(object obj) {
    2423      Obj = obj;
    25       customValues = new List<object>();
     24      customValues = new List<Tag>();
    2625    }
    2726
    28     public void AddValue(object value) {
    29       customValues.Add(value);
     27    public void AddValue(string name, object value) {
     28      customValues.Add(new Tag(name, value));
    3029    }
    3130
    32     public Setter GetSetter(string name) {
     31    public Setter GetSetter(string name) {     
    3332      int index = customValues.Count - 1;
    34       return value => customValues[index] = value;
     33      Tag t = customValues[index];     
     34      return value => t.Value = value;
    3535    }
    3636  }
     
    122122      if ( serializerMapping.ContainsKey(type) )
    123123        decomposer = serializerMapping[type] as IDecomposer;     
    124       if (decomposer != null) {
    125         instance = new ParentReference();
     124      if (decomposer != null) {       
     125        instance = decomposer.CreateInstance(type);
     126        if (instance == null)
     127          instance = new ParentReference();
    126128        parentStack.Push(new CustomComposite(instance));       
    127129      } else {       
     
    143145      if (decomposer != null) {
    144146        CustomComposite customComposite = (CustomComposite)parentStack.Pop();
    145         object deserializedObject =
    146           decomposer.Compose(customComposite.customValues, type);
     147        object deserializedObject =         
     148          decomposer.Populate(customComposite.Obj, customComposite.customValues, type);
    147149        if ( end.Id != null )
    148150          id2obj[(int)end.Id] = deserializedObject;       
     
    188190          ((StorableComposite)accessibleObject).SetValue(name, value);
    189191        } else if (accessibleObject is CustomComposite) {
    190           ((CustomComposite)accessibleObject).AddValue(value);
     192          ((CustomComposite)accessibleObject).AddValue(name, value);
    191193        }
    192194      }
  • branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs

    r1404 r1419  
    6767    public IEnumerator<ISerializationToken> GetEnumerator() {
    6868      DataMemberAccessor rootAccessor = new DataMemberAccessor(
    69         rootName, obj.GetType(), null, () => obj, null);
     69        rootName, null, () => obj, null);
    7070      IEnumerator<ISerializationToken> iterator = Serialize(rootAccessor);
    7171      while (iterator.MoveNext())
     
    110110
    111111    private IEnumerator<ISerializationToken> CompositeEnumerator(string name,
    112         IEnumerable values, int? id, int typeId) {
     112        IEnumerable<Tag> tags, int? id, int typeId) {
    113113      yield return new BeginToken(name, typeId, id);     
    114         foreach (object o in values) {
    115           IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o));
     114        foreach (var tag in tags) {
     115          IEnumerator<ISerializationToken> iterator = Serialize(           
     116            new DataMemberAccessor(tag.Value, tag.Name));
    116117          while (iterator.MoveNext())
    117118            yield return iterator.Current;
     
    132133        }
    133134      }
    134       if (nSubComponents == 0 && ! EmptyStorableClassAttribute.IsEmpyStorable(value)) {
     135      if (nSubComponents == 0 && ! EmptyStorableClassAttribute.IsEmpyStorable(value.GetType())) {
    135136        throw new ApplicationException(
    136137          String.Format(
  • branches/New Persistence Exploration/Persistence/Persistence/Core/StorableAttribute.cs

    r1404 r1419  
    1212  public class EmptyStorableClassAttribute : Attribute {
    1313    private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>();
    14     public static bool IsEmpyStorable(object o) {
    15       Type type = o.GetType();
     14    public static bool IsEmpyStorable(Type type) {     
    1615      if (emptyTypeInfo.ContainsKey(type))
    1716        return emptyTypeInfo[type];
    18       foreach (var attribute in o.GetType().GetCustomAttributes(false)) {
     17      foreach (var attribute in type.GetCustomAttributes(false)) {
    1918        EmptyStorableClassAttribute empty = attribute as EmptyStorableClassAttribute;
    2019        if (empty != null) {
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/ArrayDecomposer.cs

    r1405 r1419  
    33using HeuristicLab.Persistence.Core;
    44using HeuristicLab.Persistence.Interfaces;
     5using System.Collections.Generic;
    56
    67namespace HeuristicLab.Persistence.Default.Decomposers {
     
    1314    }
    1415
    15     public IEnumerable DeCompose(object array) {
     16    public IEnumerable<Tag> DeCompose(object array) {
    1617      Array a = (Array)array;
    17       yield return a.Rank;
     18      yield return new Tag("rank", a.Rank);
    1819      for (int i = 0; i < a.Rank; i++) {
    19         yield return a.GetLength(i);
     20        yield return new Tag("length_" + i, a.GetLength(i));
    2021      }
    2122      foreach (object o in (Array)array) {
    22         yield return o;
     23        yield return new Tag(null, o);
    2324      }
    2425    }
    2526
    26     public object Compose(IEnumerable elements, Type t) {
    27       IEnumerator e = elements.GetEnumerator();
     27    public object CreateInstance(Type t) {
     28      return null;
     29    }
     30
     31    public object Populate(object instance, IEnumerable<Tag> elements, Type t) {
     32      IEnumerator<Tag> e = elements.GetEnumerator();
    2833      e.MoveNext();
    29       int rank = (int)e.Current;
     34      int rank = (int)e.Current.Value;
    3035      int[] lengths = new int[rank];
    3136      for (int i = 0; i < rank; i++) {
    3237        e.MoveNext();
    33         lengths[i] = (int)e.Current;
     38        lengths[i] = (int)e.Current.Value;
    3439      }
    3540      Array a = Array.CreateInstance(t.GetElementType(), lengths);     
    3641      int[] positions = new int[rank];
    3742      while (e.MoveNext()) {
    38         a.SetValue(e.Current, positions);
     43        a.SetValue(e.Current.Value, positions);
    3944        positions[0] += 1;
    4045        for (int i = 0; i < rank-1; i++) {
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/DictionaryDecomposer.cs

    r1405 r1419  
    33using HeuristicLab.Persistence.Core;
    44using HeuristicLab.Persistence.Interfaces;
     5using System.Collections.Generic;
    56
    67namespace HeuristicLab.Persistence.Default.Decomposers {
     
    1314    }
    1415
    15     public IEnumerable DeCompose(object o) {
     16    public IEnumerable<Tag> DeCompose(object o) {
    1617      IDictionary dict = (IDictionary)o;     
    1718      foreach ( DictionaryEntry entry in dict) {
    18         yield return entry.Key;
    19         yield return entry.Value;
     19        yield return new Tag("key", entry.Key);
     20        yield return new Tag("value", entry.Value);
    2021      }
    2122    }
    2223
    23     public object Compose(IEnumerable o, Type t) {
    24       IDictionary dict = (IDictionary)Activator.CreateInstance(t, true);
    25       IEnumerator iter = o.GetEnumerator();
     24    public object CreateInstance(Type t) {
     25      return Activator.CreateInstance(t, true);
     26    }
     27
     28    public object Populate(object instance, IEnumerable<Tag> o, Type t) {
     29      IDictionary dict = (IDictionary)instance;
     30      IEnumerator<Tag> iter = o.GetEnumerator();
    2631      while (iter.MoveNext()) {
    27         object key = iter.Current;
     32        object key = iter.Current.Value;
    2833        iter.MoveNext();
    29         object value = iter.Current;
     34        object value = iter.Current.Value;
    3035        dict.Add(key, value);
    3136      }
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/EnumerableDecomposer.cs

    r1405 r1419  
    44using HeuristicLab.Persistence.Core;
    55using HeuristicLab.Persistence.Interfaces;
     6using System.Collections.Generic;
    67
    78namespace HeuristicLab.Persistence.Default.Decomposers {
     
    2021          BindingFlags.Instance,
    2122          null, Type.EmptyTypes, null) != null;       
    22     }   
    23 
    24     public IEnumerable DeCompose(object obj) {
    25       return (IEnumerable)obj;
    2623    }
    2724
    28     public object Compose(IEnumerable objects, Type type) {
    29       object instance = Activator.CreateInstance(type, true);
     25    public IEnumerable<Tag> DeCompose(object obj) {
     26      foreach (object o in (IEnumerable)obj) {
     27        yield return new Tag(null, o);
     28      }
     29    }
     30
     31    public object CreateInstance(Type type) {
     32      return Activator.CreateInstance(type, true);     
     33    }
     34
     35    public object Populate(object instance, IEnumerable<Tag> objects, Type type) {
    3036      MethodInfo addMethod = type.GetMethod("Add");
    31       foreach (object o in objects) {
    32         addMethod.Invoke(instance, new[] {o});       
     37      foreach (var pair in objects) {
     38        addMethod.Invoke(instance, new[] {pair.Value});
    3339      }
    3440      return instance;
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/KeyValuePairDecomposer.cs

    r1405 r1419  
    11using System;
     2using System.Linq;
    23using System.Collections;
    34using System.Collections.Generic;
     
    1617    }
    1718
    18     public IEnumerable DeCompose(object o) {     
     19    public IEnumerable<Tag> DeCompose(object o) {     
    1920      Type t = o.GetType();
    20       yield return t.GetProperty("Key").GetValue(o, null);
    21       yield return t.GetProperty("Value").GetValue(o, null);
     21      yield return new Tag("key", t.GetProperty("Key").GetValue(o, null));
     22      yield return new Tag("value", t.GetProperty("Value").GetValue(o, null));
    2223    }
    2324
    24     public object Compose(IEnumerable o, Type t) {     
    25       return Activator.CreateInstance(t,
    26         new List<object>((IEnumerable<object>)o).ToArray());
     25    public object CreateInstance(Type type) {
     26      return null;
     27    }
     28
     29    public object Populate(object instance, IEnumerable<Tag> o, Type t) {
     30      IEnumerator<Tag> iter = o.GetEnumerator();
     31      iter.MoveNext();
     32      object key = iter.Current.Value;
     33      iter.MoveNext();
     34      object value = iter.Current.Value;
     35      return Activator.CreateInstance(t, new object[] { key, value });     
    2736    }   
    2837  }
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Decomposers/TypeDecomposer.cs

    r1408 r1419  
    33using HeuristicLab.Persistence.Core;
    44using HeuristicLab.Persistence.Interfaces;
     5using System.Collections.Generic;
    56
    67namespace HeuristicLab.Persistence.Default.Decomposers {
     
    1314    }
    1415
    15     public IEnumerable DeCompose(object obj) {
     16    public IEnumerable<Tag> DeCompose(object obj) {
    1617      Type t = (Type) obj;
    17       yield return t.AssemblyQualifiedName;
     18      yield return new Tag("AssemblyQualifiedName", t.AssemblyQualifiedName);
    1819    }
    1920
    20     public object Compose(IEnumerable objects, Type type) {
    21       foreach ( string typeName in objects ) {
    22         return Type.GetType(typeName);
     21    public object CreateInstance(Type type) {
     22      return null;
     23    }
     24
     25    public object Populate(object instance, IEnumerable<Tag> objects, Type type) {
     26      foreach ( var typeName in objects ) {
     27        return Type.GetType((string)typeName.Value);
    2328      }
    2429      return null;
  • branches/New Persistence Exploration/Persistence/Persistence/HeuristicLab.Persistence.csproj

    r1406 r1419  
    6161    <Compile Include="Default\Decomposers\KeyValuePairDecomposer.cs" />
    6262    <Compile Include="Default\Decomposers\DictionaryDecomposer.cs" />
     63    <Compile Include="Default\Decomposers\StorableDecomposer.cs" />
    6364    <Compile Include="Default\Decomposers\TypeDecomposer.cs" />
    6465    <Compile Include="Default\Xml\Compact\IntArray2XmlFormatters.cs" />
  • branches/New Persistence Exploration/Persistence/Persistence/Interfaces/IDecomposer.cs

    r1360 r1419  
    11using System;
    22using System.Collections;
     3using System.Collections.Generic;
    34
    45namespace HeuristicLab.Persistence.Interfaces {
    56
    6   public interface IDecomposer {
    7     bool CanDecompose(Type type);
    8     IEnumerable DeCompose(object obj);
    9     object Compose(IEnumerable objects, Type type);
     7  public struct Tag {
     8    public string Name;
     9    public object Value;
     10    public Tag(string name, object value) {
     11      Name = name;
     12      Value = value;
     13    }
     14    public Tag(object value) {
     15      Name = null;
     16      Value = value;
     17    }
    1018  }
    1119
     20  public interface IDecomposer {
     21
     22    /// <summary>
     23    /// Determines for every type whether the decomposer is applicable.
     24    /// </summary>   
     25    bool CanDecompose(Type type);
     26
     27    /// <summary>
     28    /// Decompose an object into KeyValuePairs, the Key can be null,
     29    /// the order in which elements are generated is guaranteed to be
     30    /// the same as they are supplied in the Compose method.
     31    /// </summary>   
     32    IEnumerable<Tag> DeCompose(object obj);
     33
     34    /// <summary>
     35    /// Create an instance of the object if possible. May return null
     36    /// in which case the Populate method must create the instance.
     37    /// </summary>
     38    /// <param name="type"></param>
     39    /// <returns></returns>
     40    object CreateInstance(Type type);
     41
     42    /// <summary>
     43    /// Compose an object from the KeyValuePairs previously generated
     44    /// in DeCompose. The order in which the values are supplied is
     45    /// the same as they where generated. Keys might be null.
     46    /// </summary>   
     47    object Populate(object instance, IEnumerable<Tag> tags, Type type);
     48  } 
     49
    1250}
Note: See TracChangeset for help on using the changeset viewer.