BGC Tools
Data Structures | Public Member Functions | Static Public Member Functions | Data Fields | Static Public Attributes | Properties | Static Private Member Functions | Private Attributes | Static Private Attributes
BGC.Study.ProtocolManager Class Reference
Collaboration diagram for BGC.Study.ProtocolManager:
Collaboration graph
[legend]

Data Structures

class  DataKeys
 

Public Member Functions

delegate void MigrateProtocols (ref JsonObject protocols)
 
delegate void SessionElementOverrun ()
 
delegate void PrepareNextElement (bool resuming)
 
delegate SessionElement ParseSessionElement (JsonObject sessionElement)
 

Static Public Member Functions

static void PrepareProtocol (string protocolName, int protocolID)
 
static ProtocolStatus TryUpdateProtocol (string protocolName, int protocolID, int sessionIndex, int sessionElementIndex=0)
 
static ProtocolStatus SetSession (int session, int element=0)
 
static JsonValue GetEnvValue (string key, JsonValue defaultReturn=default(JsonValue))
 
static bool GetEnvBool (string key, bool defaultValue=false)
 
static int GetEnvInt (string key, int defaultValue=0)
 
static string GetEnvStr (string key, string defaultValue="")
 
static float GetEnvFloat (string key, float defaultValue=0f)
 
static void RegisterSpecializers (SessionElementOverrun sessionElementOverrun, PrepareNextElement prepareNextElement, ParseSessionElement parseSessionElement, MigrateProtocols migrateProtocols)
 
static ProtocolStatus ExecuteNextElement (bool resuming=false)
 
static void SaveAs (string protocolName)
 
static void SerializeAll ()
 
static bool LoadProtocolSet (string protocolName)
 
static JsonArray SerializeProtocols ()
 
static void DeserializeProtocols (JsonArray protocols)
 
static JsonArray SerializeSessions ()
 
static void DeserializeSessions (JsonArray sessions)
 
static JsonArray SerializeSessionElements ()
 
static void DeserializeSessionElements (JsonArray elements)
 
static void UpdateDefaults ()
 
static string GetProtocolName (int protocolID)
 
static void HardClearAll ()
 

Data Fields

const int protocolDataVersion = 2
 

Static Public Attributes

static Dictionary< int, ProtocolprotocolDictionary = new Dictionary<int, Protocol>()
 
static Dictionary< int, SessionsessionDictionary = new Dictionary<int, Session>()
 
static Dictionary< int, SessionElementsessionElementDictionary
 
static Protocol currentProtocol = null
 
static Session currentSession = null
 
static SessionElement currentSessionElement = null
 
static int nextSessionElementIndex = -1
 

Properties

static int ElementNumber [get, set]
 
static int SessionNumber [get, set]
 
static bool SessionInProgress [get, set]
 

Static Private Member Functions

static void RemoveRedundancies ()
 

Private Attributes

const string protocolDataDir = "Protocols"
 

Static Private Attributes

static string loadedProtocol = ""
 
static SessionElementOverrun sessionElementOverrun = null
 
static PrepareNextElement prepareNextElement = null
 
static MigrateProtocols migrateProtocols = null
 
static ParseSessionElement parseSessionElement = null
 

Detailed Description

Definition at line 21 of file ProtocolManager.cs.

Member Function Documentation

◆ DeserializeProtocols()

static void BGC.Study.ProtocolManager.DeserializeProtocols ( JsonArray  protocols)
inlinestatic

Definition at line 471 of file ProtocolManager.cs.

References BGC.Study.ProtocolKeys.Protocol.EnvironmentValues, BGC.Study.ProtocolKeys.Protocol.Id, and BGC.Study.ProtocolKeys.Protocol.SessionIDs.

472  {
473  protocolDictionary.Clear();
474 
475  foreach (JsonObject protocol in protocols)
476  {
477  List<SessionID> sessions = new List<SessionID>();
478  foreach (int sessionID in protocol[ProtocolKeys.Protocol.SessionIDs].AsJsonArray)
479  {
480  sessions.Add(sessionID);
481  }
482 
483  JsonObject envVals;
484  if (protocol.ContainsKey(ProtocolKeys.Protocol.EnvironmentValues))
485  {
486  envVals = protocol[ProtocolKeys.Protocol.EnvironmentValues].AsJsonObject;
487  }
488  else
489  {
490  envVals = new JsonObject();
491  }
492 
493  protocolDictionary.Add(
494  protocol[ProtocolKeys.Protocol.Id],
495  new Protocol(protocol[ProtocolKeys.Protocol.Id].AsInteger)
496  {
497  name = protocol[ProtocolKeys.Protocol.Name],
498  sessions = sessions,
499  envVals = envVals
500  });
501  }
502  }
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
static Dictionary< int, Protocol > protocolDictionary

