Ejemplo n.º 1
0
        public string Run(
            string template,
            IDictionary <string, object> model)
        {
            if (template == null)
            {
                throw new ArgumentNullException(nameof(template));
            }
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var output = new StringBuilder();
            var reader = new StringReader(template);

            var currentLine = reader.ReadLine();

            while (currentLine != null)
            {
                var statement = Statements.FirstOrDefault(_ => _.Accept(currentLine));
                if (statement == null)
                {
                    throw new Exception($"Not recognized statement: `{ statement }`.");
                }

                (output, reader) = statement.Execute(currentLine, reader, output, model);

                currentLine = reader.ReadLine();
            }

            return(output.ToString().TrimEnd());
        }
Ejemplo n.º 2
0
        public IAnswer SelectFirstConcept(
            Func <StatementT, IConcept> resultConceptSelector,
            Func <ILanguage, String> answerFormat,
            Func <StatementT, IDictionary <String, IKnowledge> > getParameters)
        {
            ProcessChildrenIfNeed();

            IAnswer answer = null;

            var statement = Statements.FirstOrDefault();

            if (statement != null)
            {
                answer = new ConceptAnswer(
                    resultConceptSelector(statement),
                    new FormattedText(
                        answerFormat,
                        getParameters(statement)),
                    new Explanation(Statements.OfType <IStatement>()));

                answer.Explanation.Expand(AdditionalTransitives);
            }

            if (answer == null)
            {
                var childAnswer = ChildAnswers.FirstOrDefault();
                if (childAnswer != null)
                {
                    childAnswer.Answer.Explanation.Expand(childAnswer.TransitiveStatements);
                    answer = childAnswer.Answer;
                }
            }

            return(answer ?? Answer.CreateUnknown());
        }
Ejemplo n.º 3
0
 /// <summary>
 /// The first statement in the list or null if the list is empty.
 /// </summary>
 /// <returns></returns>
 public StatementSyntax FirstOrDefault()
 {
     return(Statements.FirstOrDefault());
 }