Skip to content
Permalink
Browse files

Pull join conditions down from beta node to join node

  • Loading branch information...
snikolayev committed Apr 3, 2019
1 parent e2adfbe commit a4abadf20e9c888480da5222c3b60a36d0d7b822
@@ -28,7 +28,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Tuple> tup
var aggregation = new Aggregation();
foreach (var set in joinedSets)
{
var matchingFacts = GetMatchingFacts(context, set);
var matchingFacts = set.Facts;
IFactAggregator aggregator = CreateFactAggregator(set.Tuple);
AddToAggregate(aggregationContext, aggregator, aggregation, set.Tuple, matchingFacts);
}
@@ -45,17 +45,17 @@ public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tup
IFactAggregator aggregator = GetFactAggregator(set.Tuple);
if (aggregator != null)
{
if (_isSubnetJoin && HasRightFacts(context, set))
if (_isSubnetJoin && set.Facts.Count > 0)
{
//Update already propagated from the right
continue;
}
var matchingFacts = GetMatchingFacts(context, set);
var matchingFacts = set.Facts;
UpdateInAggregate(aggregationContext, aggregator, aggregation, set.Tuple, matchingFacts);
}
else
{
var matchingFacts = GetMatchingFacts(context, set);
var matchingFacts = set.Facts;
aggregator = CreateFactAggregator(set.Tuple);
AddToAggregate(aggregationContext, aggregator, aggregation, set.Tuple, matchingFacts);
}
@@ -85,7 +85,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Fact> fact
foreach (var set in joinedSets)
{
if (set.Facts.Count == 0) continue;
var matchingFacts = GetMatchingFacts(context, set);
var matchingFacts = set.Facts;
if (matchingFacts.Count > 0)
{
IFactAggregator aggregator = GetFactAggregator(set.Tuple);
@@ -94,7 +94,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Fact> fact
aggregator = CreateFactAggregator(set.Tuple);

var originalSet = JoinedSet(context, set.Tuple);
var matchingOriginalFacts = GetMatchingFacts(context, originalSet);
var matchingOriginalFacts = originalSet.Facts;
AddToAggregate(aggregationContext, aggregator, aggregation, originalSet.Tuple, matchingOriginalFacts);
}

@@ -112,7 +112,7 @@ public override void PropagateUpdate(IExecutionContext context, IList<Fact> fact
foreach (var set in joinedSets)
{
if (set.Facts.Count == 0) continue;
var matchingFacts = GetMatchingFacts(context, set);
var matchingFacts = set.Facts;
if (matchingFacts.Count > 0)
{
IFactAggregator aggregator = GetFactAggregator(set.Tuple);
@@ -123,7 +123,7 @@ public override void PropagateUpdate(IExecutionContext context, IList<Fact> fact
else
{
var fullSet = JoinedSet(context, set.Tuple);
var allMatchingFacts = GetMatchingFacts(context, fullSet);
var allMatchingFacts = fullSet.Facts;
aggregator = CreateFactAggregator(fullSet.Tuple);
AddToAggregate(aggregationContext, aggregator, aggregation, fullSet.Tuple, allMatchingFacts);
}
@@ -140,7 +140,7 @@ public override void PropagateRetract(IExecutionContext context, IList<Fact> fac
foreach (var set in joinedSets)
{
if (set.Facts.Count == 0) continue;
var matchingFacts = GetMatchingFacts(context, set);
var matchingFacts = set.Facts;
if (matchingFacts.Count > 0)
{
IFactAggregator aggregator = GetFactAggregator(set.Tuple);
@@ -158,17 +158,6 @@ public override void Accept<TContext>(TContext context, ReteNodeVisitor<TContext
visitor.VisitAggregateNode(context, this);
}

private List<Fact> GetMatchingFacts(IExecutionContext context, TupleFactSet set)
{
var matchingFacts = new List<Fact>();
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
matchingFacts.Add(fact);
}
return matchingFacts;
}

private void AddToAggregate(AggregationContext context, IFactAggregator aggregator, Aggregation aggregation, Tuple tuple, IList<Fact> facts)
{
try
@@ -186,7 +175,7 @@ private void AddToAggregate(AggregationContext context, IFactAggregator aggregat
}
}

private void UpdateInAggregate(AggregationContext context, IFactAggregator aggregator, Aggregation aggregation, Tuple tuple, List<Fact> facts)
private void UpdateInAggregate(AggregationContext context, IFactAggregator aggregator, Aggregation aggregation, Tuple tuple, IList<Fact> facts)
{
try
{
@@ -266,17 +255,5 @@ private IFactAggregator RemoveFactAggregator(Tuple tuple)
var factAggregator = tuple.RemoveState<IFactAggregator>(this);
return factAggregator;
}

private bool HasRightFacts(IExecutionContext context, TupleFactSet set)
{
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
{
return true;
}
}
return false;
}
}
}
@@ -12,33 +12,19 @@ internal abstract class BinaryBetaNode : BetaNode, IObjectSink
public ITupleSource LeftSource { get; }
public IObjectSource RightSource { get; }

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public IList<IBetaCondition> Conditions { get; }

protected BinaryBetaNode(ITupleSource leftSource, IObjectSource rightSource)
{
LeftSource = leftSource;
RightSource = rightSource;

LeftSource.Attach(this);
RightSource.Attach(this);

Conditions = new List<IBetaCondition>();
}

public abstract void PropagateAssert(IExecutionContext context, IList<Fact> facts);
public abstract void PropagateUpdate(IExecutionContext context, IList<Fact> facts);
public abstract void PropagateRetract(IExecutionContext context, IList<Fact> facts);

protected bool MatchesConditions(IExecutionContext context, Tuple left, Fact right)
{
foreach (var condition in Conditions)
{
if (!condition.IsSatisfiedBy(context, NodeInfo, left, right)) return false;
}
return true;
}

protected TupleFactSet JoinedSet(IExecutionContext context, Tuple tuple)
{
int level = tuple.Level;
@@ -17,10 +17,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Tuple> tup
var quantifier = set.Tuple.CreateQuantifier(this);
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
{
quantifier.Value++;
}
quantifier.Value++;
}
if (quantifier.Value > 0)
{
@@ -66,10 +63,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Fact> fact
int startingCount = quantifier.Value;
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
{
quantifier.Value++;
}
quantifier.Value++;
}
if (startingCount == 0 && quantifier.Value > 0)
{
@@ -94,10 +88,7 @@ public override void PropagateRetract(IExecutionContext context, IList<Fact> fac
int startingCount = quantifier.Value;
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
{
quantifier.Value--;
}
quantifier.Value--;
}
if (startingCount > 0 && quantifier.Value == 0)
{
@@ -1,15 +1,20 @@
using System.Collections.Generic;
using System.Diagnostics;

namespace NRules.Rete
{
internal class JoinNode : BinaryBetaNode
{
private readonly bool _isSubnetJoin;

[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public IList<IBetaCondition> Conditions { get; }

public JoinNode(ITupleSource leftSource, IObjectSource rightSource, bool isSubnetJoin)
: base(leftSource, rightSource)
{
_isSubnetJoin = isSubnetJoin;
Conditions = new List<IBetaCondition>();
}

public override void PropagateAssert(IExecutionContext context, IList<Tuple> tuples)
@@ -106,6 +111,16 @@ public override void PropagateRetract(IExecutionContext context, IList<Fact> fac
MemoryNode.PropagateRetract(context, toRetract);
}

private bool MatchesConditions(IExecutionContext context, Tuple left, Fact right)
{
foreach (var condition in Conditions)
{
if (!condition.IsSatisfiedBy(context, NodeInfo, left, right))
return false;
}
return true;
}

public override void Accept<TContext>(TContext context, ReteNodeVisitor<TContext> visitor)
{
visitor.VisitJoinNode(context, this);
@@ -17,10 +17,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Tuple> tup
var quantifier = set.Tuple.CreateQuantifier(this);
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
{
quantifier.Value++;
}
quantifier.Value++;
}
if (quantifier.Value == 0)
{
@@ -66,10 +63,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Fact> fact
int startingCount = quantifier.Value;
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
{
quantifier.Value++;
}
quantifier.Value++;
}
if (startingCount == 0 && quantifier.Value > 0)
{
@@ -94,10 +88,7 @@ public override void PropagateRetract(IExecutionContext context, IList<Fact> fac
int startingCount = quantifier.Value;
foreach (var fact in set.Facts)
{
if (MatchesConditions(context, set.Tuple, fact))
{
quantifier.Value--;
}
quantifier.Value--;
}
if (startingCount > 0 && quantifier.Value == 0)
{

0 comments on commit a4abadf

Please sign in to comment.
You can’t perform that action at this time.