Ejemplo n.º 1
0
        public void SeveralExpressionsInSingleCallToByEvaluating(
            IEvaluationEngine engine,
            int answer)
        {
            "establish an evaluation engine with expression aggregator"._(() =>
            {
                engine = new EvaluationEngine();
                engine.Solve <HowManyFruitsAreThere, int>()
                .AggregateWithExpressionAggregator(0, (aggregate, value) => aggregate + value);
            });

            "when defining several expressions with a single call to by evaluating"._(() =>
            {
                engine.Solve <HowManyFruitsAreThere, int>()
                .ByEvaluating(q => new[] { new FruitCountExpression {
                                               NumberOfFruits = 2
                                           }, new FruitCountExpression {
                                               NumberOfFruits = 3
                                           } });

                answer = engine.Answer(new HowManyFruitsAreThere());
            });

            "it should evaluate all expressions for the question"._(() =>
            {
                answer.Should().Be(5);
            });
        }
Ejemplo n.º 2
0
        public void SeveralCallsWithSingleExpressionToSolve(
            IEvaluationEngine engine,
            int answer)
        {
            "establish an evaluation engine with expression aggregator"._(() =>
            {
                engine = new EvaluationEngine();
                engine.Solve <HowManyFruitsAreThere, int>()
                .AggregateWithExpressionAggregator(0, (aggregate, value) => aggregate + value);
            });

            "when defining several expressions with individual calls to solve"._(() =>
            {
                engine.Solve <HowManyFruitsAreThere, int>()
                .ByEvaluating(q => new FruitCountExpression {
                    NumberOfFruits = 2
                });

                engine.Solve <HowManyFruitsAreThere, int>()
                .ByEvaluating(q => new FruitCountExpression {
                    NumberOfFruits = 3
                });

                answer = engine.Answer(new HowManyFruitsAreThere());
            });

            "it should evaluate all expressions for the question"._(() =>
            {
                answer.Should().Be(5);
            });
        }
Ejemplo n.º 3
0
        public void AggregatorStrategy(
            IEvaluationEngine engine,
            IAggregator <int, int, Missing> aggregator,
            int answer)
        {
            "establish an evaluatione engine"._(() =>
            {
                engine = new EvaluationEngine();

                aggregator = A.Fake <IAggregator <int, int, Missing> >();
                A.CallTo(() => aggregator.Aggregate(A <IEnumerable <IExpression <int, Missing> > > ._, Missing.Value, A <Context> ._)).Returns(TheAnswer);
            });

            "when defining aggregator strategy"._(() =>
            {
                engine.Solve <HowManyFruitsAreThere, int>()
                .WithAggregatorStrategy()
                .AggregateWith(aggregator);

                answer = engine.Answer(new HowManyFruitsAreThere());
            });

            "it should use aggregator strategy to answer the question"._(() =>
            {
                answer.Should().Be(TheAnswer);
            });
        }
Ejemplo n.º 4
0
        public void LoadModule(
            IEvaluationEngine testee)
        {
            "establish an evaluation engine"._(() =>
            {
                testee = new EvaluationEngine();
                testee.Solve <HowManyFruitsAreThere, int>()
                .AggregateWithExpressionAggregator(0, (aggregate, value) => aggregate + value)
                .ByEvaluating(q => new FruitCountExpression {
                    Kind = "Apples", NumberOfFruits = NumberOfApples
                });
            });

            "when loading a module into an evaluation engine"._(() =>
            {
                IEvaluationEngineModule module = new FruitModule();
                testee.Load(module);
            });

            "it should use definitions from module to answer questions too"._(() =>
            {
                int answer = testee.Answer(new HowManyFruitsAreThere());

                answer.Should().Be(NumberOfApples + NumberOfAnanas);
            });
        }
