Exemple #1
0
        private void BuildJoinNode(ReteBuilderContext context, IEnumerable <ConditionElement> conditions = null)
        {
            var betaConditions = new List <BetaCondition>();

            if (conditions != null)
            {
                foreach (var condition in conditions)
                {
                    var factIndexMap  = FactIndexMap.CreateMap(condition.References, context.Declarations);
                    var betaCondition = new BetaCondition(condition.Expression, factIndexMap);
                    betaConditions.Add(betaCondition);
                }
            }

            var node = context.BetaSource
                       .Sinks.OfType <JoinNode>()
                       .FirstOrDefault(x =>
                                       x.RightSource == context.AlphaSource &&
                                       x.LeftSource == context.BetaSource &&
                                       ConditionComparer.AreEqual(x.Conditions, betaConditions));

            if (node == null)
            {
                node = new JoinNode(context.BetaSource, context.AlphaSource);
                foreach (var betaCondition in betaConditions)
                {
                    node.Conditions.Add(betaCondition);
                }
            }
            BuildBetaMemoryNode(context, node);
            context.ResetAlphaSource();
        }
Exemple #2
0
        private TerminalNode BuildTerminalNode(ReteBuilderContext context, IEnumerable <Declaration> ruleDeclarations)
        {
            if (context.AlphaSource != null)
            {
                BuildJoinNode(context);
            }
            var factIndexMap = FactIndexMap.CreateMap(ruleDeclarations, context.Declarations);
            var terminalNode = new TerminalNode(context.BetaSource, factIndexMap);

            return(terminalNode);
        }
Exemple #3
0
        public bool IsSatisfiedBy(IExecutionContext context, Tuple leftTuple, Fact rightFact)
        {
            var args  = new object[_compiledExpression.ParameterCount];
            int index = leftTuple.Count - 1;

            foreach (var fact in leftTuple.Facts)
            {
                FactIndexMap.SetElementAt(ref args, _conditionFactMap.Map(index), 0, fact.Object);
                index--;
            }
            FactIndexMap.SetElementAt(ref args, _conditionFactMap.Map(leftTuple.Count), 0, rightFact.Object);

            try
            {
                return(_compiledExpression.Delegate(args));
            }
            catch (Exception e)
            {
                context.EventAggregator.RaiseConditionFailed(context.Session, e, _expression, leftTuple, rightFact);
                throw new RuleConditionEvaluationException("Failed to evaluate condition", _expression.ToString(), e);
            }
        }
Exemple #4
0
 public BetaCondition(LambdaExpression expression, FactIndexMap conditionFactMap)
 {
     _expression         = expression;
     _conditionFactMap   = conditionFactMap;
     _compiledExpression = FastDelegate.Create <Func <object[], bool> >(expression);
 }
Exemple #5
0
 public Activation(ICompiledRule rule, Tuple tuple, FactIndexMap tupleFactMap)
 {
     _rule         = rule;
     _tuple        = tuple;
     _tupleFactMap = tupleFactMap;
 }