Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/18/09 18:13:40 (15 years ago)
Author:
epitzer
Message:

Pluginification and major refactoring. (#506)

Location:
branches/New Persistence Exploration/Persistence/Persistence
Files:
8 added
3 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs

    r1356 r1357  
    11using System.Collections.Generic;
    22using System;
     3using HeuristicLab.Persistence.Interfaces;
    34
    4 namespace Persistence {
     5namespace 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
    570
    671  public class DeSerializer {
    772
    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 
    5973    private delegate void Handler(ISerializationToken token);
     74    private delegate void Thunk();
    6075
    6176    private readonly Dictionary<int, object> id2obj;
     
    6378    private readonly Stack<IAccessibleObject> compositeStack;
    6479    private readonly PersistenceConfiguration persistenceConfiguration;
    65     private readonly Dictionary<int, Type> typeIds;
    66 
    67     delegate void Thunk();
     80    private readonly Dictionary<int, Type> typeIds;   
    6881    private List<Thunk> finalFixes;
    6982
     
    115128        id2obj.Add((int)start.Id, instance);
    116129    }
     130
    117131    private void CompositeEndHandler(ISerializationToken token) {
    118132      EndToken end = (EndToken)token;
     
    132146      }
    133147    }
     148
    134149    private void PrimitiveHandler(ISerializationToken token) {
    135150      PrimitiveToken primitive = (PrimitiveToken)token;
     
    142157      SetValue(primitive.Name, value);
    143158    }
     159
    144160    private void ReferenceHandler(ISerializationToken token) {
    145161      ReferenceToken reference = (ReferenceToken)token;
     
    152168      }
    153169    }
     170
    154171    private void NullHandler(ISerializationToken token) {
    155172      NullReferenceToken nil = (NullReferenceToken)token;
    156173      SetValue(nil.Name, null);
    157174    }
     175
    158176    private void SetValue(string name, object value) {
    159177      if (compositeStack.Count == 0) {
  • branches/New Persistence Exploration/Persistence/Persistence/Decomposers.cs

    r1354 r1357  
    33using System.Reflection;
    44using System.Collections.Generic;
    5 namespace Persistence {
     5using HeuristicLab.Persistence.Interfaces;
     6
     7namespace HeuristicLab.Persistence {
    68 
    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 
    159  public class EnumerableDecomposer : IDecomposer {
    1610
  • branches/New Persistence Exploration/Persistence/Persistence/PersistenceConfiguration.cs

    r1349 r1357  
    22using System.Collections.Generic;
    33using System.Reflection;
     4using HeuristicLab.Persistence.Interfaces;
    45
    5 namespace Persistence {
     6namespace HeuristicLab.Persistence {
    67
    78  public class PersistenceConfiguration {
  • branches/New Persistence Exploration/Persistence/Persistence/PrimitiveFormatters.cs

    r1348 r1357  
    11using System;
    2 using System.Collections;
    3 using System.Collections.Generic;
    42using System.Text;
    53using System.Globalization;
    6 namespace Persistence {
     4using HeuristicLab.Persistence.Interfaces;
    75
    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   }
     6namespace HeuristicLab.Persistence {
    187
    198  public class XmlFormat : IFormat {
     
    8574  }
    8675
    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 
    19877}
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1356 r1357  
    33using System;
    44using System.Linq;
     5using HeuristicLab.Persistence.Interfaces;
    56
    6 namespace Persistence {
     7namespace HeuristicLab.Persistence {
    78
    89  public class Serializer : IEnumerable<ISerializationToken> {
     
    4647    }
    4748
     49   
    4850    private IEnumerator<ISerializationToken> Serialize(DataMemberAccessor accessor) {
    49 
    5051      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]);             
    6256      if ( ! typeCache.ContainsKey(value.GetType()))
    6357        typeCache.Add(value.GetType(), typeCache.Count);
    6458      int typeId = typeCache[value.GetType()];
    65 
    6659      int? id = null;
    6760      if ( ! value.GetType().IsValueType) {
     
    6962        obj2id.Add(value, (int)id);
    7063      }
     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    }
    7172
    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    }
    8476
    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) {
    8788          IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o));
    8889          while (iterator.MoveNext())
    8990            yield return iterator.Current;
    9091        }
    91         yield return new EndToken(accessor.Name, typeId, id);
    92         yield break;
    93       }
     92      yield return new EndToken(name, typeId, id);       
     93    }
    9494
     95    private IEnumerator<ISerializationToken> StorableEnumeration(string name, object value, int? id, int typeId) {           
     96      yield return new BeginToken(name, typeId, id);
    9597      int nSubComponents = 0;
    9698      foreach (KeyValuePair<string, DataMemberAccessor> mapping in
     
    107109                                         value.GetType().FullName));
    108110      }
    109       yield return new EndToken(accessor.Name, typeId, id);
     111      yield return new EndToken(name, typeId, id);
    110112    }
    111    
    112113  }
     114
     115
    113116}
  • branches/New Persistence Exploration/Persistence/Persistence/StorableAttribute.cs

    r1348 r1357  
    33using System.Reflection;
    44using System.Linq;
    5 
    6 namespace Persistence {
     5using HeuristicLab.Persistence.Interfaces;
     6
     7namespace HeuristicLab.Persistence {
    78
    89  [AttributeUsage(
  • branches/New Persistence Exploration/Persistence/Persistence/Util.cs

    r1329 r1357  
    44using System.Reflection;
    55
    6 namespace Persistence { 
     6namespace HeuristicLab.Persistence { 
     7
    78  public class InterfaceInstantiatior {
    89    public static List<T> InstantiateAll<T>() {
  • branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs

    r1355 r1357  
    22using System;
    33using System.Text;
    4 namespace Persistence {
     4using HeuristicLab.Persistence.Interfaces;
     5
     6namespace HeuristicLab.Persistence {
    57  public class XmlFormatter {
    68
  • branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs

    r1356 r1357  
    44using System.Collections;
    55using System.IO;
     6using HeuristicLab.Persistence.Interfaces;
    67
    7 namespace Persistence {
     8namespace HeuristicLab.Persistence {
    89
    910
Note: See TracChangeset for help on using the changeset viewer.