Ejemplo n.º 1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldHandleLocalLearnMessagesWithoutInstanceIdInTheMessageHeaderWhenCatchingUp() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldHandleLocalLearnMessagesWithoutInstanceIdInTheMessageHeaderWhenCatchingUp()
        {
            // Given
            LearnerState learner = LearnerState.Learner;

            Org.Neo4j.cluster.InstanceId instanceId = new Org.Neo4j.cluster.InstanceId(42);
            long payload = 12L;

            LearnerContext context = mock(typeof(LearnerContext));

            when(context.MyId).thenReturn(instanceId);
            when(context.LastKnownLearnedInstanceInCluster).thenReturn(11L);
            when(context.LastLearnedInstanceId).thenReturn(payload);

//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @SuppressWarnings("unchecked") org.neo4j.cluster.com.message.Message<LearnerMessage> message = mock(org.neo4j.cluster.com.message.Message.class);
            Message <LearnerMessage> message = mock(typeof(Message));

            when(message.MessageType).thenReturn(LearnerMessage.CatchUp);
            when(message.HasHeader(Message.HEADER_INSTANCE_ID)).thenReturn(false);
            when(message.GetHeader(Message.HEADER_INSTANCE_ID)).thenThrow(new System.ArgumentException());
            when(message.Payload).thenReturn(payload);

            // When
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: org.neo4j.cluster.statemachine.State<?,?> state = learner.handle(context, message, mock(org.neo4j.cluster.com.message.MessageHolder.class));
            State <object, ?> state = learner.handle(context, message, mock(typeof(MessageHolder)));

            // Then
            assertSame(state, learner);
            verify(context, times(1)).setLastKnownLearnedInstanceInCluster(payload, instanceId);
        }
Ejemplo n.º 2
0
        public Score Classify(string parameter, LearnerState learnerState)
        {
            var words = parameter.Tokenize();
            int allElementsQuantity = learnerState.CategoryPerQuantity.Select(x => x.Value).Sum();
            var keys = learnerState.CategoryPerQuantity.Keys.ToList();
            var aprioriProbability = GetAprioriProbability(keys, allElementsQuantity, learnerState);
            var prioriProbability  = GetPrioriProbability(keys, words, allElementsQuantity, learnerState);

            var res = aprioriProbability.Zip(prioriProbability, (apriori, priori) =>
            {
                if (apriori.Key == priori.Key)
                {
                    double probability = 0.0;
                    Probability aprioriProbabilityValue;
                    if (aprioriProbability.TryGetValue(apriori.Key, out aprioriProbabilityValue))
                    {
                        Probability prioriProbabilityValue;
                        if (prioriProbability.TryGetValue(apriori.Key, out prioriProbabilityValue))
                        {
                            probability = (aprioriProbabilityValue * prioriProbabilityValue).Value;
                        }
                    }
                    return(Tuple.Create(apriori.Key, new Probability(probability)));
                }
                return(Tuple.Create(apriori.Key, new Probability(-1d)));
            }).MaxBy(x => x.Item2.Value);

            return(new Score(new Sentence(parameter, res.Item1), res.Item2));
        }
