Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6103


Ignore:
Timestamp:
05/03/11 11:04:02 (13 years ago)
Author:
cneumuel
Message:

#1424

  • created extension method GetObjectGraphObjects for objects
  • created IStatefulItem interface, which means an object has a state which can be initialized and cleared
  • after an algorithm stopped, all objects of the algorithm-objectgraph with the type IStatefulItem are cleared
  • on prepare, all IStatefulItem objects are initialized
Location:
trunk/sources
Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Common/3.3/HeuristicLab.Common-3.3.csproj

    r5837 r6103  
    121121    <Compile Include="Content\IStorableContent.cs" />
    122122    <Compile Include="Constants.cs" />
     123    <Compile Include="ObjectExtensions.cs" />
    123124    <Compile Include="DeepCloneable.cs" />
    124125    <Compile Include="Content\IContent.cs" />
  • trunk/sources/HeuristicLab.Common/3.3/TypeExtensions.cs

    r5445 r6103  
    2121
    2222using System;
     23using System.Collections.Generic;
     24using System.Reflection;
    2325using System.Text;
    2426
     
    4547      return sb.ToString();
    4648    }
     49    public static IEnumerable<FieldInfo> GetAllFields(this Type type) {
     50      foreach(var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
     51        yield return field;
     52
     53      foreach (var field in type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic))
     54        yield return field;
     55     
     56      if (type.BaseType != null) {
     57        foreach (var field in type.BaseType.GetAllFields())
     58          yield return field;
     59      }
     60    }
     61    // http://stackoverflow.com/questions/457676/c-reflection-check-if-a-class-is-derived-from-a-generic-class
     62    public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic) {
     63      while (toCheck != typeof(object)) {
     64        var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
     65        if (generic == cur) {
     66          return true;
     67        }
     68        toCheck = toCheck.BaseType; // baseType is null when toCheck is an Interface
     69        if (toCheck == null)
     70          return false;
     71      }
     72      return false;
     73    }
    4774  }
    4875}
  • trunk/sources/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj

    r5809 r6103  
    147147    <Compile Include="Interfaces\IMultiOperator.cs" />
    148148    <Compile Include="Interfaces\IFixedValueParameter.cs" />
     149    <Compile Include="Interfaces\IStatefulItem.cs" />
    149150    <Compile Include="OperatorExecutionException.cs" />
    150151    <Compile Include="Interfaces\IScopeTreeLookupParameter.cs" />
  • trunk/sources/HeuristicLab.Operators/3.3/Operator.cs

    r5445 r6103  
    3333  [Item("Operator", "Base class for operators.")]
    3434  [StorableClass]
    35   public abstract class Operator : ParameterizedNamedItem, IOperator {
     35  public abstract class Operator : ParameterizedNamedItem, IOperator, IStatefulItem {
    3636    public override Image ItemImage {
    3737      get {
     
    7474    protected Operator(bool deserializing)
    7575      : base(deserializing) {
    76       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     76      InitializeState();
    7777    }
    7878    protected Operator(Operator original, Cloner cloner)
    7979      : base(original, cloner) {
    80       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     80      InitializeState();
    8181      this.breakpoint = original.breakpoint;
    8282    }
    8383    protected Operator()
    8484      : base() {
    85       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     85      InitializeState();
    8686      breakpoint = false;
    8787    }
    8888    protected Operator(string name)
    8989      : base(name) {
    90       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     90      InitializeState();
    9191      breakpoint = false;
    9292    }
    9393    protected Operator(string name, ParameterCollection parameters)
    9494      : base(name, parameters) {
    95       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     95      InitializeState();
    9696      breakpoint = false;
    9797    }
    9898    protected Operator(string name, string description)
    9999      : base(name, description) {
    100       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     100      InitializeState();
    101101      breakpoint = false;
    102102    }
    103103    protected Operator(string name, string description, ParameterCollection parameters)
    104104      : base(name, description, parameters) {
     105      InitializeState();
     106      breakpoint = false;
     107    }
     108
     109    public virtual void InitializeState() {
    105110      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    106       breakpoint = false;
     111    }
     112    public virtual void ClearState() {
     113      executionContexts = null;
    107114    }
    108115
  • trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs

    r5954 r6103  
    261261    protected virtual void OnPrepared() {
    262262      ExecutionTime = TimeSpan.Zero;
     263      foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects().OfType<IStatefulItem>()) {
     264        statefulObject.InitializeState();
     265      }
    263266      ExecutionState = ExecutionState.Prepared;
    264267      EventHandler handler = Prepared;
     
    280283    protected virtual void OnStopped() {
    281284      ExecutionState = ExecutionState.Stopped;
     285      foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects().OfType<IStatefulItem>()) {
     286        statefulObject.ClearState();
     287      }
    282288      runsCounter++;
    283289      runs.Add(new Run(string.Format("{0} Run {1}", Name, runsCounter), this));
  • trunk/sources/HeuristicLab.Parameters/3.3/Parameter.cs

    r5768 r6103  
    3333  [Item("Parameter", "A base class for parameters.")]
    3434  [StorableClass]
    35   public abstract class Parameter : NamedItem, IParameter {
     35  public abstract class Parameter : NamedItem, IParameter, IStatefulItem {
    3636    public override Image ItemImage {
    3737      get {
     
    9292    protected Parameter(bool deserializing)
    9393      : base(deserializing) {
    94       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    95       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     94      InitializeState();
    9695    }
    9796    protected Parameter(Parameter original, Cloner cloner)
     
    9998      dataType = original.dataType;
    10099      hidden = original.hidden;
    101       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    102       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     100      InitializeState();
    103101    }
    104102    protected Parameter()
     
    106104      dataType = typeof(IItem);
    107105      hidden = false;
    108       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    109       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     106      InitializeState();
    110107    }
    111108    protected Parameter(string name, Type dataType)
     
    114111      this.dataType = dataType;
    115112      hidden = false;
    116       cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    117       executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     113      InitializeState();
    118114    }
    119115    protected Parameter(string name, string description, Type dataType)
     
    122118      this.dataType = dataType;
    123119      hidden = false;
     120      InitializeState();
     121    }
     122
     123    public virtual void InitializeState() {
    124124      cachedActualValues = new Lazy<ThreadLocal<IItem>>(() => { return new ThreadLocal<IItem>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
    125125      executionContexts = new Lazy<ThreadLocal<IExecutionContext>>(() => { return new ThreadLocal<IExecutionContext>(); }, LazyThreadSafetyMode.ExecutionAndPublication);
     126    }
     127    public virtual void ClearState() {
     128      cachedActualValues = null;
     129      executionContexts = null;
    126130    }
    127131
Note: See TracChangeset for help on using the changeset viewer.