示例#1
0
        private Question GetQuestion()
        {
            //Question question = new Question { Year = _question.Year, Type = _question.Type, Number = _question.Number, QuestionText = _question.QuestionText, Responses = _question.Responses.ToArray() };
            Question question = new Question {
                Year = _question.Year, Type = _question.Type, QuestionNumber = _question.Number, SourceFile = _question.SourceFile, SourceLine = _question.SourceLine
            };

            if (_associationQuestion != null)
            {
                //if (!_newVersion)
                //    question.QuestionText = _associationQuestion.QuestionText + "<br>" + question.QuestionText;
                //else
                //
                //question.QuestionText = _associationQuestion.QuestionText + "<br>" + _question.QuestionText;
                question.QuestionText = _associationQuestion.QuestionText + _newLine + _question.QuestionText;
                question.Choices      = _associationQuestion.Choices.ToArray();
            }
            else
            {
                question.QuestionText = _question.QuestionText;
                question.Choices      = _question.Choices.ToArray();
            }
            //Trace.WriteLine($"{question.Year} - question {question.Number} - choices count {question.Choices.Length}");
            if (question.Choices.Length == 0)
            {
                throw new PBFileException($"question without choice - {question.Year} - question {question.QuestionNumber} - line {question.SourceLine} file \"{_filename}\"", _file, question.SourceLine);
            }
            _question = null;
            return(question);
        }
