forked from kyordhel/GPSRCmdGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Category.cs
240 lines (212 loc) · 6.56 KB
/
Category.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
namespace GPSRCmdGen
{
/// <summary>
/// Represents a real-wolrd object category
/// according to the RoboCup@Home Rulebook 2015
/// </summary>
[Serializable]
public class Category : INameable
{
#region Variables
/// <summary>
/// Stores the default placement default location for objects in the category
/// </summary>
protected SpecificLocation defaultLocation;
/// <summary>
/// Stores the list of objects in the category
/// </summary>
protected Dictionary<string, GPSRObject> objects;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Category"/> class.
/// </summary>
/// <remarks>Intended for serialization purposes</remarks>
public Category() : this("Unknown objects", null){
}
/// <summary>
/// Initializes a new instance of the <see cref="GPSRCmdGen.Category"/> class.
/// </summary>
/// <param name="name">The name of the category.</param>
/// <param name="defaultLocation">The default placement location for objects in the category.</param>
public Category(string name, SpecificLocation defaultLocation){
this.Name = name;
this.defaultLocation = defaultLocation;
this.objects = new Dictionary<string, GPSRObject>();
}
#endregion
#region Properties
/// <summary>
/// Returns the number of objects in the category
/// </summary>
public int ObjectCount { get { return this.objects.Count; } }
/// <summary>
/// Gets the name of the Category
/// </summary>
[XmlAttribute("name")]
public string Name{get;set;}
/// <summary>
/// Gets or sets the name of the default location for objects in the category
/// </summary>
/// <remarks>Use for (de)serialization purposes only</remarks>
[XmlAttribute("defaultLocation")]
public string LocationString {
get {
if (this.defaultLocation == null)
return null;
return this.defaultLocation.Name;
}
set {
if (this.defaultLocation == null)
this.defaultLocation = new SpecificLocation(value, true, false);
else
this.defaultLocation.Name = value;
}
}
/// <summary>
/// Gets or sets the name of the default location's room for objects in the category
/// </summary>
/// <remarks>Use for (de)serialization purposes only</remarks>
[XmlAttribute("room")]
public string RoomString
{
get
{
if (this.defaultLocation == null)
return null;
return this.defaultLocation.Room.Name;
}
set
{
if(this.defaultLocation == null)
this.defaultLocation = new SpecificLocation("unknown", true, false);
this.defaultLocation.Room = new Room(value);
}
}
/// <summary>
/// Gets or sets the default location for objects in the category
/// </summary>
[XmlIgnore]
public SpecificLocation DefaultLocation {
get{ return this.defaultLocation;}
set {
this.defaultLocation = value;
}
}
/// <summary>
/// Gets or sets the list of objects in the category.
/// </summary>
/// <remarks>Use for (de)serialization purposes only</remarks>
[XmlElement("object")]
public GPSRObject[] Objects
{
get { return new List<GPSRObject>(this.objects.Values).ToArray(); }
set {
if(value == null) return;
foreach (GPSRObject o in value)
AddObject(o);
}
}
#endregion
#region Methods
/// <summary>
/// Adds an object to the category
/// </summary>
/// <param name="item">The ibject to add to the category</param>
public void AddObject(GPSRObject item){
if (item == null)
return;
if ((item.Category != null) && (item.Category != this))
item.Category.RemoveObject(item);
if (!this.objects.ContainsKey(item.Name))
this.objects.Add(item.Name, item);
if (item.Category != this)
item.Category = this;
}
/// <summary>
/// Adds an object to the category
/// </summary>
/// <param name="name">The name of the object to add.</param>
/// <param name="type">The type of the object to add.</param>
/// <remarks>Object difficulty degree is set automatically
/// based on type as follows:
/// Alike -> Moderate /
/// Known -> Easy
/// Unknown -> Unknown</remarks>
public void AddObject(string name, GPSRObjectType type){
DifficultyDegree tier;
switch (type) {
case GPSRObjectType.Alike:
tier = DifficultyDegree.Moderate;
break;
case GPSRObjectType.Known:
tier = DifficultyDegree.Easy;
break;
default:
case GPSRObjectType.Unknown:
tier = DifficultyDegree.Unknown;
break;
}
AddObject (name, type, tier);
}
/// <summary>
/// Adds an object to the category
/// </summary>
/// <param name="name">The name of the object to add.</param>
/// <param name="type">The type of the object to add.</param>
/// <param name="type">The difficulty degree of the object to add.</param>
public void AddObject(string name, GPSRObjectType type, DifficultyDegree tier){
GPSRObject o = new GPSRObject (name, type, tier);
this.AddObject (o);
}
/// <summary>
/// Gets a value indicating if the category contains an object with the given name
/// </summary>
/// <param name="objectName">The name of the object to look for</param>
/// <returns>true if the category contains a object with the given name, false otherwise</returns>
public bool Contains(string objectName)
{
return this.objects.ContainsKey(objectName);
}
/// <summary>
/// Removes all objects
/// </summary>
public void Clear()
{
this.objects.Clear();
}
/// <summary>
/// Removes the given GPSRObject from the Category
/// </summary>
/// <param name="item">The GPSRObject to remove</param>
/// <returns>true if the GPSRObject was in the collection, false otherwise</returns>
private bool RemoveObject(GPSRObject item)
{
if (item == null)
return false;
return RemoveObject(item.Name);
}
/// <summary>
/// Removes the given GPSRObject from the room
/// </summary>
/// <param name="objectName">The name of the GPSRObject to remove</param>
/// <returns>true if the GPSRObject was in the collection, false otherwise</returns>
private bool RemoveObject(string objectName)
{
return this.objects.Remove(objectName);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Category"/>.
/// </summary>
/// <returns>A <see cref="System.String"/> that represents the current <see cref="GPSRCmdGen.Category"/>.</returns>
public override string ToString ()
{
return string.Format ("{0} [{2} Objects | {1} ]", Name, DefaultLocation.Name, ObjectCount);
}
#endregion
}
}