Esempio n. 1
0
        /// <summary>
        /// Create a new variable.
        /// </summary>
        /// <remarks>
        /// Variables created using this method cannot be found back using the method <see cref="MapNode(string,VariableType)"/>.
        /// </remarks>
        /// <param name="id">The identifier of the new variable.</param>
        /// <param name="type">The type of the variable.</param>
        /// <returns>A new variable.</returns>
        public Variable Create(string id, VariableType type)
        {
            if (_locked)
            {
                throw new CircuitException("Nodes are locked, mapping is not allowed anymore");
            }

            // Create the node
            var index = _unknowns.Count + 1;
            var node  = new Variable(id, type, index);

            _unknowns.Add(node);

            // Call the event
            var args = new VariableEventArgs(node);

            OnVariableAdded(args);
            return(node);
        }
Esempio n. 2
0
        /// <summary>
        /// This method maps a variable in the circuit. If a variable with the same identifier already exists, then that variable is returned.
        /// </summary>
        /// <remarks>
        /// If the variable already exists, the variable type is ignored.
        /// </remarks>
        /// <param name="id">The identifier of the variable.</param>
        /// <param name="type">The type of the variable.</param>
        /// <returns>A new variable with the specified identifier and type, or a previously mapped variable if it already existed.</returns>
        public Variable MapNode(string id, VariableType type)
        {
            if (_locked)
            {
                throw new CircuitException("Nodes are locked, mapping is not allowed anymore");
            }

            // Check the node
            if (_map.ContainsKey(id))
            {
                return(_map[id]);
            }

            var node = new Variable(id, type, _unknowns.Count + 1);

            _unknowns.Add(node);
            _map.Add(id, node);

            // Call the event
            var args = new VariableEventArgs(node);

            OnVariableAdded(args);
            return(node);
        }
Esempio n. 3
0
 /// <summary>
 /// Method that calls the <see cref="VariableAdded"/> event.
 /// </summary>
 /// <param name="variable"></param>
 protected virtual void OnVariableAdded(VariableEventArgs args) => VariableAdded?.Invoke(this, args);