/// <summary> /// Evaluates the tokens bound to this AggregatorNode and generates the list of InferredFacts. /// </summary> internal void Evaluate() { int cntOfEarlierConditions = lhs.Count - 1; VariableSubstituter vs = null; for (int i = cntOfEarlierConditions; i >= 0; i--) { Condition earlier_cond = lhs[i]; if (earlier_cond.ConditionType == ConditionType.Positive) { for (int f2 = 0; f2 < 3; f2++) { Variable o = earlier_cond.Fields[f2] as Variable; if (o != null && o.Equals(_groupBy)) { vs = new VariableSubstituter(); vs.FieldNumber = f2; vs.NumberOfLevelsUp = (cntOfEarlierConditions - i); vs.BindingPair.Variable = o; f2 = 3; i = -1; //escape loop of cntOfEarlierConditions } } } } if (vs == null) { throw new ApplicationException("Bad rule"); } Dictionary <Term, List <WME> > sorter = new Dictionary <Term, List <WME> >(); foreach (Token token in _aggregatorNode.Items) { Token ptoken = token.GetTokenUp(vs.NumberOfLevelsUp); Term key = ptoken.WME.Fields[vs.FieldNumber]; if (sorter.ContainsKey(key)) { List <WME> list = sorter[key]; if (list.Contains(token.WME) == false) { list.Add(token.WME); } } else { List <WME> list = new List <WME>(); list.Add(token.WME); sorter.Add(key, list); } } foreach (KeyValuePair <Term, List <WME> > pair in sorter) { Term val = _aggregator.Evaluate(pair.Value); _inferredFacts.Add(new Activation(new WME(pair.Key, _aggregator.Alias, val), _conditionType)); } }