Skip to content

Commit

Permalink
Added Multion pattern to instantiate just one object of each *Format.…
Browse files Browse the repository at this point in the history
…cs type. This object is then utilized and controlled by the Formatter class.
  • Loading branch information
dustin authored and dustin committed Jul 15, 2019
1 parent a0a276e commit cea308a
Show file tree
Hide file tree
Showing 24 changed files with 24,159 additions and 167 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ $RECYCLE.BIN/

# Windows shortcuts
*.lnk
/Assets/Experica/Serialization/JSONFormat.cs.meta
*.meta
9 changes: 9 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
8 changes: 4 additions & 4 deletions Assets/Experica/ExperimentLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,17 @@ public virtual void SaveData()
ex.Config = config;
}
ex.EnvParam = envmanager.GetActiveParams();

//
switch (config.SaveDataFormat)
{
// Currently Not Implemented.
case DataFormat.EXPERICA:
string serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.EXPERICA);
File.WriteAllText(DataPath(DataFormat.EXPERICA), serialzedData);
break;
//case DataFormat.HDF5:
//DataPath(DataFormat.YAML).WriteToFile(ex);
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:
serialzedData = Formatter.Instance.SerialzeDataToFormat(ex, DataFormat.YAML);
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
29 changes: 25 additions & 4 deletions Assets/Experica/Serialization/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,27 @@ OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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
{
// This can be static in a console/desktop application, just be wary of potential memory issues
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>
/// Get the current active policy of the given type.
/// Gets the active singleton object for the corresponding data format
/// </summary>
/// <param name="type">The type of policy to retrieve.</param>
/// <returns>The current active policy of the given type</returns>
/// <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))
Expand All @@ -51,12 +58,26 @@ private IFormat GetActiveFormat(DataFormat format)
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);
Expand Down
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();
}
}
}
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;
}
}
}
27 changes: 12 additions & 15 deletions Assets/Experica/Serialization/YAMLFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,23 @@ public YAMLFormat()
deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().IgnoreFields().WithTypeConverter(yamlvlabconverter).Build();
}

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

//public static T ReadYamlFile<T>(string path)
//{
// return deserializer.Deserialize<T>(File.ReadAllText(path));
//}

//public static T DeserializeYaml<T>(string data)
//{
// return deserializer.Deserialize<T>(data);
//}

/// <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 serializer.Serialize(obj);
}

/// <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
93 changes: 0 additions & 93 deletions Assets/Experica/Yaml.cs

This file was deleted.

12 changes: 0 additions & 12 deletions Assets/Experica/Yaml.cs.meta

This file was deleted.

Loading

0 comments on commit cea308a

Please sign in to comment.