Beispiel #1
0
        private Question_v2 GetQuestion()
        {
            Question_v2 question = new Question_v2 {
                Year = _question.Year, Type = _question.Type, QuestionNumber = _question.Number, SourceFile = _question.SourceFile, SourceLine = _question.SourceLine
            };

            if (_associationQuestion != null)
            {
                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 \"{question.SourceFile}\"", question.SourceFile, question.SourceLine);
            }
            _question = null;
            return(question);
        }
Beispiel #2
0
        public IEnumerable <QuestionResponse_v2> Read(IEnumerable <string> files, string baseDirectory = null)
        {
            _lastValueType        = null;
            _dontSetLastValueType = false;
            _emptyLineCount       = 0;

            _questionYear        = null;
            _questionType        = QuestionType.None;
            _question            = null;
            _questionTmp         = null;
            _associationQuestion = null;
            _sendCurrentQuestion = false;

            _responseYears         = null;
            _responseQuestions     = null;
            _responseCategory      = null;
            _responseQuestionBlock = false;
            _responseQuestion      = null;
            _responseEnd           = false;

            _baseDirectory = baseDirectory;
            TextDataReader textDataReader = new TextDataReader();

            textDataReader.SetRegexList(_questionRegexValuesList);
            bool readResponse = false;
            //int emptyLineCount = 0;
            int pageNumber = 1;

            foreach (QuestionData value in textDataReader.Read(files).Select(textData => QuestionData.CreateQuestionData(textData)))
            {
                if (value.Type != QuestionDataType.EmptyLine)
                {
                    _emptyLineCount = 0;
                }
                else
                {
                    _emptyLineCount++;
                }
                _sendCurrentQuestion  = false;
                _dontSetLastValueType = false;
                switch (value.Type)
                {
                case QuestionDataType.Responses:
                    if (pageNumber >= _minPageForResponses)
                    {
                        if (_trace)
                        {
                            Trace.WriteLine($"start read response (line {value.LineNumber} \"{value.Filename}\")");
                        }
                        textDataReader.SetRegexList(_responseRegexValuesList);
                        textDataReader.ContiguousSearch = true;
                        _sendCurrentQuestion            = true;
                        readResponse           = true;
                        _responseYears         = new List <ResponseReader.ResponseYear>();
                        _responseQuestions     = new List <ResponseReader.ResponseQuestion>();
                        _responseQuestionBlock = false;
                    }
                    break;

                case QuestionDataType.PageNumber:
                    pageNumber++;
                    _sendCurrentQuestion = true;
                    break;

                default:
                    //throw new PBFileException($"unknow value type {value.Type} line {value.LineNumber} file \"{value.Filename}\"", value.File, value.LineNumber);
                    if (!readResponse)
                    {
                        ReadQuestion(value);
                    }
                    else
                    {
                        QuestionResponse_v2 response = ReadResponse(value);
                        if (response != null)
                        {
                            yield return(response);
                        }
                    }
                    break;
                }
                if (_sendCurrentQuestion)
                {
                    if (_question != null)
                    {
                        yield return(GetQuestion());
                    }
                    _question    = _questionTmp;
                    _questionTmp = null;
                }
                //if (readResponse)
                //    break;
                if (!_dontSetLastValueType)
                {
                    _lastValueType = value.Type;
                }
                if (_responseEnd)
                {
                    break;
                }
            }
            if (_question != null)
            {
                yield return(GetQuestion());
            }
            ControlRemindResponseQuestions();
        }
Beispiel #3
0
        private void ReadQuestion(QuestionData value)
        {
            //bool lastValueType = false;
            //bool sendCurrentQuestion = false;
            //QuestionReader.QuestionTmp question = null;
            switch (value.Type)
            {
            case QuestionDataType.EmptyLine:
                if (_lastValueType == QuestionDataType.QuestionChoice ||
                    (_associationQuestion != null && _associationQuestion.Complete && _lastValueType == QuestionDataType.QuestionText))
                {
                    if (_question != null)
                    {
                        _sendCurrentQuestion = true;
                    }
                    else if (_associationQuestion != null)
                    {
                        _associationQuestion.Complete = true;
                    }
                }
                else
                {
                    // dont change _lastValueType
                    _dontSetLastValueType = true;
                }
                break;

            case QuestionDataType.QuestionYear:
                _sendCurrentQuestion = true;
                _questionYear        = ((QuestionDataQuestionYear)value).Year;
                _associationQuestion = null;
                break;

            case QuestionDataType.QuestionType:
                _sendCurrentQuestion = true;
                _questionType        = ((QuestionDataQuestionType)value).QuestionType;
                _associationQuestion = null;
                break;

            case QuestionDataType.QuestionNumber:
                _sendCurrentQuestion = true;
                if (_questionYear == null)
                {
                    throw new PBFileException($"unknow year, line {value.LineNumber} file \"{value.Filename}\"", value.File, value.LineNumber);
                }
                //string sourceFile = GetSubPath(value.File);
                _questionTmp = new QuestionReader.QuestionTmp {
                    Year = (int)_questionYear, Type = _questionType, Number = ((QuestionDataQuestionNumber)value).QuestionNumber,
                    QuestionLineCount = 0, SourceFile = GetSubPath(value.File), SourceLine = value.LineNumber
                };
                if (_associationQuestion != null)
                {
                    _associationQuestion.Complete = true;
                }
                break;

            case QuestionDataType.QuestionChoice:
                QuestionAddChoice((QuestionDataQuestionChoice)value);
                break;

            case QuestionDataType.Unknow:
                switch (_lastValueType)
                {
                case QuestionDataType.QuestionNumber:
                    _question.QuestionText = value.TrimedLine;
                    _question.QuestionLineCount++;
                    _lastValueType        = QuestionDataType.QuestionText;
                    _dontSetLastValueType = true;
                    break;

                case QuestionDataType.QuestionText:
                    QuestionAddLine(value);
                    // keep _lastValueType to QuestionDataType.QuestionText
                    _dontSetLastValueType = true;
                    break;

                case QuestionDataType.QuestionChoice:
                    QuestionAddChoiceLine(value);
                    // keep _lastValueType to QuestionDataType.QuestionChoice
                    _dontSetLastValueType = true;
                    break;

                default:
                    // _manageAssociationQuestion
                    if (_lastValueType == QuestionDataType.QuestionType && _questionType == QuestionType.Association)
                    {
                        _associationQuestion = new QuestionReader.QuestionAssociationTmp {
                            Type = QuestionType.Association, QuestionText = value.TrimedLine, QuestionLineCount = 1, Complete = false
                        };
                        _lastValueType        = QuestionDataType.QuestionText;
                        _dontSetLastValueType = true;
                    }
                    else
                    {
                        _TraceUnknowValue(value);
                    }
                    break;
                }
                break;
            }
        }