Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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);
            }
        }
Esempio n. 4
0
        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);
            }
        }
Esempio n. 5
0
        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);
            }
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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);
            }
        }
Esempio n. 8
0
        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);
        }