This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathA2SCoderFactory.cs
114 lines (107 loc) · 4.01 KB
/
A2SCoderFactory.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
using System;
using System.Xml.Linq;
namespace RCNet.Neural.Data.Coders.AnalogToSpiking
{
/// <summary>
/// Provides a proper instantiation of the A2S coders and also proper loading of their configurations.
/// </summary>
public static class A2SCoderFactory
{
/// <summary>
/// Based on the xml element name loads the proper type of A2S coder configuration.
/// </summary>
/// <param name="elem">A xml element containing the configuration.</param>
public static RCNetBaseSettings LoadSettings(XElement elem)
{
switch (elem.Name.LocalName)
{
case "gaussianReceptorsCoder":
return new A2SCoderGaussianReceptorsSettings(elem);
case "signalStrengthCoder":
return new A2SCoderSignalStrengthSettings(elem);
case "upDirArrowsCoder":
return new A2SCoderUpDirArrowsSettings(elem);
case "downDirArrowsCoder":
return new A2SCoderDownDirArrowsSettings(elem);
default:
throw new ArgumentException($"Unexpected element name {elem.Name.LocalName}", "elem");
}
}
/// <summary>
/// Based on the xml element name checks whether it is an existing type of A2S coder configuration.
/// </summary>
/// <param name="elem">A xml element containing the configuration.</param>
public static bool CheckSettingsElemName(XElement elem)
{
switch (elem.Name.LocalName)
{
case "gaussianReceptorsCoder":
return true;
case "signalStrengthCoder":
return true;
case "upDirArrowsCoder":
return true;
case "downDirArrowsCoder":
return true;
default:
return false;
}
}
/// <summary>
/// Instantiates the appropriate A2S coder.
/// </summary>
/// <param name="cfg">The coder configuration.</param>
public static A2SCoderBase Create(RCNetBaseSettings cfg)
{
Type type = cfg.GetType();
if (type == typeof(A2SCoderGaussianReceptorsSettings))
{
return new A2SCoderGaussianReceptors((A2SCoderGaussianReceptorsSettings)cfg);
}
else if (type == typeof(A2SCoderSignalStrengthSettings))
{
return new A2SCoderSignalStrength((A2SCoderSignalStrengthSettings)cfg);
}
else if (type == typeof(A2SCoderUpDirArrowsSettings))
{
return new A2SCoderUpDirArrows((A2SCoderUpDirArrowsSettings)cfg);
}
else if (type == typeof(A2SCoderDownDirArrowsSettings))
{
return new A2SCoderDownDirArrows((A2SCoderDownDirArrowsSettings)cfg);
}
else
{
throw new ArgumentException($"Unexpected A2S coder type {type.Name}", "settings");
}
}
/// <summary>
/// Checks whether the specified configuration is an existing type of A2S coder configuration.
/// </summary>
/// <param name="cfg">The coder configuration.</param>
public static bool CheckSettings(RCNetBaseSettings cfg)
{
Type type = cfg.GetType();
if (type == typeof(A2SCoderGaussianReceptorsSettings))
{
return true;
}
else if (type == typeof(A2SCoderSignalStrengthSettings))
{
return true;
}
else if (type == typeof(A2SCoderUpDirArrowsSettings))
{
return true;
}
else if (type == typeof(A2SCoderDownDirArrowsSettings))
{
return true;
}
else
{
return false;
}
}
}//A2SCoderFactory
}//Namespace