Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Assets/Experica/ConditionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public Dictionary<string, List<object>> ReadConditionFile(string path)
{
return null;
}
return path.ReadYamlFile<Dictionary<string, List<object>>>();
string serializedData = File.ReadAllText(path);
return Formatter.Instance.DeserializeUsingFormat<Dictionary<string, List<object>>>(serializedData, DataFormat.YAML);
}

public Dictionary<string, List<object>> ProcessCondition(Dictionary<string, List<object>> cond)
Expand Down
16 changes: 16 additions & 0 deletions Assets/Experica/Experiment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using Fasterflect;
using MsgPack.Serialization;
using Newtonsoft.Json;

namespace Experica
{
Expand Down Expand Up @@ -96,9 +97,11 @@ public class Experiment
public string Subject_Log { get; set; } = "";

public string EnvPath { get; set; } = "";
[JsonIgnore] // Json cannont serialize this property
[MessagePackRuntimeCollectionItemType]
public Dictionary<string, object> EnvParam { get; set; } = new Dictionary<string, object>();
public string CondPath { get; set; } = "";
[JsonIgnore] // Json cannont serialze this property
[MessagePackRuntimeCollectionItemType]
public Dictionary<string, IList> Cond { get; set; }
public string ExLogicPath { get; set; } = "";
Expand Down Expand Up @@ -130,6 +133,7 @@ public class Experiment
public List<CONDTESTPARAM> NotifyParam { get; set; }
public List<string> ExInheritParam { get; set; } = new List<string>();
public List<string> EnvInheritParam { get; set; } = new List<string>();
[JsonIgnore] // Json cannont serialize this property
[MessagePackRuntimeCollectionItemType]
public Dictionary<string, object> Param { get; set; } = new Dictionary<string, object>();
public double TimerDriftSpeed { get; set; }
Expand All @@ -139,14 +143,19 @@ public class Experiment
public uint Version { get; set; } = 2;
public CommandConfig Config { get; set; }

[JsonIgnore]
[MessagePackIgnore]
public Dictionary<CONDTESTPARAM, List<object>> CondTest { get; set; }
[JsonIgnore]
[MessagePackIgnore]
public CONDTESTSHOWLEVEL CondTestShowLevel { get; set; }
[JsonIgnore]
[MessagePackIgnore]
public bool SendMail { get; set; } = false;
[JsonIgnore]
[MessagePackIgnore]
public static readonly Dictionary<string, PropertyAccess> Properties;
[JsonIgnore]
[MessagePackIgnore]
public Action<string, object> OnNotifyUI;

Expand Down Expand Up @@ -227,6 +236,13 @@ public object GetProperty(Experiment ex, PropertyAccess p)
return p.Getter(ex);
}

/// <summary>
/// Searches the directory for the exact same experiment ran, if it has been ran before, appends or advances the ending
/// number to a datapath and returns it
/// </summary>
/// <param name="ext">The extension to append to end of datapath when creating a new path</param>
/// <param name="searchext">The extenstion to serach for in the directory</param>
/// <returns>A new DataPath corresponding to the experiment ran in order to save it.</returns>
public virtual string GetDataPath(string ext = "", string searchext = "yaml")
{
if (string.IsNullOrEmpty(DataPath))
Expand Down
25 changes: 20 additions & 5 deletions Assets/Experica/ExperimentLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,20 @@ protected virtual void SamplePushBlock(int manualblockidx = 0)
condmanager.PushBlock(condmanager.SampleBlockSpace(manualblockidx), envmanager, pushexcludefactors);
}

/// <summary>
/// Returns the
/// </summary>
/// <param name="dataFormat"></param>
/// <returns></returns>
public virtual string DataPath(DataFormat dataFormat)
{
var extension = dataFormat.ToString().ToLower();
return ex.GetDataPath(ext: extension, searchext: extension);
var extension = dataFormat.ToString().ToLower(); // String representation of dataFormat
return ex.GetDataPath(ext: extension, searchext: extension); //
}

/// <summary>
///
/// </summary>
public virtual void SaveData()
{
var ct = condtestmanager.condtest;
Expand All @@ -308,14 +316,21 @@ public virtual void SaveData()
ex.Config = config;
}
ex.EnvParam = envmanager.GetActiveParams();

switch (config.SaveDataFormat)
{
// Currently Not Implemented.
case DataFormat.EXPERICA:
DataPath(DataFormat.EXPERICA).Save(ex);
string serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.EXPERICA);
File.WriteAllText(DataPath(DataFormat.EXPERICA), serialzedData);
break;
case DataFormat.JSON:
serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.JSON);
File.WriteAllText(DataPath(DataFormat.JSON), serialzedData);
break;
// Save the Experiment, enviroment, and data as a YAML File.
default:
DataPath(DataFormat.YAML).WriteYamlFile(ex);
serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.YAML);
File.WriteAllText(DataPath(DataFormat.YAML), serialzedData);
break;
}
ex.DataPath = null;
Expand Down
3 changes: 2 additions & 1 deletion Assets/Experica/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public enum CONDTESTPARAM
public enum DataFormat
{
YAML,
EXPERICA
EXPERICA,
JSON
}

public static class Extension
Expand Down
87 changes: 87 additions & 0 deletions Assets/Experica/Serialization/Formatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Formatter.cs is part of the Experica.
Copyright (c) 2016 Li Alex Zhang and Contributors

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Collections.Generic;

