Exemple #1
0
        public override void PropagateAssert(IExecutionContext context, IList <Tuple> tuples)
        {
            var joinedSets  = JoinedSets(context, tuples);
            var aggregation = new Aggregation();

            foreach (var set in joinedSets)
            {
                var matchingFacts = new List <Fact>();
                foreach (var fact in set.Facts)
                {
                    if (MatchesConditions(context, set.Tuple, fact))
                    {
                        matchingFacts.Add(fact);
                    }
                }
                IAggregator aggregator = CreateAggregator(set.Tuple);
                var         results    = aggregator.Add(set.Tuple, matchingFacts);
                aggregation.Add(set.Tuple, results);
            }
            PropagateAggregation(context, aggregation);
        }
Exemple #2
0
        public override void PropagateRetract(IExecutionContext context, List <Tuple> tuples)
        {
            var aggregation = new Aggregation();

            using (var counter = PerfCounter.Retract(context, this))
            {
                foreach (var tuple in tuples)
                {
                    IFactAggregator aggregator = RemoveFactAggregator(context, tuple);
                    if (aggregator != null)
                    {
                        aggregation.Remove(tuple, aggregator.AggregateFacts);
                    }
                }

                counter.AddInputs(tuples.Count);
                counter.AddOutputs(aggregation.Count);
            }

            PropagateAggregation(context, aggregation);
        }
Exemple #3
0
        public override void PropagateRetract(IExecutionContext context, IList <Fact> facts)
        {
            var joinedSets  = JoinedSets(context, facts);
            var aggregation = new Aggregation();

            foreach (var set in joinedSets)
            {
                if (set.Facts.Count == 0)
                {
                    continue;
                }
                var matchingFacts = GetMatchingFacts(context, set);
                if (matchingFacts.Count > 0)
                {
                    IFactAggregator aggregator = GetFactAggregator(set.Tuple);
                    if (aggregator != null)
                    {
                        RetractFromAggregate(context, aggregator, aggregation, set.Tuple, set.Facts);
                    }
                }
            }
            PropagateAggregation(context, aggregation);
        }
Exemple #4
0
        private void AddAggregationResult(Aggregation aggregation, Tuple tuple, IEnumerable <AggregationResult> results)
        {
            foreach (var result in results)
            {
                switch (result.Action)
                {
                case AggregationAction.Added:
                    var addedFact = CreateAggregateFact(result.Aggregate);
                    aggregation.Add(tuple, addedFact);
                    break;

                case AggregationAction.Modified:
                    var modifiedFact = GetAggregateFact(result.Aggregate, result.Previous);
                    aggregation.Modify(tuple, modifiedFact);
                    break;

                case AggregationAction.Removed:
                    var removedFact = RemoveAggregateFact(result.Aggregate);
                    aggregation.Remove(tuple, removedFact);
                    break;
                }
            }
        }
Exemple #5
0
 private void ResetAggregator(Aggregation aggregation, Tuple tuple, IFactAggregator aggregator)
 {
     tuple.RemoveState <IFactAggregator>(this);
     aggregation.Remove(tuple, aggregator.AggregateFacts);
 }
Exemple #6
0
 private void RetractFromAggregate(IExecutionContext context, IFactAggregator aggregator, Aggregation aggregation, Tuple tuple, IList <Fact> facts)
 {
     try
     {
         aggregator.Remove(aggregation, tuple, facts);
     }
     catch (AggregateExpressionException e)
     {
         bool isHandled = false;
         context.EventAggregator.RaiseAggregateFailed(context.Session, e.InnerException,
                                                      e.Expression, e.Tuple, e.Fact, ref isHandled);
         if (!isHandled)
         {
             throw new RuleExpressionEvaluationException("Failed to evaluate aggregate expression",
                                                         e.Expression.ToString(), e.InnerException);
         }
         ResetAggregator(aggregation, tuple, aggregator);
     }
 }
Exemple #7
0
 private void PropagateAggregation(IExecutionContext context, Aggregation aggregation)
 {
     PropagateAggregateRetracts(context, aggregation);
     PropagateAggregateAsserts(context, aggregation);
     PropagateAggregateUpdates(context, aggregation);
 }
Exemple #8
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);
     }
 }
Exemple #9
0
        public void Remove(Aggregation aggregation, Tuple tuple, IEnumerable <Fact> facts)
        {
            var results = _aggregator.Remove(tuple, facts);

            AddAggregationResult(aggregation, tuple, results);
        }
Exemple #10
0
        public void Modify(AggregationContext context, Aggregation aggregation, Tuple tuple, IEnumerable <Fact> facts)
        {
            var results = _aggregator.Modify(context, tuple, facts);

            AddAggregationResult(aggregation, tuple, results);
        }
Exemple #11
0
 private void ResetAggregator(IExecutionContext context, Aggregation aggregation, Tuple tuple, IFactAggregator aggregator)
 {
     context.WorkingMemory.RemoveState <IFactAggregator>(this, tuple);
     aggregation.Remove(tuple, aggregator.AggregateFacts);
 }