Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3239 was 3239, checked in by gkronber, 13 years ago

Extracted view for artificial ant problem into a separate plugin/project. #952 (Artificial Ant Problem for 3.3)

File size: 5.4 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    private SymbolicExpressionTree expression;
37    [Storable]
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    private BoolMatrix world;
51    [Storable]
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    private IntValue maxTimeSteps;
64    [Storable]
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      World = world;
83      SymbolicExpressionTree = expression;
84      MaxTimeSteps = maxTimeSteps;
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      AntTrail clone = new AntTrail();
89      cloner.RegisterClonedObject(this, clone);
90      clone.expression = (SymbolicExpressionTree)cloner.Clone(expression);
91      clone.world = (BoolMatrix)cloner.Clone(world);
92      clone.maxTimeSteps = (IntValue)cloner.Clone(maxTimeSteps);
93      return clone;
94    }
95
96    #region Events
97    public event EventHandler SymbolicExpressionTreeChanged;
98    private void OnSymbolicExpressionTreeChanged() {
99      var changed = SymbolicExpressionTreeChanged;
100      if (changed != null)
101        changed(this, EventArgs.Empty);
102    }
103    public event EventHandler WorldChanged;
104    private void OnWorldChanged() {
105      var changed = WorldChanged;
106      if (changed != null)
107        changed(this, EventArgs.Empty);
108    }
109    public event EventHandler MaxTimeStepsChanged;
110    private void OnMaxTimeStepsChanged() {
111      var changed = MaxTimeStepsChanged;
112      if (changed != null)
113        changed(this, EventArgs.Empty);
114    }
115
116    //private void RegisterSymbolicExpressionTreeEvents() {
117    //  SymbolicExpressionTree.ItemChanged += new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
118    //  SymbolicExpressionTree.Reset += new EventHandler(SymbolicExpressionTree_Reset);
119    //}
120    //private void DeregisterSymbolicExpressionTreeEvents() {
121    //  SymbolicExpressionTree.ItemChanged -= new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
122    //  SymbolicExpressionTree.Reset -= new EventHandler(SymbolicExpressionTree_Reset);
123    //}
124
125    private void RegisterWorldEvents() {
126      World.ItemChanged += new EventHandler<EventArgs<int, int>>(World_ItemChanged);
127      World.Reset += new EventHandler(World_Reset);
128    }
129    private void DeregisterWorldEvents() {
130      World.ItemChanged -= new EventHandler<EventArgs<int, int>>(World_ItemChanged);
131      World.Reset -= new EventHandler(World_Reset);
132    }
133    private void RegisterMaxTimeStepsEvents() {
134      MaxTimeSteps.ValueChanged += new EventHandler(MaxTimeSteps_ValueChanged);
135    }
136    private void DeregisterMaxTimeStepsEvents() {
137      MaxTimeSteps.ValueChanged -= new EventHandler(MaxTimeSteps_ValueChanged);
138    }
139
140    void MaxTimeSteps_ValueChanged(object sender, EventArgs e) {
141      OnMaxTimeStepsChanged();
142    }
143    private void World_ItemChanged(object sender, EventArgs<int, int> e) {
144      OnWorldChanged();
145    }
146    private void World_Reset(object sender, EventArgs e) {
147      OnWorldChanged();
148    }
149    #endregion
150  }
151}
Note: See TracBrowser for help on using the repository browser.