◆ DeserializeSessionElements()

static void BGC.Study.ProtocolManager.DeserializeSessionElements ( JsonArray  elements)
inlinestatic

Definition at line 540 of file ProtocolManager.cs.

References BGC.Study.SessionElement.id.

541  {
542  sessionElementDictionary.Clear();
543 
544  foreach (JsonObject element in elements)
545  {
546  SessionElement parsedElement = parseSessionElement?.Invoke(element);
547 
548  if (parsedElement == null)
549  {
550  Debug.LogError($"Failed to parse SessionElement: {element.ToString()}");
551  continue;
552  }
553 
554  sessionElementDictionary.Add(parsedElement.id, parsedElement);
555  }
556  }
static ParseSessionElement parseSessionElement
static Dictionary< int, SessionElement > sessionElementDictionary
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13

◆ DeserializeSessions()

static void BGC.Study.ProtocolManager.DeserializeSessions ( JsonArray  sessions)
inlinestatic

Definition at line 516 of file ProtocolManager.cs.

References BGC.Study.Session.id.

517  {
518  sessionDictionary.Clear();
519 
520  foreach (JsonObject sessionData in sessions)
521  {
522  Session session = new Session(sessionData);
523 
524  sessionDictionary.Add(session.id, session);
525  }
526  }
static Dictionary< int, Session > sessionDictionary
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13

◆ ExecuteNextElement()

static ProtocolStatus BGC.Study.ProtocolManager.ExecuteNextElement ( bool  resuming = false)
inlinestatic

Definition at line 251 of file ProtocolManager.cs.

References BGC.Study.SessionElement.CleanupElement(), BGC.Study.Session.Count, BGC.Study.SessionElement.ExecuteElement(), and BGC.Users.PlayerData.Save().

252  {
253  if (nextSessionElementIndex == -1)
254  {
255  return ProtocolStatus.Uninitialized;
256  }
257 
259  {
260  //In principle, this should only happen if an EndSessionElement was forgotten
261  ElementNumber = 0;
262  SessionInProgress = false;
263  ++SessionNumber;
264 
265  sessionElementOverrun?.Invoke();
266 
267  PlayerData.Save();
268 
269  return ProtocolStatus.SessionFinished;
270  }
271 
272  //This only happens when the last element was running
274 
276 
278  SessionInProgress = true;
279 
280  prepareNextElement?.Invoke(resuming);
281 
283 
284  PlayerData.Save();
285 
287 
288  return ProtocolStatus.SessionReady;
289  }
abstract void ExecuteElement(bool resuming=false)
static void Save()
Serialize the current user&#39;s data
static SessionElementOverrun sessionElementOverrun
virtual void CleanupElement()
static Session currentSession
static PrepareNextElement prepareNextElement
static SessionElement currentSessionElement
Here is the call graph for this function:

◆ GetEnvBool()

static bool BGC.Study.ProtocolManager.GetEnvBool ( string  key,
bool  defaultValue = false 
)
inlinestatic

Definition at line 181 of file ProtocolManager.cs.

References LightJson.JsonValue.AsBoolean, LightJson.JsonValue.AsInteger, LightJson.JsonValue.IsBoolean, and LightJson.JsonValue.IsInteger.

182  {
183  JsonValue val = GetEnvValue(key, defaultValue);
184 
185  if (val.IsBoolean)
186  {
187  return val.AsBoolean;
188  }
189 
190  if (val.IsInteger)
191  {
192  Debug.LogWarning($"EnvVal type mismatch: Found {val.Type}, Expected: {defaultValue.GetType().Name}. Converting.");
193  return val.AsInteger != 0;
194  }
195 
196  Debug.LogError($"EnvVal type mismatch: Found {val.Type}, Expected: {defaultValue.GetType().Name}");
197  return defaultValue;
198  }
A wrapper object that contains a valid JSON value.
Definition: JsonValue.cs:13
bool IsInteger
Indicates whether this JsonValue is an Integer.
Definition: JsonValue.cs:33
bool IsBoolean
Indicates whether this JsonValue is a Boolean.
Definition: JsonValue.cs:29
static JsonValue GetEnvValue(string key, JsonValue defaultReturn=default(JsonValue))
bool AsBoolean
This value as a Boolean type.
Definition: JsonValue.cs:65
int AsInteger
This value as an Integer type.
Definition: JsonValue.cs:82