Ejemplo n.º 3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldUseLastKnownOnlineClusterMemberAndSetTimeoutForCatchup() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldUseLastKnownOnlineClusterMemberAndSetTimeoutForCatchup()
        {
            // Given
            LearnerState   state    = LearnerState.Learner;
            LearnerContext ctx      = mock(typeof(LearnerContext));
            MessageHolder  outgoing = mock(typeof(MessageHolder));

            Org.Neo4j.cluster.InstanceId upToDateClusterMember = new Org.Neo4j.cluster.InstanceId(1);

            // What we know
            when(ctx.LastLearnedInstanceId).thenReturn(0L);
            when(ctx.GetPaxosInstance(new Org.Neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(1L))).thenReturn(new PaxosInstance(null, new Org.Neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId(1L)));
            when(ctx.LastKnownAliveUpToDateInstance).thenReturn(upToDateClusterMember);
            when(ctx.GetUriForId(upToDateClusterMember)).thenReturn(new URI("c:/1"));

            // What we know the cluster knows
            when(ctx.LastKnownLearnedInstanceInCluster).thenReturn(1L);

            // When
            Message <LearnerMessage> message = Message.to(LearnerMessage.CatchUp, new URI("c:/2"), 2L).setHeader(Message.HEADER_FROM, "c:/2").setHeader(Message.HEADER_INSTANCE_ID, "2");
            State newState = state.handle(ctx, message, outgoing);

            // Then

            assertThat(newState, equalTo(LearnerState.Learner));
            verify(outgoing).offer(Message.to(LearnerMessage.LearnRequest, new URI("c:/1"), new LearnerMessage.LearnRequestState()).setHeader(Org.Neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId.INSTANCE, Convert.ToString(1L)));
            verify(ctx).setTimeout("learn", Message.timeout(LearnerMessage.LearnTimedout, message));
        }
Ejemplo n.º 4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerServingOldInstanceShouldNotLogErrorIfItDoesNotHaveIt()
        {
            // Given
            LearnerState   state    = LearnerState.Learner;
            LearnerContext ctx      = mock(typeof(LearnerContext));
            MessageHolder  outgoing = mock(typeof(MessageHolder));
            // The instance will be asked for paxos instance 4...
            InstanceId paxosInstanceIdIDontHave          = new InstanceId(4);
            Message <LearnerMessage> messageRequestingId = Message.to(LearnerMessage.LearnRequest, URI.create("c:/1")).setHeader(Message.HEADER_FROM, "c:/2").setHeader(InstanceId.INSTANCE, "4");

            // ...but it does not have it yet
            when(ctx.GetPaxosInstance(paxosInstanceIdIDontHave)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIDontHave));

            // When
            state.handle(ctx, messageRequestingId, outgoing);

            // Then
            // verify there is no logging of the failure
            verify(ctx, never()).notifyLearnMiss(paxosInstanceIdIDontHave);
            // but the learn failed went out anyway
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnFailed).to(URI.create("c:/2"))));
        }
Ejemplo n.º 5
0
        private void OnAccepted(Accepted accepted)
        {
            LearnerState state = learnerState.GetOrAdd(accepted.ProposalNumber, new LearnerState
            {
                BallotNumber    = accepted.BallotNumber,
                NumberOfAccepts = 1,
                ProposalNumber  = accepted.ProposalNumber
            });

            if (state.BallotNumber < accepted.BallotNumber)
            {
                return;
            }
            if (state.Accepted)              // duplicate
            {
                return;
            }

            state.NumberOfAccepts += 1;
            if (state.NumberOfAccepts < acceptors.Length / 2)
            {
                return;
            }
            OnAcceptedValue(accepted.Value);
        }
Ejemplo n.º 6
0
 public LearnerState Learn(LearnerState oldState, Sentence source)
 {
     var result = source.Text.Tokenize().Aggregate(oldState, (acc, x) =>
     {
         var newAcc = acc.IncrementFeature(source.Category, x);
         return newAcc.IncrementCategory(source.Category);
     });        
     return result;
 }
