Beispiel #1
0
        //---------------------------------------------------------------------

        private void PrintInputVarException(StringReader reader)
        {
            try {
                strVar.ReadValue(reader);
            }
            catch (InputVariableException exc) {
                Data.Output.WriteLine(exc.Message);
                throw exc;
            }
        }
Beispiel #2
0
        //---------------------------------------------------------------------

        private void PrintInputVarException(string input)
        {
            try {
                StringReader reader = new StringReader(input);
                seedAlgVar.ReadValue(reader);
            }
            catch (InputVariableException exc) {
                Data.Output.WriteLine(exc.Message);
                throw exc;
            }
        }
        public void ReadMethod_JustDigits()
        {
            StringReader reader = new StringReader("1234");

            inputVar.ReadValue(reader);
            Assert.AreEqual(1234, inputVar.Value.Actual);
            Assert.AreEqual("1234", inputVar.Value.String);
            Assert.AreEqual(0, inputVar.Index);
        }
Beispiel #4
0
        //---------------------------------------------------------------------

        private TValue ReadInputValue <TValue>(StringReader currentLine)
        {
            InputVar <TValue> var = new InputVar <TValue>("(dummy)");

            var.ReadValue(currentLine);
            return(var.Value.Actual);
        }
            protected override ParseResult[] Parse()
            {
                List <ParseResult> result = new List <ParseResult>();

                ReadName("TableName");

                InputVar <string> strVar   = new InputVar <string>("StringVar");
                InputVar <float>  floatVar = new InputVar <float>("FloatVar");
                InputVar <int>    intVar   = new InputVar <int>("IntVar");

                while (!AtEndOfInput)
                {
                    StringReader reader = new StringReader(CurrentLine);
                    strVar.ReadValue(reader);
                    floatVar.ReadValue(reader);
                    intVar.ReadValue(reader);

                    result.Add(new ParseResult(intVar.Value.Actual,
                                               floatVar.Value.Actual,
                                               strVar.Value.Actual));

                    CheckNoDataAfter("the " + intVar.Name + " column", reader);
                    GetNextLine();
                }

                return(result.ToArray());
            }
Beispiel #6
0
        //---------------------------------------------------------------------

        private ISpecies ReadSpecies(StringReader currentLine)
        {
            InputVar <string> speciesName = new InputVar <string>("Species");

            speciesName.ReadValue(currentLine);
            ISpecies species = speciesDataset[speciesName.Value.Actual];

            Assert.IsNotNull(species);
            return(species);
        }
Beispiel #7
0
        //---------------------------------------------------------------------

        private List <IEcoregion> ReadEcoregions()
        {
            List <IEcoregion> ecoregions = new List <IEcoregion>();

            InputVar <string> ecoregionName = new InputVar <string>("Ecoregion");

            StringReader currentLine = new StringReader(inputLine.ToString());

            TextReader.SkipWhitespace(currentLine);
            while (currentLine.Peek() != -1)
            {
                ecoregionName.ReadValue(currentLine);
                IEcoregion ecoregion = ecoregionDataset[ecoregionName.Value.Actual];
                Assert.IsNotNull(ecoregion);
                ecoregions.Add(ecoregion);
                TextReader.SkipWhitespace(currentLine);
            }
            inputLine.GetNext();
            return(ecoregions);
        }