Ejemplo n.º 5
0
        public void ParameterlessQuestion(
            IEvaluationEngine evaluationEngine,
            int answer)
        {
            const int NumberOfApples  = 3;
            const int NumberOfBananas = 2;

            "establish an evaluation engine"._(() =>
            {
                evaluationEngine = new EvaluationEngine();
            });

            "when calling answer on evaluation engine"._(() =>
            {
                evaluationEngine.Solve <HowManyFruitsAreThere, int>()
                .AggregateWithExpressionAggregator(0, (aggregate, value) => aggregate + value)
                .ByEvaluating(q => new FruitCountExpression {
                    Kind = "Apples", NumberOfFruits = NumberOfApples
                })
                .ByEvaluating(q => new FruitCountExpression {
                    Kind = "Bananas", NumberOfFruits = NumberOfBananas
                });

                answer = evaluationEngine.Answer(new HowManyFruitsAreThere());
            });

            "it should aggregate the result of all expressions into a single result"._(() =>
            {
                answer.Should().Be(NumberOfApples + NumberOfBananas);
            });
        }
        public void AskingParent(
            string parentAnswer,
            IEvaluationEngine parentEngine,
            IEvaluationEngine childEngine)
        {
            "establish an hierarchical evaluation engine"._(() =>
            {
                parentEngine = new EvaluationEngine();
                childEngine  = new EvaluationEngine(parentEngine);

                parentEngine.Solve <Question, string>()
                .AggregateWithExpressionAggregator(ParentAggregator, (aggregate, value) => aggregate + value)
                .ByEvaluating((q, p) => ParentExpression);

                childEngine.Solve <Question, string>()
                .AggregateWithExpressionAggregator(ChildAggregator, (aggregate, value) => aggregate + value)
                .ByEvaluating((q, p) => ChildExpression);
            });

            "when calling answer on a parent evaluation engine"._(() =>
            {
                parentAnswer = parentEngine.Answer(new Question());
            });

            "it should use parent aggregator"._(() =>
            {
                parentAnswer.Should().Contain(ParentAggregator);
            });

            "it should use expressions only from parent"._(() =>
            {
                parentAnswer.Should().NotContain(ChildExpression);
            });
        }
Ejemplo n.º 7
0
        public void ParametrizedQuestion()
        {
            const string Parameter = "hello world of questions and answers.";

            IEvaluationEngine evaluationEngine = null;

            WordCountExpression expression = null;

            "establish"._(() =>
            {
                evaluationEngine = new EvaluationEngine();
                expression       = new WordCountExpression();

                evaluationEngine.Solve <HowManyWordsDoesThisTextHave, int, string>()
                .AggregateWithSingleExpressionAggregator()
                .ByEvaluating(q => expression);
            });

            "when calling answer on evaluation engine with a parameter"._(() =>
            {
                evaluationEngine.Answer(new HowManyWordsDoesThisTextHave(), Parameter);
            });

            "it should pass parameter to the evaluation expressions"._(() =>
            {
                expression.ReceivedParameter.Should().Be(Parameter);
            });
        }
Ejemplo n.º 8
0
        public void InlineExpressions(
            IEvaluationEngine engine,
            int answer)
        {
            "establish an evaluation engine with expression aggregator"._(() =>
            {
                engine = new EvaluationEngine();
                engine.Solve <HowManyFruitsAreThere, int>()
                .AggregateWithExpressionAggregator(0, (aggregate, value) => aggregate + value);
            });

            "when defining inline expressions in the call to by evaluating"._(() =>
            {
                engine.Solve <HowManyFruitsAreThere, int>()
                .ByEvaluating((q, p) => 2)
                .ByEvaluating((q, p) => 3);

                answer = engine.Answer(new HowManyFruitsAreThere());
            });

            "it should evaluate all expressions for the question"._(() =>
            {
                answer.Should().Be(5);
            });
        }
Ejemplo n.º 9
0
 public DoorSensor(
     IVhptDoor door, 
     IStateMachine<States, Events> stateMachine, 
     IAsynchronousFileLogger fileLogger,
     IVhptTravelCoordinator travelCoordinator,
     IEvaluationEngine evaluationEngine)
 {
     this.door = door;
     this.stateMachine = stateMachine;
     this.fileLogger = fileLogger;
     this.travelCoordinator = travelCoordinator;
     this.evaluationEngine = evaluationEngine;
 }
