예제 #1
0
        private void QuestionAddChoice(QuestionDataQuestionChoice value)
        {
            QuestionReader.QuestionBaseTmp question;
            if (_associationQuestion != null)
            {
                if (_associationQuestion.Complete)
                {
                    throw new PBFileException($"unknow response, association question is complete, line {value.LineNumber} file \"{value.Filename}\"", value.File, value.LineNumber);
                }
                question = _associationQuestion;
            }
            else if (_question != null)
            {
                question = _question;
            }
            else
            {
                if (_lastValueType == QuestionDataType.QuestionType && _questionType == QuestionType.Association)
                {
                    _associationQuestion = new QuestionReader.QuestionAssociationTmp {
                        Type = QuestionType.Association, QuestionLineCount = 0, Complete = false
                    };
                    question = _associationQuestion;
                }
                else
                {
                    throw new PBFileException($"unknow question, line {value.LineNumber} file \"{value.Filename}\"", value.File, value.LineNumber);
                }
            }

            if (question.QuestionText == null && question.Type != QuestionType.Association)
            {
                throw new PBFileException($"missing question line {value.LineNumber} file \"{value.Filename}\"", value.File, value.LineNumber);
            }
            if (value.ChoiceNumber != question.Choices.Count + 1)
            {
                throw new PBFileException($"wrong response code \"{(char)('A' + value.ChoiceNumber - 1)}\" line {value.LineNumber} file \"{value.Filename}\"", value.File, value.LineNumber);
            }
            question.Choices.Add(value.ChoiceText);
            question.ChoiceLineCount = 1;
        }
예제 #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();
        }
예제 #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;
            }
        }