public void GetValues()
        {
            HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();
            TimeSeriesComponent ts = new TimeSeriesComponent();

            ts.Initialize(new Argument[0]);
            Link triggerLink = new Link();

            triggerLink.ID = "TriggerLink";
            triggerLink.SourceComponent  = ts;
            triggerLink.SourceElementSet = ts.GetOutputExchangeItem(0).ElementSet;
            triggerLink.SourceQuantity   = ts.GetOutputExchangeItem(0).Quantity;
            triggerLink.TargetComponent  = trigger;
            triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
            triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;

            trigger.AddLink(triggerLink);
            ts.AddLink(triggerLink);

            double tt = ts.TimeHorizon.Start.ModifiedJulianDay;

            TimeStamp[] triggerTimes = new TimeStamp[4];
            triggerTimes[0] = new TimeStamp(tt + 0.5);
            triggerTimes[1] = new TimeStamp(tt + 1.5);
            triggerTimes[2] = new TimeStamp(tt + 1.9);
            triggerTimes[3] = new TimeStamp(tt + 2.1);

            trigger.Run(triggerTimes);
            Assert.AreEqual(0, ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0));
            Assert.AreEqual(1, ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0));
            Assert.AreEqual(1, ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(2)).GetScalar(0));
            Assert.AreEqual(2, ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(3)).GetScalar(0));

            //Teting with timespans
            Assert.AreEqual(2.0, ((ScalarSet)ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2), new TimeStamp(tt + 2.0 + 1.0 / 24.0)), "TriggerLink")).GetScalar(0));
            Assert.AreEqual(2.0, ((ScalarSet)ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 1.0 / 24.0), new TimeStamp(tt + 2.0 + 2.0 / 24.0)), "TriggerLink")).GetScalar(0));
            Assert.AreEqual(2.0, ((ScalarSet)ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 2.0 / 24.0), new TimeStamp(tt + 2.0 + 3.0 / 24.0)), "TriggerLink")).GetScalar(0));
            Assert.AreEqual(2.0, ((ScalarSet)ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 5.0 / 24.0), new TimeStamp(tt + 2.0 + 6.0 / 24.0)), "TriggerLink")).GetScalar(0));
            Assert.AreEqual(2.0, ((ScalarSet)ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 15.0 / 24.0), new TimeStamp(tt + 2.0 + 16.0 / 24.0)), "TriggerLink")).GetScalar(0));
            Assert.AreEqual(2.0, ((ScalarSet)ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 23.0 / 24.0), new TimeStamp(tt + 2.0 + 24.0 / 24.0)), "TriggerLink")).GetScalar(0));
            Assert.AreEqual(3.0, ((ScalarSet)ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 3.0), new TimeStamp(tt + 3.0 + 1.0 / 24.0)), "TriggerLink")).GetScalar(0));
        }
		public void GetValues()
		{
			HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();
			TimeSeriesComponent ts = new TimeSeriesComponent();

			ts.Initialize(new Argument[0]);
			Link triggerLink = new Link();

			triggerLink.ID				 = "TriggerLink";
			triggerLink.SourceComponent  = ts;
			triggerLink.SourceElementSet = ts.GetOutputExchangeItem(0).ElementSet;
			triggerLink.SourceQuantity   = ts.GetOutputExchangeItem(0).Quantity;
			triggerLink.TargetComponent  = trigger;
			triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
			triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;

			trigger.AddLink(triggerLink);
			ts.AddLink(triggerLink);

			double tt = ts.TimeHorizon.Start.ModifiedJulianDay;
			TimeStamp[] triggerTimes = new TimeStamp[4];
			triggerTimes[0] = new TimeStamp(tt + 0.5);
			triggerTimes[1] = new TimeStamp(tt + 1.5);
			triggerTimes[2] = new TimeStamp(tt + 1.9);
			triggerTimes[3] = new TimeStamp(tt + 2.1);

			trigger.Run(triggerTimes);
			Assert.AreEqual(0,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0));
			Assert.AreEqual(1,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0));
			Assert.AreEqual(1,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(2)).GetScalar(0));
			Assert.AreEqual(2,((IScalarSet)trigger.ResultsBuffer.GetValuesAt(3)).GetScalar(0));

			//Teting with timespans
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt+2),new TimeStamp(tt + 2.0 + 1.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 1.0/24.0),new TimeStamp(tt + 2.0 + 2.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 2.0/24.0),new TimeStamp(tt + 2.0 + 3.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 5.0/24.0),new TimeStamp(tt + 2.0 + 6.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 15.0/24.0),new TimeStamp(tt + 2.0 + 16.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(2.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 2.0 + 23.0/24.0),new TimeStamp(tt + 2.0 + 24.0/24.0)),"TriggerLink")).GetScalar(0));
			Assert.AreEqual(3.0,((ScalarSet) ts.GetValues(new HydroNumerics.OpenMI.Sdk.Backbone.TimeSpan(new TimeStamp(tt + 3.0 ),new TimeStamp(tt + 3.0 + 1.0/24.0)),"TriggerLink")).GetScalar(0));
		
		}
        public void ComponentID()
        {
            TimeSeriesComponent ts = new TimeSeriesComponent();

            Assert.AreEqual("TimeSeriesComponentID", ts.ComponentID);
        }
		public void ComponentID()
		{
			TimeSeriesComponent ts = new TimeSeriesComponent();
			Assert.AreEqual("TimeSeriesComponentID",ts.ComponentID);
		}
