Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntTrail.cs @ 4203

Last change on this file since 4203 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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    [StorableConstructor]
92    private AntTrail(bool deserializing) : base(deserializing) { }
93
94    [StorableHook(HookType.AfterDeserialization)]
95    private void Initialize() {
96      //if (expression != null) RegisterSymbolicExpressionTreeEvents();
97      if (world != null) RegisterWorldEvents();
98      if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
99    }
100
101    public override IDeepCloneable Clone(Cloner cloner) {
102      AntTrail clone = new AntTrail();
103      cloner.RegisterClonedObject(this, clone);
104      clone.expression = (SymbolicExpressionTree)cloner.Clone(expression);
105      clone.world = (BoolMatrix)cloner.Clone(world);
106      clone.maxTimeSteps = (IntValue)cloner.Clone(maxTimeSteps);
107      clone.Initialize();
108      return clone;
109    }
110
111    #region Events
112    public event EventHandler SymbolicExpressionTreeChanged;
113    private void OnSymbolicExpressionTreeChanged() {
114      var changed = SymbolicExpressionTreeChanged;
115      if (changed != null)
116        changed(this, EventArgs.Empty);
117    }
118    public event EventHandler WorldChanged;
119    private void OnWorldChanged() {
120      var changed = WorldChanged;
121      if (changed != null)
122        changed(this, EventArgs.Empty);
123    }
124    public event EventHandler MaxTimeStepsChanged;
125    private void OnMaxTimeStepsChanged() {
126      var changed = MaxTimeStepsChanged;
127      if (changed != null)
128        changed(this, EventArgs.Empty);
129    }
130
131    //private void RegisterSymbolicExpressionTreeEvents() {
132    //  SymbolicExpressionTree.ItemChanged += new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
133    //  SymbolicExpressionTree.Reset += new EventHandler(SymbolicExpressionTree_Reset);
134    //}
135    //private void DeregisterSymbolicExpressionTreeEvents() {
136    //  SymbolicExpressionTree.ItemChanged -= new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
137    //  SymbolicExpressionTree.Reset -= new EventHandler(SymbolicExpressionTree_Reset);
138    //}
139
140    private void RegisterWorldEvents() {
141      World.ItemChanged += new EventHandler<EventArgs<int, int>>(World_ItemChanged);
142      World.Reset += new EventHandler(World_Reset);
143    }
144    private void DeregisterWorldEvents() {
145      World.ItemChanged -= new EventHandler<EventArgs<int, int>>(World_ItemChanged);
146      World.Reset -= new EventHandler(World_Reset);
147    }
148    private void RegisterMaxTimeStepsEvents() {
149      MaxTimeSteps.ValueChanged += new EventHandler(MaxTimeSteps_ValueChanged);
150    }
151    private void DeregisterMaxTimeStepsEvents() {
152      MaxTimeSteps.ValueChanged -= new EventHandler(MaxTimeSteps_ValueChanged);
153    }
154
155    void MaxTimeSteps_ValueChanged(object sender, EventArgs e) {
156      OnMaxTimeStepsChanged();
157    }
158    private void World_ItemChanged(object sender, EventArgs<int, int> e) {
159      OnWorldChanged();
160    }
161    private void World_Reset(object sender, EventArgs e) {
162      OnWorldChanged();
163    }
164    #endregion
165  }
166}
Note: See TracBrowser for help on using the repository browser.