示例#1
0
 internal Activation(ICompiledRule compiledRule, Tuple tuple, IndexMap tupleFactMap)
 {
     CompiledRule  = compiledRule;
     Tuple         = tuple;
     TupleFactMap  = tupleFactMap;
     _matchedFacts = new Lazy <FactMatch[]>(CreateMatchedFacts);
 }
示例#2
0
 public override int GetHashCode()
 {
     unchecked
     {
         return((CompiledRule.GetHashCode() * 397) ^ Tuple.GetHashCode());
     }
 }
示例#3
0
 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;
 }
示例#5
0
        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);
                }
            }
        }
示例#6
0
        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);
示例#7
0
        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);
        }
示例#8
0
        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);
        }
示例#9
0
        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");
        }
示例#10
0
        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);
                }
            }
        }
示例#11
0
        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);
                }
            }
        }
示例#12
0
        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);
        }
示例#13
0
        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);
        }
示例#14
0
 internal BindingErrorEventArgs(Exception exception, Expression expression, Tuple tuple) : base(exception)
 {
     _tuple     = tuple;
     Expression = expression;
 }
示例#15
0
 internal Activation(ICompiledRule compiledRule, Tuple tuple, IndexMap factMap)
 {
     CompiledRule = compiledRule;
     Tuple        = tuple;
     FactMap      = factMap;
 }
示例#16
0
        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);
        }
示例#17
0
 private void ResetAggregator(Aggregation aggregation, Tuple tuple, IFactAggregator aggregator)
 {
     tuple.RemoveState <IFactAggregator>(this);
     aggregation.Remove(tuple, aggregator.AggregateFacts);
 }
示例#18
0
        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);
            }
        }
示例#19
0
        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;
            }
        }
示例#20
0
 internal Activation(ICompiledRule compiledRule, Tuple tuple)
 {
     CompiledRule = compiledRule;
     Tuple        = tuple;
 }
示例#21
0
        private IFactAggregator RemoveFactAggregator(Tuple tuple)
        {
            var factAggregator = tuple.RemoveState <IFactAggregator>(this);

            return(factAggregator);
        }
示例#22
0
        private IFactAggregator GetFactAggregator(Tuple tuple)
        {
            var factAggregator = tuple.GetState <IFactAggregator>(this);

            return(factAggregator);
        }
示例#23
0
 public TupleStateKey(INode node, Tuple tuple)
 {
     _node  = node;
     _tuple = tuple;
 }
示例#24
0
        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);
        }
示例#25
0
 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);
     }
 }
示例#26
0
 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);
     }
 }
示例#27
0
 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);
     }
 }