private Func <string, Action <Rule> > FindOrCreateRuleList(IDictionary <string, RuleList> dict, bool immediate, bool clearFirst) { return((s) => { PNode node = FindKB(s); if (immediate) { if (node == null) { node = FindOrCreateKB(s); } } RuleList rl; lock (dict) { if (!dict.TryGetValue(s, out rl)) { if (node != null) { if (clearFirst && immediate) { node.Clear(); } } rl = dict[s] = new RuleList(); } } bool immedAdd = immediate && node != null; if (!immedAdd) { return (r) => rl.Add(r); } return r => { rl.Add(r); node.AddImmediate(r); }; }); }