Exemple #1
0
        /// <summary>
        /// Must be called within OnEditorGUI()
        /// </summary>
        void DisplayFailedModelChecks()
        {
            if (m_RequireReload && m_TimeSinceModelReload > k_TimeBetweenModelReloads)
            {
                m_RequireReload        = false;
                m_TimeSinceModelReload = 0;
            }
            // Display all failed checks
            D.logEnabled = false;
            Model barracudaModel     = null;
            var   model              = (NNModel)serializedObject.FindProperty("m_Model").objectReferenceValue;
            var   behaviorParameters = (BehaviorParameters)target;

            // Grab the sensor components, since we need them to determine the observation sizes.
            SensorComponent[] sensorComponents;
            if (behaviorParameters.UseChildSensors)
            {
                sensorComponents = behaviorParameters.GetComponentsInChildren <SensorComponent>();
            }
            else
            {
                sensorComponents = behaviorParameters.GetComponents <SensorComponent>();
            }

            // Get the total size of the sensors generated by ObservableAttributes.
            // If there are any errors (e.g. unsupported type, write-only properties), display them too.
            int observableAttributeSensorTotalSize = 0;
            var agent = behaviorParameters.GetComponent <Agent>();

            if (agent != null && behaviorParameters.ObservableAttributeHandling != ObservableAttributeOptions.Ignore)
            {
                List <string> observableErrors = new List <string>();
                observableAttributeSensorTotalSize = ObservableAttribute.GetTotalObservationSize(agent, false, observableErrors);
                foreach (var check in observableErrors)
                {
                    EditorGUILayout.HelpBox(check, MessageType.Warning);
                }
            }

            var brainParameters = behaviorParameters.BrainParameters;

            if (model != null)
            {
                barracudaModel = ModelLoader.Load(model);
            }
            if (brainParameters != null)
            {
                var failedChecks = Inference.BarracudaModelParamLoader.CheckModel(
                    barracudaModel, brainParameters, sensorComponents,
                    observableAttributeSensorTotalSize, behaviorParameters.BehaviorType
                    );
                foreach (var check in failedChecks)
                {
                    if (check != null)
                    {
                        EditorGUILayout.HelpBox(check, MessageType.Warning);
                    }
                }
            }
        }
Exemple #2
0
 static IEnumerable <PropertyInfo> GetPropertiesAnnotatedAsObservable([NotNull] Type type)
 {
     foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
     {
         if (property.CanRead && ObservableAttribute.IsDefined(property))
         {
             yield return(property);
         }
     }
 }
Exemple #3
0
        public override void OnGUI(Rect position,
                                   SerializedProperty property,
                                   GUIContent label)
        {
            property.serializedObject.Update();

            object[] attributes = fieldInfo.GetCustomAttributes(true);

            ObservableAttribute observableAttribute = null;

            for (int i = 0; i < attributes.Length; i++)
            {
                if (attributes[i].GetType() == typeof(ObservableAttribute))
                {
                    observableAttribute = attributes[i] as ObservableAttribute;
                    break;
                }
            }

            if (observableAttribute != null)
            {
                _willShowAllChildren = observableAttribute.WillShowAllChildren;
                _isEditable          = observableAttribute.IsEditable;
            }

            GUI.enabled = _isEditable;

            SerializedProperty valueSP = property.FindPropertyRelative("_value");

            if (valueSP != null)
            {
                EditorGUI.BeginChangeCheck();
                EditorGUI.PropertyField(position, valueSP, label, true);
                if (EditorGUI.EndChangeCheck())
                {
                    // Use reflection to prompt the class
                    // to invoke its OnChange. This method is protected
                    // because it otherwise should not be called externally.
                    Observable observable = fieldInfo.GetValue(property.serializedObject.targetObject) as Observable;
                    Type       thisType   = observable.GetType();

                    MethodInfo OnValidate = thisType.GetMethod("InvokeOnValidate", BindingFlags.NonPublic | BindingFlags.Instance);
                    OnValidate.Invoke(observable, null);

                    MethodInfo invokeOnChanged = thisType.GetMethod("InvokeOnChanged", BindingFlags.NonPublic | BindingFlags.Instance);
                    invokeOnChanged.Invoke(observable, null);
                }
            }

            if (_willShowAllChildren)
            {
                SerializedProperty onChangedSP = property.FindPropertyRelative("OnChanged");
                if (onChangedSP != null)
                {
                    position.y     += EditorGUIUtility.singleLineHeight + GapVertical;
                    position.x     += EditorGUIUtility.labelWidth;
                    position.width -= EditorGUIUtility.labelWidth;
                    EditorGUI.PropertyField(position, onChangedSP, new GUIContent("OnChanged"), true);
                }
            }

            GUI.enabled = false;
            property.serializedObject.ApplyModifiedProperties();
        }
