/// <summary> /// Fetches a variable from the collection of variables matching the given type /// </summary> /// <typeparam name="T">The type of the collection of variables to search</typeparam> /// <param name="name">The string representation of the given variable to search for</param> /// <returns>Returns the variable if it was found, else returns null</returns> public Variable TryGetVariable <T>(string name) where T : Variable { Type varType = typeof(T); if (varType == typeof(IndependentVariable)) { if (IndVars.ContainsKey(name)) { return(IndVars[name]); } } else if (varType == typeof(DependentVariable)) { if (DepVars.ContainsKey(name)) { return(DepVars[name]); } } else if (varType == typeof(Variable)) { if (AllVars.ContainsKey(name)) { return(AllVars[name]); } } return(null); }
/// <summary> /// Adds a variable to the collection of variables of the given type /// </summary> /// <typeparam name="T">The type matching the target collection of variables</typeparam> /// <param name="v">The variable to add to the collection of matching type</param> /// <returns>Returns true if the variable was successfully added</returns> public bool AddVariable <T>(T v) { Type varType = typeof(T); if (varType == typeof(IndependentVariable)) { IndependentVariable iv = (IndependentVariable)Convert.ChangeType(v, typeof(IndependentVariable)); if (!IndVars.ContainsKey(iv.Name)) { IndVars.Add(iv.Name, iv); } if (!AllVars.ContainsKey(iv.Name)) { AllVars.Add(iv.Name, iv); } } else { DependentVariable dv = (DependentVariable)Convert.ChangeType(v, typeof(DependentVariable)); if (!DepVars.ContainsKey(dv.Name)) { DepVars.Add(dv.Name, dv); } if (!AllVars.ContainsKey(dv.Name)) { AllVars.Add(dv.Name, dv); } } return(true); }
/// <summary> /// Sets specific value /// </summary> /// <param name="variableName"></param> /// <param name="value"></param> public void SetValue(string variableName, bool value, bool updateAltClocks = true) { if (IndVars.ContainsKey(variableName)) { IndVars[variableName].Value = value; foreach (string dependent in DependencyLists.Keys) { if (DependencyLists[dependent].Contains(variableName)) { Expressions[dependent].Evaluate(); } } } else if (DepVars.ContainsKey(variableName)) { DepVars[variableName].Value = value; foreach (string dependent in DependencyLists.Keys) { if (DependencyLists[dependent].Contains(variableName)) { Expressions[dependent].Evaluate(); } } } if (updateAltClocks) { if (AltClocks.Count > 0 && AltClocks.ContainsKey(variableName)) { if (!AltClocks[variableName] && value) { foreach (var clockStatement in ClockStatements) { if (clockStatement.Clock == variableName) { clockStatement.Tick(); } } } // Update alternate clock value AltClocks[variableName] = value; } } }