示例#1
0
 /// <summary>
 /// Toggles the value of the given variable in its corresponding collections
 /// </summary>
 /// <param name="variableName">The name of the variable to search for</param>
 public void FlipValue(string variableName)
 {
     if (IndVars.ContainsKey(variableName))
     {
         SetValue(variableName, !IndVars[variableName].Value);
     }
 }
示例#2
0
        /// <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);
        }
示例#3
0
        /// <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);
        }
示例#4
0
        /// <summary>
        /// Converts an independent variable to a dependent variable.
        /// </summary>
        /// <param name="name">Variable to convert</param>
        public void MakeDependent(string name)
        {
            // Get the value of the variable
            bool value = IndVars[name].Value;

            // Remove variable from independent variables dictionary
            IndVars.Remove(name);
            // Remove variable from variables dictionary
            AllVars.Remove(name);
            // Add new dependent variable to the dictionaries
            AddVariable(new DependentVariable(name, value));
        }
示例#5
0
        /// <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;
                }
            }
        }