KleveR_

Untitled

Jan 25th, 2021
124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import akka.actor.{ActorSystem, Actor, ActorRef, Props}
  2. import scala.io.StdIn
  3.  
  4. case class Init(n: Int, actors: List[ActorRef] = List())
  5. case class Zlecenie(tekst: List[String])
  6. case class Wykonaj(slowo: String)
  7. case class Wynik(nadzorca: ActorRef)
  8. case class OstatecznyWynik(n: Int)
  9. case object Praca
  10.  
  11. class Nadzorca extends Actor {
  12.   def receive: Receive = {
  13.     case Init(n, list) =>
  14.       val pracownik = context.actorOf(Props[Pracownik], s"pracownik$n")
  15.       pracownik ! Praca
  16.       if (n != 0) {
  17.         self ! Init(n-1, List(pracownik) ++ list)
  18.       }
  19.       else
  20.         context.become(praca(list))
  21.   }
  22.   def praca(pracownicy: List[ActorRef], suma: Int = 0): Receive = {
  23.     case Zlecenie(list) =>
  24.       val slowa = list.flatMap(e => e.split(" ").filter(!_.isEmpty))
  25.       for (p <- slowa.zipWithIndex)
  26.         pracownicy(p._2%pracownicy.length) ! Wykonaj(p._1)
  27.       for {
  28.         p <- pracownicy
  29.       } p ! Wynik(self)
  30.     case OstatecznyWynik(n) =>
  31.       if (pracownicy.tail.isEmpty) {
  32.         println(suma + n)
  33.         context.become(receive)
  34.       }
  35.       else context.become(praca(pracownicy.tail, suma + n))
  36.   }
  37. }
  38.  
  39. class Pracownik extends Actor {
  40.   def receive: Receive = {
  41.     case Praca =>
  42.       context.become(praca(0))
  43.   }
  44.   def praca(n :Int) : Receive = {
  45.     case Wykonaj(slowo) =>
  46.       context.become(praca(n + 1))
  47.     case Wynik(nadzorca) =>
  48.       nadzorca ! OstatecznyWynik(n)
  49.   }
  50. }
  51.  
  52. object Zlicz extends App {
  53.   def dane(): List[String] = {
  54.     scala.io.Source.fromResource("ogniem_i_mieczem.txt").getLines.toList
  55.   }
  56.   val system = ActorSystem("WordCounter")
  57.   val leonardo = system.actorOf(Props[Nadzorca], "leonardo")
  58.   val testList = List("zdanie jeden dwa trzy cztery", "siema eniu co tam u ciebie byczku", "ale jazda normalnie tak o nie")
  59.   leonardo ! Init(5)
  60.   // leonardo ! Zlecenie(testList)
  61.   leonardo ! Zlecenie(dane())
  62.   StdIn.readLine()
  63.   system.terminate()
  64. }
  65.  
  66.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×