internal Activation(ICompiledRule compiledRule, Tuple tuple, IndexMap tupleFactMap) { CompiledRule = compiledRule; Tuple = tuple; TupleFactMap = tupleFactMap; _matchedFacts = new Lazy <FactMatch[]>(CreateMatchedFacts); }
public override int GetHashCode() { unchecked { return((CompiledRule.GetHashCode() * 397) ^ Tuple.GetHashCode()); } }
internal ConditionErrorEventArgs(Exception exception, Expression expression, Tuple tuple, Fact fact) : base(exception) { Condition = expression; _tuple = tuple; _fact = fact; }
internal ConditionErrorEventArgs(Exception exception, Expression expression, Tuple tuple, Fact fact) : base(exception) { _tuple = tuple; _fact = fact; Condition = expression; }
public void Invoke(IExecutionContext context, IContext actionContext, Tuple tuple, FactIndexMap tupleFactMap) { var args = new object[_compiledAction.ParameterCount]; args[0] = actionContext; int index = tuple.Count - 1; foreach (var fact in tuple.Facts) { var mappedIndex = _actionFactMap.Map(tupleFactMap.Map(index)); FactIndexMap.SetElementAt(ref args, mappedIndex, 1, fact.Object); index--; } try { _compiledAction.Delegate.Invoke(args); } catch (Exception e) { bool isHandled; context.EventAggregator.RaiseActionFailed(context.Session, e, _expression, tuple, out isHandled); if (!isHandled) { throw new RuleActionEvaluationException("Failed to evaluate rule action", _expression.ToString(), e); } } }
public T GetState <T>(INode node, Tuple tuple) { var key = new TupleStateKey(node, tuple); if (_tupleStateMap.TryGetValue(key, out var value)) { return((T)value); } return(default);
public void Invoke(IContext context, Tuple tuple) { //todo: optimize IEnumerable <Fact> facts = _tupleMask.Select( idx => tuple.ElementAtOrDefault(idx)); object[] args = Enumerable.Repeat(context, 1).Union(facts.Select(f => f.Object)).ToArray(); _compiledAction.DynamicInvoke(args); }
private FactMatch[] GetMatchedFacts() { var matches = CompiledRule.Declarations.Select(x => new FactMatch(x)).ToArray(); int index = Tuple.Count - 1; var enumerator = Tuple.GetEnumerator(); while (enumerator.MoveNext()) { int factIndex = CompiledRule.FactMap[index]; var factMatch = matches[factIndex]; factMatch.SetFact(enumerator.Current); index--; } return(matches); }
public BenchmarkLhsExpression() { _nodeInfo = new NodeDebugInfo(); Expression <Func <string, int, decimal, bool> > betaExpression = (s, i, d) => s.Length == i; var betaElement = Element.Condition(betaExpression); _lhsExpression = ExpressionCompiler.CompileLhsTupleFactExpression <bool>(betaElement, betaElement.Imports.ToList()); _lhsTupleExpression = ExpressionCompiler.CompileLhsTupleExpression <bool>(betaElement, betaElement.Imports.ToList()); _tuple = ToTuple("abcd", 4, 1.0m); Expression <Func <string, bool> > alphaExpression = s => s.Length == 1; var alphaElement = Element.Condition(alphaExpression); _lhsFactExpression = ExpressionCompiler.CompileLhsFactExpression <bool>(alphaElement); _fact = new Fact("abcd"); }
public void Invoke(IExecutionContext context, IContext actionContext, Tuple tuple, IndexMap tupleFactMap, IEnumerable <IRuleDependency> dependencies) { var args = new object[_compiledAction.ParameterCount]; args[0] = actionContext; int index = tuple.Count - 1; foreach (var fact in tuple.Facts) { var mappedIndex = _factIndexMap[tupleFactMap[index]]; IndexMap.SetElementAt(ref args, mappedIndex, 1, fact.Object); index--; } index = 0; var dependencyResolver = context.Session.DependencyResolver; foreach (var dependency in dependencies) { var mappedIndex = _dependencyIndexMap[index]; if (mappedIndex >= 0) { var resolvedDependency = dependency.Factory(dependencyResolver, new ResolutionContext(context.Session, actionContext.Rule)); IndexMap.SetElementAt(ref args, mappedIndex, 1, resolvedDependency); } index++; } try { _compiledAction.Delegate.Invoke(args); } catch (Exception e) { bool isHandled; context.EventAggregator.RaiseActionFailed(context.Session, e, _expression, tuple, out isHandled); if (!isHandled) { throw new RuleActionEvaluationException("Failed to evaluate rule action", _expression.ToString(), e); } } }
public void Invoke(IExecutionContext context, IContext actionContext, Tuple tuple, IndexMap tupleFactMap, IEnumerable<IRuleDependency> dependencies) { var args = new object[_compiledAction.ParameterCount]; args[0] = actionContext; int index = tuple.Count - 1; foreach (var fact in tuple.Facts) { var mappedIndex = _factIndexMap[tupleFactMap[index]]; IndexMap.SetElementAt(ref args, mappedIndex, 1, fact.Object); index--; } index = 0; var dependencyResolver = context.Session.DependencyResolver; foreach (var dependency in dependencies) { var mappedIndex = _dependencyIndexMap[index]; if (mappedIndex >= 0) { var resolvedDependency = dependency.Factory(dependencyResolver, new ResolutionContext(context.Session, actionContext.Rule)); IndexMap.SetElementAt(ref args, mappedIndex, 1, resolvedDependency); } index++; } try { _compiledAction.Delegate.Invoke(args); } catch (Exception e) { bool isHandled; context.EventAggregator.RaiseActionFailed(context.Session, e, _expression, tuple, out isHandled); if (!isHandled) { throw new RuleActionEvaluationException("Failed to evaluate rule action", _expression.ToString(), e); } } }
public void RaiseConditionFailed(ISession session, Exception exception, Expression expression, Tuple tuple, Fact fact, ref bool isHandled) { var hanlder = ConditionFailedEvent; if (hanlder != null) { var @event = new ConditionErrorEventArgs(exception, expression, tuple, fact); hanlder(session, @event); isHandled |= @event.IsHandled; } _parent?.RaiseConditionFailed(session, exception, expression, tuple, fact, ref isHandled); }
public void RaiseBindingFailed(ISession session, Exception exception, Expression expression, Tuple tuple, ref bool isHandled) { var hanlder = BindingFailedEvent; if (hanlder != null) { var @event = new BindingErrorEventArgs(exception, expression, tuple); hanlder(session, @event); isHandled |= @event.IsHandled; } _parent?.RaiseBindingFailed(session, exception, expression, tuple, ref isHandled); }
internal BindingErrorEventArgs(Exception exception, Expression expression, Tuple tuple) : base(exception) { _tuple = tuple; Expression = expression; }
internal Activation(ICompiledRule compiledRule, Tuple tuple, IndexMap factMap) { CompiledRule = compiledRule; Tuple = tuple; FactMap = factMap; }
public void RaiseLhsExpressionEvaluated(ISession session, Exception exception, Expression expression, IArgumentMap argumentMap, object result, Tuple tuple, Fact fact, NodeInfo nodeInfo) { var handler = LhsExpressionEvaluatedEvent; if (handler != null) { var arguments = new LhsExpressionArguments(argumentMap, tuple, fact); var @event = new LhsExpressionEventArgs(expression, exception, arguments, result, tuple, fact, nodeInfo.Rules); handler(session, @event); } _parent?.RaiseLhsExpressionEvaluated(session, exception, expression, argumentMap, result, tuple, fact, nodeInfo); }
private void ResetAggregator(Aggregation aggregation, Tuple tuple, IFactAggregator aggregator) { tuple.RemoveState <IFactAggregator>(this); aggregation.Remove(tuple, aggregator.AggregateFacts); }
public void RaiseConditionFailed(ISession session, Exception exception, Expression expression, Tuple tuple, Fact fact) { var hanlder = ConditionFailedEvent; if (hanlder != null) { var @event = new ConditionErrorEventArgs(exception, expression, tuple, fact); hanlder(session, @event); } }
public void RaiseActionFailed(ISession session, Exception exception, Expression expression, Tuple tuple, out bool isHandled) { isHandled = false; var handler = ActionFailedEvent; if (handler != null) { var @event = new ActionErrorEventArgs(exception, expression, tuple); handler(session, @event); isHandled = @event.IsHandled; } }
internal Activation(ICompiledRule compiledRule, Tuple tuple) { CompiledRule = compiledRule; Tuple = tuple; }
private IFactAggregator RemoveFactAggregator(Tuple tuple) { var factAggregator = tuple.RemoveState <IFactAggregator>(this); return(factAggregator); }
private IFactAggregator GetFactAggregator(Tuple tuple) { var factAggregator = tuple.GetState <IFactAggregator>(this); return(factAggregator); }
public TupleStateKey(INode node, Tuple tuple) { _node = node; _tuple = tuple; }
public void RaiseLhsExpressionFailed(ISession session, Exception exception, Expression expression, IArgumentMap argumentMap, Tuple tuple, Fact fact, NodeInfo nodeInfo, ref bool isHandled) { var handler = LhsExpressionFailedEvent; if (handler != null) { var arguments = new LhsExpressionArguments(argumentMap, tuple, fact); var @event = new LhsExpressionErrorEventArgs(expression, exception, arguments, tuple, fact, nodeInfo.Rules); handler(session, @event); isHandled |= @event.IsHandled; } _parent?.RaiseLhsExpressionFailed(session, exception, expression, argumentMap, tuple, fact, nodeInfo, ref isHandled); }
public void RaiseActionFailed(ISession session, Exception exception, Expression expression, Tuple tuple, out bool isHandled) { isHandled = false; var handler = ActionFailedEvent; if (handler != null) { var @event = new ActionErrorEventArgs(exception, expression, tuple); handler(session, @event); isHandled = @event.IsHandled; } if (_parent != null && !isHandled) { _parent.RaiseActionFailed(session, exception, expression, tuple, out isHandled); } }
private void RetractFromAggregate(AggregationContext context, IFactAggregator aggregator, Aggregation aggregation, Tuple tuple, List <Fact> facts) { try { aggregator.Remove(context, aggregation, tuple, facts); } catch (ExpressionEvaluationException e) { if (!e.IsHandled) { throw new RuleLhsExpressionEvaluationException("Failed to evaluate aggregate expression", e.Expression.ToString(), e.InnerException); } ResetAggregator(aggregation, tuple, aggregator); } }
public void RaiseConditionFailed(ISession session, Exception exception, Expression expression, Tuple tuple, Fact fact) { var hanlder = ConditionFailedEvent; if (hanlder != null) { var @event = new ConditionErrorEventArgs(exception, expression, tuple, fact); hanlder(session, @event); } if (_parent != null) { _parent.RaiseConditionFailed(session, exception, expression, tuple, fact); } }