Ejemplo n.º 10
0
        public void Calculation()
        {
            IEvaluationEngine engine = null;

            Logger logExtension = null;

            "establish"._(() =>
            {
                logExtension = new Logger();

                engine = new EvaluationEngine();
                engine.SetLogExtension(logExtension);

                engine.Solve <HowManyFruitsAreThereStartingWith, int, char>()
                .AggregateWithExpressionAggregator(0, (aggregate, value) => aggregate + value)
                .ByEvaluating(q => new ParametrizedFruitExpression {
                    Kind = "Apple", Count = 1
                })
                .When(q => true)
                .ByEvaluating(q => new[] { new ParametrizedFruitExpression {
                                               Kind = "Ananas", Count = 2
                                           }, new ParametrizedFruitExpression {
                                               Kind = "Banana", Count = 4
                                           } })
                .ByEvaluating((q, p) => p == 'B' ? 8 : 0)
                .When(q => false)
                .ByEvaluating(q => new ParametrizedFruitExpression {
                    Kind = "Unknown"
                });
            });

            "when an answer is calculated"._(() =>
            {
                engine.Answer(new HowManyFruitsAreThereStartingWith(), 'A');
            });

            "it should log how answer was derived"._(() =>
            {
                logExtension.FoundAnswerLog
                .Should().Contain(HowManyFruitsAreThereStartingWith.Description, "answered question")
                .And.Contain("aggregator strategy", "used strategy")
                .And.Contain("expression aggregator with seed '0' and aggregate function (aggregate, value) => (aggregate + value)", "used aggregator")
                .And.Contain("Parameter = A", "provided parameter")
                .And.Contain("Answer = 3", "calculated answer")
                .And.Contain("1 Apple returned 1", "used expression with result")
                .And.Contain("2 Ananas returned 2", "used expression with result")
                .And.Contain("4 Banana returned 0", "used expression with result")
                .And.Contain("inline expression = (q, p) => IIF((Convert(p) == 66), 8, 0) returned 0", "used expression with result")
                .And.NotContain("Unknown", "unknwon expression should not be evaluated due to unfulfilled constraint");
            });
        }
        public void AskingChild(
            string childAnswer,
            IEvaluationEngine parentEngine,
            IEvaluationEngine childEngine)
        {
            "establish an hierarchical evaluation engine"._(() =>
            {
                parentEngine = new EvaluationEngine();
                childEngine  = new EvaluationEngine(parentEngine);

                parentEngine.Solve <Question, string>()
                .AggregateWithExpressionAggregator(ParentAggregator, (aggregate, value) => aggregate + value)
                .ByEvaluating((q, p) => ParentExpression);

                childEngine.Solve <Question, string>()
                .AggregateWithExpressionAggregator(ChildAggregator, (aggregate, value) => aggregate + value)
                .ByEvaluating((q, p) => ChildExpression);
            });

            "when calling answer on a child evaluation engine"._(() =>
            {
                childAnswer = childEngine.Answer(new Question());
            });

            "it should override parent aggregator with child aggregator"._(() =>
            {
                childAnswer.Should().Contain(ChildAggregator);
            });

            "it should use expressions from child and parent"._(() =>
            {
                childAnswer
                .Should().Contain(ParentExpression)
                .And.Contain(ChildExpression);
            });

            "it should evaluate expressions from parent first"._(() =>
            {
                childAnswer.EndsWith(ParentExpression + ChildExpression, StringComparison.Ordinal)
                .Should().BeTrue();
            });
        }
