C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
Nested.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5using System.Runtime.Serialization;
6
9
10namespace AnalyzeRe.Layers
11{
20 [APITypeAlias("NestedLayer")]
21 public class Nested : Layer
22 {
23 #region Public Properties
31 [DataMember(Order = 20)]
32 [NotNull]
33 public IReference<ILayerSource> sink { get; set; }
34
40 [IgnoreDataMember]
41 [NotNull]
42 [NotEmpty]
43 public Sources sources { get; set; }
44
47 [DataMember(Order = 21, Name = "sources"), InternalMember]
48 private List<IReference<ILayerSource>> SourcesAsReferenceList
49 {
50 get => sources?.References;
51 set => sources = new Sources(value);
52 }
53 #endregion Public Properties
54
57 public Nested()
58 {
59 sources = new Sources();
60 }
61
65 public sealed class Sources : APIType, IList<IReference<ILayerSource>>, IList
66 , IReadOnlyList<IReference<ILayerSource>>
67 {
69 public List<IReference<ILayerSource>> References { get; set; }
70
71 #region Constructors
73 public Sources()
74 {
75 References = new List<IReference<ILayerSource>>();
76 }
77
90
98 this(new List<IReference<ILayerSource>>(sources ??
99 Enumerable.Empty<IReference<ILayerSource>>())) { }
100
108 public Sources(params IReference[] sources) : this(sources, true) { }
109
118 public Sources(IEnumerable<IReference> sources, bool automaticTyping = true) :
119 this(sources?.Select(r => ResolveReference(r, automaticTyping))) { }
120
125 public static implicit operator Sources(List<IReference<ILayer>> layerSources) =>
126 new Sources(layerSources);
127
132 public static implicit operator Sources(List<IReference<ILayerSource>> sourceReferenceList) =>
133 new Sources(sourceReferenceList);
134
139 public static implicit operator List<IReference<ILayerSource>>(Sources sourcesInstance) =>
140 sourcesInstance?.References;
141 #endregion Constructors
142
143 #region Public Methods
152 public void Add(IReference reference, bool automaticTyping = true) =>
153 References.Add(ResolveReference(reference, automaticTyping));
154
165 public void Add(IReference<ILayerView> layerViewReference) => References.Add(layerViewReference);
166
172 public void Add(IReference<ILayer> layerReference) => References.Add(layerReference);
173
182 public void Add(ILayer layer) => References.Add(layer.ToReference());
183 #endregion Public Methods
184
185 #region Private Methods
190 private static IReference<ILayerSource> ResolveReference(IReference weaklyTyped, bool automaticTyping)
191 {
192 if (weaklyTyped is IReference<ILayer> || weaklyTyped is IReference<ILayerView>)
193 return (IReference<ILayerSource>)weaklyTyped;
194 if (automaticTyping && TryCreateLayerReference(weaklyTyped, out IReference<ILayer> converted))
195 return converted;
196 return ThrowInvalidLossSourceException(weaklyTyped);
197 }
198
203 private static bool TryCreateLayerReference(IReference reference, out IReference<ILayer> result,
204 bool suppress_throw = false)
205 {
206 result = null;
207 IReference<IAPIResource> weaklyTyped = reference as IReference<IAPIResource>;
208 // If this reference has a generic type argument,
209 // ensure it does not resolve to some non-layer collection.
210 if (weaklyTyped?.CollectionName != null &&
212 {
213 if (suppress_throw) return false;
214 // Special case: if the user added a loss set reference, raise a more meaningful
215 // error message to guide them in the correct direction.
216 string additionalMessage = null;
218 additionalMessage = "You can use LossSet references as loss sources by " +
219 "simply attaching them to the sink layer's list of loss_sets, or by " +
220 "wrapping them with a no-op layer and including that layer as a source.";
221 ThrowInvalidLossSourceException(reference, additionalMessage);
222 }
223
224 // If this reference contains a layer instance, create a strongly typed layer reference
225 if ((weaklyTyped?.resolved ?? false) && weaklyTyped.GetValue() is ILayer layerValue)
226 {
227 Type layerType = layerValue.GetType();
228 // Only bother creating a runtime type if it's more derived than what we're doing below.
229 if (layerType != typeof(ILayer))
230 result = (IReference<ILayer>)Activator.CreateInstance(
231 typeof(IReference<>).MakeGenericTypeFast(layerValue.GetType()), reference);
232 }
233
234 // Otherwise, create a weakly-typed layer reference instance
235 if (result == null)
236 result = new Reference<ILayer>(reference);
237 return true;
238 }
239
242 private static IReference<ILayerSource> ThrowInvalidLossSourceException(
243 IReference reference, string additionalMessage = null)
244 {
245 string message = $"Invalid reference type {reference.GetType().NiceTypeName()}" +
246 " encountered. All sources should either be Layer or LayerView references.";
247 if (additionalMessage != null)
248 message += $" {additionalMessage}";
249 throw new InvalidCastException(message);
250 }
251 #endregion Private Methods
252
253 #region List<T> Delegated Implementation
255 public IEnumerator<IReference<ILayerSource>> GetEnumerator() => References.GetEnumerator();
256
260 void ICollection<IReference<ILayerSource>>.Add(IReference<ILayerSource> item) => References.Add(item);
261
263 public void Clear() => References.Clear();
264
266 public bool Contains(IReference<ILayerSource> item) => References.Contains(item);
267
269 public void CopyTo(IReference<ILayerSource>[] array, int arrayIndex) => References.CopyTo(array, arrayIndex);
270
272 public bool Remove(IReference<ILayerSource> item) => References.Remove(item);
273
275 public int Count => References.Count;
276
278 public bool IsReadOnly => false;
279
281 public int IndexOf(IReference<ILayerSource> item) => References.IndexOf(item);
282
284 public void Insert(int index, IReference<ILayerSource> item) => References.Insert(index, item);
285
287 public void RemoveAt(int index) => References.RemoveAt(index);
288
290 public IReference<ILayerSource> this[int index]
291 {
292 get => References[index];
293 set => References[index] = value;
294 }
295
296 #region Explicit Implementations
297 IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)References).GetEnumerator();
298 int IList.Add(object value) => ((IList)References).Add(value);
299 bool IList.Contains(object value) => ((IList)References).Contains(value);
300 void ICollection.CopyTo(Array array, int index) => ((ICollection)References).CopyTo(array, index);
301 object ICollection.SyncRoot => ((ICollection)References).SyncRoot;
302 bool ICollection.IsSynchronized => ((ICollection)References).IsSynchronized;
303 int IList.IndexOf(object value) => ((IList)References).IndexOf(value);
304 void IList.Insert(int index, object value) => ((IList)References).Insert(index, value);
305 void IList.Remove(object value) => ((IList)References).Remove(value);
306 bool IList.IsFixedSize => ((IList)References).IsFixedSize;
307 object IList.this[int index]
308 {
309 get => ((IList)References)[index];
310 set => ((IList)References)[index] = value;
311 }
312 #endregion Explicit Implementations
313 #endregion List<T> Delegated Implementation
314 }
315 }
316}
Base class used by all types and resources.
Definition APIType.cs:8
Abstract representation of a layer. This resource type cannot be instantiated instead derived resourc...
Definition Layer.cs:12
static readonly string CLASS_COLLECTION_NAME
The collection endpoint at which resources of this type reside on the server.
Definition Layer.cs:15
A wrapper for a List<T> of IReference<ILayerSource> references that simplifies the construction and u...
Definition Nested.cs:67
Sources(IEnumerable< IReference > sources, bool automaticTyping=true)
Construct a new sources list from any enumerable of references.
Definition Nested.cs:118
IEnumerator< IReference< ILayerSource > > GetEnumerator()
void Insert(int index, IReference< ILayerSource > item)
bool Remove(IReference< ILayerSource > item)
Sources(List< IReference< ILayerSource > > sources)
Construct a new sources list and bind its internal list of references to the supplied source list.
Definition Nested.cs:86
void Add(IReference< ILayerView > layerViewReference)
Add a IReference<ILayerView> (LayerView reference) to this nested layer's sources.
Sources(params IReference[] sources)
Construct a new sources list from any number of references arguments.
Definition Nested.cs:108
void Add(ILayer layer)
Add an inlined IReference<Layer> to this nested layer's sources.
void CopyTo(IReference< ILayerSource >[] array, int arrayIndex)
void Add(IReference< ILayer > layerReference)
Add a IReference<ILayer> (saved Layer reference) to this nested layer's sources.
int IndexOf(IReference< ILayerSource > item)
List< IReference< ILayerSource > > References
The underlying list of IReference<ILayerSource> references.
Definition Nested.cs:69
Sources()
Construct a new empty sources list.
Definition Nested.cs:73
bool Contains(IReference< ILayerSource > item)
void Add(IReference reference, bool automaticTyping=true)
Allows a weakly-typed reference to be added to this collection, with the possibility of runtime check...
Sources(IEnumerable< IReference< ILayerSource > > sources)
Construct a new sources list by copying the enumerable of references with the expected compile-time t...
Definition Nested.cs:97
Allows one or more source layers or layer_views to be attached as loss sources to some other layer de...
Definition Nested.cs:22
Nested()
Construct a new Nested Layer with the default perspective and empty list of source layers.
Definition Nested.cs:57
Sources sources
A list of the source layers forwarding losses to this layer.
Definition Nested.cs:43
IReference< ILayerSource > sink
The layer that will take on the sources' forwarded losses.
Definition Nested.cs:33
Base class for all LossSet sub-types. A LossSet is a resource that generates sample (trial) losses wh...
Definition LossSet.cs:13
static readonly string CLASS_COLLECTION_NAME
The collection endpoint at which resources of this type reside on the server.
Definition LossSet.cs:41
Implements the reference entity interface, with support for resolving references using lazy loading.
An interface for an object that provides access to some layer instance.
Abstract representation of a layer.
Definition ILayer.cs:7
Base interface for all reference entities.
string CollectionName
The name of the collection at which this resource resides.
T GetValue(IEnumerable< Parameter > requestParameters=null, int? timeout=null, bool updateCache=false)
Gets the resource that this reference refers to by requesting it from the server.