File size: 3,240 Bytes
05c9ac2 |
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 |
using System;
using System.Reflection;
namespace Unity.MLAgents.Sensors.Reflection
{
/// <summary>
/// Construction info for a ReflectionSensorBase.
/// </summary>
internal struct ReflectionSensorInfo
{
public object Object;
public FieldInfo FieldInfo;
public PropertyInfo PropertyInfo;
public ObservableAttribute ObservableAttribute;
public string SensorName;
public Type GetMemberType()
{
return FieldInfo != null ? FieldInfo.FieldType : PropertyInfo.PropertyType;
}
}
/// <summary>
/// Abstract base class for reflection-based sensors.
/// </summary>
internal abstract class ReflectionSensorBase : ISensor, IBuiltInSensor
{
protected object m_Object;
// Exactly one of m_FieldInfo and m_PropertyInfo should be non-null.
protected FieldInfo m_FieldInfo;
protected PropertyInfo m_PropertyInfo;
// Not currently used, but might want later.
protected ObservableAttribute m_ObservableAttribute;
// Cached sensor names and shapes.
string m_SensorName;
ObservationSpec m_ObservationSpec;
int m_NumFloats;
public ReflectionSensorBase(ReflectionSensorInfo reflectionSensorInfo, int size)
{
m_Object = reflectionSensorInfo.Object;
m_FieldInfo = reflectionSensorInfo.FieldInfo;
m_PropertyInfo = reflectionSensorInfo.PropertyInfo;
m_ObservableAttribute = reflectionSensorInfo.ObservableAttribute;
m_SensorName = reflectionSensorInfo.SensorName;
m_ObservationSpec = ObservationSpec.Vector(size);
m_NumFloats = size;
}
/// <inheritdoc/>
public ObservationSpec GetObservationSpec()
{
return m_ObservationSpec;
}
/// <inheritdoc/>
public int Write(ObservationWriter writer)
{
WriteReflectedField(writer);
return m_NumFloats;
}
internal abstract void WriteReflectedField(ObservationWriter writer);
/// <summary>
/// Get either the reflected field, or return the reflected property.
/// This should be used by implementations in their WriteReflectedField() method.
/// </summary>
/// <returns></returns>
protected object GetReflectedValue()
{
return m_FieldInfo != null ?
m_FieldInfo.GetValue(m_Object) :
m_PropertyInfo.GetMethod.Invoke(m_Object, null);
}
/// <inheritdoc/>
public byte[] GetCompressedObservation()
{
return null;
}
/// <inheritdoc/>
public void Update() { }
/// <inheritdoc/>
public void Reset() { }
/// <inheritdoc/>
public CompressionSpec GetCompressionSpec()
{
return CompressionSpec.Default();
}
/// <inheritdoc/>
public string GetName()
{
return m_SensorName;
}
/// <inheritdoc/>
public BuiltInSensorType GetBuiltInSensorType()
{
return BuiltInSensorType.ReflectionSensor;
}
}
}
|