Ejemplo n.º 12
0
        public void Constraints(
            IEvaluationEngine engine,
            string answer)
        {
            const string NoConstraint        = "N";
            const string WithTrueConstraint  = "T";
            const string WithFalseConstraint = "F";

            "establish a solving strategy with constraints"._(() =>
            {
                engine = new EvaluationEngine();

                engine.Solve <WhatIsTheText, string>()
                .AggregateWithExpressionAggregator(string.Empty, (aggregate, value) => aggregate + value)
                .ByEvaluating((q, p) => NoConstraint)
                .When(q => false)
                .ByEvaluating((q, p) => WithFalseConstraint)
                .When(q => true)
                .ByEvaluating((q, p) => WithTrueConstraint);
            });

            "when calling answer"._(() =>
            {
                answer = engine.Answer(new WhatIsTheText());
            });

            "it should evaluate expressions without constraints"._(() =>
            {
                answer.Should().Contain(NoConstraint);
            });

            "it should evaluate expressions with fulfilled constraints"._(() =>
            {
                answer.Should().Contain(WithTrueConstraint);
            });

            "it should ignore expressions with constraints that are not fulfilled"._(() =>
            {
                answer.Should().NotContain(WithFalseConstraint);
            });
        }
Ejemplo n.º 13
0
        public void CustomStrategy(
            IEvaluationEngine engine,
            int answer)
        {
            "establish an evaluatione engine"._(() =>
            {
                engine = new EvaluationEngine();
            });

            "when defining an own strategy"._(() =>
            {
                engine.Solve <HowManyFruitsAreThere, int>()
                .With(new SpecialStrategy());

                answer = engine.Answer(new HowManyFruitsAreThere());
            });

            "it should use own strategy instead of default strategy to answer the question"._(() =>
            {
                answer.Should().Be(TheAnswer);
            });
        }
Ejemplo n.º 14
0
        public void MissingAggregator()
        {
            IEvaluationEngine engine = null;

            Exception exception = null;

            "establish"._(() =>
            {
                engine = new EvaluationEngine();

                engine.Solve <HowManyFruitsAreThere, int>();
            });

            "when calling answer on evaluation engine and no aggregator is specified"._(() =>
            {
                exception = Catch.Exception(() => engine.Answer(new HowManyFruitsAreThere()));
            });

            "it should throw invalid operation exception"._(() =>
            {
                exception.Should().BeOfType <InvalidOperationException>();
            });
        }
Ejemplo n.º 15
0
 SolveHowCoolIsTheEvaluationEngine(this IEvaluationEngine engine)
 {
     return(engine.Solve <HowCoolIsTheEvaluationEngine, string>());
 }
Ejemplo n.º 16
0
 public LeastSquaresResolutionStrategy(IEvaluationEngine engine)
 {
     _engine = engine;
     _minimizationStrategy = _engine.CreatemMinimizationStrategy("LeastSquares");
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Questioner"/> class.
 /// </summary>
 /// <param name="evaluationEngine">The evaluation engine.</param>
 public Questioner(IEvaluationEngine evaluationEngine)
 {
     this.evaluationEngine = evaluationEngine;
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Answerer"/> class.
 /// </summary>
 /// <param name="evaluationEngine">The evaluation engine.</param>
 public Answerer(IEvaluationEngine evaluationEngine)
 {
     this.evaluationEngine = evaluationEngine;
 }
Ejemplo n.º 19
0
 public X2ResolutionStrategy(IEvaluationEngine engine)
 {
     _engine = engine;
     _minimizationStrategy = _engine.CreatemMinimizationStrategy("X2");
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Questioner"/> class.
 /// </summary>
 /// <param name="evaluationEngine">The evaluation engine.</param>
 public Questioner(IEvaluationEngine evaluationEngine)
 {
     this.evaluationEngine = evaluationEngine;
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Answerer"/> class.
 /// </summary>
 /// <param name="evaluationEngine">The evaluation engine.</param>
 public Answerer(IEvaluationEngine evaluationEngine)
 {
     this.evaluationEngine = evaluationEngine;
 }
Ejemplo n.º 22
0
 SolveHowManyVowelsAreInThisText(this IEvaluationEngine engine)
 {
     return(engine.Solve <HowManyVowelsAreInThisText, int, string>());
 }
Ejemplo n.º 23
0
 public FuzzyPreferenceProgramming(IEvaluationEngine engine)
 {
     _engine = engine;
 }
Ejemplo n.º 24
0
 public MainEigenvectorResolutionStrategy(IEvaluationEngine engine)
 {
     _engine = engine;
 }