namespace Experica
{
/// <summary>
/// The purpose of this class is to have a multion capable of formatting (serializing/deserializing) in
/// any format class that is derived from IFormat. This class will have a list of singleton objects
/// corresponding to each format possible. To add a type of format, simply create the class, have it
/// end in Format, and add the type to the enum DataType in Extension.cs
/// </summary>
public sealed class Formatter
{
private Dictionary<DataFormat, IFormat> _formats = new Dictionary<DataFormat, IFormat>();

// With lazy instantiation, it is only created once referenced.
private static readonly Lazy<Formatter> lazy = new Lazy<Formatter>(() => new Formatter());

// The singleton Instance of the Formatter, its thread safe.
public static Formatter Instance { get { return lazy.Value; } }

/// <summary>
/// Gets the active singleton object for the corresponding data format
/// </summary>
/// <param name="type">The type of DataFormat to retrieve.</param>
/// <returns>The current active DataFormat of the given type</returns>
private IFormat GetActiveFormat(DataFormat format)
{
if (!_formats.ContainsKey(format))
{
var str = format.ToString() + "Format";
Type type = Type.GetType("Experica." + format.ToString() + "Format");
var obj = (IFormat)Activator.CreateInstance(type);
_formats[format] = obj;
}

return _formats[format];
}

/// <summary>
/// Serializes the object using a specific type of DataFormat
/// </summary>
/// <typeparam name="T">type of the object to serialize.</typeparam>
/// <param name="obj">Object to serialize.</param>
/// <param name="format">The format to serialize the Object to.</param>
/// <returns></returns>
public string SerialzeDataToFormat<T>(T obj, DataFormat format)
{
IFormat formatToUse = GetActiveFormat(format);
return formatToUse.Serialize(obj);
}

/// <summary>
/// Deserializes an object
/// </summary>
/// <typeparam name="T">Type to deserialize to</typeparam>
/// <param name="data">The data in the specific format to deserialize</param>
/// <param name="format">The format the the string is in to deserialize.</param>
/// <returns></returns>
public T DeserializeUsingFormat<T>(string data, DataFormat format)
{
IFormat formatToUse = GetActiveFormat(format);
return formatToUse.Deserialize<T>(data);
}
}
}
34 changes: 34 additions & 0 deletions Assets/Experica/Serialization/HDF5Format.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Experica;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experica
{
class HDF5Format : IFormat
{
/// <summary>
/// NOT IMPLEMENTED. Will deserialize a string of text into an object.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public T Deserialize<T>(string data)
{
throw new NotImplementedException();
}

/// <summary>
/// NOT IMPLEMENTED. Will Serialize an object into a serialize of bytes/text in hdf5 format
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public string Serialize<T>(T obj)
{
throw new NotImplementedException();
}
}
}
29 changes: 29 additions & 0 deletions Assets/Experica/Serialization/IFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
IFormat.cs is part of the Experica.
Copyright (c) 2016 Li Alex Zhang and Contributors

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
namespace Experica
{
public interface IFormat
{
string Serialize<T>(T obj);
T Deserialize<T>(string data);
}
}
32 changes: 32 additions & 0 deletions Assets/Experica/Serialization/JSONFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Experica;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experica
{
class JSONFormat : IFormat
{

public T Deserialize<T>(string data)
{
T obj = JsonConvert.DeserializeObject<T>(data);
return obj;
}

public string Serialize<T>(T obj)
{
string json = JsonConvert.SerializeObject(obj, Formatting.Indented, new JsonSerializerSettings()
{
// This handles self-referencing loops.
//PreserveReferencesHandling = PreserveReferencesHandling.All
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
//PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
return json;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,19 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using UnityEngine;
using System;
using System.IO;
using System.Text;
using YamlDotNet.Serialization;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using System.Runtime.Serialization;
using System.IO;

namespace Experica
{
public class YamlTypeConverter : IYamlTypeConverter
{
Type TVector2 = typeof(Vector2);
Type TVector3 = typeof(Vector3);
Type TVector4 = typeof(Vector4);
Type TColor = typeof(Color);

public bool Accepts(Type type)
{
if (type == TVector2 || type == TVector3 || type == TVector4 || type == TColor)
if (type == typeof(Vector2) || type == typeof(Vector3) || type == typeof(Vector4) || type == typeof(Color))
{
return true;
}
Expand All @@ -58,34 +53,36 @@ public void WriteYaml(IEmitter emitter, object value, Type type)
}
}

public static class Yaml
public class YAMLFormat : IFormat
{
static ISerializer serializer;
static IDeserializer deserializer;
ISerializer serializer;
IDeserializer deserializer;

static Yaml()
public YAMLFormat()
{
var yamlvlabconverter = new YamlTypeConverter();
serializer = new SerializerBuilder().DisableAliases().EmitDefaults().IgnoreFields().WithTypeConverter(yamlvlabconverter).Build();
deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().IgnoreFields().WithTypeConverter(yamlvlabconverter).Build();
}

public static void WriteYamlFile<T>(this string path, T data)
{
File.WriteAllText(path, SerializeYaml(data));
}

public static string SerializeYaml<T>(this T data)
{
return serializer.Serialize(data);
}

public static T ReadYamlFile<T>(this string path)
/// <summary>
/// Serializes a serializable object
/// </summary>
/// <typeparam name="T">The type of the object to serialize</typeparam>
/// <param name="obj">The object to serialize</param>
/// <returns>A string in YAML format forrepsonding to the object.</returns>
public string Serialize<T>(T obj)
{
return deserializer.Deserialize<T>(File.ReadAllText(path));
return serializer.Serialize(obj);
}

public static T DeserializeYaml<T>(this string data)
/// <summary>
/// Deserialize the Yaml string
/// </summary>
/// <typeparam name="T">Type returned</typeparam>
/// <param name="data">The data to deserialize.</param>
/// <returns>Deserialized Object of type T</returns>
public T Deserialize<T>(string data)
{
return deserializer.Deserialize<T>(data);
}
Expand Down
Loading