Ejemplo n.º 1
0
 public Boolean Contains(SequenceData data)
 {
     return(List.Contains(data));
 }
Ejemplo n.º 2
0
        public static Boolean TryParse(String path, Configuration aConfiguration, out WordList aWordList)
        {
            StreamReader fileIn = new StreamReader(path);

            Errors    = new List <String>();
            aWordList = new WordList(path, aConfiguration);

            /// Split the sequence file, and extract the WordDataValidator.
            aWordList.OriginalList = fileIn.ReadToEnd().Split(WordSeparators).Where(x => x != String.Empty).ToArray();

            /// Validate the SequenceValidator; possible case of invalid fields.
            SequenceData seq;

            SequenceData.TryParse(aWordList.OriginalList.First(), IS_VALIDATOR, out seq);
            if (SequenceData.Errors.Any())
            {
                Errors.AddRange(SequenceData.Errors);
            }
            else
            {
                aWordList.SequenceValidator = seq;
                aWordList.OriginalList      = aWordList.OriginalList.Skip(1).ToArray(); /// existence of magic number.

                // Check each field in the wordlist.
                int totalPotentialScore     = 0,
                    totalPotentialLength    = 0,
                    totalPotentialAsciiSum  = 0,
                    totalPotentialHashTotal = 0;
                foreach (String potentialWord in aWordList.OriginalList)
                {
                    SequenceData data;
                    SequenceData.TryParse(potentialWord, !IS_VALIDATOR, out data);
                    if (SequenceData.Errors.Any())
                    {
                        Errors.AddRange(SequenceData.Errors);
                    }
                    else
                    {
                        /// Check that the sequence is not empty.
                        if (data.Sequence.Length > 0)
                        {
                            /// Check if sequence is alphabetic, then check if sequence length matches its pre-determined length.
                            if (Regex.IsMatch(data.Sequence, aWordList.SequenceValidator.Sequence))
                            {
                                if (data.ReturnLength() != data.SequenceLength)
                                {
                                    Errors.Add(String.Format(WordListErrors.NonIdenticalLengthError, data.OriginalData));
                                }
                                else
                                {
                                    aWordList.Add(data);
                                    totalPotentialLength += data.SequenceLength;
                                }
                            }
                            else
                            {
                                Errors.Add(String.Format(WordListErrors.AlphabeticError, data.Sequence, data.OriginalData));
                            }

                            totalPotentialScore += data.Score;

                            /// Check if sequence's ASCII sum matches its pre-determined sum.
                            if (data.ReturnAsciiSum() != data.AsciiSum)
                            {
                                Errors.Add(String.Format(WordListErrors.NonIdenticalAsciiSumError, data.OriginalData));
                            }
                            else
                            {
                                totalPotentialAsciiSum += data.AsciiSum;
                            }

                            /// Check if seqence's hash total matches its pre-determined total.
                            if (data.ReturnHashTotal() != data.HashTotal)
                            {
                                Errors.Add(String.Format(WordListErrors.NonIdenticalHashTotalError, data.OriginalData));
                            }
                            else
                            {
                                totalPotentialHashTotal += data.HashTotal;
                            }
                        }
                        else
                        {
                            Errors.Add(String.Format(WordListErrors.MissingWordError, data.OriginalData));
                        }
                    }
                }

                /// Check if totaled potential values match the sequence validator.
                if (totalPotentialScore != aWordList.SequenceValidator.Score)
                {
                    Errors.Add(String.Format(WordListErrors.IncorrectBatchScoreError, totalPotentialScore, aWordList.SequenceValidator.Score));
                }
                if (totalPotentialLength != aWordList.SequenceValidator.SequenceLength)
                {
                    Errors.Add(String.Format(WordListErrors.IncorrectBatchLengthError, totalPotentialLength, aWordList.SequenceValidator.SequenceLength));
                }
                if (totalPotentialAsciiSum != aWordList.SequenceValidator.AsciiSum)
                {
                    Errors.Add(String.Format(WordListErrors.IncorrectBatchAsciiSumError, totalPotentialAsciiSum, aWordList.SequenceValidator.SequenceLength));
                }
                if (totalPotentialHashTotal != aWordList.SequenceValidator.HashTotal)
                {
                    Errors.Add(String.Format(WordListErrors.IncorrectBatchHashTotalError, totalPotentialHashTotal, aWordList.SequenceValidator.HashTotal));
                }

                // Check the minimmum word limit.
                if (aWordList.Count < aConfiguration.MinimumNumberOfUniqueWords)
                {
                    Errors.Add(String.Format(WordListErrors.MinimumSizeError, aWordList.Count, aConfiguration.MinimumNumberOfUniqueWords));
                }

                // Check the maximum word limit.
                if (aWordList.Count > aConfiguration.MaximumNumberOfUniqueWords)
                {
                    Errors.Add(String.Format(WordListErrors.MaximumSizeError, aWordList.Count, aConfiguration.MaximumNumberOfUniqueWords));
                }
            }

            aWordList.Valid = Errors.Count == 0;
            return(aWordList.Valid);
        }
Ejemplo n.º 3
0
 public void Add(SequenceData data)
 {
     List.Add(data);
 }
        public static Boolean TryParse(String originalSequenceData, Boolean isSequenceValidator, out SequenceData sequenceData)
        {
            sequenceData = new SequenceData();
            Errors       = new List <string>();

            if (String.IsNullOrWhiteSpace(originalSequenceData))
            {
                Errors.Add(String.Format(""));
            }
            else
            {
                sequenceData.OriginalData = originalSequenceData;

                if (isSequenceValidator)
                {
                    /// Separate the regex specified in the sequence validator from the other fields.
                    sequenceData.Sequence = originalSequenceData.Substring(0, originalSequenceData.LastIndexOf('"') + 1);
                    originalSequenceData  = originalSequenceData.Remove(0, sequenceData.Sequence.Length);
                    sequenceData.Sequence = sequenceData.Sequence.Trim('"');
                }
                else
                {
                    sequenceData.Sequence = originalSequenceData.Substring(0, originalSequenceData.IndexOf(','));
                    originalSequenceData  = originalSequenceData.Remove(0, sequenceData.Sequence.Length);
                }

                String[] data = originalSequenceData.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (data.Length != 4)
                {
                    Errors.Add(String.Format(""));
                }
                else
                {
                    int parsedValue;
                    for (int i = 0; i < data.Length; i++)
                    {
                        switch (i)
                        {
                        case 0:
                            if (!Int32.TryParse(data[i], out parsedValue))
                            {
                                Errors.Add(String.Format(""));
                            }
                            else
                            {
                                sequenceData.Score = parsedValue;
                            }
                            break;

                        case 1:
                            if (!Int32.TryParse(data[i], out parsedValue))
                            {
                                Errors.Add(String.Format(""));
                            }
                            else
                            {
                                sequenceData.SequenceLength = parsedValue;
                            }
                            break;

                        case 2:
                            if (!Int32.TryParse(data[i], out parsedValue))
                            {
                                Errors.Add(String.Format(""));
                            }
                            else
                            {
                                sequenceData.AsciiSum = parsedValue;
                            }
                            break;

                        case 3:
                            if (!Int32.TryParse(data[i], out parsedValue))
                            {
                                Errors.Add(String.Format(""));
                            }
                            else
                            {
                                sequenceData.HashTotal = parsedValue;
                            }
                            break;
                        }
                    }
                }
            }

            return(Errors.Count == 0);
        }