public void UnsubscribeFromVote <T, C>(string queryName, QueryDelegate <Vote <T>, C> answer) { QueryKey key = new QueryKey(queryName, typeof(T)); if (!voteListeners.ContainsKey(key)) { voteListeners.Add(key, new Dictionary <int, VoteQueryDelegateWrapper>()); } int answerHash = answer.GetHashCode(); if (voteListeners[key].ContainsKey(answerHash)) { voteListeners[key].Remove(answerHash); } }
public void UnsubscribeFromBool <C>(string queryName, QueryDelegate <BoolResult, C> answer) { QueryKey key = new QueryKey(queryName, typeof(BoolResult)); if (!boolListeners.ContainsKey(key)) { boolListeners.Add(key, new Dictionary <int, QueryDelegateWrapper>()); } int answerHash = answer.GetHashCode(); if (boolListeners[key].ContainsKey(answerHash)) { boolListeners[key].Remove(answerHash); } }
public void UnsubscribeFromSum <T, C>(string queryName, QueryDelegate <T, C> answer) where T : struct { QueryKey key = new QueryKey(queryName, typeof(T)); if (!numberListeners.ContainsKey(key)) { numberListeners.Add(key, new Dictionary <int, QueryDelegateWrapper>()); } int answerHash = answer.GetHashCode(); if (numberListeners[key].ContainsKey(answerHash)) { numberListeners[key].Remove(answerHash); } }
public void SubscribeToBool <C>(string queryName, QueryDelegate <BoolResult, C> answer) { QueryKey key = new QueryKey(queryName, typeof(BoolResult)); if (!boolListeners.ContainsKey(key)) { boolListeners.Add(key, new Dictionary <int, QueryDelegateWrapper>()); } int answerHash = answer.GetHashCode(); if (!boolListeners[key].ContainsKey(answerHash)) { QueryDelegateWrapper wrappedAnswer = (object context) => { return(answer((C)context)); }; boolListeners[key].Add(answerHash, wrappedAnswer); } }
public void SubscribeToSum <T, C>(string queryName, QueryDelegate <T, C> answer) where T : struct { QueryKey key = new QueryKey(queryName, typeof(T)); if (!numberListeners.ContainsKey(key)) { numberListeners.Add(key, new Dictionary <int, QueryDelegateWrapper>()); } int answerHash = answer.GetHashCode(); if (!numberListeners[key].ContainsKey(answerHash)) { QueryDelegateWrapper wrappedAnswer = (object context) => { return(answer((C)context)); }; numberListeners[key].Add(answerHash, wrappedAnswer); } }
public T Vote <T>(string queryName, object context) { QueryKey key = new QueryKey(queryName, typeof(T)); if (!voteListeners.ContainsKey(key)) { return(default(T)); } Dictionary <int, VoteQueryDelegateWrapper> subscribers = voteListeners[key]; Dictionary <T, int> votes = new Dictionary <T, int>(); foreach (VoteQueryDelegateWrapper sub in subscribers.Values) { Vote <object> vote = sub(context); if (vote.isApplicable) { T candidate = (T)vote.candidate; if (!votes.ContainsKey(candidate)) { votes.Add(candidate, 0); } votes[candidate] += vote.weight; } } int highestWeight = 0; T bestCandidate = default(T); foreach (T candidate in votes.Keys) { if (votes[candidate] > highestWeight) { bestCandidate = candidate; highestWeight = votes[candidate]; } } return(bestCandidate); }
public void SubscribeToVote <T, C>(string queryName, QueryDelegate <Vote <T>, C> answer) { QueryKey key = new QueryKey(queryName, typeof(T)); if (!voteListeners.ContainsKey(key)) { voteListeners.Add(key, new Dictionary <int, VoteQueryDelegateWrapper>()); } int answerHash = answer.GetHashCode(); if (!voteListeners[key].ContainsKey(answerHash)) { VoteQueryDelegateWrapper wrappedAnswer = (object context) => { Vote <T> answerVote = answer((C)context); return(new Vote <object>(answerVote.isApplicable, answerVote.candidate, answerVote.weight)); }; voteListeners[key].Add(answerHash, wrappedAnswer); } }
public bool Or(string queryName, object context) { QueryKey key = new QueryKey(queryName, typeof(BoolResult)); if (!boolListeners.ContainsKey(key)) { return(false); } Dictionary <int, QueryDelegateWrapper> subscribers = boolListeners[key]; foreach (QueryDelegateWrapper sub in subscribers.Values) { BoolResult answer = (BoolResult)sub(context); if (answer.isApplicable && answer.result) { return(true); } } return(false); }