예제 #1
0
        public CountAggregator(IWriteStuff writer)
        {
            _wordCounts = new Dictionary <String, Int32>();
            _writer     = writer;

            Receive <CountWord>(msg =>
            {
                if (_wordCounts.ContainsKey(msg.Word))
                {
                    _wordCounts[msg.Word] += 1;
                }
                else
                {
                    _wordCounts.Add(msg.Word, 1);
                }
            });

            Receive <CountCompleted>(msg =>
            {
                //all words are there...display the top 25 in order
                var topWords = _wordCounts.OrderByDescending(w => w.Value)
                               .Take(25);
                foreach (var word in topWords)
                {
                    _writer.WriteLine($"{word.Key} == {word.Value} times");
                }
            });
        }
        private void SetupBehaviors(IWriteStuff writer)
        {
            Receive <StartCount>(msg =>
            {
                var lineNumber = 0;

                var fileInfo = new FileInfo(msg.FileName);
                // TODO
                // (1) Create a child reader actor (LineReaderActor) using the current Context.
                // To instantiate an actor you should use a "Props".
                // for true parallelism, instantiate the actor using a Pool that load-balance
                // the work across the actor-children.

                // (2) Use the previous FileInfo "fileInfo" to read the text content,
                // and then push each line of the text to the reader actor created in (1).
                // use the message types "ReadLineForCounting" and then complete when all the
                // lines have been sent "Completed".

                // Note: the message "ReadLineForCounting" take a line-number, so the
                //       variable "var lineNumber = 0;" could be helpful here.

                // Note: if you have instantiated the LineReaderActor actor using a pool, the
                //       "Complete" message should be sent to all the children (Broadcast)

                // CODE HERE
            });

            Receive <MappedList>(msg =>
            {
                foreach (var key in msg.LineWordCount.Keys)
                {
                    if (_wordCount.ContainsKey(key))
                    {
                        _wordCount[key] += msg.LineWordCount[key];
                    }
                    else
                    {
                        _wordCount.Add(key, msg.LineWordCount[key]);
                    }
                }
            });

            Receive <Complete>(msg =>
            {
                _completeRoutees++;

                if (_completeRoutees == _numberOfRoutees)
                {
                    var topWords = _wordCount.OrderByDescending(w => w.Value).Take(25);
                    foreach (var word in topWords)
                    {
                        _writer.WriteLine($"{word.Key} == {word.Value} times");
                    }

                    _stopwatch.Stop();
                    writer.WriteLine($"Elapsed time: {_stopwatch.ElapsedMilliseconds}");
                }
            });
        }
예제 #3
0
        private void SetupBehaviors(IWriteStuff writer)
        {
            Receive <StartCount>(msg =>
            {
                var fileInfo   = new FileInfo(msg.FileName);
                var lineNumber = 0;

                var lineReader = Context.ActorOf(new RoundRobinPool(_numberOfRoutees)
                                                 .Props(LineReaderActor.Create(writer)));



                using (var reader = fileInfo.OpenText())
                {
                    while (!reader.EndOfStream)
                    {
                        lineNumber++;

                        var line = reader.ReadLine();
                        lineReader.Tell(new ReadLineForCounting(lineNumber, line));
                    }
                }

                lineReader.Tell(new Broadcast(new Complete()));
            });

            Receive <MappedList>(msg =>
            {
                foreach (var key in msg.LineWordCount.Keys)
                {
                    if (_wordCount.ContainsKey(key))
                    {
                        _wordCount[key] += msg.LineWordCount[key];
                    }
                    else
                    {
                        _wordCount.Add(key, msg.LineWordCount[key]);
                    }
                }
            });

            Receive <Complete>(msg =>
            {
                _completeRoutees++;

                if (_completeRoutees == _numberOfRoutees)
                {
                    var topWords = _wordCount.OrderByDescending(w => w.Value).Take(25);
                    foreach (var word in topWords)
                    {
                        _writer.WriteLine($"{word.Key} == {word.Value} times");
                    }

                    _stopwatch.Stop();
                    writer.WriteLine($"Elapsed time: {_stopwatch.ElapsedMilliseconds}");
                }
            });
        }
예제 #4
0
        private static String PrintInstructionsAndGetFile(IReadStuff reader, IWriteStuff writer)
        {
            writer.WriteLine("Word counter.  Select the document to count:");
            writer.WriteLine(" (1) Magna Carta");
            writer.WriteLine(" (2) Declaration of Independence");
            var choice = reader.ReadLine();
            String file = AppDomain.CurrentDomain.BaseDirectory + @"\Files\";

            if (choice.Equals("1"))
            {
                file += @"MagnaCarta.txt";
            }
            else if (choice.Equals("2"))
            {
                file += @"DeclarationOfIndependence.txt";
            }
            else
            {
                writer.WriteLine("Invalid -- bye!");
                return null;
            }

            return file;
        }
예제 #5
0
        private static String PrintInstructionsAndGetFile(IReadStuff reader, IWriteStuff writer)
        {
            writer.WriteLine("Word counter.  Select the document to count:");
            writer.WriteLine(" (1) Magna Carta");
            writer.WriteLine(" (2) Declaration of Independence");
            var    choice = reader.ReadLine();
            String file   = AppDomain.CurrentDomain.BaseDirectory + @"\Files\";

            if (choice.Equals("1"))
            {
                file += @"MagnaCarta.txt";
            }
            else if (choice.Equals("2"))
            {
                file += @"DeclarationOfIndependence.txt";
            }
            else
            {
                writer.WriteLine("Invalid -- bye!");
                return(null);
            }

            return(file);
        }
예제 #6
0
 private void DetermineIfCountsShouldBeDisplayed()
 {
     if (_totalWords > 0)
     {
         if (_wordCounts.Keys.Count == _totalWords)
         {
             //all words are there...display the top 25 in order
             var topWords = _wordCounts.OrderByDescending(w => w.Value)
                            .Take(25);
             foreach (var word in topWords)
             {
                 _writer.WriteLine($"{word.Key} == {word.Value} times");
             }
         }
     }
 }
        private void SetupBehaviors(IWriteStuff writer)
        {
            Receive<StartCount>(msg =>
            {
                var fileInfo = new FileInfo(msg.FileName);
                var lineNumber = 0;

                var lineReader = Context.ActorOf(new RoundRobinPool(_numberOfRoutees)
                                    .Props(LineReaderActor.Create(writer)));

                using (var reader = fileInfo.OpenText())
                {
                    while (!reader.EndOfStream)
                    {
                        lineNumber++;

                        var line = reader.ReadLine();
                        lineReader.Tell(new ReadLineForCounting(lineNumber, line));
                    }
                }

                lineReader.Tell(new Broadcast(new Complete()));
            });

            Receive<MappedList>(msg =>
            {
                foreach (var key in msg.LineWordCount.Keys)
                {
                    if (_wordCount.ContainsKey(key))
                    {
                        _wordCount[key] += msg.LineWordCount[key];
                    }
                    else
                    {
                        _wordCount.Add(key, msg.LineWordCount[key]);
                    }
                }
            });

            Receive<Complete>(msg =>
            {
                _completeRoutees++;

                if (_completeRoutees == _numberOfRoutees)
                {
                    var topWords = _wordCount.OrderByDescending(w => w.Value).Take(25);
                    foreach (var word in topWords)
                    {
                        _writer.WriteLine($"{word.Key} == {word.Value} times");
                    }

                    _stopwatch.Stop();
                    writer.WriteLine($"Elapsed time: {_stopwatch.ElapsedMilliseconds}");
                }
            });
        }