Skip to content
Permalink
Browse files

Expose rules related to expression on expression events #152

  • Loading branch information...
snikolayev committed Apr 2, 2019
1 parent 156b66b commit 655f5f5e7277545d5194c1737f11accb668ff2a3
@@ -54,12 +54,12 @@ public object Invoke(AggregationContext context, ITuple tuple, IFact fact)
{
exception = e;
bool isHandled = false;
context.EventAggregator.RaiseLhsExpressionFailed(context.Session, e, _expression, factValue, tuple, fact, ref isHandled);
context.EventAggregator.RaiseLhsExpressionFailed(context.Session, e, _expression, factValue, tuple, fact, context.NodeInfo, ref isHandled);
throw new ExpressionEvaluationException(e, _expression, isHandled);
}
finally
{
context.EventAggregator.RaiseLhsExpressionEvaluated(context.Session, exception, _expression, factValue, result, tuple, fact);
context.EventAggregator.RaiseLhsExpressionEvaluated(context.Session, exception, _expression, factValue, result, tuple, fact, context.NodeInfo);
}
}
}
@@ -102,12 +102,12 @@ public object Invoke(AggregationContext context, ITuple tuple, IFact fact)
{
exception = e;
bool isHandled = false;
context.EventAggregator.RaiseLhsExpressionFailed(context.Session, e, _expression, args, tuple, fact, ref isHandled);
context.EventAggregator.RaiseLhsExpressionFailed(context.Session, e, _expression, args, tuple, fact, context.NodeInfo, ref isHandled);
throw new ExpressionEvaluationException(e, _expression, isHandled);
}
finally
{
context.EventAggregator.RaiseLhsExpressionEvaluated(context.Session, exception, _expression, args, result, tuple, fact);
context.EventAggregator.RaiseLhsExpressionEvaluated(context.Session, exception, _expression, args, result, tuple, fact, context.NodeInfo);
}
}

@@ -1,4 +1,5 @@
using NRules.Diagnostics;
using NRules.Rete;

namespace NRules.Aggregators
{
@@ -9,11 +10,13 @@ public class AggregationContext
{
internal ISessionInternal Session { get; }
internal IEventAggregator EventAggregator { get; }
internal NodeDebugInfo NodeInfo { get; }

internal AggregationContext(ISessionInternal session, IEventAggregator eventAggregator)
internal AggregationContext(ISessionInternal session, IEventAggregator eventAggregator, NodeDebugInfo nodeInfo)
{
Session = session;
EventAggregator = eventAggregator;
NodeInfo = nodeInfo;
}
}
}
@@ -2,6 +2,7 @@
using System.Linq.Expressions;
using NRules.AgendaFilters;
using NRules.Extensibility;
using NRules.Rete;
using NRules.RuleModel;

namespace NRules.Diagnostics
@@ -128,10 +129,10 @@ internal interface IEventAggregator : IEventProvider
void RaiseFactUpdated(ISession session, IFact fact);
void RaiseFactRetracting(ISession session, IFact fact);
void RaiseFactRetracted(ISession session, IFact fact);
void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object argument, object result, ITuple tuple, IFact fact);
void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, object result, ITuple tuple, IFact fact);
void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object argument, ITuple tuple, IFact fact, ref bool isHandled);
void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object[] arguments, ITuple tuple, IFact fact, ref bool isHandled);
void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object argument, object result, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo);
void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, object result, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo);
void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object argument, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo, ref bool isHandled);
void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object[] arguments, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo, ref bool isHandled);
void RaiseAgendaExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, object result, IMatch match);
void RaiseAgendaExpressionFailed(ISession session, Exception exception, Expression expression, object[] arguments, IMatch match, ref bool isHandled);
void RaiseRhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, IMatch match);
@@ -290,59 +291,59 @@ public void RaiseFactRetracted(ISession session, IFact fact)
_parent?.RaiseFactRetracted(session, fact);
}