◆ GetEnvFloat()

static float BGC.Study.ProtocolManager.GetEnvFloat ( string  key,
float  defaultValue = 0f 
)
inlinestatic

Definition at line 226 of file ProtocolManager.cs.

References LightJson.JsonValue.AsNumber, and LightJson.JsonValue.IsNumber.

227  {
228  JsonValue val = GetEnvValue(key, defaultValue);
229 
230  if (val.IsNumber)
231  {
232  return (float)val.AsNumber;
233  }
234 
235  Debug.LogError($"EnvVal type mismatch: Found {val.Type}, Expected: {defaultValue.GetType().Name}");
236  return defaultValue;
237  }
A wrapper object that contains a valid JSON value.
Definition: JsonValue.cs:13
static JsonValue GetEnvValue(string key, JsonValue defaultReturn=default(JsonValue))
double AsNumber
This value as a Number type.
Definition: JsonValue.cs:104
bool IsNumber
Indicates whether this JsonValue is a Number.
Definition: JsonValue.cs:49

◆ GetEnvInt()

static int BGC.Study.ProtocolManager.GetEnvInt ( string  key,
int  defaultValue = 0 
)
inlinestatic

Definition at line 200 of file ProtocolManager.cs.

References LightJson.JsonValue.AsInteger, and LightJson.JsonValue.IsInteger.

201  {
202  JsonValue val = GetEnvValue(key, defaultValue);
203 
204  if (val.IsInteger)
205  {
206  return val.AsInteger;
207  }
208 
209  Debug.LogError($"EnvVal type mismatch: Found {val.Type}, Expected: {defaultValue.GetType().Name}");
210  return defaultValue;
211  }
A wrapper object that contains a valid JSON value.
Definition: JsonValue.cs:13
bool IsInteger
Indicates whether this JsonValue is an Integer.
Definition: JsonValue.cs:33
static JsonValue GetEnvValue(string key, JsonValue defaultReturn=default(JsonValue))
int AsInteger
This value as an Integer type.
Definition: JsonValue.cs:82

◆ GetEnvStr()

static string BGC.Study.ProtocolManager.GetEnvStr ( string  key,
string  defaultValue = "" 
)
inlinestatic

Definition at line 213 of file ProtocolManager.cs.

References LightJson.JsonValue.AsString, and LightJson.JsonValue.IsString.

214  {
215  JsonValue val = GetEnvValue(key, defaultValue);
216 
217  if (val.IsString)
218  {
219  return val.AsString;
220  }
221 
222  Debug.LogError($"EnvVal type mismatch: Found {val.Type}, Expected: {defaultValue.GetType().Name}");
223  return defaultValue;
224  }
string AsString
This value as a String type.
Definition: JsonValue.cs:131
A wrapper object that contains a valid JSON value.
Definition: JsonValue.cs:13
static JsonValue GetEnvValue(string key, JsonValue defaultReturn=default(JsonValue))
bool IsString
Indicates whether this JsonValue is a String.
Definition: JsonValue.cs:52

◆ GetEnvValue()

static JsonValue BGC.Study.ProtocolManager.GetEnvValue ( string  key,
JsonValue  defaultReturn = default(JsonValue) 
)
inlinestatic

Definition at line 153 of file ProtocolManager.cs.

References LightJson.JsonObject.ContainsKey(), BGC.Study.SessionElement.envVals, BGC.Study.Protocol.envVals, and BGC.Study.Session.envVals.

154  {
155  if (loadedProtocol == "" ||
156  currentProtocol == null || currentSession == null || currentSessionElement == null)
157  {
158  Debug.Log($"EnvVal not ready for query: {key}");
159  return defaultReturn;
160  }
161 
163  {
164  return currentSessionElement.envVals[key];
165  }
166 
168  {
169  return currentSession.envVals[key];
170  }
171 
173  {
174  return currentProtocol.envVals[key];
175  }
176 
177  Debug.Log($"EnvVal not found: {key}");
178  return defaultReturn;
179  }
bool ContainsKey(string key)
Determines whether this collection contains an item assosiated with the given key.
static Session currentSession
static Protocol currentProtocol
JsonObject envVals
Definition: Protocol.cs:16
JsonObject envVals
Definition: Protocol.cs:81
static SessionElement currentSessionElement
Here is the call graph for this function:

