Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3659 was 3431, checked in by swagner, 14 years ago

Removed property ReadOnlyView (#969)

File size: 5.8 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28
29namespace HeuristicLab.Problems.ArtificialAnt {
30  /// <summary>
31  /// Represents a trail of an artificial ant which can be visualized in the GUI.
32  /// </summary>
33  [Item("AntTrail", "Represents a trail of an artificial ant which can be visualized in the GUI.")]
34  [StorableClass]
35  public sealed class AntTrail : Item {
36    [Storable]
37    private SymbolicExpressionTree expression;
38    public SymbolicExpressionTree SymbolicExpressionTree {
39      get { return expression; }
40      set {
41        if (expression != value) {
42          //if (expression != null) DeregisterSymbolicExpressionTreeEvents();
43          expression = value;
44          //if (expression != null) RegisterSymbolicExpressionTreeEvents();
45          OnSymbolicExpressionTreeChanged();
46        }
47      }
48    }
49
50    [Storable]
51    private BoolMatrix world;
52    public BoolMatrix World {
53      get { return world; }
54      set {
55        if (world != value) {
56          if (world != null) DeregisterWorldEvents();
57          world = value;
58          if (world != null) RegisterWorldEvents();
59          OnWorldChanged();
60        }
61      }
62    }
63    [Storable]
64    private IntValue maxTimeSteps;
65    public IntValue MaxTimeSteps {
66      get { return maxTimeSteps; }
67      set {
68        if (maxTimeSteps != value) {
69          if (maxTimeSteps != value) {
70            if (maxTimeSteps != null) DeregisterMaxTimeStepsEvents();
71            maxTimeSteps = value;
72            if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
73            OnWorldChanged();
74          }
75        }
76      }
77    }
78
79    public AntTrail() : base() { }
80    public AntTrail(BoolMatrix world, SymbolicExpressionTree expression, IntValue maxTimeSteps)
81      : this() {
82      this.world = world;
83      this.expression = expression;
84      this.maxTimeSteps = maxTimeSteps;
85      Initialize();
86    }
87    [StorableConstructor]
88    private AntTrail(bool deserializing) : base(deserializing) { }
89
90    [StorableHook(HookType.AfterDeserialization)]
91    private void Initialize() {
92      //if (expression != null) RegisterSymbolicExpressionTreeEvents();
93      if (world != null) RegisterWorldEvents();
94      if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      AntTrail clone = new AntTrail();
99      cloner.RegisterClonedObject(this, clone);
100      clone.expression = (SymbolicExpressionTree)cloner.Clone(expression);
101      clone.world = (BoolMatrix)cloner.Clone(world);
102      clone.maxTimeSteps = (IntValue)cloner.Clone(maxTimeSteps);
103      clone.Initialize();
104      return clone;
105    }
106
107    #region Events
108    public event EventHandler SymbolicExpressionTreeChanged;
109    private void OnSymbolicExpressionTreeChanged() {
110      var changed = SymbolicExpressionTreeChanged;
111      if (changed != null)
112        changed(this, EventArgs.Empty);
113    }
114    public event EventHandler WorldChanged;
115    private void OnWorldChanged() {
116      var changed = WorldChanged;
117      if (changed != null)
118        changed(this, EventArgs.Empty);
119    }
120    public event EventHandler MaxTimeStepsChanged;
121    private void OnMaxTimeStepsChanged() {
122      var changed = MaxTimeStepsChanged;
123      if (changed != null)
124        changed(this, EventArgs.Empty);
125    }
126
127    //private void RegisterSymbolicExpressionTreeEvents() {
128    //  SymbolicExpressionTree.ItemChanged += new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
129    //  SymbolicExpressionTree.Reset += new EventHandler(SymbolicExpressionTree_Reset);
130    //}
131    //private void DeregisterSymbolicExpressionTreeEvents() {
132    //  SymbolicExpressionTree.ItemChanged -= new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
133    //  SymbolicExpressionTree.Reset -= new EventHandler(SymbolicExpressionTree_Reset);
134    //}
135
136    private void RegisterWorldEvents() {
137      World.ItemChanged += new EventHandler<EventArgs<int, int>>(World_ItemChanged);
138      World.Reset += new EventHandler(World_Reset);
139    }
140    private void DeregisterWorldEvents() {
141      World.ItemChanged -= new EventHandler<EventArgs<int, int>>(World_ItemChanged);
142      World.Reset -= new EventHandler(World_Reset);
143    }
144    private void RegisterMaxTimeStepsEvents() {
145      MaxTimeSteps.ValueChanged += new EventHandler(MaxTimeSteps_ValueChanged);
146    }
147    private void DeregisterMaxTimeStepsEvents() {
148      MaxTimeSteps.ValueChanged -= new EventHandler(MaxTimeSteps_ValueChanged);
149    }
150
151    void MaxTimeSteps_ValueChanged(object sender, EventArgs e) {
152      OnMaxTimeStepsChanged();
153    }
154    private void World_ItemChanged(object sender, EventArgs<int, int> e) {
155      OnWorldChanged();
156    }
157    private void World_Reset(object sender, EventArgs e) {
158      OnWorldChanged();
159    }
160    #endregion
161  }
162}
Note: See TracBrowser for help on using the repository browser.