コード例 #1
0
        public LineReaderActor(IWriteStuff writer)
        {
            _writer         = writer;
            _wordAggregator = Context.ActorOf(WordCountAggregatorActor.Create(writer), "aggregator");

            Receive <ReadLineForCounting>(msg =>
            {
                var cleanFileContents = Regex.Replace(msg.Line, @"[^\u0000-\u007F]", " ");

                var wordArray = cleanFileContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in wordArray)
                {
                    var wordCounter = Context.Child(word);
                    if (wordCounter.IsNobody())
                    {
                        wordCounter = Context.ActorOf(WordCounterActor.Create(_writer, _wordAggregator, word), word);
                    }

                    wordCounter.Tell(new CountWord());
                }
            });

            Receive <Complete>(msg =>
            {
                var childCount = -1;    //TODO: doesn't work with 0
                var children   = Context.GetChildren();

                foreach (var child in children)
                {
                    child.Tell(new DisplayWordCount());
                    childCount++;
                }
                _wordAggregator.Tell(new TotalWordCount(childCount));
            });
        }
コード例 #2
0
        public LineReaderActor(IWriteStuff writer)
        {
            _writer = writer;
            _wordAggregator = Context.ActorOf(WordCountAggregatorActor.Create(writer), "aggregator");

            Receive<ReadLineForCounting>(msg =>
            {
                var cleanFileContents = Regex.Replace(msg.Line, @"[^\u0000-\u007F]", " ");

                var wordArray = cleanFileContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in wordArray)
                {
                    var wordCounter = Context.Child(word);
                    if (wordCounter == ActorRefs.Nobody)
                    {
                        wordCounter = Context.ActorOf(WordCounterActor.Create(_writer, _wordAggregator, word), word);
                    }

                    wordCounter.Tell(new CountWord());
                }
            });

            Receive<Complete>(msg =>
            {
                var childCount = -1;    //TODO: doesn't work with 0
                var children = Context.GetChildren();

                foreach (var child in children)
                {
                    child.Tell(new DisplayWordCount());
                    childCount++;
                }
                _wordAggregator.Tell(new TotalWordCount(childCount));
            });
        }
コード例 #3
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");
                }
            });
        }
コード例 #4
0
        public CountSupervisor(IWriteStuff writer)
        {
            _writer          = writer;
            _numberOfRoutees = 5;
            _completeReaders = 0;

            var _aggregator = Context.ActorOf(CountAggregator.Create(_writer), "aggregator");

            Receive <StartCount>(msg =>
            {
                var fileInfo   = new FileInfo(msg.FileName);
                var lineReader = Context.ActorOf(new RoundRobinPool(_numberOfRoutees).Props(LineReaderActor.Create(writer, _aggregator)));

                using (var reader = fileInfo.OpenText())
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        lineReader.Tell(new ReadLineForCounting(line));
                    }
                }

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

            Receive <CountCompleted>(msg =>
            {
                _completeReaders += 1;
                if (_completeReaders == _numberOfRoutees)
                {
                    _aggregator.Tell(msg);
                }
            });
        }
コード例 #5
0
        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}");
                }
            });
        }
コード例 #6
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}");
                }
            });
        }
コード例 #7
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}");
                }
            });
        }
コード例 #8
0
        public CountSupervisor(IWriteStuff writer, Stopwatch sw)
        {
            _writer          = writer;
            _wordCount       = new Dictionary <String, Int32>();
            _numberOfRoutees = 5;
            _completeRoutees = 0;
            _stopwatch       = sw;

            SetupBehaviors(writer);
        }
コード例 #9
0
        public CountSupervisor(IWriteStuff writer, Stopwatch sw)
        {
            _writer = writer;
            _wordCount = new Dictionary<String, Int32>();
            _numberOfRoutees = 5;
            _completeRoutees = 0;
            _stopwatch = sw;

            SetupBehaviors(writer);
        }
コード例 #10
0
        public WordCountAggregatorActor(IWriteStuff writer)
        {
            _wordCounts = new Dictionary<String, Int32>();
            _writer = writer;

            Receive<TotalWordCount>(msg =>
            {
                _totalWords = msg.TotalCount;
                DetermineIfCountsShouldBeDisplayed();
            });

            Receive<WordCount>(msg =>
            {
                _wordCounts.Add(msg.TheWord, msg.Count);
                DetermineIfCountsShouldBeDisplayed();
            });
        }
コード例 #11
0
        public WordCountAggregatorActor(IWriteStuff writer)
        {
            _wordCounts = new Dictionary <String, Int32>();
            _writer     = writer;

            Receive <TotalWordCount>(msg =>
            {
                _totalWords = msg.TotalCount;
                DetermineIfCountsShouldBeDisplayed();
            });

            Receive <WordCount>(msg =>
            {
                _wordCounts.Add(msg.TheWord, msg.Count);
                DetermineIfCountsShouldBeDisplayed();
            });
        }