◆ GetProtocolName()

static string BGC.Study.ProtocolManager.GetProtocolName ( int  protocolID)
inlinestatic

Definition at line 570 of file ProtocolManager.cs.

571  {
572  if (protocolDictionary.ContainsKey(protocolID))
573  {
574  return protocolDictionary[protocolID].name;
575  }
576 
577  return "Invalid";
578  }
static Dictionary< int, Protocol > protocolDictionary

◆ HardClearAll()

static void BGC.Study.ProtocolManager.HardClearAll ( )
inlinestatic

Definition at line 580 of file ProtocolManager.cs.

References BGC.Study.Protocol.HardClear(), BGC.Study.SessionElement.HardClear(), and BGC.Study.Session.HardClear().

581  {
582  loadedProtocol = "";
583  protocolDictionary.Clear();
584  sessionDictionary.Clear();
585  sessionElementDictionary.Clear();
586 
587  Protocol.HardClear();
588  Session.HardClear();
589  SessionElement.HardClear();
590  }
static Dictionary< int, Session > sessionDictionary
static Dictionary< int, SessionElement > sessionElementDictionary
static Dictionary< int, Protocol > protocolDictionary
Here is the call graph for this function:

◆ LoadProtocolSet()

static bool BGC.Study.ProtocolManager.LoadProtocolSet ( string  protocolName)
inlinestatic

Definition at line 402 of file ProtocolManager.cs.

References BGC.IO.DataManagement.PathForDataDirectory(), BGC.Study.ProtocolKeys.Protocols, BGC.IO.FileReader.ReadJsonFile(), BGC.Study.ProtocolKeys.SessionElements, and BGC.Study.ProtocolKeys.Sessions.

403  {
404  if (protocolName == "")
405  {
406  protocolName = "DefaultSet";
407  }
408 
409  if (loadedProtocol == protocolName)
410  {
411  return true;
412  }
413 
414  string previousLoadedProtocol = loadedProtocol;
415  loadedProtocol = protocolName;
416  foreach (char c in Path.GetInvalidFileNameChars())
417  {
418  loadedProtocol = loadedProtocol.Replace(c.ToString(), "");
419  }
420 
421  return FileReader.ReadJsonFile(
422  path: Path.Combine(DataManagement.PathForDataDirectory(protocolDataDir), $"{loadedProtocol}.json"),
423  successCallback: (JsonObject jsonProtocols) =>
424  {
425  migrateProtocols?.Invoke(ref jsonProtocols);
426 
427  DeserializeProtocols(jsonProtocols[ProtocolKeys.Protocols]);
428  DeserializeSessions(jsonProtocols[ProtocolKeys.Sessions]);
429  DeserializeSessionElements(jsonProtocols[ProtocolKeys.SessionElements]);
430  },
431  failCallback: () =>
432  {
433  loadedProtocol = "";
434  protocolDictionary.Clear();
435  sessionDictionary.Clear();
436  sessionElementDictionary.Clear();
437  },
438  fileNotFoundCallback: () => loadedProtocol = previousLoadedProtocol);
439  }
static bool ReadJsonFile(string path, Action< JsonObject > successCallback, Action failCallback=null, Action fileNotFoundCallback=null, Action< string, Exception > overrideExceptionHandling=null)
Offers two error callbacks for when a file is not found and for when a file failed to parse correctly...
Definition: FileReader.cs:24
static Dictionary< int, Session > sessionDictionary
static string PathForDataDirectory(string dataDirectory)
Returns the full path to the dataDirectory directory.
static void DeserializeSessionElements(JsonArray elements)
static Dictionary< int, SessionElement > sessionElementDictionary
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
static Dictionary< int, Protocol > protocolDictionary
static MigrateProtocols migrateProtocols
static void DeserializeProtocols(JsonArray protocols)
static void DeserializeSessions(JsonArray sessions)
Here is the call graph for this function:

◆ MigrateProtocols()

delegate void BGC.Study.ProtocolManager.MigrateProtocols ( ref JsonObject  protocols)

◆ ParseSessionElement()

delegate SessionElement BGC.Study.ProtocolManager.ParseSessionElement ( JsonObject  sessionElement)

◆ PrepareNextElement()

delegate void BGC.Study.ProtocolManager.PrepareNextElement ( bool  resuming)