Exemple #4
0
        /// <summary>
        /// Must be called within OnEditorGUI()
        /// </summary>
        void DisplayFailedModelChecks()
        {
            if (m_RequireReload && m_TimeSinceModelReload > k_TimeBetweenModelReloads)
            {
                m_RequireReload        = false;
                m_TimeSinceModelReload = 0;
            }
            // Display all failed checks
            D.logEnabled = false;
            Model barracudaModel     = null;
            var   model              = (NNModel)serializedObject.FindProperty(k_ModelName).objectReferenceValue;
            var   behaviorParameters = (BehaviorParameters)target;

            // Grab the sensor components, since we need them to determine the observation sizes.
            // TODO make these methods of BehaviorParameters
            var agent = behaviorParameters.gameObject.GetComponent <Agent>();

            if (agent == null)
            {
                return;
            }
            agent.sensors = new List <ISensor>();
            agent.InitializeSensors();
            var sensors = agent.sensors.ToArray();

            ActuatorComponent[] actuatorComponents;
            if (behaviorParameters.UseChildActuators)
            {
                actuatorComponents = behaviorParameters.GetComponentsInChildren <ActuatorComponent>();
            }
            else
            {
                actuatorComponents = behaviorParameters.GetComponents <ActuatorComponent>();
            }

            // Get the total size of the sensors generated by ObservableAttributes.
            // If there are any errors (e.g. unsupported type, write-only properties), display them too.
            int observableAttributeSensorTotalSize = 0;

            if (agent != null && behaviorParameters.ObservableAttributeHandling != ObservableAttributeOptions.Ignore)
            {
                List <string> observableErrors = new List <string>();
                observableAttributeSensorTotalSize = ObservableAttribute.GetTotalObservationSize(agent, false, observableErrors);
                foreach (var check in observableErrors)
                {
                    EditorGUILayout.HelpBox(check, MessageType.Warning);
                }
            }

            var brainParameters = behaviorParameters.BrainParameters;

            if (model != null)
            {
                barracudaModel = ModelLoader.Load(model);
            }
            if (brainParameters != null)
            {
                var failedChecks = Inference.BarracudaModelParamLoader.CheckModel(
                    barracudaModel, brainParameters, sensors, actuatorComponents,
                    observableAttributeSensorTotalSize, behaviorParameters.BehaviorType, behaviorParameters.DeterministicInference
                    );
                foreach (var check in failedChecks)
                {
                    if (check != null)
                    {
                        switch (check.CheckType)
                        {
                        case CheckTypeEnum.Info:
                            EditorGUILayout.HelpBox(check.Message, MessageType.Info);
                            break;

                        case CheckTypeEnum.Warning:
                            EditorGUILayout.HelpBox(check.Message, MessageType.Warning);
                            break;

                        case CheckTypeEnum.Error:
                            EditorGUILayout.HelpBox(check.Message, MessageType.Error);
                            break;

                        default:
                            break;
                        }
                    }
                }
            }
        }