コード例 #12
0
        public CountSupervisor(IWriteStuff writer)
        {
            _writer = writer;
            Receive <StartCount>(msg =>
            {
                var fileInfo   = new FileInfo(msg.FileName);
                var lineReader = Context.ActorOf(LineReaderActor.Create(writer), fileInfo.Name);

                using (var reader = fileInfo.OpenText())
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        lineReader.Tell(new ReadLineForCounting(line));
                    }
                }

                lineReader.Tell(new Complete());
            });
        }
コード例 #13
0
        public CountSupervisor(IWriteStuff writer)
        {
            _writer = writer;
            Receive<StartCount>(msg =>
            {
                var fileInfo = new FileInfo(msg.FileName);
                var lineReader = Context.ActorOf(LineReaderActor.Create(writer), fileInfo.Name);

                using (var reader = fileInfo.OpenText())
                {
                    while (!reader.EndOfStream)
                    {
                        var line = reader.ReadLine();
                        lineReader.Tell(new ReadLineForCounting(line));
                    }
                }

                lineReader.Tell(new Complete());
            });
        }
コード例 #14
0
        public LineReaderActor(IWriteStuff writer, IActorRef aggregator)
        {
            _writer     = writer;
            _aggregator = aggregator;

            Receive <ReadLineForCounting>(msg =>
            {
                var cleanFileContents = Regex.Replace(msg.Line, @"[^\u0000-\u007F]", " ");

                var wordArray = cleanFileContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var word in wordArray)
                {
                    aggregator.Tell(new CountWord(word, Self.Path.Name));
                }
            });

            Receive <CountCompleted>(msg =>
            {
                Sender.Tell(msg);
            });
        }
コード例 #15
0
        public WordCounterActor(IWriteStuff writer, IActorRef aggregator, String word)
        {
            _writer = writer;
            _theWord = word;
            _count = 0;
            _aggregator = aggregator;

            Receive<CountWord>(msg =>
            {
                _count++;
            });

            Receive<DisplayWordCount>(msg =>
            {
                //if (_count > 25)
                //{
                //    _writer.WriteLine("The word {0} appeared {1} times", _theWord, _count);
                //}

                _aggregator.Tell(new WordCount(_theWord, _count));
            });
        }
コード例 #16
0
        public WordCounterActor(IWriteStuff writer, IActorRef aggregator, String word)
        {
            _writer     = writer;
            _theWord    = word;
            _count      = 0;
            _aggregator = aggregator;

            Receive <CountWord>(msg =>
            {
                _count++;
            });

            Receive <DisplayWordCount>(msg =>
            {
                //if (_count > 25)
                //{
                //    _writer.WriteLine("The word {0} appeared {1} times", _theWord, _count);
                //}

                _aggregator.Tell(new WordCount(_theWord, _count));
            });
        }
コード例 #17
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);
        }
コード例 #18
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;
        }
コード例 #19
0
 public static Props Create(IWriteStuff writer, Stopwatch sw)
 {
     return Props.Create(() => new CountSupervisor(writer, sw));
 }
コード例 #20
0
 public static Props Create(IWriteStuff writer)
 {
     return Props.Create(() => new CountSupervisor(writer));
 }
コード例 #21
0
 public static Props Create(IWriteStuff writer, Stopwatch sw)
 {
     return(Props.Create(() => new CountSupervisor(writer, sw)));
 }
コード例 #22
0
 public static Props Create(IWriteStuff writer)
 {
     return(Props.Create(() => new CountSupervisor(writer)));
 }
コード例 #23
0
 public static Props Create(IWriteStuff writer, IActorRef aggregator, String word)
 {
     return(Props.Create(() => new WordCounterActor(writer, aggregator, word)));
 }
コード例 #24
0
 public LineReaderActor(IWriteStuff writer)
 {
     _writer = writer;
     SetupBehaviors();
 }
コード例 #25
0
 public static Props Create(IWriteStuff writer)
 {
     return(Props.Create(() => new LineReaderActor(writer)));
 }
コード例 #26
0
 public static Props Create(IWriteStuff writer)
 {
     return(Props.Create(() => new WordCountAggregatorActor(writer)));
 }
コード例 #27
0
 public static Props Create(IWriteStuff writer)
 {
     return Props.Create(() => new WordCountAggregatorActor(writer));
 }
コード例 #28
0
        public LineReaderActor(IWriteStuff writer)
        {
            _writer = writer;

            SetupBehaviors();
        }
コード例 #29
0
 public static Props Create(IWriteStuff writer, IActorRef aggregator)
 {
     return(Props.Create(() => new LineReaderActor(writer, aggregator)));
 }
コード例 #30
0
 public static Props Create(IWriteStuff writer)
 {
     return Props.Create(() => new LineReaderActor(writer));
 }
コード例 #31
0
 public static Props Create(IWriteStuff writer, IActorRef aggregator, String word)
 {
     return Props.Create(() => new WordCounterActor(writer, aggregator, word));
 }