Programmatically build a predication

Build a predication with C# code

If you’re familiar with building expressions, buidling predications without using the Expressif language is similar. To achive this, use the class PredicationBuilder. The first predicate can be specified with the help of the Chain method.

var builder = new PredicationBuilder().Create<StartsWith>("Nik");
var predicate = builder.Build();
Assert.That(predicate.Evaluate("Nikola Tesla"), Is.True);

You can specify the combinational operator between two predicates with the help of the And, Or, Xor methods.

var builder = new PredicationBuilder()
    .Create<StartsWith>("ola")
    .Or<EndsWith>("sla")
    .And<SortedAfter>("Alan Turing")
    .Xor<SortedBefore>("Marie Curie");
var predicate = builder.Build();
Assert.That(predicate.Evaluate("Nikola Tesla"), Is.True);

To negate a predicate, you must use the AndNot, OrNot or XorNot methods from the builder class.

var builder = new PredicationBuilder()
    .Create<StartsWith>("ola")
    .OrNot<EndsWith>("Tes");
var predicate = builder.Build();
Assert.That(predicate.Evaluate("Nikola Tesla"), Is.True);

Any subpredication is automatically assigned as group. The serialization is illustrating this behaviour

var subPredicate = new PredicationBuilder()
    .Create<StartsWith>("Nik")
    .And<EndsWith>("sla");

var builder = new PredicationBuilder()
    .Create<LowerCase>()
    .Or(subPredicate)
    .Or<UpperCase>();

var str = builder.Serialize();
Assert.That(str, Is.EqualTo(" |OR upper-case}"));

Programmatically build an expression
Serialize an expression or a predication