◆ PrepareProtocol()

static void BGC.Study.ProtocolManager.PrepareProtocol ( string  protocolName,
int  protocolID 
)
inlinestatic

Definition at line 74 of file ProtocolManager.cs.

75  {
76  currentProtocol = null;
77  currentSession = null;
78  currentSessionElement = null;
80  loadedProtocol = "";
81 
82  LoadProtocolSet(protocolName);
83  if (protocolDictionary.ContainsKey(protocolID))
84  {
85  currentProtocol = protocolDictionary[protocolID];
86  }
87  else
88  {
89  Debug.LogError($"Loaded Protocol \"{loadedProtocol}\" does not contain requested protocolID {protocolID}.");
90  return;
91  }
92  }
static bool LoadProtocolSet(string protocolName)
static Session currentSession
static Protocol currentProtocol
static Dictionary< int, Protocol > protocolDictionary
static SessionElement currentSessionElement

◆ RegisterSpecializers()

static void BGC.Study.ProtocolManager.RegisterSpecializers ( SessionElementOverrun  sessionElementOverrun,
PrepareNextElement  prepareNextElement,
ParseSessionElement  parseSessionElement,
MigrateProtocols  migrateProtocols 
)
inlinestatic

Definition at line 239 of file ProtocolManager.cs.

References BGC.Study.ProtocolManager.migrateProtocols, BGC.Study.ProtocolManager.parseSessionElement, BGC.Study.ProtocolManager.prepareNextElement, and BGC.Study.ProtocolManager.sessionElementOverrun.

244  {
245  ProtocolManager.sessionElementOverrun = sessionElementOverrun;
246  ProtocolManager.prepareNextElement = prepareNextElement;
247  ProtocolManager.parseSessionElement = parseSessionElement;
248  ProtocolManager.migrateProtocols = migrateProtocols;
249  }
static ParseSessionElement parseSessionElement
static SessionElementOverrun sessionElementOverrun
static PrepareNextElement prepareNextElement
static MigrateProtocols migrateProtocols

◆ RemoveRedundancies()

static void BGC.Study.ProtocolManager.RemoveRedundancies ( )
inlinestaticprivate

Definition at line 298 of file ProtocolManager.cs.

References BGC.Study.ProtocolKeys.Session.Id, BGC.Study.ProtocolKeys.SessionElement.Id, LightJson.JsonObject.Remove(), and LightJson.JsonObject.ToString().

299  {
300  Dictionary<int, int> sessionElementRemapping = new Dictionary<int, int>();
301  Dictionary<string, int> sessionElements = new Dictionary<string, int>();
302 
303  foreach (KeyValuePair<int, SessionElement> sessionElement in sessionElementDictionary)
304  {
305  JsonObject serializedElement = sessionElement.Value.SerializeElement();
306  serializedElement.Remove(ProtocolKeys.SessionElement.Id);
307 
308  string serialization = serializedElement.ToString();
309  if (sessionElements.ContainsKey(serialization))
310  {
311  //Collision - Add it to be remapped
312  int newID = sessionElements[serialization];
313  sessionElementRemapping.Add(sessionElement.Key, newID);
314  }
315  else
316  {
317  //It's a different sessionElement - Add it to the dictionary
318  sessionElements.Add(serialization, sessionElement.Key);
319  }
320  }
321 
322  Dictionary<int, int> sessionRemapping = new Dictionary<int, int>();
323  Dictionary<string, int> sessions = new Dictionary<string, int>();
324 
325  foreach (KeyValuePair<int, Session> session in sessionDictionary)
326  {
327  //Apply Remapping To Sessions
328  List<SessionElementID> elementIDs = session.Value.sessionElements;
329 
330  for (int i = 0; i < elementIDs.Count; i++)
331  {
332  if (sessionElementRemapping.ContainsKey(elementIDs[i].id))
333  {
334  elementIDs[i] = new SessionElementID(sessionElementRemapping[elementIDs[i].id]);
335  }
336  }
337 
338  JsonObject serializedSession = session.Value.SerializeSession();
339  serializedSession.Remove(ProtocolKeys.Session.Id);
340 
341  string serialization = serializedSession.ToString();
342  if (sessions.ContainsKey(serialization))
343  {
344  //Collision - Add it to be remapped
345  int newID = sessions[serialization];
346  sessionRemapping.Add(session.Key, newID);
347  }
348  else
349  {
350  //It's a different session - Add it to the dictionary
351  sessions.Add(serialization, session.Key);
352  }
353  }
354 
355  foreach (KeyValuePair<int, Protocol> protocol in protocolDictionary)
356  {
357  //Apply Remapping To Protocols
358  List<SessionID> sessionIDs = protocol.Value.sessions;
359 
360  for (int i = 0; i < sessionIDs.Count; i++)
361  {
362  if (sessionRemapping.ContainsKey(sessionIDs[i].id))
363  {
364  sessionIDs[i] = new SessionID(sessionRemapping[sessionIDs[i].id]);
365  }
366  }
367  }
368 
369  //Remove eliminated SessionElements
370  foreach (int sessionElementID in sessionElementRemapping.Keys)
371  {
372  sessionElementDictionary.Remove(sessionElementID);
373  }
374 
375  //Remove eliminated Sessions
376  foreach (int sessionID in sessionRemapping.Keys)
377  {
378  sessionDictionary.Remove(sessionID);
379  }
380  }
override string ToString()
Returns a JSON string representing the state of the object.
static Dictionary< int, Session > sessionDictionary
bool Remove(string key)
Removes a property with the given key.
static Dictionary< int, SessionElement > sessionElementDictionary
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
static Dictionary< int, Protocol > protocolDictionary
Here is the call graph for this function:

