浏览代码

added scenario serialization

/main
Steven Leal 4 年前
当前提交
37eea993
共有 6 个文件被更改,包括 221 次插入110 次删除
  1. 2
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs
  2. 123
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs
  3. 94
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs
  4. 3
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs.meta
  5. 109
      com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs
  6. 0
      /com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs.meta

2
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/JsonConverters.cs


public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
throw new InvalidOperationException("Use default serialization.");
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

123
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs


using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEditor;
using UnityEngine.Perception.Randomization.Parameters;
using UnityEngine.Perception.Randomization.Randomizers;
using UnityEngine.Perception.Randomization.Samplers;
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization
{
static class ScenarioTemplateSerializer
{
[MenuItem("Tests/Deserialize Test")]
public static void DeserializeTest()
{
var jsonString = File.ReadAllText($"{Application.streamingAssetsPath}/data.json");
var schema = JsonConvert.DeserializeObject<TemplateConfigurationOptions>(jsonString);
var backToJson = JsonConvert.SerializeObject(schema, Formatting.Indented);
Debug.Log(backToJson);
}
[MenuItem("Tests/Serialize Scenario To Json Test")]
public static void SerializeScenarioToJsonTest()
{
var template = SerializeScenarioIntoTemplate(Object.FindObjectOfType<ScenarioBase>());
Debug.Log(JsonConvert.SerializeObject(template, Formatting.Indented));
}
public static TemplateConfigurationOptions SerializeScenarioIntoTemplate(ScenarioBase scenario)
{
return new TemplateConfigurationOptions
{
groups = SerializeRandomizers(scenario.randomizers)
};
}
static Dictionary<string, Group> SerializeRandomizers(IEnumerable<Randomizer> randomizers)
{
var serializedRandomizers = new Dictionary<string, Group>();
foreach (var randomizer in randomizers)
{
var randomizerData = SerializeRandomizer(randomizer);
if (randomizerData.items.Count == 0)
continue;
serializedRandomizers.Add(randomizer.GetType().Name, randomizerData);
}
return serializedRandomizers;
}
static Group SerializeRandomizer(Randomizer randomizer)
{
var randomizerObj = new Group();
var parameterFields = randomizer.GetType().GetFields();
foreach (var parameterField in parameterFields)
{
if (!IsSubclassOfRawGeneric(typeof(NumericParameter<>), parameterField.FieldType))
continue;
var parameter = (Randomization.Parameters.Parameter)parameterField.GetValue(randomizer);
var parameterData = SerializeParameter(parameter);
if (parameterData.items.Count == 0)
continue;
randomizerObj.items.Add(parameterField.Name, parameterData);
}
return randomizerObj;
}
static Parameter SerializeParameter(Randomization.Parameters.Parameter parameter)
{
var parameterData = new Parameter();
var samplerFields = parameter.GetType().GetFields();
foreach (var samplerField in samplerFields)
{
if (!samplerField.FieldType.IsAssignableFrom(typeof(ISampler)))
continue;
var sampler = (ISampler)samplerField.GetValue(parameter);
var samplerData = SerializeSampler(sampler);
if (samplerData.defaultSampler == null)
continue;
parameterData.items.Add(samplerField.Name, samplerData);
}
return parameterData;
}
static SamplerOptions SerializeSampler(ISampler sampler)
{
var samplerData = new SamplerOptions();
if (sampler is Samplers.ConstantSampler constantSampler)
samplerData.defaultSampler = new ConstantSampler
{
value = constantSampler.value
};
else if (sampler is Samplers.UniformSampler uniformSampler)
samplerData.defaultSampler = new UniformSampler
{
min = uniformSampler.range.minimum,
max = uniformSampler.range.maximum
};
else if (sampler is Samplers.NormalSampler normalSampler)
samplerData.defaultSampler = new NormalSampler
{
min = normalSampler.range.minimum,
max = normalSampler.range.maximum,
mean = normalSampler.mean,
standardDeviation = normalSampler.standardDeviation
};
else
throw new ArgumentException($"Invalid sampler type ({sampler.GetType()})");
return samplerData;
}
static bool IsSubclassOfRawGeneric(Type generic, Type toCheck) {
while (toCheck != null && toCheck != typeof(object)) {
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur) {
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}
}
}

94
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs


using System.Collections.Generic;
using Newtonsoft.Json;
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization
{
#region Interfaces
public interface IGroupItem { }
public interface IParameterItem { }
public interface ISamplerOption { }
public interface IScalarValue { }
#endregion
#region GroupedObjects
public class TemplateConfigurationOptions
{
public Dictionary<string, Group> groups = new Dictionary<string, Group>();
}
public class StandardMetadata
{
public string name = string.Empty;
public string description = string.Empty;
}
public class Group
{
public StandardMetadata metadata = new StandardMetadata();
[JsonConverter(typeof(GroupItemsConverter))]
public Dictionary<string, IGroupItem> items = new Dictionary<string, IGroupItem>();
}
public class Parameter : IGroupItem
{
public StandardMetadata metadata = new StandardMetadata();
[JsonConverter(typeof(ParameterItemsConverter))]
public Dictionary<string, IParameterItem> items = new Dictionary<string, IParameterItem>();
}
#endregion
#region SamplerOptions
[JsonConverter(typeof(SamplerOptionsConverter))]
public class SamplerOptions : IParameterItem
{
public StandardMetadata metadata = new StandardMetadata();
public ISamplerOption defaultSampler;
}
public class UniformSampler : ISamplerOption
{
public double min;
public double max;
}
public class NormalSampler : ISamplerOption
{
public double min;
public double max;
public double mean;
public double standardDeviation;
}
public class ConstantSampler : ISamplerOption
{
public double value;
}
#endregion
#region ScalarValues
[JsonConverter(typeof(ScalarConverter))]
public class Scalar : IGroupItem, IParameterItem
{
public StandardMetadata metadata = new StandardMetadata();
public IScalarValue value;
}
public class StringScalarValue : IScalarValue
{
public string str;
}
public class DoubleScalarValue : IScalarValue
{
public double num;
}
public class BooleanScalarValue : IScalarValue
{
public bool boolean;
}
#endregion
}

3
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/SerializationStructures.cs.meta


fileFormatVersion: 2
guid: f1253a3d94bb468b93f80c2178652540
timeCreated: 1612927921

109
com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs


using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using UnityEditor;
namespace UnityEngine.Perception.Randomization.Scenarios.Serialization
{
public static class ScenarioSerialization
{
[MenuItem("Tests/Deserialize Test")]
public static void DeserializeTest()
{
var jsonString = File.ReadAllText($"{Application.streamingAssetsPath}/data.json");
var schema = JsonConvert.DeserializeObject<TemplateConfigurationOptions>(jsonString);
var backToJson = JsonConvert.SerializeObject(schema, Formatting.Indented);
Debug.Log(backToJson);
}
}
#region Interfaces
public interface IGroupItem { }
public interface IParameterItem { }
public interface ISamplerOption { }
public interface IScalarValue { }
#endregion
#region GroupedObjects
public class TemplateConfigurationOptions
{
public Dictionary<string, Group> groups;
}
public class StandardMetadata
{
public string name = string.Empty;
public string description = string.Empty;
}
public class Group
{
public StandardMetadata metadata;
[JsonConverter(typeof(GroupItemsConverter))]
public Dictionary<string, IGroupItem> items;
}
public class Parameter : IGroupItem
{
public StandardMetadata metadata;
[JsonConverter(typeof(ParameterItemsConverter))]
public Dictionary<string, IParameterItem> items;
}
#endregion
#region SamplerOptions
[JsonConverter(typeof(SamplerOptionsConverter))]
public class SamplerOptions : IParameterItem
{
public StandardMetadata metadata;
public ISamplerOption defaultSampler;
}
public class UniformSampler : ISamplerOption
{
public double min;
public double max;
}
public class NormalSampler : ISamplerOption
{
public double min;
public double max;
public double mean;
public double standardDeviation;
}
public class ConstantSampler : ISamplerOption
{
public double value;
}
#endregion
#region ScalarValues
[JsonConverter(typeof(ScalarConverter))]
public class Scalar : IGroupItem, IParameterItem
{
public StandardMetadata metadata;
public IScalarValue value;
}
public class StringScalarValue : IScalarValue
{
public string str;
}
public class DoubleScalarValue : IScalarValue
{
public double num;
}
public class BooleanScalarValue : IScalarValue
{
public bool boolean;
}
#endregion
}

/com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioSerialization.cs.meta → /com.unity.perception/Runtime/Randomization/Scenarios/Serialization/ScenarioTemplateSerializer.cs.meta

正在加载...
取消
保存