public void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object argument, object result, ITuple tuple, IFact fact)
public void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object argument, object result, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo)
{
var hanlder = LhsExpressionEvaluatedEvent;
if (hanlder != null)
var handler = LhsExpressionEvaluatedEvent;
if (handler != null)
{
var @event = new LhsExpressionEventArgs(expression, exception, argument, result, tuple, fact);
hanlder(session, @event);
var @event = new LhsExpressionEventArgs(expression, exception, argument, result, tuple, fact, nodeInfo.Rules);
handler(session, @event);
}
_parent?.RaiseLhsExpressionEvaluated(session, exception, expression, argument, result, tuple, fact);
_parent?.RaiseLhsExpressionEvaluated(session, exception, expression, argument, result, tuple, fact, nodeInfo);
}

public void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, object result, ITuple tuple, IFact fact)
public void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, object result, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo)
{
var hanlder = LhsExpressionEvaluatedEvent;
if (hanlder != null)
var handler = LhsExpressionEvaluatedEvent;
if (handler != null)
{
var @event = new LhsExpressionEventArgs(expression, exception, arguments, result, tuple, fact);
hanlder(session, @event);
var @event = new LhsExpressionEventArgs(expression, exception, arguments, result, tuple, fact, nodeInfo.Rules);
handler(session, @event);
}
_parent?.RaiseLhsExpressionEvaluated(session, exception, expression, arguments, result, tuple, fact);
_parent?.RaiseLhsExpressionEvaluated(session, exception, expression, arguments, result, tuple, fact, nodeInfo);
}

public void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object argument, ITuple tuple, IFact fact, ref bool isHandled)
public void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object argument, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo, ref bool isHandled)
{
var hanlder = LhsExpressionFailedEvent;
if (hanlder != null)
var handler = LhsExpressionFailedEvent;
if (handler != null)
{
var @event = new LhsExpressionErrorEventArgs(expression, exception, argument, tuple, fact);
hanlder(session, @event);
var @event = new LhsExpressionErrorEventArgs(expression, exception, argument, tuple, fact, nodeInfo.Rules);
handler(session, @event);
isHandled |= @event.IsHandled;
}
_parent?.RaiseLhsExpressionFailed(session, exception, expression, argument, tuple, fact, ref isHandled);
_parent?.RaiseLhsExpressionFailed(session, exception, expression, argument, tuple, fact, nodeInfo, ref isHandled);
}

public void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object[] arguments, ITuple tuple, IFact fact, ref bool isHandled)
public void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, object[] arguments, ITuple tuple, IFact fact, NodeDebugInfo nodeInfo, ref bool isHandled)
{
var hanlder = LhsExpressionFailedEvent;
if (hanlder != null)
var handler = LhsExpressionFailedEvent;
if (handler != null)
{
var @event = new LhsExpressionErrorEventArgs(expression, exception, arguments, tuple, fact);
hanlder(session, @event);
var @event = new LhsExpressionErrorEventArgs(expression, exception, arguments, tuple, fact, nodeInfo.Rules);
handler(session, @event);
isHandled |= @event.IsHandled;
}
_parent?.RaiseLhsExpressionFailed(session, exception, expression, arguments, tuple, fact, ref isHandled);
_parent?.RaiseLhsExpressionFailed(session, exception, expression, arguments, tuple, fact, nodeInfo, ref isHandled);
}