◆ SaveAs()

static void BGC.Study.ProtocolManager.SaveAs ( string  protocolName)
inlinestatic

Definition at line 291 of file ProtocolManager.cs.

292  {
293  loadedProtocol = protocolName;
295  SerializeAll();
296  }
static void RemoveRedundancies()

◆ SerializeAll()

static void BGC.Study.ProtocolManager.SerializeAll ( )
inlinestatic

Definition at line 382 of file ProtocolManager.cs.

References BGC.IO.DataManagement.PathForDataDirectory(), BGC.Study.ProtocolKeys.Protocols, BGC.Study.ProtocolKeys.SessionElements, BGC.Study.ProtocolKeys.Sessions, BGC.Study.ProtocolKeys.Version, and BGC.IO.FileWriter.WriteJson().

383  {
384  if (loadedProtocol == "")
385  {
386  Debug.LogError("Can't serialize protocol - none loaded");
387  return;
388  }
389 
391  path: Path.Combine(DataManagement.PathForDataDirectory(protocolDataDir), $"{loadedProtocol}.json"),
392  createJson: () => new JsonObject()
393  {
394  { ProtocolKeys.Version, protocolDataVersion },
395  { ProtocolKeys.Protocols, SerializeProtocols() },
396  { ProtocolKeys.Sessions, SerializeSessions() },
397  { ProtocolKeys.SessionElements, SerializeSessionElements() }
398  },
399  pretty: true);
400  }
static bool WriteJson(string path, Func< JsonObject > createJson, bool pretty=false, Action failCallback=null, Action< string, Exception > overrideExceptionHandling=null)
Definition: FileWriter.cs:11
static string PathForDataDirectory(string dataDirectory)
Returns the full path to the dataDirectory directory.
static JsonArray SerializeProtocols()
static JsonArray SerializeSessionElements()
static JsonArray SerializeSessions()
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
Here is the call graph for this function:

◆ SerializeProtocols()

static JsonArray BGC.Study.ProtocolManager.SerializeProtocols ( )
inlinestatic

Definition at line 441 of file ProtocolManager.cs.

References LightJson.JsonObject.Add(), LightJson.JsonObject.Count, BGC.Study.ProtocolKeys.Protocol.EnvironmentValues, BGC.Study.Protocol.envVals, BGC.Study.Protocol.id, BGC.Study.ProtocolKeys.Protocol.Id, BGC.Study.SessionID.id, BGC.Study.Protocol.name, BGC.Study.ProtocolKeys.Protocol.Name, BGC.Study.ProtocolKeys.Protocol.SessionIDs, and BGC.Study.Protocol.sessions.

