-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcofreeactor.scala
129 lines (102 loc) · 3.29 KB
/
cofreeactor.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.hablapps.gist
package coalgebras
package scalazimpl
import akka.actor.{Actor, ActorLogging, ActorRef, ActorSystem, Props}
import akka.pattern.{ask, pipe}
import akka.util.Timeout
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.reflect.ClassTag
import scalaz.{Const, StateT}
import scalaz.std.scalaFuture._
import Automata.Input.{Next, IsFinal}
object ActorEntity {
trait Proof
object Proof {
implicit val instance: Proof = new Proof {}
}
case object Label
case class LabelResponse[Y](y: Y)
// ACTOR
class AutomataActor[I: ClassTag, X, Y](
machine: Automata[Future, I, X],
initialState: X,
f: X => Y) extends Actor {
import context.dispatcher
var state: Future[X] = Future.successful(initialState)
def receive = {
case Next(i: I) =>
val step = state flatMap machine.next(i).run
state = step.map(_._1)
step.map(_._2) pipeTo sender
case IsFinal() =>
val step = state flatMap machine.isFinal().run
state = step.map(_._1)
step.map(_._2) pipeTo sender
case Label =>
state map (f andThen LabelResponse.apply) pipeTo sender
}
}
object AutomataActor {
def props[I: ClassTag, X, Y](
machine: Automata[Future, I, X],
initialState: X,
f: X => Y) = Props(new AutomataActor(machine, initialState, f))
}
// COFREE COALGEBRA FOR ENTITIES
type CofreeActor[Y] = Const[ActorRef,Y]
def cofree[I: ClassTag](
as: ActorSystem)(implicit
timeout: Timeout) =
new CofreeCoalgebra2[CofreeActor, Automata[Future, I, ?]] {
import as.dispatcher
type YCat[Y] = Proof
def label[Y: YCat](cx: Const[ActorRef, Y]): Y =
Await.result((cx.getConst ? Label).mapTo[LabelResponse[Y]].map(_.y), 5 seconds)
def machine[Y]: Automata[Future, I, Const[ActorRef, Y]] =
new Automata[Future, I, Const[ActorRef, Y]] {
def isFinal(): StateT[Future, Const[ActorRef, Y], Boolean] =
StateT { case c@Const(actor) =>
(actor ? IsFinal())
.mapTo[Boolean]
.map((c, _))
}
def next(i: I): StateT[Future, Const[ActorRef, Y], Unit] =
StateT { case c@Const(actor) =>
(actor ? Next(i))
.mapTo[Unit]
.map((c, _))
}
}
def trace[X: Automata[Future, I, ?], Y: YCat](
f: X => Y): X => Const[ActorRef, Y] =
x => Const[ActorRef, Y] {
as.actorOf(AutomataActor.props[I, X, Y](implicitly[Automata[Future, I, X]], x, f))
}
}
}
object ActorEntityTest extends App {
import ActorEntity._
import AnAutomata._
implicit val system = ActorSystem("cofree-actor")
import system.dispatcher
implicit val timeout = Timeout(5 seconds)
val CofreeAutomata = cofree[Boolean](system)
val Const(even) = CofreeAutomata.trace[Int, String](_.toString)(Even[Future], implicitly)(0)
system.actorOf(Props(new Actor with ActorLogging {
even ! IsFinal()
even ! IsFinal()
even ! IsFinal()
even ! Next(true)
even ! IsFinal()
even ! IsFinal()
even ! IsFinal()
even ! Next(false)
even ! IsFinal()
even ! IsFinal()
even ! IsFinal()
def receive = {
case x => log.info(s"Received: $x")
}
}))
}