//		[Ignore ("This test fails when dt = 1 hour but works for dt = 0.5 day")]
		public void GetValues2C()
		{
			// This test is a variation of GetValues 2B. What makes this test different is that the the two
			// connected river models are not using the same timesteps.
			// This test will test:
			// - is the buffer working correcly with respect to interpolations and buffering
			// - is the buffer working correctly with respect to clearing the buffer.
			
			try
			{
				ILinkableComponent timeSeries = new TimeSeriesComponent();
				ILinkableComponent upperRiver = new RiverModelLC();
				ILinkableComponent lowerRiver = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger     = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();

				timeSeries.Initialize(new Argument[0]);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] upperRiverArguments = new Argument[2];
				upperRiverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				upperRiverArguments[1] = new Argument("TimeStepLength","21600",true,"A time step length of 1 day");
				upperRiver.Initialize(upperRiverArguments);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] lowerRiverArguments = new Argument[2];
				lowerRiverArguments[0] = new Argument("ModelID","lowerRiverModel",true,"argument");
				lowerRiverArguments[1] = new Argument("TimeStepLength","86400",true,"xx");
				lowerRiver.Initialize(lowerRiverArguments);
				trigger.Initialize(new Argument[0]);

				Assert.AreEqual("upperRiverModel",upperRiver.ModelID);
				Assert.AreEqual("lowerRiverModel",lowerRiver.ModelID);

				Link timeSeriesToUpperRiverLink = new Link();
				timeSeriesToUpperRiverLink.ID = "timeSeriesToUpperRiverLink";
				timeSeriesToUpperRiverLink.SourceComponent  = timeSeries;
				timeSeriesToUpperRiverLink.SourceElementSet = timeSeries.GetOutputExchangeItem(0).ElementSet; //last branch in the river
				timeSeriesToUpperRiverLink.SourceQuantity   = timeSeries.GetOutputExchangeItem(0).Quantity;
				timeSeriesToUpperRiverLink.TargetComponent  = upperRiver;
				timeSeriesToUpperRiverLink.TargetElementSet = upperRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				timeSeriesToUpperRiverLink.TargetQuantity   = upperRiver.GetInputExchangeItem(0).Quantity;
				
				Link link = new Link();
				link.ID = "RiverToRiverLink";
				link.SourceComponent  = upperRiver;
				link.SourceElementSet = upperRiver.GetOutputExchangeItem(2).ElementSet; //last branch in the river
				link.SourceQuantity   = upperRiver.GetOutputExchangeItem(2).Quantity;
				link.TargetComponent  = lowerRiver;
				link.TargetElementSet = lowerRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				link.TargetQuantity   = lowerRiver.GetInputExchangeItem(0).Quantity;

				Link triggerLink = new Link();

				triggerLink.ID				 = "TriggerLink";
				triggerLink.SourceComponent  = lowerRiver;
				triggerLink.SourceElementSet = lowerRiver.GetOutputExchangeItem(2).ElementSet;
				triggerLink.SourceQuantity   = lowerRiver.GetOutputExchangeItem(2).Quantity;
				triggerLink.TargetComponent  = trigger;
				triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
				triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;
		
				timeSeries.AddLink(timeSeriesToUpperRiverLink);
				upperRiver.AddLink(timeSeriesToUpperRiverLink);

				upperRiver.AddLink(link);
				lowerRiver.AddLink(link);

				lowerRiver.AddLink(triggerLink);
				trigger.AddLink(triggerLink);
				
				timeSeries.Prepare();
				upperRiver.Prepare();
				lowerRiver.Prepare();
				trigger.Prepare();

				double firstTriggerGetValuesTime = lowerRiver.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.5);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.2);

				trigger.Run(triggerTimes);

				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 13.0/64.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 17.0/64.0,x2);

				Assert.AreEqual(10,((RiverModelLC) upperRiver)._maxBufferSize); //Test that the buffer is cleared

				upperRiver.Finish();
				lowerRiver.Finish();

				upperRiver.Dispose();
				lowerRiver.Dispose();

			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
		public void GetValues2B()
		{
			// This is a variation of GetValues2A. In this test the timeSeries is linked ID based to the 
			// top node of the upperRiver. The last upperRiver branch of the upperRiver is ID based connected
			// to the top node of the lowerRiver. The last branch in the lowerRiver is linked to the trigger.
			// The timeSeries provides data that changes over time. This is what makes this test different from
			// GetValues2A, where everytning is the same for every time step.
			
			try
			{
				ILinkableComponent timeSeries = new TimeSeriesComponent();
				ILinkableComponent upperRiver = new RiverModelLC();
				ILinkableComponent lowerRiver = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger     = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();

				timeSeries.Initialize(new Argument[0]);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] upperRiverArguments = new Argument[1];
				upperRiverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				upperRiver.Initialize(upperRiverArguments);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] lowerRiverArguments = new Argument[1];
				lowerRiverArguments[0] = new Argument("ModelID","lowerRiverModel",true,"argument");
				lowerRiver.Initialize(lowerRiverArguments);
				trigger.Initialize(new Argument[0]);

				Assert.AreEqual("upperRiverModel",upperRiver.ModelID);
				Assert.AreEqual("lowerRiverModel",lowerRiver.ModelID);

				Link timeSeriesToUpperRiverLink = new Link();
				timeSeriesToUpperRiverLink.ID = "timeSeriesToUpperRiverLink";
				timeSeriesToUpperRiverLink.SourceComponent  = timeSeries;
				timeSeriesToUpperRiverLink.SourceElementSet = timeSeries.GetOutputExchangeItem(0).ElementSet; //last branch in the river
				timeSeriesToUpperRiverLink.SourceQuantity   = timeSeries.GetOutputExchangeItem(0).Quantity;
				timeSeriesToUpperRiverLink.TargetComponent  = upperRiver;
				timeSeriesToUpperRiverLink.TargetElementSet = upperRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				timeSeriesToUpperRiverLink.TargetQuantity   = upperRiver.GetInputExchangeItem(0).Quantity;
				
				Link link = new Link();
				link.ID = "RiverToRiverLink";
				link.SourceComponent  = upperRiver;
				link.SourceElementSet = upperRiver.GetOutputExchangeItem(2).ElementSet; //last branch in the river
				link.SourceQuantity   = upperRiver.GetOutputExchangeItem(2).Quantity;
				link.TargetComponent  = lowerRiver;
				link.TargetElementSet = lowerRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				link.TargetQuantity   = lowerRiver.GetInputExchangeItem(0).Quantity;

				Link triggerLink = new Link();

				triggerLink.ID				 = "TriggerLink";
				triggerLink.SourceComponent  = lowerRiver;
				triggerLink.SourceElementSet = lowerRiver.GetOutputExchangeItem(2).ElementSet;
				triggerLink.SourceQuantity   = lowerRiver.GetOutputExchangeItem(2).Quantity;
				triggerLink.TargetComponent  = trigger;
				triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
				triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;
		
				timeSeries.AddLink(timeSeriesToUpperRiverLink);
				upperRiver.AddLink(timeSeriesToUpperRiverLink);

				upperRiver.AddLink(link);
				lowerRiver.AddLink(link);

				lowerRiver.AddLink(triggerLink);
				trigger.AddLink(triggerLink);

				timeSeries.Prepare();
				upperRiver.Prepare();
				lowerRiver.Prepare();
				trigger.Prepare();

				double firstTriggerGetValuesTime = lowerRiver.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.5);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.2);

				trigger.Run(triggerTimes);

				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 13.0/64.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(315.0/32.0 + 17.0/64.0,x2);

				upperRiver.Finish();
				lowerRiver.Finish();

				upperRiver.Dispose();
				lowerRiver.Dispose();

			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
		public void GetValues1B()
		{
			// Running with one instances of riverModelLC linked ID-based to trigger and to 
			// an instance of the TimeSeriesComponent.
			try
			{
				ILinkableComponent timeSeries = new TimeSeriesComponent();
				ILinkableComponent riverModelLC = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();
	
				timeSeries.Initialize(new Argument[0]);

				Argument[] riverArguments = new Argument[2];
				riverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				riverArguments[1] = new Argument("TimeStepLength","3600",true,"A time step length of 1 day");

				riverModelLC.Initialize(riverArguments);
				trigger.Initialize(new Argument[0]);

				Link timeSeriesToRiverLink = new Link();
				timeSeriesToRiverLink.ID = "timeSeriesToUpperRiverLink";
				timeSeriesToRiverLink.SourceComponent  = timeSeries;
				timeSeriesToRiverLink.SourceElementSet = timeSeries.GetOutputExchangeItem(0).ElementSet; //last branch in the river
				timeSeriesToRiverLink.SourceQuantity   = timeSeries.GetOutputExchangeItem(0).Quantity;
				timeSeriesToRiverLink.TargetComponent  = riverModelLC;
				timeSeriesToRiverLink.TargetElementSet = riverModelLC.GetInputExchangeItem(0).ElementSet;  //first node in the river
				timeSeriesToRiverLink.TargetQuantity   = riverModelLC.GetInputExchangeItem(0).Quantity;
				

				Link link = new Link(riverModelLC, riverModelLC.GetOutputExchangeItem(2).ElementSet,riverModelLC.GetOutputExchangeItem(2).Quantity,trigger,trigger.GetInputExchangeItem(0).ElementSet,trigger.GetInputExchangeItem(0).Quantity,"LinkID");
				
                timeSeries.AddLink(timeSeriesToRiverLink);
				riverModelLC.AddLink(timeSeriesToRiverLink);

				riverModelLC.AddLink(link);
				trigger.AddLink(link);

				riverModelLC.Prepare();
				
				double firstTriggerGetValuesTime = riverModelLC.TimeHorizon.Start.ModifiedJulianDay;
				TimeStamp[] triggerTimes = new TimeStamp[2];
				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.1);
				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.7);
	
				trigger.Run(triggerTimes);
				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
				Assert.AreEqual(35.0/4.0 + 13.0/8.0,x1);

				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
				Assert.AreEqual(35.0/4.0 + 17.0/8.0,x2);

				riverModelLC.Finish();
				riverModelLC.Dispose();
				
			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}
		public void RestoreState()
		{
			//  This test is based on GetValues2C
			try
			{
				ILinkableComponent timeSeries = new TimeSeriesComponent();
				ILinkableComponent upperRiver = new RiverModelLC();
				ILinkableComponent lowerRiver = new RiverModelLC();
				HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger trigger     = new HydroNumerics.OpenMI.Sdk.Wrapper.UnitTest.Trigger();

				timeSeries.Initialize(new Argument[0]);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] upperRiverArguments = new Argument[2];
				upperRiverArguments[0] = new Argument("ModelID","upperRiverModel",true,"argument");
				upperRiverArguments[1] = new Argument("TimeStepLength","21600",true,"xx");
				upperRiver.Initialize(upperRiverArguments);

				// The ModelID is passes in ordet to make it easier to debug, otherwise you cannot se the difference between the two istances of RiverModelLC
				Argument[] lowerRiverArguments = new Argument[2];
				lowerRiverArguments[0] = new Argument("ModelID","lowerRiverModel",true,"argument");
				lowerRiverArguments[1] = new Argument("TimeStepLength","86400",true,"xx");
				lowerRiver.Initialize(lowerRiverArguments);
				trigger.Initialize(new Argument[0]);

				Assert.AreEqual("upperRiverModel",upperRiver.ModelID);
				Assert.AreEqual("lowerRiverModel",lowerRiver.ModelID);

				Link timeSeriesToUpperRiverLink = new Link();
				timeSeriesToUpperRiverLink.ID = "timeSeriesToUpperRiverLink";
				timeSeriesToUpperRiverLink.SourceComponent  = timeSeries;
				timeSeriesToUpperRiverLink.SourceElementSet = timeSeries.GetOutputExchangeItem(0).ElementSet; //last branch in the river
				timeSeriesToUpperRiverLink.SourceQuantity   = timeSeries.GetOutputExchangeItem(0).Quantity;
				timeSeriesToUpperRiverLink.TargetComponent  = upperRiver;
				timeSeriesToUpperRiverLink.TargetElementSet = upperRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				timeSeriesToUpperRiverLink.TargetQuantity   = upperRiver.GetInputExchangeItem(0).Quantity;
				
				Link link = new Link();
				link.ID = "RiverToRiverLink";
				link.SourceComponent  = upperRiver;
				link.SourceElementSet = upperRiver.GetOutputExchangeItem(2).ElementSet; //last branch in the river
				link.SourceQuantity   = upperRiver.GetOutputExchangeItem(2).Quantity;
				link.TargetComponent  = lowerRiver;
				link.TargetElementSet = lowerRiver.GetInputExchangeItem(0).ElementSet;  //first node in the river
				link.TargetQuantity   = lowerRiver.GetInputExchangeItem(0).Quantity;

				Link triggerLink = new Link();

				triggerLink.ID				 = "TriggerLink";
				triggerLink.SourceComponent  = lowerRiver;
				triggerLink.SourceElementSet = lowerRiver.GetOutputExchangeItem(2).ElementSet;
				triggerLink.SourceQuantity   = lowerRiver.GetOutputExchangeItem(2).Quantity;
				triggerLink.TargetComponent  = trigger;
				triggerLink.TargetElementSet = trigger.GetInputExchangeItem(0).ElementSet;
				triggerLink.TargetQuantity   = trigger.GetInputExchangeItem(0).Quantity;
		
				timeSeries.AddLink(timeSeriesToUpperRiverLink);
				upperRiver.AddLink(timeSeriesToUpperRiverLink);

				upperRiver.AddLink(link);
				lowerRiver.AddLink(link);

				lowerRiver.AddLink(triggerLink);
				trigger.AddLink(triggerLink);
				
				timeSeries.Prepare();
				upperRiver.Prepare();
				lowerRiver.Prepare();
				trigger.Prepare();

//				double firstTriggerGetValuesTime = lowerRiver.TimeHorizon.Start.ModifiedJulianDay;
//				TimeStamp[] triggerTimes = new TimeStamp[2];
//				triggerTimes[0] = new TimeStamp(firstTriggerGetValuesTime + 12.5);
//				triggerTimes[1] = new TimeStamp(firstTriggerGetValuesTime + 16.2);

				double t = lowerRiver.TimeHorizon.Start.ModifiedJulianDay;
				Assert.AreEqual(315.0/32.0 + 13.0/64.0, ((ScalarSet)lowerRiver.GetValues(new TimeStamp(t+12.5),"TriggerLink")).GetScalar(0));
				string lowerRiverStateID = ((IManageState) lowerRiver).KeepCurrentState();
				string upperRiverStateID = ((IManageState) upperRiver).KeepCurrentState();
				Assert.AreEqual(315.0/32.0 + 17.0/64.0, ((ScalarSet)lowerRiver.GetValues(new TimeStamp(t+16.2),"TriggerLink")).GetScalar(0));
				((IManageState) lowerRiver).RestoreState(lowerRiverStateID);
				((IManageState) upperRiver).RestoreState(upperRiverStateID);
				lowerRiver.GetValues(new TimeStamp(t + 14.0),"TriggerLink");
				Assert.AreEqual(315.0/32.0 + 17.0/64.0, ((ScalarSet)lowerRiver.GetValues(new TimeStamp(t+16.2),"TriggerLink")).GetScalar(0));



			

//				double x1 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(0)).GetScalar(0);
//				Assert.AreEqual(315.0/32.0 + 13.0/64.0,x1);
//
//				double x2 = ((IScalarSet)trigger.ResultsBuffer.GetValuesAt(1)).GetScalar(0);
//				Assert.AreEqual(315.0/32.0 + 17.0/64.0,x2);
//
//				Assert.AreEqual(2,((RiverModelLC) upperRiver)._maxBufferSize); //Test that the buffer is cleared

				upperRiver.Finish();
				lowerRiver.Finish();

				upperRiver.Dispose();
				lowerRiver.Dispose();

			}
			catch (System.Exception e)
			{
				ExceptionHandler.WriteException(e);
				throw (e);
			}
		}