Ejemplo n.º 7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerShouldAskAllAliveInstancesAndTheseOnlyForMissingValue()
        {
            // Given

            IList <URI> allMembers = new List <URI>(3);
            URI         instance1  = URI.create("c:/1");        // this one is failed
            URI         instance2  = URI.create("c:/2");        // this one is ok and will respond
            URI         instance3  = URI.create("c:/3");        // this one is the requesting instance
            URI         instance4  = URI.create("c:/4");        // and this one is ok and will respond too

            allMembers.Add(instance1);
            allMembers.Add(instance2);
            allMembers.Add(instance3);
            allMembers.Add(instance4);

            ISet <Org.Neo4j.cluster.InstanceId> aliveInstanceIds = new HashSet <Org.Neo4j.cluster.InstanceId>();

            Org.Neo4j.cluster.InstanceId id2 = new Org.Neo4j.cluster.InstanceId(2);
            Org.Neo4j.cluster.InstanceId id4 = new Org.Neo4j.cluster.InstanceId(4);
            aliveInstanceIds.Add(id2);
            aliveInstanceIds.Add(id4);

            LearnerState   state    = LearnerState.Learner;
            LearnerContext ctx      = mock(typeof(LearnerContext));
            MessageHolder  outgoing = mock(typeof(MessageHolder));
            InstanceId     paxosInstanceIdIAskedFor = new InstanceId(4);

            when(ctx.LastDeliveredInstanceId).thenReturn(3L);
            when(ctx.LastKnownLearnedInstanceInCluster).thenReturn(5L);
            when(ctx.MemberURIs).thenReturn(allMembers);
            when(ctx.Alive).thenReturn(aliveInstanceIds);
            when(ctx.GetUriForId(id2)).thenReturn(instance2);
            when(ctx.GetUriForId(id4)).thenReturn(instance4);
            when(ctx.GetPaxosInstance(paxosInstanceIdIAskedFor)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIAskedFor));

            Message <LearnerMessage> theCause = Message.to(LearnerMessage.CatchUp, instance2);                // could be anything, really

            // When
            state.handle(ctx, Message.timeout(LearnerMessage.LearnTimedout, theCause), outgoing);

            // Then
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnRequest).to(instance2)));
//JAVA TO C# CONVERTER WARNING: Java wildcard generics have no direct equivalent in .NET:
//ORIGINAL LINE: verify(outgoing, times(1)).offer(org.mockito.ArgumentMatchers.argThat<org.neo4j.cluster.com.message.Message<? extends org.neo4j.cluster.com.message.MessageType>>(new org.neo4j.cluster.protocol.MessageArgumentMatcher()
            verify(outgoing, times(1)).offer(ArgumentMatchers.argThat <Message <MessageType> >(new MessageArgumentMatcher()
                                                                                               .onMessageType(LearnerMessage.LearnRequest).to(instance4)));
            verifyNoMoreInteractions(outgoing);
        }
Ejemplo n.º 8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void learnerReceivingLearnFailedShouldLogIt() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void LearnerReceivingLearnFailedShouldLogIt()
        {
            // Given
            LearnerState             state    = LearnerState.Learner;
            LearnerContext           ctx      = mock(typeof(LearnerContext));
            MessageHolder            outgoing = mock(typeof(MessageHolder));
            InstanceId               paxosInstanceIdIAskedFor = new InstanceId(4);
            Message <LearnerMessage> theLearnFailure          = Message.to(LearnerMessage.LearnFailed, URI.create("c:/1")).setHeader(Message.HEADER_FROM, "c:/2").setHeader(InstanceId.INSTANCE, "4");

            when(ctx.GetPaxosInstance(paxosInstanceIdIAskedFor)).thenReturn(new PaxosInstance(mock(typeof(PaxosInstanceStore)), paxosInstanceIdIAskedFor));
            when(ctx.MemberURIs).thenReturn(Collections.singletonList(URI.create("c:/2")));

            // When
            state.handle(ctx, theLearnFailure, outgoing);

            // Then
            // verify that the failure was logged
            verify(ctx, times(1)).notifyLearnMiss(paxosInstanceIdIAskedFor);
        }