示例#2
0
        public IEnumerable <Question> Read(IEnumerable <string> files, string baseDirectory = null)
        {
            //Trace.WriteLine("read questions files");

            if (_maxLinesPerQuestion == -1)
            {
                throw new PBException("undefined MaxLinesPerQuestion");
            }
            if (_maxLinesPerChoice == -1)
            {
                throw new PBException("undefined MaxLinesPerResponse");
            }

            _baseDirectory = baseDirectory;

            foreach (string file in files)
            {
                _file     = file;
                _filename = zPath.GetFileName(_file);
                //_year = null;
                //_questionType = QuestionType.None;
                _lastValueType = QuestionValueType.None;
                _question      = null;
                _lineNumber    = 0;
                int emptyLineCount = 0;
                _endOfPage = false;

                if (_trace)
                {
                    Trace.WriteLine($"read question file \"{file}\"");
                }

                foreach (string line in zFile.ReadLines(file))
                {
                    _lineNumber++;
                    string line2 = line.Trim();
                    if (line2 == "")
                    {
                        //if (_lastValueType == QuestionValueType.Response)
                        if (_lastValueType == QuestionValueType.Response || (_associationQuestion != null && _associationQuestion.Complete && _lastValueType == QuestionValueType.Question))
                        {
                            if (_question != null)
                            {
                                //if (!_newVersion)
                                //    yield return new Question { Year = _question.Year, Type = _question.Type, Number = _question.Number, QuestionText = _question.QuestionText, Choices = _question.Choices.ToArray(),
                                //        SourceFile = _question.SourceFile, SourceLine = _question.SourceLine };
                                //else
                                yield return(GetQuestion());

                                _question = null;
                            }
                            else if (_associationQuestion != null)
                            {
                                _associationQuestion.Complete = true;
                            }
                            _lastValueType = QuestionValueType.None;
                        }

                        if (++emptyLineCount == _maxEmptyLine)
                        {
                            _endOfPage = true;
                        }

                        continue;
                    }

                    emptyLineCount = 0;

                    //FindText_v2 findText = _regexList.Find(line2);
                    FindText_v2 findText = _regexList.Find(line);
                    if (findText.Success)
                    {
                        bool          newQuestion = false;
                        QuestionTmp   question    = null;
                        QuestionValue value       = GetQuestionValue(findText.GetValues());
                        if (_trace)
                        {
                            Trace.WriteLine($"  line {_lineNumber} - {value}");
                        }
                        switch (value.Type)
                        {
                        case QuestionValueType.Year:
                            newQuestion          = true;
                            _year                = ((QuestionValueYear)value).Year;
                            _associationQuestion = null;
                            break;

                        case QuestionValueType.QuestionType:
                            newQuestion          = true;
                            _questionType        = ((QuestionValueQuestionType)value).QuestionType;
                            _associationQuestion = null;
                            break;

                        case QuestionValueType.QuestionNumber:
                            newQuestion = true;
                            if (_year == null)
                            {
                                throw new PBFileException($"unknow year, line {_lineNumber} file \"{_filename}\"", _file, _lineNumber);
                            }
                            //string sourceFile = _file;
                            //if (baseDirectory != null && sourceFile.StartsWith(baseDirectory))
                            //{
                            //    int l = baseDirectory.Length;
                            //    if (sourceFile[l] == '\\')
                            //        l++;
                            //    sourceFile = sourceFile.Substring(l);
                            //}
                            question = new QuestionTmp {
                                Year = (int)_year, Type = _questionType, Number = ((QuestionValueQuestionNumber)value).QuestionNumber,
                                QuestionLineCount = 0, SourceFile = GetSubPath(_file), SourceLine = _lineNumber
                            };
                            if (_associationQuestion != null)
                            {
                                _associationQuestion.Complete = true;
                            }
                            break;

                        case QuestionValueType.Response:
                            //if (_question == null)
                            //    throw new PBFileException($"unknow question, line {_lineNumber} file \"{_filename}\"", _file, _lineNumber);
                            //AddResponse(_question, (QuestionValueResponse)value);
                            AddResponse((QuestionValueResponse)value);
                            break;

                        case QuestionValueType.None:
                            _TraceUnknowValue(line2);
                            break;

                        default:
                            throw new PBFileException($"wrong value \"{line2}\" line {_lineNumber} file \"{_filename}\"", _file, _lineNumber);
                        }
                        if (newQuestion)
                        {
                            if (_question != null)
                            {
                                //yield return new Question { Year = _question.Year, Type = _question.Type, Number = _question.Number, QuestionText = _question.QuestionText, Responses = _question.Responses.ToArray() };
                                yield return(GetQuestion());
                            }
                            _question = question;
                        }
                        _lastValueType = value.Type;
                    }
                    else
                    {
                        if (_trace)
                        {
                            Trace.WriteLine($"  line {_lineNumber} - text - {line2}");
                        }
                        switch (_lastValueType)
                        {
                        case QuestionValueType.QuestionNumber:
                            _question.QuestionText = line2;
                            _question.QuestionLineCount++;
                            _lastValueType = QuestionValueType.Question;
                            break;

                        case QuestionValueType.Question:
                            //if (_question.QuestionLineCount > _maxLinesPerQuestion - 1)
                            //    throw new PBFileException($"to many lines for question \"{line2}\" line {_lineNumber} file \"{_filename}\"", _file, _lineNumber);
                            //_question.QuestionText += " " + line2;
                            //_question.QuestionLineCount++;
                            AddQuestion(line2);
                            break;

                        case QuestionValueType.Response:
                            //if (_question.ResponseLineCount > 1)
                            //    throw new PBFileException($"to many lines for response \"{line2}\" line {_lineNumber} file \"{_filename}\"", _file, _lineNumber);
                            //int i = _question.Responses.Count - 1;
                            //_question.Responses[i] += " " + line2;
                            //_question.ResponseLineCount++;
                            AddResponse(line2);
                            break;

                        case QuestionValueType.None:
                            _TraceUnknowValue(line2);
                            break;

                        default:
                            if (_lastValueType == QuestionValueType.QuestionType && _questionType == QuestionType.Association && _manageAssociationQuestion)
                            {
                                _associationQuestion = new QuestionAssociationTmp {
                                    Type = QuestionType.Association, QuestionText = line2, QuestionLineCount = 1, Complete = false
                                };
                                _lastValueType = QuestionValueType.Question;
                            }
                            //else if (_year != null)
                            //    throw new PBFileException($"wrong value \"{line2}\" line {_lineNumber} file \"{_filename}\"", _file, _lineNumber);
                            else
                            {
                                _TraceUnknowValue(line2);
                            }
                            break;
                        }
                    }
                }
                if (_question != null)
                {
                    //yield return new Question { Year = _question.Year, Type = _question.Type, Number = _question.Number, QuestionText = _question.QuestionText, Responses = _question.Responses.ToArray() };
                    yield return(GetQuestion());
                }
            }
        }