public void RaiseAgendaExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, object result, IMatch match)
{
var hanlder = AgendaExpressionEvaluatedEvent;
if (hanlder != null)
var handler = AgendaExpressionEvaluatedEvent;
if (handler != null)
{
var @event = new AgendaExpressionEventArgs(expression, exception, arguments, result, match);
hanlder(session, @event);
handler(session, @event);
}
_parent?.RaiseAgendaExpressionEvaluated(session, exception, expression, arguments, result, match);
}
@@ -361,11 +362,11 @@ public void RaiseAgendaExpressionFailed(ISession session, Exception exception, E

public void RaiseRhsExpressionEvaluated(ISession session, Exception exception, Expression expression, object[] arguments, IMatch match)
{
var hanlder = RhsExpressionEvaluatedEvent;
if (hanlder != null)
var handler = RhsExpressionEvaluatedEvent;
if (handler != null)
{
var @event = new RhsExpressionEventArgs(expression, exception, arguments, match);
hanlder(session, @event);
handler(session, @event);
}
_parent?.RaiseRhsExpressionEvaluated(session, exception, expression, arguments, match);
}
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using NRules.RuleModel;

@@ -17,8 +18,9 @@ public class LhsExpressionErrorEventArgs : LhsExpressionEventArgs, IRecoverableE
/// <param name="arguments">Expression arguments.</param>
/// <param name="tuple">Tuple related to the event.</param>
/// <param name="fact">Fact related to the event.</param>
public LhsExpressionErrorEventArgs(Expression expression, Exception exception, object[] arguments, ITuple tuple, IFact fact)
: base(expression, exception, arguments, null, tuple, fact)
/// <param name="rules">Rules that contain the expression that generated the event.</param>
public LhsExpressionErrorEventArgs(Expression expression, Exception exception, object[] arguments, ITuple tuple, IFact fact, IEnumerable<IRuleDefinition> rules)
: base(expression, exception, arguments, null, tuple, fact, rules)
{
}

@@ -30,8 +32,9 @@ public LhsExpressionErrorEventArgs(Expression expression, Exception exception, o
/// <param name="argument">Expression argument.</param>
/// <param name="tuple">Tuple related to the event.</param>
/// <param name="fact">Fact related to the event.</param>
public LhsExpressionErrorEventArgs(Expression expression, Exception exception, object argument, ITuple tuple, IFact fact)
: base(expression, exception, argument, null, tuple, fact)
/// <param name="rules">Rules that contain the expression that generated the event.</param>
public LhsExpressionErrorEventArgs(Expression expression, Exception exception, object argument, ITuple tuple, IFact fact, IEnumerable<IRuleDefinition> rules)
: base(expression, exception, argument, null, tuple, fact, rules)
{
}

@@ -23,11 +23,13 @@ public class LhsExpressionEventArgs : ExpressionEventArgs
/// <param name="result">Expression result.</param>
/// <param name="tuple">Tuple related to the event.</param>
/// <param name="fact">Fact related to the event.</param>
public LhsExpressionEventArgs(Expression expression, Exception exception, object[] arguments, object result, ITuple tuple, IFact fact)
/// <param name="rules">Rules that contain the expression that generated the event.</param>
public LhsExpressionEventArgs(Expression expression, Exception exception, object[] arguments, object result, ITuple tuple, IFact fact, IEnumerable<IRuleDefinition> rules)
: base(expression, exception, arguments, result)
{
_tuple = tuple;
_fact = fact;
Rules = rules;
}

/// <summary>
@@ -39,11 +41,13 @@ public LhsExpressionEventArgs(Expression expression, Exception exception, object
/// <param name="result">Expression result.</param>
/// <param name="tuple">Tuple related to the event.</param>
/// <param name="fact">Fact related to the event.</param>
public LhsExpressionEventArgs(Expression expression, Exception exception, object argument, object result, ITuple tuple, IFact fact)
/// <param name="rules">Rules that contain the expression that generated the event.</param>
public LhsExpressionEventArgs(Expression expression, Exception exception, object argument, object result, ITuple tuple, IFact fact, IEnumerable<IRuleDefinition> rules)
: base(expression, exception, argument, result)
{
_tuple = tuple;
_fact = fact;
Rules = rules;
}

/// <summary>
@@ -67,5 +71,10 @@ public IEnumerable<IFact> Facts
}
}
}