Ejemplo n.º 9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void shouldNotCloseTheGapIfItsTheCoordinatorAndTheGapIsSmallerThanTheThreshold() throws Throwable
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#:
        public virtual void ShouldNotCloseTheGapIfItsTheCoordinatorAndTheGapIsSmallerThanTheThreshold()
        {
            // Given
            // A coordinator that knows that the last Paxos instance delivered is 3
            long         lastDelivered = 3L;
            LearnerState learner       = LearnerState.Learner;

            Org.Neo4j.cluster.InstanceId memberId = new Org.Neo4j.cluster.InstanceId(42);

            LearnerContext context = mock(typeof(LearnerContext));

            when(context.IsMe(any())).thenReturn(true);
            when(context.Coordinator).thenReturn(memberId);                   // so it's the coordinator
            when(context.LastDeliveredInstanceId).thenReturn(lastDelivered);
            // and has this list of pending instances (up to id 14)
            IList <PaxosInstance> pendingInstances = new LinkedList <PaxosInstance>();

            for (int i = 1; i < 12; i++)                 // start at 1 because instance 3 is already delivered
            {
                InstanceId    instanceId = new InstanceId(lastDelivered + i);
                PaxosInstance value      = new PaxosInstance(mock(typeof(PaxosInstanceStore)), instanceId);
                value.Closed("", "");
                when(context.GetPaxosInstance(instanceId)).thenReturn(value);
                pendingInstances.Add(value);
            }
            when(context.GetLog(any())).thenReturn(mock(typeof(Log)));

            Message <LearnerMessage> incomingInstance = Message.to(LearnerMessage.Learn, URI.create("c:/1"), new LearnerMessage.LearnState(new object())).setHeader(Message.HEADER_FROM, "c:/2").setHeader(Message.HEADER_CONVERSATION_ID, "conversation-id").setHeader(InstanceId.INSTANCE, "" + (lastDelivered + LearnerContext_Fields.LEARN_GAP_THRESHOLD));

            // When
            // it receives a message with Paxos instance id at the threshold
            learner.handle(context, incomingInstance, mock(typeof(MessageHolder)));

            // Then
            // it waits and doesn't deliver anything
            foreach (PaxosInstance pendingInstance in pendingInstances)
            {
                assertFalse(pendingInstance.IsState(PaxosInstance.State.Delivered));
            }
            verify(context, times(0)).LastDeliveredInstanceId = anyLong();
        }
Ejemplo n.º 10
0
 public TweetTrainer(ITweetLearner learner, LearnerState oldState)
 {
     _learner     = learner;
     CurrentState = oldState;
 }
Ejemplo n.º 11
0
 public ImmutableDictionary <WordCategory, Probability> GetAprioriProbability(IEnumerable <WordCategory> categories, int allElementsQuantity, LearnerState learnerState)
 {
     return(categories.Select(key =>
     {
         int count = learnerState.CategoryPerQuantity.GetValueOrDefault(key);
         return new KeyValuePair <WordCategory, Probability>(key, new Probability((double)count / allElementsQuantity));
     }).ToImmutableDictionary());
 }
Ejemplo n.º 12
0
 public IEnumerable <KeyValuePair <WordCategory, Probability> > GetPrioriProbabilityForSingleWord(IEnumerable <WordCategory> categories, string word, int quantity, LearnerState learnerState)
 {
     return(categories.Select(category =>
     {
         ImmutableDictionary <string, int> wordPerQuantity;
         if (learnerState.CategoryPerWords.TryGetValue(category, out wordPerQuantity))
         {
             int count = wordPerQuantity.GetValueOrDefault(word);
             return new KeyValuePair <WordCategory, Probability>(category, new Probability((double)count / quantity));
         }
         return new KeyValuePair <WordCategory, Probability>(category, 0.0d);
     }));
 }
Ejemplo n.º 13
0
        public ImmutableDictionary <WordCategory, Probability> GetPrioriProbability(IEnumerable <WordCategory> categories, IEnumerable <string> words, int quantity, LearnerState learnerState)
        {
            var prob = words.SelectMany(word => GetPrioriProbabilityForSingleWord(categories, word, quantity, learnerState)).Where(x => !(Math.Abs(x.Value) < Epsilon)).ToList();

            return(categories.Select(category =>
            {
                var res = prob.Where(x => x.Key == category)
                          .Aggregate(new Probability(1.0d), (acc, x) => x.Value * acc);
                return new KeyValuePair <WordCategory, Probability>(category, new Probability(Math.Abs(res.Value - 1.0d) < Epsilon ? 0.0d : res.Value));
            }).ToImmutableDictionary());
        }