A range of cohort ages.
		//---------------------------------------------------------------------
		
		/// <summary>
		/// constructor, assign members of the stucture
		/// </summary>
		public InclusionRule(string inclusion_type,
	                            AgeRange age_range,
	                            string temp_percent,
	                            List<string> species_list) {

			//assign members of the struct		
			this.inclusion_type = inclusion_type;
			this.age_range = age_range;
			//check for type 'percentage' by looking for the % character
			string [] split = temp_percent.Split(new char [] {'%'});
			//try to make a percentage.  if this doesn't work then check for keyword 'highest'
			try {
				percentOfCells = ((double) Convert.ToInt32(split[0])) / 100;
			}
			catch (Exception) {
				//and set percentOfCells to -1 (the flag for InclusionRequirement to handle)
				percentOfCells = -1;
			}
			this.species_list = species_list;
			//get the species index list using species name
			this.species_index_list = new List<int>();
			foreach (string species in species_list) {
				if (Model.Core.Species[species] != null) {
					this.species_index_list.Add(Model.Core.Species[species].Index);
				}				
			}
			//UI.WriteLine("species index = {0}", this.species_index);
		}
        //---------------------------------------------------------------------

        public void AgeOrRangeWasRead(AgeRange   ageRange,
                                      Percentage percentage)
        {
            Assert.AreEqual(expectedRange, ageRange);
            if (expectedPercentage == null)
                Assert.IsNull(percentage);
            else
                Assert.AreEqual((double) expectedPercentage, (double) percentage);
            eventHandlerCalled = true;
        }
 public void ReadAgeOrRange_AgeWhitespacePercentage()
 {
     StringReader reader = new StringReader("66 ( 50% )\t");
     int index;
     eventHandlerCalled = false;
     expectedRange = new AgeRange(66, 66);
     expectedPercentage = Percentage.Parse("50%");
     InputValue<AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
     Assert.IsTrue(eventHandlerCalled);
     Assert.AreEqual(0, index);
     Assert.AreEqual('\t', reader.Peek());
 }
 public void ReadAgeOrRange_RangeWhitespacePercentage()
 {
     StringReader reader = new StringReader(" 1-100 (22.2%)Hi");
     int index;
     eventHandlerCalled = false;
     expectedRange = new AgeRange(1, 100);
     expectedPercentage = Percentage.Parse("22.2%");
     InputValue<AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
     Assert.IsTrue(eventHandlerCalled);
     Assert.AreEqual(1, index);
     Assert.AreEqual('H', reader.Peek());
 }
 public void ReadAgeOrRange_RangePercentage()
 {
     StringReader reader = new StringReader("30-75(10%)");
     int index;
     eventHandlerCalled = false;
     expectedRange = new AgeRange(30, 75);
     expectedPercentage = Percentage.Parse("10%");
     InputValue<AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
     Assert.IsTrue(eventHandlerCalled);
     Assert.AreEqual(0, index);
     Assert.AreEqual(-1, reader.Peek());
 }
        //---------------------------------------------------------------------

        /// <summary>
        /// Does the range overlap another range?
        /// </summary>
        public bool Overlaps(AgeRange other)
        {
            return Contains(other.Start) || other.Contains(start);
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// make a string key for the dictionary.
        /// format is <species> <age range start> <age range end>
        /// </summary>

        private string get_key(string key_start, AgeRange age_range) {
            //the key is formed by concatenating the key_start (species list) with the age range parameters
            string key = key_start + age_range.Start + " " + age_range.End;
            return key;
        }
        public void ReadAgeOrRange_Multiple()
        {
            StringReader reader = new StringReader(" 1-40 (50%)  50(65%)\t 65-70  71-107 ( 15% )  109");
            int index;                            //0123456789_123456789_^123456789_123456789_12345678

            eventHandlerCalled = false;
            expectedRange = new AgeRange(1, 40);
            expectedPercentage = Percentage.Parse("50%");
            InputValue<AgeRange> ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
            Assert.IsTrue(eventHandlerCalled);
            Assert.AreEqual(1, index);
            Assert.AreEqual(' ', reader.Peek());

            eventHandlerCalled = false;
            expectedRange = new AgeRange(50, 50);
            expectedPercentage = Percentage.Parse("65%");
            ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
            Assert.IsTrue(eventHandlerCalled);
            Assert.AreEqual(13, index);
            Assert.AreEqual('\t', reader.Peek());

            eventHandlerCalled = false;
            expectedRange = new AgeRange(65, 70);
            expectedPercentage = null;
            ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
            Assert.IsTrue(eventHandlerCalled);
            Assert.AreEqual(22, index);
            Assert.AreEqual('7', reader.Peek());

            eventHandlerCalled = false;
            expectedRange = new AgeRange(71, 107);
            expectedPercentage = Percentage.Parse("15%");
            ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
            Assert.IsTrue(eventHandlerCalled);
            Assert.AreEqual(29, index);
            Assert.AreEqual(' ', reader.Peek());

            eventHandlerCalled = false;
            expectedRange = new AgeRange(109, 109);
            expectedPercentage = null;
            ageRange = PartialThinning.ReadAgeOrRange(reader, out index);
            Assert.IsTrue(eventHandlerCalled);
            Assert.AreEqual(45, index);
            Assert.AreEqual(-1, reader.Peek());
        }
        //---------------------------------------------------------------------

        public static void AgeOrRangeWasRead(AgeRange   ageRange,
                                             Percentage percentage)
        {
            ageOrRangeWasRead = true;

            //  Have we started reading ages and ranges for another species?
            //  If so, then first create a cohort selector for the previous
            //  species.
            if (currentSpecies != SpeciesDataset.MostRecentlyFetchedSpecies) {
                if (currentSpecies != null)
                    ageSelectors[currentSpecies.Index] = new SpecificAgesCohortSelector(ages, ranges, percentages);
                currentSpecies = SpeciesDataset.MostRecentlyFetchedSpecies;
                ages.Clear();
                ranges.Clear();
                percentages.Clear();
            }

            if (ageRange.Start == ageRange.End)
                ages.Add(ageRange.Start);
            else
                ranges.Add(ageRange);
            if (percentage != null)
                percentages[ageRange.Start] = percentage;
        }
 public void Overlap_S1IsE2()
 {
     AgeRange range1 = new AgeRange(   10, 20);
     AgeRange range2 = new AgeRange(3, 10);
     Assert.IsTrue(range1.Overlaps(range2));
 }
 public void Overlap_R1InR2()
 {
     AgeRange range1 = new AgeRange(   10, 20);
     AgeRange range2 = new AgeRange(5,        50);
     Assert.IsTrue(range1.Overlaps(range2));
 }
 public void Overlap_R1BeforeR2()
 {
     AgeRange range1 = new AgeRange(10, 20);
     AgeRange range2 = new AgeRange(      21, 50);
     Assert.IsFalse(range1.Overlaps(range2));
 }
 public void Overlap_R1AfterR2()
 {
     AgeRange range1 = new AgeRange(       100, 200);
     AgeRange range2 = new AgeRange(15, 50);
     Assert.IsFalse(range1.Overlaps(range2));
 }
 public void Overlap_E1IsS2()
 {
     AgeRange range1 = new AgeRange(10, 20);
     AgeRange range2 = new AgeRange(    20, 50);
     Assert.IsTrue(range1.Overlaps(range2));
 }