Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Problems.ArtificialAnt/3.4/AntTrail.cs @ 13828

Last change on this file since 13828 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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
37    [Storable]
38    private SymbolicExpressionTree expression;
39    public SymbolicExpressionTree SymbolicExpressionTree
40    {
41      get { return expression; }
42      set
43      {
44        if (expression != value) {
45          //if (expression != null) DeregisterSymbolicExpressionTreeEvents();
46          expression = value;
47          //if (expression != null) RegisterSymbolicExpressionTreeEvents();
48          OnSymbolicExpressionTreeChanged();
49        }
50      }
51    }
52
53    [Storable]
54    private BoolMatrix world;
55    public BoolMatrix World
56    {
57      get { return world; }
58      set
59      {
60        if (world != value) {
61          if (world != null) DeregisterWorldEvents();
62          world = value;
63          if (world != null) RegisterWorldEvents();
64          OnWorldChanged();
65        }
66      }
67    }
68    [Storable]
69    private IntValue maxTimeSteps;
70    public IntValue MaxTimeSteps
71    {
72      get { return maxTimeSteps; }
73      set
74      {
75        if (maxTimeSteps != value) {
76          if (maxTimeSteps != value) {
77            if (maxTimeSteps != null) DeregisterMaxTimeStepsEvents();
78            maxTimeSteps = value;
79            if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
80            OnWorldChanged();
81          }
82        }
83      }
84    }
85
86    public AntTrail() : base() { }
87    public AntTrail(BoolMatrix world, SymbolicExpressionTree expression, IntValue maxTimeSteps)
88      : this() {
89      this.world = world;
90      this.expression = expression;
91      this.maxTimeSteps = maxTimeSteps;
92      Initialize();
93    }
94
95    [StorableConstructor]
96    private AntTrail(bool deserializing) : base(deserializing) { }
97    [StorableHook(HookType.AfterDeserialization)]
98    private void AfterDeserialization() {
99      Initialize();
100    }
101    private AntTrail(AntTrail original, Cloner cloner)
102      : base(original, cloner) {
103      expression = cloner.Clone(original.expression);
104      world = cloner.Clone(original.world);
105      maxTimeSteps = cloner.Clone(original.maxTimeSteps);
106      Initialize();
107    }
108    public override IDeepCloneable Clone(Cloner cloner) {
109      return new AntTrail(this, cloner);
110    }
111
112    private void Initialize() {
113      //if (expression != null) RegisterSymbolicExpressionTreeEvents();
114      if (world != null) RegisterWorldEvents();
115      if (maxTimeSteps != null) RegisterMaxTimeStepsEvents();
116    }
117
118    #region Events
119    public event EventHandler SymbolicExpressionTreeChanged;
120    private void OnSymbolicExpressionTreeChanged() {
121      var changed = SymbolicExpressionTreeChanged;
122      if (changed != null) changed(this, EventArgs.Empty);
123    }
124    public event EventHandler WorldChanged;
125    private void OnWorldChanged() {
126      var changed = WorldChanged;
127      if (changed != null) changed(this, EventArgs.Empty);
128    }
129    public event EventHandler MaxTimeStepsChanged;
130    private void OnMaxTimeStepsChanged() {
131      var changed = MaxTimeStepsChanged;
132      if (changed != null) changed(this, EventArgs.Empty);
133    }
134
135    //private void RegisterSymbolicExpressionTreeEvents() {
136    //  SymbolicExpressionTree.ItemChanged += new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
137    //  SymbolicExpressionTree.Reset += new EventHandler(SymbolicExpressionTree_Reset);
138    //}
139    //private void DeregisterSymbolicExpressionTreeEvents() {
140    //  SymbolicExpressionTree.ItemChanged -= new EventHandler<EventArgs<int>>(SymbolicExpressionTree_ItemChanged);
141    //  SymbolicExpressionTree.Reset -= new EventHandler(SymbolicExpressionTree_Reset);
142    //}
143
144    private void RegisterWorldEvents() {
145      World.ItemChanged += new EventHandler<EventArgs<int, int>>(World_ItemChanged);
146      World.Reset += new EventHandler(World_Reset);
147    }
148    private void DeregisterWorldEvents() {
149      World.ItemChanged -= new EventHandler<EventArgs<int, int>>(World_ItemChanged);
150      World.Reset -= new EventHandler(World_Reset);
151    }
152    private void RegisterMaxTimeStepsEvents() {
153      MaxTimeSteps.ValueChanged += new EventHandler(MaxTimeSteps_ValueChanged);
154    }
155    private void DeregisterMaxTimeStepsEvents() {
156      MaxTimeSteps.ValueChanged -= new EventHandler(MaxTimeSteps_ValueChanged);
157    }
158
159    void MaxTimeSteps_ValueChanged(object sender, EventArgs e) {
160      OnMaxTimeStepsChanged();
161    }
162    private void World_ItemChanged(object sender, EventArgs<int, int> e) {
163      OnWorldChanged();
164    }
165    private void World_Reset(object sender, EventArgs e) {
166      OnWorldChanged();
167    }
168    #endregion
169  }
170}
Note: See TracBrowser for help on using the repository browser.