442  {
443  JsonArray protocols = new JsonArray();
444 
445  foreach (Protocol protocol in protocolDictionary.Values)
446  {
447  JsonArray jsonSessionIDs = new JsonArray();
448  foreach (SessionID sessionID in protocol.sessions)
449  {
450  jsonSessionIDs.Add(sessionID.id);
451  }
452 
453  JsonObject newProtocol = new JsonObject()
454  {
455  { ProtocolKeys.Protocol.Id, protocol.id },
456  { ProtocolKeys.Protocol.Name, protocol.name },
457  { ProtocolKeys.Protocol.SessionIDs, jsonSessionIDs }
458  };
459 
460  if (protocol.envVals.Count > 0)
461  {
462  newProtocol.Add(ProtocolKeys.Protocol.EnvironmentValues, protocol.envVals);
463  }
464 
465  protocols.Add(newProtocol);
466  }
467 
468  return protocols;
469  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Represents a key-value pair collection of JsonValue objects.
Definition: JsonObject.cs:13
static Dictionary< int, Protocol > protocolDictionary
JsonObject Add(string key)
Adds a key with a null value to this collection.
Here is the call graph for this function:

◆ SerializeSessionElements()

static JsonArray BGC.Study.ProtocolManager.SerializeSessionElements ( )
inlinestatic

Definition at line 528 of file ProtocolManager.cs.

References BGC.Study.SessionElement.SerializeElement().

529  {
530  JsonArray sessions = new JsonArray();
531 
532  foreach (SessionElement element in sessionElementDictionary.Values)
533  {
534  sessions.Add(element.SerializeElement());
535  }
536 
537  return sessions;
538  }
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
static Dictionary< int, SessionElement > sessionElementDictionary
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Here is the call graph for this function:

◆ SerializeSessions()

static JsonArray BGC.Study.ProtocolManager.SerializeSessions ( )
inlinestatic

Definition at line 504 of file ProtocolManager.cs.

References BGC.Study.Session.SerializeSession().

505  {
506  JsonArray sessions = new JsonArray();
507 
508  foreach (Session session in sessionDictionary.Values)
509  {
510  sessions.Add(session.SerializeSession());
511  }
512 
513  return sessions;
514  }
static Dictionary< int, Session > sessionDictionary
Represents an ordered collection of JsonValues.
Definition: JsonArray.cs:11
JsonArray Add(JsonValue value)
Adds the given value to this collection.
Definition: JsonArray.cs:65
Here is the call graph for this function:

◆ SessionElementOverrun()

delegate void BGC.Study.ProtocolManager.SessionElementOverrun ( )

◆ SetSession()

static ProtocolStatus BGC.Study.ProtocolManager.SetSession ( int  session,
int  element = 0 
)
inlinestatic

Definition at line 118 of file ProtocolManager.cs.

References BGC.Study.Protocol.Count, BGC.Study.Session.Count, and BGC.Study.Protocol.id.

119  {
120  currentSession = null;
121  currentSessionElement = null;
123 
124  if (loadedProtocol == "")
125  {
126  return ProtocolStatus.Uninitialized;
127  }
128 
129  if (!protocolDictionary.ContainsKey(currentProtocol.id))
130  {
131  return ProtocolStatus.InvalidProtocol;
132  }
133 
134  if (session >= currentProtocol.Count)
135  {
136  return ProtocolStatus.SessionLimitExceeded;
137  }
138 
139  SessionNumber = session;
140  currentSession = currentProtocol[session];
141 
142  if (element >= currentSession.Count)
143  {
144  return ProtocolStatus.SessionElementLimitExceeded;
145  }
146 
147  //The next session element to run will be the current one
148  nextSessionElementIndex = element;
149 
150  return ProtocolStatus.SessionReady;
151  }
static Session currentSession
static Protocol currentProtocol
readonly int id
Definition: Protocol.cs:12
static Dictionary< int, Protocol > protocolDictionary
static SessionElement currentSessionElement

◆ TryUpdateProtocol()

static ProtocolStatus BGC.Study.ProtocolManager.TryUpdateProtocol ( string  protocolName,
int  protocolID,
int  sessionIndex,
int  sessionElementIndex = 0 
)
inlinestatic

Definition at line 94 of file ProtocolManager.cs.

99  {
100  if (LoadProtocolSet(protocolName))
101  {
102  if (protocolDictionary.ContainsKey(protocolID))
103  {
104  currentProtocol = protocolDictionary[protocolID];
105 
106  return SetSession(sessionIndex, sessionElementIndex);
107  }
108  else
109  {
110  Debug.LogError($"Loaded Protocol \"{loadedProtocol}\" does not contain requested protocolID {protocolID}.");
111  return ProtocolStatus.InvalidProtocol;
112  }
113  }
114 
115  return ProtocolStatus.Uninitialized;
116  }
static ProtocolStatus SetSession(int session, int element=0)
static bool LoadProtocolSet(string protocolName)
static Protocol currentProtocol
static Dictionary< int, Protocol > protocolDictionary

◆ UpdateDefaults()

static void BGC.Study.ProtocolManager.UpdateDefaults ( )
inlinestatic

Definition at line 558 of file ProtocolManager.cs.

References BGC.IO.FileExtensions.AddJsonExtension(), and BGC.IO.DataManagement.PathForDataDirectory().

559  {
561  TextAsset[] assets = Resources.LoadAll<TextAsset>(protocolDataDir);
562  foreach (TextAsset asset in assets)
563  {
564  string currentFile = Path.Combine(path, FileExtensions.AddJsonExtension(asset.name));
565 
566  File.WriteAllText(currentFile, asset.text);
567  }
568  }
static string PathForDataDirectory(string dataDirectory)
Returns the full path to the dataDirectory directory.
static string AddJsonExtension(string str)
Add json extension to string
Here is the call graph for this function:

Field Documentation

◆ currentProtocol

Protocol BGC.Study.ProtocolManager.currentProtocol = null
static

Definition at line 40 of file ProtocolManager.cs.

◆ currentSession

Session BGC.Study.ProtocolManager.currentSession = null
static

Definition at line 41 of file ProtocolManager.cs.

◆ currentSessionElement

SessionElement BGC.Study.ProtocolManager.currentSessionElement = null
static

Definition at line 42 of file ProtocolManager.cs.

◆ loadedProtocol

string BGC.Study.ProtocolManager.loadedProtocol = ""
staticprivate

Definition at line 26 of file ProtocolManager.cs.

◆ migrateProtocols

MigrateProtocols BGC.Study.ProtocolManager.migrateProtocols = null
staticprivate

Definition at line 71 of file ProtocolManager.cs.

Referenced by BGC.Study.ProtocolManager.RegisterSpecializers().

◆ nextSessionElementIndex

int BGC.Study.ProtocolManager.nextSessionElementIndex = -1
static

Definition at line 44 of file ProtocolManager.cs.

◆ parseSessionElement

ParseSessionElement BGC.Study.ProtocolManager.parseSessionElement = null
staticprivate

Definition at line 72 of file ProtocolManager.cs.

Referenced by BGC.Study.ProtocolManager.RegisterSpecializers().

◆ prepareNextElement

PrepareNextElement BGC.Study.ProtocolManager.prepareNextElement = null
staticprivate

Definition at line 70 of file ProtocolManager.cs.

Referenced by BGC.Study.ProtocolManager.RegisterSpecializers().

◆ protocolDataDir

const string BGC.Study.ProtocolManager.protocolDataDir = "Protocols"
private

Definition at line 23 of file ProtocolManager.cs.

◆ protocolDataVersion

const int BGC.Study.ProtocolManager.protocolDataVersion = 2

Definition at line 24 of file ProtocolManager.cs.

◆ protocolDictionary

Dictionary<int, Protocol> BGC.Study.ProtocolManager.protocolDictionary = new Dictionary<int, Protocol>()
static

Definition at line 35 of file ProtocolManager.cs.

Referenced by BGC.Study.Protocol.Protocol().

◆ sessionDictionary

Dictionary<int, Session> BGC.Study.ProtocolManager.sessionDictionary = new Dictionary<int, Session>()
static

Definition at line 36 of file ProtocolManager.cs.

Referenced by BGC.Study.Session.Session().

◆ sessionElementDictionary

Dictionary<int, SessionElement> BGC.Study.ProtocolManager.sessionElementDictionary
static
Initial value:
=
new Dictionary<int, SessionElement>()

Definition at line 37 of file ProtocolManager.cs.

Referenced by BGC.Study.SessionElement.SessionElement().

◆ sessionElementOverrun

SessionElementOverrun BGC.Study.ProtocolManager.sessionElementOverrun = null
staticprivate

Definition at line 69 of file ProtocolManager.cs.

Referenced by BGC.Study.ProtocolManager.RegisterSpecializers().

Property Documentation

◆ ElementNumber

int BGC.Study.ProtocolManager.ElementNumber
staticgetset

Definition at line 47 of file ProtocolManager.cs.

◆ SessionInProgress

bool BGC.Study.ProtocolManager.SessionInProgress
staticgetset

Definition at line 59 of file ProtocolManager.cs.

◆ SessionNumber

int BGC.Study.ProtocolManager.SessionNumber
staticgetset

Definition at line 53 of file ProtocolManager.cs.


The documentation for this class was generated from the following file: