/// <summary> /// Initializes a new instance of the QuestionBuilder class according to given values. /// </summary> /// <param name="oper">The given value of operator.</param> /// <param name="value">The given value.</param> public QuestionBuilder(QuestionOperator oper, int value) { if (value < 0) { throw new ArgumentException("The given integer value shouldn't be less than 0"); } // If the operator is belong to, it can be consider as equal since the value set only have one value. if (oper == QuestionOperator.Belong) { oper = QuestionOperator.Equal; } // If the operator is greater or equal, it can be consider as less. // So, the returned question will be an equalable question with opposite answer. // The opposite answer won't impact the decision tree and related logic. if (oper == QuestionOperator.GreaterEqual) { oper = QuestionOperator.Less; } // If the operator is greater, it can be consider as less or equal. // So, the returned question will be an equalable question with opposite answer. // The opposite answer won't impact the decision tree and related logic. if (oper == QuestionOperator.Greater) { oper = QuestionOperator.LessEqual; } Question question = new Question { Oper = oper, ValueSetName = value.ToString(CultureInfo.InvariantCulture), }; switch (oper) { case QuestionOperator.Equal: question.ValueSet = new List<string> { question.ValueSetName }.AsReadOnly(); break; case QuestionOperator.Less: question.ValueSet = BuildLessQuestionValueSetStartsWithDigit(value).AsReadOnly(); break; case QuestionOperator.LessEqual: question.ValueSet = BuildLessQuestionValueSetStartsWithDigit(value + 1).AsReadOnly(); break; default: throw new NotSupportedException(Helper.NeutralFormat("Unsupported question operator \"{0}\"", oper.ToString())); } _questionList.Add(question); }
/// <summary> /// Updates the fields of this object according to the given name. /// The name looks like: Phone.PhoneIdentity_High /// Phone.BwPosInSyllable==6 /// Phone.BwPosInSyllable<=2. /// </summary> /// <param name="name">The given name.</param> private void UpdateName(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } Match matchName = Regex.Match(name, NameRegex); if (matchName.Success) { _featureName = matchName.Groups[1].Value; _oper = ToOperator(matchName.Groups[2].Value); _valueSetName = matchName.Groups[3].Value; } else { int index = name.IndexOfAny(new[] { '-', '_' }); if (index < 0 && index == name.Length) { throw new ArgumentException(Helper.NeutralFormat("Unknown format of question name \"{0}\"", name)); } _featureName = name.Substring(0, index); _oper = QuestionOperator.Belong; _valueSetName = name.Substring(index + 1); } ResetName(); }
/// <summary> /// Initializes a new instance of the QuestionBuilder class according to given values. /// </summary> /// <param name="oper"> /// The given value of operator. /// </param> /// <param name="name"> /// The given name. /// </param> /// <param name="valueSet"> /// The given value set. /// </param> public QuestionBuilder(QuestionOperator oper, string name, List<string> valueSet) { Question question = new Question { Oper = oper, ValueSetName = name, ValueSet = valueSet.AsReadOnly(), }; _questionList.Add(question); }
/// <summary> /// Converts question operator to string. /// </summary> /// <param name="oper">Question operator.</param> /// <returns>Question string.</returns> private static string ToOperator(QuestionOperator oper) { return OperatorStrings.Where(o => o.Value == oper).Single().Key; }