public void GetValues(ITimeSpaceValueSet<double> targetSet, IBaseExchangeItem querySpecifier)
      {
        // Copy values from the adaptee to the targetSet
        ITimeSpaceValueSet sourceValues = _adaptee.GetValues(querySpecifier);

        for (int i = 0; i < targetSet.TimesCount(); i++)
        {
          double[] sourceTimeValues = sourceValues.GetElementValuesForTime<double>(i);
          for (int j = 0; j < targetSet.ElementCount(); j++)
          {
            targetSet.Values2D[i][j] += sourceTimeValues[j];
          }
        }
      }
        public override ITimeSpaceValueSet GetValues(IBaseExchangeItem querySpecifier2)
        {
            ITimeSpaceExchangeItem timeSpaceQuery = querySpecifier2 as ITimeSpaceExchangeItem;

            if (timeSpaceQuery == null)
            {
                throw new ArgumentException("querySpecifier must be an ITimeSpaceExchangeItem - add an adaptor");
            }

            if (!(timeSpaceQuery is ITimeSpaceInput))
            {
                throw new OpenMIException("Get Values can only be called with an Input as argument")
                      {
                          Component = Adaptee.Component, Output = this
                      };
            }


            /// Using a _query item in the
            ///     _adaptee.GetValues(_query)
            /// instead of
            ///     _adaptee.GetValues(this)
            ///
            /// Reason:
            /// If using the latter, it would be required to set the internal
            ///     TimeSet = timeSpaceQuery.TimeSet;
            /// but then internal Values (retrieved from adaptee) does not match TimeSet.
            /// And also:
            /// - the TimeSet is set to override and return _adaptee.TimeSet
            /// - the spatial definition would not match the adaptee

            // If uncommenting this, comment out also the overidden TimeSet property above
            //TimeSet = timeSpaceQuery.TimeSet;

            // Set query time to internal query item
            _query.TimeSet = timeSpaceQuery.TimeSet;

            ITimeSpaceValueSet          incomingValues = _adaptee.GetValues(_query);
            ITimeSpaceValueSet <double> resultValues   = ElementMapper.CreateResultValueSet(incomingValues.TimesCount(), SpatialDefinition.ElementCount);

            // Transform the values from the adaptee
            _elementMapper.MapValues(ref resultValues, ref incomingValues);

            return(resultValues);
        }
        protected internal override void Update()
        {
            if (_providers.Count > 0)
            {
                ITime inputTime = GetInputTime();
                if (inputTime == null)
                {
                    return;
                }

                TimeSet.SetSingleTime(inputTime);

                // the set of values that is set to the engine
                ITimeSpaceValueSet values = null;

                // Loop through all providers
                for (int i = 0; i < _providers.Count; i++)
                {
                    ITimeSpaceOutput provider = _providers[i];

                    // Create the values variable for the first provider
                    // and add the result from the remaining providers to this
                    if (i == 0)
                    {
                        values = provider.GetValues(this);
                    }
                    else
                    {
                        ITimeSpaceValueSet addValues = provider.GetValues(this);

                        // Add the addValues to the values
                        int times = values.TimesCount();
                        int elmts = values.ElementCount();

                        if (addValues.TimesCount() != times ||
                            addValues.ElementCount() != elmts)
                        {
                            throw new Exception("Size of inputs differs, valuesets can not be added");
                        }

                        for (int j = 0; j < times; j++)
                        {
                            for (int k = 0; k < elmts; k++)
                            {
                                // would be really nice if the value set was templated (to avoid casting)
                                values.Values2D[j][k] = ((double)values.Values2D[j][k]) + ((double)addValues.Values2D[j][k]);
                            }
                        }
                    }
                }
                ITimeSpaceValueSet incomingValues = values;


                if (StoreValuesInExchangeItem)
                {
                    _values = incomingValues;
                    _linkableEngine.InputItemsToBeProcessed.Add(this);
                    HasBeenProcessed = false;
                }
                else
                {
                    // Here we do not add this input item to the list of InputItemsToBeProcessed, we process immediately
                    SetValuesToEngine(incomingValues);
                    HasBeenProcessed = true;
                }
            }
            else
            {
                throw new Exception("Trying to update an input item without a provider.");
            }
        }
        /// <summary>
        /// MapValues calculates for each set of timestep data 
        /// a resulting IValueSet through multiplication of an inputValues IValueSet
        /// vector with the mapping maprix. 
        /// </summary>
        /// <param name="inputValues">IValueSet of values to be mapped.</param>
        /// <returns>
        /// A IValueSet found by mapping of the inputValues on to the toElementSet.
        /// </returns>
        public TimeSpaceValueSet<double> MapValues(ITimeSpaceValueSet inputValues)
        {
            if (!_isInitialised)
            {
                throw new Exception(
                    "ElementMapper objects needs to be initialised before the MapValue method can be used");
            }
            if (!ValueSet.GetElementCount(inputValues).Equals(_numberOfColumns))
            {
                throw new Exception("Dimension mismatch between inputValues and mapping matrix");
            }

            // Make a time-space value set of the correct size
            TimeSpaceValueSet<double> result = CreateResultValueSet(inputValues.TimesCount(), _numberOfRows);

            MapValues(result, inputValues);
            
            return result;
        }