Beispiel #8
0
        //---------------------------------------------------------------------

        /// <summary>
        /// Reads a list of species and their cohorts that should be removed.
        /// </summary>
        protected ICohortSelector ReadSpeciesAndCohorts(params string[] names)
        {
            List <string> namesThatFollow;

            if (names == null)
            {
                namesThatFollow = new List <string>();
            }
            else
            {
                namesThatFollow = new List <string>(names);
            }

            SpeciesLineNumbers.Clear();

            //if we are reading a single repeat
            if (names.Length == 3)
            {
                additionalCohortSelector = new MultiSpeciesCohortSelector();
            }
            else
            {
                cohortSelector = new MultiSpeciesCohortSelector();
            }

            while (!AtEndOfInput && !namesThatFollow.Contains(CurrentName))
            {
                StringReader currentLine = new StringReader(CurrentLine);

                // Species name
                ISpecies species = ReadSpecies(currentLine);

                //  Cohort keyword, cohort age or cohort age range
                //  keyword = (All, Youngest, AllExceptYoungest, Oldest,
                //             AllExceptOldest, 1/{N})
                TextReader.SkipWhitespace(currentLine);
                int    indexOfDataAfterSpecies = currentLine.Index;
                string word = TextReader.ReadWord(currentLine);
                if (word == "")
                {
                    throw NewParseException("No cohort keyword, age or age range after the species name");
                }

                bool foundKeyword = false;
                if (keywordsEnabled)
                {
                    if (word == "All")
                    {
                        if (names.Length == 3)
                        {
                            additionalCohortSelector[species] = SelectCohorts.All;
                        }
                        else
                        {
                            cohortSelector[species] = SelectCohorts.All;
                        }
                        foundKeyword = true;
                    }
                    else if (word == "Youngest")
                    {
                        if (names.Length == 3)
                        {
                            additionalCohortSelector[species] = SelectCohorts.Youngest;
                        }
                        else
                        {
                            cohortSelector[species] = SelectCohorts.Youngest;
                        }
                        foundKeyword = true;
                    }
                    else if (word == "AllExceptYoungest")
                    {
                        if (names.Length == 3)
                        {
                            additionalCohortSelector[species] = SelectCohorts.AllExceptYoungest;
                        }
                        else
                        {
                            cohortSelector[species] = SelectCohorts.AllExceptYoungest;
                        }
                        foundKeyword = true;
                    }
                    else if (word == "Oldest")
                    {
                        if (names.Length == 3)
                        {
                            additionalCohortSelector[species] = SelectCohorts.Oldest;
                        }
                        else
                        {
                            cohortSelector[species] = SelectCohorts.Oldest;
                        }
                        foundKeyword = true;
                    }
                    else if (word == "AllExceptOldest")
                    {
                        if (names.Length == 3)
                        {
                            additionalCohortSelector[species] = SelectCohorts.AllExceptOldest;
                        }
                        else
                        {
                            cohortSelector[species] = SelectCohorts.AllExceptOldest;
                        }
                        foundKeyword = true;
                    }
                    else if (word.StartsWith("1/"))
                    {
                        InputVar <ushort> N = new InputVar <ushort>("1/N");
                        N.ReadValue(new StringReader(word.Substring(2)));
                        if (N.Value.Actual == 0)
                        {
                            throw NewParseException("For \"1/N\", N must be > 0");
                        }

                        if (names.Length == 3)
                        {
                            additionalCohortSelector[species] = new EveryNthCohort(N.Value.Actual).SelectCohorts;
                        }
                        else
                        {
                            cohortSelector[species] = new EveryNthCohort(N.Value.Actual).SelectCohorts;
                        }
                        foundKeyword = true;
                    }
                }

                if (foundKeyword)
                {
                    CheckNoDataAfter("the keyword \"" + word + "\"", currentLine);
                }
                else
                {
                    //  Read one or more ages or age ranges
                    List <ushort>   ages   = new List <ushort>();
                    List <AgeRange> ranges = new List <AgeRange>();
                    currentLine = new StringReader(CurrentLine.Substring(indexOfDataAfterSpecies));
                    InputVar <AgeRange> ageOrRange = new InputVar <AgeRange>("Age or Age Range");
                    while (currentLine.Peek() != -1)
                    {
                        ReadValue(ageOrRange, currentLine);
                        ValidateAgeOrRange(ageOrRange.Value, ages, ranges);
                        TextReader.SkipWhitespace(currentLine);
                    }
                    if (names.Length == 3)
                    {
                        CreateAdditionalCohortSelectionMethodFor(species, ages, ranges);
                    }
                    else
                    {
                        CreateCohortSelectionMethodFor(species, ages, ranges);
                    }
                }

                GetNextLine();
            }

            if (SpeciesLineNumbers.Count == 0)
            {
                throw NewParseException("Expected a line starting with a species name");
            }

            if (names.Length == 3)
            {
                return(additionalCohortSelector);
            }
            else
            {
                return(cohortSelector);
            }
        }