/// <summary>
/// Rules that contain the expression that generated the event.
/// </summary>
public IEnumerable<IRuleDefinition> Rules { get; }
}
}
@@ -23,7 +23,7 @@ public AggregateNode(ITupleSource leftSource, IObjectSource rightSource, string

public override void PropagateAssert(IExecutionContext context, IList<Tuple> tuples)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator);
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, tuples);
var aggregation = new Aggregation();
foreach (var set in joinedSets)
@@ -37,7 +37,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Tuple> tup

public override void PropagateUpdate(IExecutionContext context, IList<Tuple> tuples)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator);
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, tuples);
var aggregation = new Aggregation();
foreach (var set in joinedSets)
@@ -79,7 +79,7 @@ public override void PropagateRetract(IExecutionContext context, IList<Tuple> tu

public override void PropagateAssert(IExecutionContext context, IList<Fact> facts)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator);
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, facts);
var aggregation = new Aggregation();
foreach (var set in joinedSets)
@@ -106,7 +106,7 @@ public override void PropagateAssert(IExecutionContext context, IList<Fact> fact

public override void PropagateUpdate(IExecutionContext context, IList<Fact> facts)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator);
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, facts);
var aggregation = new Aggregation();
foreach (var set in joinedSets)
@@ -134,7 +134,7 @@ public override void PropagateUpdate(IExecutionContext context, IList<Fact> fact

public override void PropagateRetract(IExecutionContext context, IList<Fact> facts)
{
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator);
var aggregationContext = new AggregationContext(context.Session, context.EventAggregator, NodeInfo);
var joinedSets = JoinedSets(context, facts);
var aggregation = new Aggregation();
foreach (var set in joinedSets)
@@ -6,7 +6,7 @@ namespace NRules.Rete
{
internal interface IAlphaCondition
{
bool IsSatisfiedBy(IExecutionContext context, Fact fact);
bool IsSatisfiedBy(IExecutionContext context, NodeDebugInfo nodeInfo, Fact fact);
}

internal sealed class AlphaCondition : IAlphaCondition, IEquatable<AlphaCondition>
@@ -20,7 +20,7 @@ public AlphaCondition(LambdaExpression expression, FastDelegate<Func<object, boo
_compiledExpression = compiledExpression;
}

public bool IsSatisfiedBy(IExecutionContext context, Fact fact)
public bool IsSatisfiedBy(IExecutionContext context, NodeDebugInfo nodeInfo, Fact fact)
{
var factValue = fact.Object;
Exception exception = null;
@@ -34,7 +34,7 @@ public bool IsSatisfiedBy(IExecutionContext context, Fact fact)
{
exception = e;
bool isHandled = false;
context.EventAggregator.RaiseLhsExpressionFailed(context.Session, e, _expression, factValue, null, fact, ref isHandled);
context.EventAggregator.RaiseLhsExpressionFailed(context.Session, e, _expression, factValue, null, fact, nodeInfo, ref isHandled);
if (!isHandled)
{
throw new RuleLhsExpressionEvaluationException("Failed to evaluate condition", _expression.ToString(), e);
@@ -43,7 +43,7 @@ public bool IsSatisfiedBy(IExecutionContext context, Fact fact)
}
finally
{
context.EventAggregator.RaiseLhsExpressionEvaluated(context.Session, exception, _expression, factValue, result, null, fact);
context.EventAggregator.RaiseLhsExpressionEvaluated(context.Session, exception, _expression, factValue, result, null, fact, nodeInfo);
}
}

@@ -8,8 +8,10 @@ internal abstract class AlphaNode : IObjectSink
protected AlphaNode()
{
ChildNodes = new List<AlphaNode>();
NodeInfo = new NodeDebugInfo();
}

public NodeDebugInfo NodeInfo { get; }
public AlphaMemoryNode MemoryNode { get; set; }

[DebuggerDisplay("Count = {ChildNodes.Count}")]
Oops, something went wrong.

0 comments on commit 655f5f5

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