Skip to content

Commit

Permalink
feat: combine existing predicates (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Dec 27, 2023
1 parent 99f12f6 commit e0df8c4
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Expressif.Testing/Predicates/PredicateCombinerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Expressif.Predicates.Text;
using Expressif.Predicates;

namespace Expressif.Testing.Predicates;
public class PredicateCombinerTest
{
[Test]
[TestCase("Nikola Tesla", true)]
[TestCase("Albert Einstein", false)]
public void Build_Combination_Success(string value, bool expected)
{
var combiner = new PredicateCombiner();
var combination = combiner.With(new StartsWith(() => "Alb"))
.Or(new EndsWith(() => "sla"))
.And(new StartsWith(() => "Nik"));
var combined = combination.Build();
Assert.That(combined.Evaluate(value), Is.EqualTo(expected));
}

[Test]
[TestCase("Nikola Tesla", true)]
[TestCase("Albert Einstein", false)]
public void Build_CombinationWithNot_Success(string value, bool expected)
{
var combiner = new PredicateCombiner();
var combination = combiner.WithNot(new StartsWith(() => "Alb"))
.Or(new EndsWith(() => "ring"))
.And(new StartsWith(() => "Nik"));
var combined = combination.Build();
Assert.That(combined.Evaluate(value), Is.EqualTo(expected));
}

[Test]
[TestCase("Nikola Tesla", true)]
[TestCase("Albert Einstein", false)]
public void Build_CombinationUsingNot_Success(string value, bool expected)
{
var combiner = new PredicateCombiner();
var combination = combiner.With(new StartsWith(() => "Nik"))
.Or(new EndsWith(() => "stein"))
.AndNot(new StartsWith(() => "Alb"));
var combined = combination.Build();
Assert.That(combined.Evaluate(value), Is.EqualTo(expected));
}
}
56 changes: 56 additions & 0 deletions Expressif/Predicates/PredicateCombiner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Expressif.Predicates.Operators;

namespace Expressif.Predicates;
public class PredicateCombiner
{
protected UnaryOperatorFactory UnaryFactory { get; }
protected BinaryOperatorFactory BinaryFactory { get; }

public PredicateCombiner()
: this(new(), new()) { }

public PredicateCombiner(UnaryOperatorFactory unaryFactory , BinaryOperatorFactory binaryFactory)
=> (UnaryFactory, BinaryFactory) = (unaryFactory, binaryFactory);

public PredicateRightCombiner With(IPredicate left)
=> new(UnaryFactory, BinaryFactory, left);

public PredicateRightCombiner WithNot(IPredicate left)
=> new(UnaryFactory, BinaryFactory, UnaryFactory.Instantiate("!", left));

public class PredicateRightCombiner
{
protected UnaryOperatorFactory UnaryFactory { get; }
protected BinaryOperatorFactory BinaryFactory { get; }
private IPredicate State { get; }

internal PredicateRightCombiner(UnaryOperatorFactory unaryFactory, BinaryOperatorFactory binaryFactory, IPredicate state)
=> (UnaryFactory, BinaryFactory, State) = (unaryFactory, binaryFactory, state);

public PredicateRightCombiner Or(IPredicate right)
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("OR", State, right));

public PredicateRightCombiner OrNot(IPredicate right)
=> Or(UnaryFactory.Instantiate("!", right));

public PredicateRightCombiner And(IPredicate right)
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("AND", State, right));

public PredicateRightCombiner AndNot(IPredicate right)
=> And(UnaryFactory.Instantiate("!", right));

public PredicateRightCombiner Xor(IPredicate right)
=> new(UnaryFactory, BinaryFactory, BinaryFactory.Instantiate("XOR", State, right));

public PredicateRightCombiner XorNot(IPredicate right)
=> Xor(UnaryFactory.Instantiate("!", right));

public IPredicate Build()
=> State!;
}
}

0 comments on commit e0df8c4

Please sign in to comment.