source: branches/CloningRefactoring/HeuristicLab.Problems.ArtificialAnt/3.3/AntTrail.cs @ 4676

Last change on this file since 4676 was 4676, checked in by mkommend, 9 years ago

Refactored ArtificialAntProble (ticket #922).

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.ArtificialAnt {
31  /// <summary>
32  /// Represents a trail of an artificial ant which can be visualized in the GUI.
33  /// </summary>
34  [Item("AntTrail", "Represents a trail of an artificial ant which can be visualized in the GUI.")]
35  [StorableClass]
36  public sealed class AntTrail : Item {
37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Image; }
39    }
40    [Storable]
41    private SymbolicExpressionTree expression;
42    public SymbolicExpressionTree SymbolicExpressionTree {
43      get { return expression; }
44      set {
45        if (expression != value) {
46          //if (expression != null) DeregisterSymbolicExpressionTreeEvents();
47          expression = value;
48          //if (expression != null) RegisterSymbolicExpressionTreeEvents();
49          OnSymbolicExpressionTreeChanged();
50        }
51      }
52    }
53
54    [Storable]
55    private BoolMatrix world;
56    public BoolMatrix World {
57      get { return world; }
58      set {
59        if (world != value) {
60          if (world != null) DeregisterWorldEvents();
61          world = value;
62          if (world != null) RegisterWorldEvents();
63          OnWorldChanged();
64        }
65      }
66    }
67    [Storable]
68    private IntValue maxTimeSteps;
69    public IntValue MaxTimeSteps {
70      get { return maxTimeSteps; }
71      set {
72        if (maxTimeSteps != value) {
73          if (maxTimeSteps != value) {
74            if (maxTimeSteps != null) DeregisterMaxTimeStepsEvents();
75            maxTimeSteps = value;
76            if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
77            OnWorldChanged();
78          }
79        }
80      }
81    }
82
83    public AntTrail() : base() { }
84    public AntTrail(BoolMatrix world, SymbolicExpressionTree expression, IntValue maxTimeSteps)
85      : this() {
86      this.world = world;
87      this.expression = expression;
88      this.maxTimeSteps = maxTimeSteps;
89      Initialize();
90    }
91
92    [StorableConstructor]
93    private AntTrail(bool deserializing) : base(deserializing) { }
94    [StorableHook(HookType.AfterDeserialization)]
95    private void AfterDeserialization() {
96      Initialize();
97    }
98    private AntTrail(AntTrail original, Cloner cloner)
99      : base(original, cloner) {
100      expression = cloner.Clone(original.expression);
101      world = cloner.Clone(original.world);
102      maxTimeSteps = cloner.Clone(original.maxTimeSteps);
103      Initialize();
104    }
105    public override IDeepCloneable Clone(Cloner cloner) {
106      return new AntTrail(this, cloner);
107    }
108
109    private void Initialize() {
110      //if (expression != null) RegisterSymbolicExpressionTreeEvents();
111      if (world != null) RegisterWorldEvents();
112      if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
113    }
114
115    #region Events
116    public event EventHandler SymbolicExpressionTreeChanged;
117    private void OnSymbolicExpressionTreeChanged() {
118      var changed = SymbolicExpressionTreeChanged;
119      if (changed != null) changed(this, EventArgs.Empty);
120    }
121    public event EventHandler WorldChanged;
122    private void OnWorldChanged() {
123      var changed = WorldChanged;
124      if (changed != null) changed(this, EventArgs.Empty);
125    }
126    public event EventHandler MaxTimeStepsChanged;
127    private void OnMaxTimeStepsChanged() {
128      var changed = MaxTimeStepsChanged;
129      if (changed != null) changed(this, EventArgs.Empty);
130    }
131
132    //private void RegisterSymbolicExpressionTreeEvents() {
133    //  SymbolicExpressionTree.ItemChanged += new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
134    //  SymbolicExpressionTree.Reset += new EventHandler(SymbolicExpressionTree_Reset);
135    //}
136    //private void DeregisterSymbolicExpressionTreeEvents() {
137    //  SymbolicExpressionTree.ItemChanged -= new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
138    //  SymbolicExpressionTree.Reset -= new EventHandler(SymbolicExpressionTree_Reset);
139    //}
140
141    private void RegisterWorldEvents() {
142      World.ItemChanged += new EventHandler<EventArgs<int, int>>(World_ItemChanged);
143      World.Reset += new EventHandler(World_Reset);
144    }
145    private void DeregisterWorldEvents() {
146      World.ItemChanged -= new EventHandler<EventArgs<int, int>>(World_ItemChanged);
147      World.Reset -= new EventHandler(World_Reset);
148    }
149    private void RegisterMaxTimeStepsEvents() {
150      MaxTimeSteps.ValueChanged += new EventHandler(MaxTimeSteps_ValueChanged);
151    }
152    private void DeregisterMaxTimeStepsEvents() {
153      MaxTimeSteps.ValueChanged -= new EventHandler(MaxTimeSteps_ValueChanged);
154    }
155
156    void MaxTimeSteps_ValueChanged(object sender, EventArgs e) {
157      OnMaxTimeStepsChanged();
158    }
159    private void World_ItemChanged(object sender, EventArgs<int, int> e) {
160      OnWorldChanged();
161    }
162    private void World_Reset(object sender, EventArgs e) {
163      OnWorldChanged();
164    }
165    #endregion
166  }
167}
Note: See TracBrowser for help on using the repository browser.