2015-01-28 14:19:50 +01:00
|
|
|
|
/**
|
2016-01-25 10:16:14 +01:00
|
|
|
|
* Copyright (C) 2015-2016 Typesafe Inc. <http://www.typesafe.com>
|
2015-01-28 14:19:50 +01:00
|
|
|
|
*/
|
|
|
|
|
|
package akka.stream.impl
|
|
|
|
|
|
|
2015-08-01 00:13:14 +02:00
|
|
|
|
import java.util.concurrent.atomic.{ AtomicInteger, AtomicReference }
|
2015-12-14 17:02:00 +01:00
|
|
|
|
import java.{ util ⇒ ju }
|
2016-01-20 10:00:37 +02:00
|
|
|
|
import akka.NotUsed
|
2015-06-22 18:02:42 +02:00
|
|
|
|
import akka.stream.impl.MaterializerSession.MaterializationPanic
|
2015-03-30 14:22:12 +02:00
|
|
|
|
import akka.stream.impl.StreamLayout.Module
|
2015-12-14 17:02:00 +01:00
|
|
|
|
import akka.stream.impl.fusing.GraphStages.MaterializedValueSource
|
2015-04-10 16:49:49 +02:00
|
|
|
|
import akka.stream.scaladsl.Keep
|
2015-01-28 14:19:50 +01:00
|
|
|
|
import akka.stream._
|
2015-06-06 14:36:49 +02:00
|
|
|
|
import org.reactivestreams.{ Processor, Subscription, Publisher, Subscriber }
|
2015-06-22 18:02:42 +02:00
|
|
|
|
import scala.util.control.{ NoStackTrace, NonFatal }
|
2015-04-23 13:32:07 +02:00
|
|
|
|
import akka.event.Logging.simpleName
|
2015-06-16 15:34:54 +02:00
|
|
|
|
import scala.annotation.tailrec
|
|
|
|
|
|
import java.util.concurrent.atomic.AtomicLong
|
2015-12-14 17:02:00 +01:00
|
|
|
|
import scala.collection.JavaConverters._
|
|
|
|
|
|
import akka.stream.impl.fusing.GraphStageModule
|
|
|
|
|
|
import akka.stream.impl.fusing.GraphStages.MaterializedValueSource
|
|
|
|
|
|
import akka.stream.impl.fusing.GraphStages.MaterializedValueSource
|
|
|
|
|
|
import akka.stream.impl.fusing.GraphModule
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
2015-12-15 16:44:48 +01:00
|
|
|
|
object StreamLayout {
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
// compile-time constant
|
2015-05-07 15:20:25 +02:00
|
|
|
|
final val Debug = false
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
final def validate(m: Module, level: Int = 0, doPrint: Boolean = false, idMap: ju.Map[AnyRef, Integer] = new ju.HashMap): Unit = {
|
2015-07-06 22:00:21 +02:00
|
|
|
|
val ids = Iterator from 1
|
|
|
|
|
|
def id(obj: AnyRef) = idMap get obj match {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
case null ⇒
|
2015-07-06 22:00:21 +02:00
|
|
|
|
val x = ids.next()
|
2015-12-14 17:02:00 +01:00
|
|
|
|
idMap.put(obj, x)
|
2015-07-06 22:00:21 +02:00
|
|
|
|
x
|
2015-12-14 17:02:00 +01:00
|
|
|
|
case x ⇒ x
|
2015-07-06 22:00:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
def in(i: InPort) = s"${i.toString}@${id(i)}"
|
|
|
|
|
|
def out(o: OutPort) = s"${o.toString}@${id(o)}"
|
|
|
|
|
|
def ins(i: Iterable[InPort]) = i.map(in).mkString("In[", ",", "]")
|
|
|
|
|
|
def outs(o: Iterable[OutPort]) = o.map(out).mkString("Out[", ",", "]")
|
|
|
|
|
|
def pair(p: (OutPort, InPort)) = s"${in(p._2)}->${out(p._1)}"
|
|
|
|
|
|
def pairs(p: Iterable[(OutPort, InPort)]) = p.map(pair).mkString("[", ",", "]")
|
|
|
|
|
|
|
|
|
|
|
|
import m._
|
|
|
|
|
|
|
|
|
|
|
|
val inset: Set[InPort] = shape.inlets.toSet
|
|
|
|
|
|
val outset: Set[OutPort] = shape.outlets.toSet
|
|
|
|
|
|
var problems: List[String] = Nil
|
|
|
|
|
|
|
|
|
|
|
|
if (inset.size != shape.inlets.size) problems ::= "shape has duplicate inlets: " + ins(shape.inlets)
|
|
|
|
|
|
if (inset != inPorts) problems ::= s"shape has extra ${ins(inset -- inPorts)}, module has extra ${ins(inPorts -- inset)}"
|
|
|
|
|
|
if (inset.intersect(upstreams.keySet).nonEmpty) problems ::= s"found connected inlets ${inset.intersect(upstreams.keySet)}"
|
|
|
|
|
|
if (outset.size != shape.outlets.size) problems ::= "shape has duplicate outlets: " + outs(shape.outlets)
|
|
|
|
|
|
if (outset != outPorts) problems ::= s"shape has extra ${outs(outset -- outPorts)}, module has extra ${outs(outPorts -- outset)}"
|
|
|
|
|
|
if (outset.intersect(downstreams.keySet).nonEmpty) problems ::= s"found connected outlets ${outset.intersect(downstreams.keySet)}"
|
|
|
|
|
|
val ups = upstreams.toSet
|
|
|
|
|
|
val ups2 = ups.map(_.swap)
|
|
|
|
|
|
val downs = downstreams.toSet
|
|
|
|
|
|
val inter = ups2.intersect(downs)
|
|
|
|
|
|
if (downs != ups2) problems ::= s"inconsistent maps: ups ${pairs(ups2 -- inter)} downs ${pairs(downs -- inter)}"
|
|
|
|
|
|
val (allIn, dupIn, allOut, dupOut) =
|
|
|
|
|
|
subModules.foldLeft((Set.empty[InPort], Set.empty[InPort], Set.empty[OutPort], Set.empty[OutPort])) {
|
2015-08-01 00:13:14 +02:00
|
|
|
|
case ((ai, di, ao, doo), sm) ⇒
|
|
|
|
|
|
(ai ++ sm.inPorts, di ++ ai.intersect(sm.inPorts), ao ++ sm.outPorts, doo ++ ao.intersect(sm.outPorts))
|
2015-07-06 22:00:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
if (dupIn.nonEmpty) problems ::= s"duplicate ports in submodules ${ins(dupIn)}"
|
|
|
|
|
|
if (dupOut.nonEmpty) problems ::= s"duplicate ports in submodules ${outs(dupOut)}"
|
|
|
|
|
|
if (!isSealed && (inset -- allIn).nonEmpty) problems ::= s"foreign inlets ${ins(inset -- allIn)}"
|
|
|
|
|
|
if (!isSealed && (outset -- allOut).nonEmpty) problems ::= s"foreign outlets ${outs(outset -- allOut)}"
|
|
|
|
|
|
val unIn = allIn -- inset -- upstreams.keySet
|
|
|
|
|
|
if (unIn.nonEmpty && !isCopied) problems ::= s"unconnected inlets ${ins(unIn)}"
|
|
|
|
|
|
val unOut = allOut -- outset -- downstreams.keySet
|
|
|
|
|
|
if (unOut.nonEmpty && !isCopied) problems ::= s"unconnected outlets ${outs(unOut)}"
|
|
|
|
|
|
|
|
|
|
|
|
def atomics(n: MaterializedValueNode): Set[Module] =
|
|
|
|
|
|
n match {
|
|
|
|
|
|
case Ignore ⇒ Set.empty
|
|
|
|
|
|
case Transform(f, dep) ⇒ atomics(dep)
|
2015-08-01 00:13:14 +02:00
|
|
|
|
case Atomic(module) ⇒ Set(module)
|
2015-07-06 22:00:21 +02:00
|
|
|
|
case Combine(f, left, right) ⇒ atomics(left) ++ atomics(right)
|
|
|
|
|
|
}
|
|
|
|
|
|
val atomic = atomics(materializedValueComputation)
|
2015-12-14 17:02:00 +01:00
|
|
|
|
val graphValues = subModules.flatMap {
|
|
|
|
|
|
case GraphModule(_, _, _, mvids) ⇒ mvids
|
|
|
|
|
|
case _ ⇒ Nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if ((atomic -- subModules -- graphValues - m).nonEmpty)
|
|
|
|
|
|
problems ::= s"computation refers to non-existent modules [${atomic -- subModules -- graphValues - m mkString ","}]"
|
2015-07-06 22:00:21 +02:00
|
|
|
|
|
|
|
|
|
|
val print = doPrint || problems.nonEmpty
|
|
|
|
|
|
|
|
|
|
|
|
if (print) {
|
|
|
|
|
|
val indent = " " * (level * 2)
|
|
|
|
|
|
println(s"$indent${simpleName(this)}($shape): ${ins(inPorts)} ${outs(outPorts)}")
|
|
|
|
|
|
downstreams foreach { case (o, i) ⇒ println(s"$indent ${out(o)} -> ${in(i)}") }
|
|
|
|
|
|
problems foreach (p ⇒ println(s"$indent -!- $p"))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
subModules foreach (sm ⇒ validate(sm, level + 1, print, idMap))
|
|
|
|
|
|
|
|
|
|
|
|
if (problems.nonEmpty && !doPrint) throw new IllegalStateException(s"module inconsistent, found ${problems.size} problems")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
|
// TODO: Materialization order
|
|
|
|
|
|
// TODO: Special case linear composites
|
|
|
|
|
|
// TODO: Cycles
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
sealed trait MaterializedValueNode {
|
|
|
|
|
|
/*
|
|
|
|
|
|
* These nodes are used in hash maps and therefore must have efficient implementations
|
|
|
|
|
|
* of hashCode and equals. There is no value in allowing aliases to be equal, so using
|
|
|
|
|
|
* reference equality.
|
|
|
|
|
|
*/
|
|
|
|
|
|
override def hashCode: Int = super.hashCode
|
|
|
|
|
|
override def equals(other: Any): Boolean = super.equals(other)
|
|
|
|
|
|
}
|
|
|
|
|
|
case class Combine(f: (Any, Any) ⇒ Any, dep1: MaterializedValueNode, dep2: MaterializedValueNode) extends MaterializedValueNode {
|
|
|
|
|
|
override def toString: String = s"Combine($dep1,$dep2)"
|
|
|
|
|
|
}
|
|
|
|
|
|
case class Atomic(module: Module) extends MaterializedValueNode {
|
2015-12-15 16:44:48 +01:00
|
|
|
|
override def toString: String = s"Atomic(${module.attributes.nameOrDefault(module.getClass.getName)}[${module.hashCode}])"
|
2015-12-14 17:02:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
case class Transform(f: Any ⇒ Any, dep: MaterializedValueNode) extends MaterializedValueNode {
|
|
|
|
|
|
override def toString: String = s"Transform($dep)"
|
|
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
case object Ignore extends MaterializedValueNode
|
|
|
|
|
|
|
|
|
|
|
|
trait Module {
|
2015-07-06 22:00:21 +02:00
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
|
def shape: Shape
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Verify that the given Shape has the same ports and return a new module with that shape.
|
|
|
|
|
|
* Concrete implementations may throw UnsupportedOperationException where applicable.
|
2016-02-12 08:28:16 +01:00
|
|
|
|
*
|
|
|
|
|
|
* Please note that this method MUST NOT be implemented using a CopiedModule since
|
|
|
|
|
|
* the purpose of replaceShape can also be to rearrange the ports (as in BidiFlow.reversed)
|
|
|
|
|
|
* and that purpose would be defeated.
|
2015-01-28 14:19:50 +01:00
|
|
|
|
*/
|
|
|
|
|
|
def replaceShape(s: Shape): Module
|
|
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
final lazy val inPorts: Set[InPort] = shape.inlets.toSet
|
|
|
|
|
|
final lazy val outPorts: Set[OutPort] = shape.outlets.toSet
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
def isRunnable: Boolean = inPorts.isEmpty && outPorts.isEmpty
|
2015-04-23 13:32:07 +02:00
|
|
|
|
final def isSink: Boolean = (inPorts.size == 1) && outPorts.isEmpty
|
|
|
|
|
|
final def isSource: Boolean = (outPorts.size == 1) && inPorts.isEmpty
|
|
|
|
|
|
final def isFlow: Boolean = (inPorts.size == 1) && (outPorts.size == 1)
|
|
|
|
|
|
final def isBidiFlow: Boolean = (inPorts.size == 2) && (outPorts.size == 2)
|
|
|
|
|
|
def isAtomic: Boolean = subModules.isEmpty
|
|
|
|
|
|
def isCopied: Boolean = false
|
2015-12-15 16:44:48 +01:00
|
|
|
|
def isFused: Boolean = false
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Fuses this Module to `that` Module by wiring together `from` and `to`,
|
|
|
|
|
|
* retaining the materialized value of `this` in the result
|
|
|
|
|
|
* @param that a Module to fuse with
|
|
|
|
|
|
* @param from the data source to wire
|
|
|
|
|
|
* @param to the data sink to wire
|
|
|
|
|
|
* @return a Module representing the fusion of `this` and `that`
|
|
|
|
|
|
*/
|
|
|
|
|
|
final def fuse(that: Module, from: OutPort, to: InPort): Module =
|
|
|
|
|
|
fuse(that, from, to, Keep.left)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Fuses this Module to `that` Module by wiring together `from` and `to`,
|
|
|
|
|
|
* transforming the materialized values of `this` and `that` using the
|
|
|
|
|
|
* provided function `f`
|
|
|
|
|
|
* @param that a Module to fuse with
|
|
|
|
|
|
* @param from the data source to wire
|
|
|
|
|
|
* @param to the data sink to wire
|
|
|
|
|
|
* @param f the function to apply to the materialized values
|
|
|
|
|
|
* @return a Module representing the fusion of `this` and `that`
|
|
|
|
|
|
*/
|
|
|
|
|
|
final def fuse[A, B, C](that: Module, from: OutPort, to: InPort, f: (A, B) ⇒ C): Module =
|
|
|
|
|
|
this.compose(that, f).wire(from, to)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Creates a new Module based on the current Module but with
|
|
|
|
|
|
* the given OutPort wired to the given InPort.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param from the OutPort to wire
|
|
|
|
|
|
* @param to the InPort to wire
|
|
|
|
|
|
* @return a new Module with the ports wired
|
|
|
|
|
|
*/
|
|
|
|
|
|
final def wire(from: OutPort, to: InPort): Module = {
|
|
|
|
|
|
if (Debug) validate(this)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
require(outPorts(from),
|
|
|
|
|
|
if (downstreams.contains(from)) s"The output port [$from] is already connected"
|
|
|
|
|
|
else s"The output port [$from] is not part of the underlying graph.")
|
|
|
|
|
|
require(inPorts(to),
|
|
|
|
|
|
if (upstreams.contains(to)) s"The input port [$to] is already connected"
|
|
|
|
|
|
else s"The input port [$to] is not part of the underlying graph.")
|
|
|
|
|
|
|
|
|
|
|
|
CompositeModule(
|
2015-12-15 16:44:48 +01:00
|
|
|
|
if (isSealed) Set(this) else subModules,
|
2015-01-28 14:19:50 +01:00
|
|
|
|
AmorphousShape(shape.inlets.filterNot(_ == to), shape.outlets.filterNot(_ == from)),
|
2015-04-23 13:32:07 +02:00
|
|
|
|
downstreams.updated(from, to),
|
|
|
|
|
|
upstreams.updated(to, from),
|
2015-01-28 14:19:50 +01:00
|
|
|
|
materializedValueComputation,
|
2016-02-12 08:28:16 +01:00
|
|
|
|
if (isSealed) Attributes.none else attributes)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
final def transformMaterializedValue(f: Any ⇒ Any): Module = {
|
2015-07-06 22:00:21 +02:00
|
|
|
|
if (Debug) validate(this)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
CompositeModule(
|
2015-12-14 17:02:00 +01:00
|
|
|
|
if (this.isSealed) Set(this) else this.subModules,
|
2015-01-28 14:19:50 +01:00
|
|
|
|
shape,
|
2015-04-23 13:32:07 +02:00
|
|
|
|
downstreams,
|
|
|
|
|
|
upstreams,
|
|
|
|
|
|
Transform(f, if (this.isSealed) Atomic(this) else this.materializedValueComputation),
|
2015-10-31 14:46:10 +01:00
|
|
|
|
if (this.isSealed) Attributes.none else attributes)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Creates a new Module which is `this` Module composed with `that` Module.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param that a Module to be composed with (cannot be itself)
|
|
|
|
|
|
* @return a Module that represents the composition of `this` and `that`
|
|
|
|
|
|
*/
|
|
|
|
|
|
def compose(that: Module): Module = compose(that, Keep.left)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* Creates a new Module which is `this` Module composed with `that` Module,
|
|
|
|
|
|
* using the given function `f` to compose the materialized value of `this` with
|
|
|
|
|
|
* the materialized value of `that`.
|
|
|
|
|
|
* @param that a Module to be composed with (cannot be itself)
|
|
|
|
|
|
* @param f a function which combines the materialized values
|
|
|
|
|
|
* @tparam A the type of the materialized value of `this`
|
|
|
|
|
|
* @tparam B the type of the materialized value of `that`
|
|
|
|
|
|
* @tparam C the type of the materialized value of the returned Module
|
|
|
|
|
|
* @return a Module that represents the composition of `this` and `that`
|
|
|
|
|
|
*/
|
|
|
|
|
|
def compose[A, B, C](that: Module, f: (A, B) ⇒ C): Module = {
|
|
|
|
|
|
if (Debug) validate(this)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-07-29 23:01:26 +01:00
|
|
|
|
require(that ne this, "A module cannot be added to itself. You should pass a separate instance to compose().")
|
2015-01-28 14:19:50 +01:00
|
|
|
|
require(!subModules(that), "An existing submodule cannot be added again. All contained modules must be unique.")
|
|
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
val modules1 = if (this.isSealed) Set(this) else this.subModules
|
|
|
|
|
|
val modules2 = if (that.isSealed) Set(that) else that.subModules
|
|
|
|
|
|
|
|
|
|
|
|
val matComputation1 = if (this.isSealed) Atomic(this) else this.materializedValueComputation
|
|
|
|
|
|
val matComputation2 = if (that.isSealed) Atomic(that) else that.materializedValueComputation
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
CompositeModule(
|
|
|
|
|
|
modules1 ++ modules2,
|
|
|
|
|
|
AmorphousShape(shape.inlets ++ that.shape.inlets, shape.outlets ++ that.shape.outlets),
|
2015-04-23 13:32:07 +02:00
|
|
|
|
downstreams ++ that.downstreams,
|
|
|
|
|
|
upstreams ++ that.upstreams,
|
2015-06-16 15:34:54 +02:00
|
|
|
|
// would like to optimize away this allocation for Keep.{left,right} but that breaks side-effecting transformations
|
|
|
|
|
|
Combine(f.asInstanceOf[(Any, Any) ⇒ Any], matComputation1, matComputation2),
|
2015-10-31 14:46:10 +01:00
|
|
|
|
Attributes.none)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Creates a new Module which is `this` Module composed with `that` Module.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The difference to compose(that) is that this version completely ignores the materialized value
|
|
|
|
|
|
* computation of `that` while the normal version executes the computation and discards its result.
|
|
|
|
|
|
* This means that this version must not be used for user-provided `that` modules because users may
|
|
|
|
|
|
* transform materialized values only to achieve some side-effect; it can only be
|
|
|
|
|
|
* used where we know that there is no meaningful computation to be done (like for
|
|
|
|
|
|
* MaterializedValueSource).
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param that a Module to be composed with (cannot be itself)
|
|
|
|
|
|
* @return a Module that represents the composition of `this` and `that`
|
|
|
|
|
|
*/
|
|
|
|
|
|
def composeNoMat(that: Module): Module = {
|
|
|
|
|
|
if (Debug) validate(this)
|
|
|
|
|
|
|
|
|
|
|
|
require(that ne this, "A module cannot be added to itself. You should pass a separate instance to compose().")
|
|
|
|
|
|
require(!subModules(that), "An existing submodule cannot be added again. All contained modules must be unique.")
|
|
|
|
|
|
|
|
|
|
|
|
val modules1 = if (this.isSealed) Set(this) else this.subModules
|
|
|
|
|
|
val modules2 = if (that.isSealed) Set(that) else that.subModules
|
|
|
|
|
|
|
|
|
|
|
|
val matComputation = if (this.isSealed) Atomic(this) else this.materializedValueComputation
|
|
|
|
|
|
|
|
|
|
|
|
CompositeModule(
|
|
|
|
|
|
modules1 ++ modules2,
|
|
|
|
|
|
AmorphousShape(shape.inlets ++ that.shape.inlets, shape.outlets ++ that.shape.outlets),
|
|
|
|
|
|
downstreams ++ that.downstreams,
|
|
|
|
|
|
upstreams ++ that.upstreams,
|
|
|
|
|
|
// would like to optimize away this allocation for Keep.{left,right} but that breaks side-effecting transformations
|
|
|
|
|
|
matComputation,
|
|
|
|
|
|
Attributes.none)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
|
def subModules: Set[Module]
|
2016-02-12 08:28:16 +01:00
|
|
|
|
final def isSealed: Boolean = isAtomic || isCopied || isFused || attributes.attributeList.nonEmpty
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
def downstreams: Map[OutPort, InPort] = Map.empty
|
|
|
|
|
|
def upstreams: Map[InPort, OutPort] = Map.empty
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
def materializedValueComputation: MaterializedValueNode = Atomic(this)
|
2016-02-12 08:28:16 +01:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* The purpose of this method is to create a copy to be included in a larger
|
|
|
|
|
|
* graph such that port identity clashes are avoided. Where a full copy is not
|
|
|
|
|
|
* possible or desirable, use a CopiedModule. The shape of the resulting
|
|
|
|
|
|
* module MUST NOT contain the same ports as this module’s shape.
|
|
|
|
|
|
*/
|
2015-01-28 14:19:50 +01:00
|
|
|
|
def carbonCopy: Module
|
|
|
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
|
def attributes: Attributes
|
|
|
|
|
|
def withAttributes(attributes: Attributes): Module
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
final override def hashCode(): Int = super.hashCode()
|
|
|
|
|
|
final override def equals(obj: scala.Any): Boolean = super.equals(obj)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
object EmptyModule extends Module {
|
2015-02-26 11:58:29 +01:00
|
|
|
|
override def shape = ClosedShape
|
2015-01-28 14:19:50 +01:00
|
|
|
|
override def replaceShape(s: Shape) =
|
2015-02-26 11:58:29 +01:00
|
|
|
|
if (s == ClosedShape) this
|
2015-01-28 14:19:50 +01:00
|
|
|
|
else throw new UnsupportedOperationException("cannot replace the shape of the EmptyModule")
|
|
|
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
|
override def compose(that: Module): Module = that
|
2015-04-23 13:32:07 +02:00
|
|
|
|
|
2015-07-06 22:00:21 +02:00
|
|
|
|
override def compose[A, B, C](that: Module, f: (A, B) ⇒ C): Module =
|
2015-04-23 13:32:07 +02:00
|
|
|
|
throw new UnsupportedOperationException("It is invalid to combine materialized value with EmptyModule")
|
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
|
override def subModules: Set[Module] = Set.empty
|
|
|
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
|
override def withAttributes(attributes: Attributes): Module =
|
2015-01-28 14:19:50 +01:00
|
|
|
|
throw new UnsupportedOperationException("EmptyModule cannot carry attributes")
|
2015-06-23 17:32:55 +02:00
|
|
|
|
override def attributes = Attributes.none
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
override def carbonCopy: Module = this
|
|
|
|
|
|
|
|
|
|
|
|
override def isRunnable: Boolean = false
|
|
|
|
|
|
override def isAtomic: Boolean = false
|
|
|
|
|
|
override def materializedValueComputation: MaterializedValueNode = Ignore
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
final case class CopiedModule(override val shape: Shape,
|
|
|
|
|
|
override val attributes: Attributes,
|
|
|
|
|
|
copyOf: Module) extends Module {
|
2015-04-23 13:32:07 +02:00
|
|
|
|
override val subModules: Set[Module] = Set(copyOf)
|
|
|
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
|
override def withAttributes(attr: Attributes): Module = this.copy(attributes = attr)
|
2015-04-23 13:32:07 +02:00
|
|
|
|
|
|
|
|
|
|
override def carbonCopy: Module = this.copy(shape = shape.deepCopy())
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
override def replaceShape(s: Shape): Module = {
|
|
|
|
|
|
shape.requireSamePortsAs(s)
|
2016-02-12 08:28:16 +01:00
|
|
|
|
CompositeModule(this, s)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
override val materializedValueComputation: MaterializedValueNode = Atomic(copyOf)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
override def isCopied: Boolean = true
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
override def toString: String = "copy of " + copyOf.toString
|
|
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
final case class CompositeModule(
|
2016-02-12 08:28:16 +01:00
|
|
|
|
override val subModules: Set[Module],
|
|
|
|
|
|
override val shape: Shape,
|
|
|
|
|
|
override val downstreams: Map[OutPort, InPort],
|
|
|
|
|
|
override val upstreams: Map[InPort, OutPort],
|
|
|
|
|
|
override val materializedValueComputation: MaterializedValueNode,
|
|
|
|
|
|
override val attributes: Attributes) extends Module {
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
override def replaceShape(s: Shape): Module = {
|
|
|
|
|
|
shape.requireSamePortsAs(s)
|
|
|
|
|
|
copy(shape = s)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-04-23 13:32:07 +02:00
|
|
|
|
override def carbonCopy: Module = CopiedModule(shape.deepCopy(), attributes, copyOf = this)
|
|
|
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
|
override def withAttributes(attributes: Attributes): Module = copy(attributes = attributes)
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
|
|
|
|
|
override def toString =
|
|
|
|
|
|
s"""
|
2015-07-06 22:00:21 +02:00
|
|
|
|
| Module: ${this.attributes.nameOrDefault("unnamed")}
|
2015-11-04 11:43:11 +02:00
|
|
|
|
| Modules: ${subModules.iterator.map(m ⇒ "\n " + m.attributes.nameOrDefault(m.getClass.getName)).mkString("")}
|
|
|
|
|
|
| Downstreams: ${downstreams.iterator.map { case (in, out) ⇒ s"\n $in -> $out" }.mkString("")}
|
|
|
|
|
|
| Upstreams: ${upstreams.iterator.map { case (out, in) ⇒ s"\n $out -> $in" }.mkString("")}
|
|
|
|
|
|
|""".stripMargin
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
2015-12-14 17:02:00 +01:00
|
|
|
|
|
2016-02-12 08:28:16 +01:00
|
|
|
|
object CompositeModule {
|
|
|
|
|
|
def apply(m: Module, s: Shape): CompositeModule = CompositeModule(Set(m), s, Map.empty, Map.empty, Atomic(m), Attributes.none)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
final case class FusedModule(
|
2016-02-12 08:28:16 +01:00
|
|
|
|
override val subModules: Set[Module],
|
|
|
|
|
|
override val shape: Shape,
|
|
|
|
|
|
override val downstreams: Map[OutPort, InPort],
|
|
|
|
|
|
override val upstreams: Map[InPort, OutPort],
|
|
|
|
|
|
override val materializedValueComputation: MaterializedValueNode,
|
|
|
|
|
|
override val attributes: Attributes,
|
|
|
|
|
|
info: Fusing.StructuralInfo) extends Module {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
|
2015-12-15 16:44:48 +01:00
|
|
|
|
override def isFused: Boolean = true
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
override def replaceShape(s: Shape): Module = {
|
|
|
|
|
|
shape.requireSamePortsAs(s)
|
|
|
|
|
|
copy(shape = s)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override def carbonCopy: Module = CopiedModule(shape.deepCopy(), attributes, copyOf = this)
|
|
|
|
|
|
|
|
|
|
|
|
override def withAttributes(attributes: Attributes): FusedModule = copy(attributes = attributes)
|
|
|
|
|
|
|
|
|
|
|
|
override def toString =
|
|
|
|
|
|
s"""
|
|
|
|
|
|
| Module: ${this.attributes.nameOrDefault("unnamed")}
|
|
|
|
|
|
| Modules:
|
|
|
|
|
|
| ${subModules.iterator.map(m ⇒ m.toString.split("\n").mkString("\n ")).mkString("\n ")}
|
|
|
|
|
|
| Downstreams: ${downstreams.iterator.map { case (in, out) ⇒ s"\n $in -> $out" }.mkString("")}
|
|
|
|
|
|
| Upstreams: ${upstreams.iterator.map { case (out, in) ⇒ s"\n $out -> $in" }.mkString("")}
|
|
|
|
|
|
|""".stripMargin
|
|
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-16 15:34:54 +02:00
|
|
|
|
private[stream] object VirtualProcessor {
|
|
|
|
|
|
sealed trait Termination
|
|
|
|
|
|
case object Allowed extends Termination
|
|
|
|
|
|
case object Completed extends Termination
|
|
|
|
|
|
case class Failed(ex: Throwable) extends Termination
|
|
|
|
|
|
|
2015-06-13 16:28:38 -04:00
|
|
|
|
private val InertSubscriber = new CancellingSubscriber[Any]
|
2015-06-06 14:36:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-16 15:34:54 +02:00
|
|
|
|
private[stream] final class VirtualProcessor[T] extends Processor[T, T] {
|
|
|
|
|
|
import VirtualProcessor._
|
|
|
|
|
|
import ReactiveStreamsCompliance._
|
|
|
|
|
|
|
|
|
|
|
|
private val subscriptionStatus = new AtomicReference[AnyRef]
|
|
|
|
|
|
private val terminationStatus = new AtomicReference[Termination]
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-04-16 16:05:49 +02:00
|
|
|
|
override def subscribe(s: Subscriber[_ >: T]): Unit = {
|
2015-06-16 15:34:54 +02:00
|
|
|
|
requireNonNullSubscriber(s)
|
|
|
|
|
|
if (subscriptionStatus.compareAndSet(null, s)) () // wait for onSubscribe
|
|
|
|
|
|
else
|
|
|
|
|
|
subscriptionStatus.get match {
|
|
|
|
|
|
case sub: Subscriber[_] ⇒ rejectAdditionalSubscriber(s, "VirtualProcessor")
|
|
|
|
|
|
case sub: Sub ⇒
|
2015-09-21 09:48:22 -04:00
|
|
|
|
try {
|
|
|
|
|
|
subscriptionStatus.set(s)
|
|
|
|
|
|
tryOnSubscribe(s, sub)
|
2015-06-16 15:34:54 +02:00
|
|
|
|
sub.closeLatch() // allow onNext only now
|
2015-09-21 09:48:22 -04:00
|
|
|
|
terminationStatus.getAndSet(Allowed) match {
|
2015-10-30 16:00:44 +01:00
|
|
|
|
case null ⇒ // nothing happened yet
|
|
|
|
|
|
case VirtualProcessor.Completed ⇒ tryOnComplete(s)
|
|
|
|
|
|
case VirtualProcessor.Failed(ex) ⇒ tryOnError(s, ex)
|
|
|
|
|
|
case VirtualProcessor.Allowed ⇒ // all good
|
2015-06-16 15:34:54 +02:00
|
|
|
|
}
|
2015-09-21 09:48:22 -04:00
|
|
|
|
} catch {
|
2015-10-07 16:50:44 +02:00
|
|
|
|
case NonFatal(ex) ⇒ sub.cancel()
|
2015-09-21 09:48:22 -04:00
|
|
|
|
}
|
2015-06-16 15:34:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override def onSubscribe(s: Subscription): Unit = {
|
|
|
|
|
|
requireNonNullSubscription(s)
|
|
|
|
|
|
val wrapped = new Sub(s)
|
|
|
|
|
|
if (subscriptionStatus.compareAndSet(null, wrapped)) () // wait for Subscriber
|
|
|
|
|
|
else
|
|
|
|
|
|
subscriptionStatus.get match {
|
|
|
|
|
|
case sub: Subscriber[_] ⇒
|
|
|
|
|
|
terminationStatus.get match {
|
|
|
|
|
|
case Allowed ⇒
|
|
|
|
|
|
/*
|
|
|
|
|
|
* There is a race condition here: if this thread reads the subscriptionStatus after
|
|
|
|
|
|
* set set() in subscribe() but then sees the terminationStatus before the getAndSet()
|
|
|
|
|
|
* is published then we will rely upon the downstream Subscriber for cancelling this
|
|
|
|
|
|
* Subscription. I only mention this because the TCK requires that we handle this here
|
|
|
|
|
|
* (since the manualSubscriber used there does not expose this behavior).
|
|
|
|
|
|
*/
|
|
|
|
|
|
s.cancel()
|
|
|
|
|
|
case _ ⇒
|
|
|
|
|
|
tryOnSubscribe(sub, wrapped)
|
|
|
|
|
|
wrapped.closeLatch() // allow onNext only now
|
|
|
|
|
|
terminationStatus.set(Allowed)
|
|
|
|
|
|
}
|
|
|
|
|
|
case sub: Subscription ⇒
|
|
|
|
|
|
s.cancel() // reject further Subscriptions
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override def onError(t: Throwable): Unit = {
|
|
|
|
|
|
requireNonNullException(t)
|
|
|
|
|
|
if (terminationStatus.compareAndSet(null, Failed(t))) () // let it be picked up by subscribe()
|
|
|
|
|
|
else tryOnError(subscriptionStatus.get.asInstanceOf[Subscriber[T]], t)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override def onComplete(): Unit =
|
|
|
|
|
|
if (terminationStatus.compareAndSet(null, Completed)) () // let it be picked up by subscribe()
|
|
|
|
|
|
else tryOnComplete(subscriptionStatus.get.asInstanceOf[Subscriber[T]])
|
|
|
|
|
|
|
|
|
|
|
|
override def onNext(t: T): Unit = {
|
|
|
|
|
|
requireNonNullElement(t)
|
|
|
|
|
|
tryOnNext(subscriptionStatus.get.asInstanceOf[Subscriber[T]], t)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private final class Sub(s: Subscription) extends AtomicLong with Subscription {
|
|
|
|
|
|
override def cancel(): Unit = {
|
|
|
|
|
|
subscriptionStatus.set(InertSubscriber)
|
|
|
|
|
|
s.cancel()
|
|
|
|
|
|
}
|
|
|
|
|
|
@tailrec
|
|
|
|
|
|
override def request(n: Long): Unit = {
|
|
|
|
|
|
val current = get
|
|
|
|
|
|
if (current < 0) s.request(n)
|
|
|
|
|
|
else if (compareAndSet(current, current + n)) ()
|
|
|
|
|
|
else request(n)
|
|
|
|
|
|
}
|
|
|
|
|
|
def closeLatch(): Unit = {
|
|
|
|
|
|
val requested = getAndSet(-1)
|
|
|
|
|
|
if (requested > 0) s.request(requested)
|
|
|
|
|
|
}
|
2015-04-16 16:05:49 +02:00
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-22 18:02:42 +02:00
|
|
|
|
/**
|
|
|
|
|
|
* INERNAL API
|
|
|
|
|
|
*/
|
|
|
|
|
|
private[stream] object MaterializerSession {
|
|
|
|
|
|
class MaterializationPanic(cause: Throwable) extends RuntimeException("Materialization aborted.", cause) with NoStackTrace
|
2015-12-14 17:02:00 +01:00
|
|
|
|
|
|
|
|
|
|
final val Debug = false
|
2015-06-22 18:02:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* INTERNAL API
|
|
|
|
|
|
*/
|
2015-10-31 14:46:10 +01:00
|
|
|
|
private[stream] abstract class MaterializerSession(val topLevel: StreamLayout.Module, val initialAttributes: Attributes) {
|
2015-01-28 14:19:50 +01:00
|
|
|
|
import StreamLayout._
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
private var subscribersStack: List[ju.Map[InPort, Subscriber[Any]]] =
|
|
|
|
|
|
new ju.HashMap[InPort, Subscriber[Any]] :: Nil
|
|
|
|
|
|
private var publishersStack: List[ju.Map[OutPort, Publisher[Any]]] =
|
|
|
|
|
|
new ju.HashMap[OutPort, Publisher[Any]] :: Nil
|
2015-04-23 13:32:07 +02:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* Please note that this stack keeps track of the scoped modules wrapped in CopiedModule but not the CopiedModule
|
|
|
|
|
|
* itself. The reason is that the CopiedModule itself is only needed for the enterScope and exitScope methods but
|
|
|
|
|
|
* not elsewhere. For this reason they are just simply passed as parameters to those methods.
|
|
|
|
|
|
*
|
|
|
|
|
|
* The reason why the encapsulated (copied) modules are stored as mutable state to save subclasses of this class
|
|
|
|
|
|
* from passing the current scope around or even knowing about it.
|
|
|
|
|
|
*/
|
|
|
|
|
|
private var moduleStack: List[Module] = topLevel :: Nil
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
private def subscribers: ju.Map[InPort, Subscriber[Any]] = subscribersStack.head
|
|
|
|
|
|
private def publishers: ju.Map[OutPort, Publisher[Any]] = publishersStack.head
|
2015-04-23 13:32:07 +02:00
|
|
|
|
private def currentLayout: Module = moduleStack.head
|
|
|
|
|
|
|
|
|
|
|
|
// Enters a copied module and establishes a scope that prevents internals to leak out and interfere with copies
|
|
|
|
|
|
// of the same module.
|
|
|
|
|
|
// We don't store the enclosing CopiedModule itself as state since we don't use it anywhere else than exit and enter
|
|
|
|
|
|
private def enterScope(enclosing: CopiedModule): Unit = {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
subscribersStack ::= new ju.HashMap
|
|
|
|
|
|
publishersStack ::= new ju.HashMap
|
2015-04-23 13:32:07 +02:00
|
|
|
|
moduleStack ::= enclosing.copyOf
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Exits the scope of the copied module and propagates Publishers/Subscribers to the enclosing scope assigning
|
|
|
|
|
|
// them to the copied ports instead of the original ones (since there might be multiple copies of the same module
|
|
|
|
|
|
// leading to port identity collisions)
|
|
|
|
|
|
// We don't store the enclosing CopiedModule itself as state since we don't use it anywhere else than exit and enter
|
|
|
|
|
|
private def exitScope(enclosing: CopiedModule): Unit = {
|
|
|
|
|
|
val scopeSubscribers = subscribers
|
|
|
|
|
|
val scopePublishers = publishers
|
|
|
|
|
|
subscribersStack = subscribersStack.tail
|
|
|
|
|
|
publishersStack = publishersStack.tail
|
|
|
|
|
|
moduleStack = moduleStack.tail
|
|
|
|
|
|
|
|
|
|
|
|
// When we exit the scope of a copied module, pick up the Subscribers/Publishers belonging to exposed ports of
|
|
|
|
|
|
// the original module and assign them to the copy ports in the outer scope that we will return to
|
|
|
|
|
|
enclosing.copyOf.shape.inlets.iterator.zip(enclosing.shape.inlets.iterator).foreach {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
case (original, exposed) ⇒ assignPort(exposed, scopeSubscribers.get(original))
|
2015-04-23 13:32:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enclosing.copyOf.shape.outlets.iterator.zip(enclosing.shape.outlets.iterator).foreach {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
case (original, exposed) ⇒ assignPort(exposed, scopePublishers.get(original))
|
2015-06-22 18:02:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
|
final def materialize(): Any = {
|
|
|
|
|
|
require(topLevel ne EmptyModule, "An empty module cannot be materialized (EmptyModule was given)")
|
|
|
|
|
|
require(
|
|
|
|
|
|
topLevel.isRunnable,
|
|
|
|
|
|
s"The top level module cannot be materialized because it has unconnected ports: ${(topLevel.inPorts ++ topLevel.outPorts).mkString(", ")}")
|
2015-10-31 14:46:10 +01:00
|
|
|
|
try materializeModule(topLevel, initialAttributes and topLevel.attributes)
|
2015-06-22 18:02:42 +02:00
|
|
|
|
catch {
|
2015-06-14 03:12:30 -04:00
|
|
|
|
case NonFatal(cause) ⇒
|
|
|
|
|
|
// PANIC!!! THE END OF THE MATERIALIZATION IS NEAR!
|
|
|
|
|
|
// Cancels all intermediate Publishers and fails all intermediate Subscribers.
|
|
|
|
|
|
// (This is an attempt to clean up after an exception during materialization)
|
|
|
|
|
|
val errorPublisher = new ErrorPublisher(new MaterializationPanic(cause), "")
|
2015-12-14 17:02:00 +01:00
|
|
|
|
for (subMap ← subscribersStack; sub ← subMap.asScala.valuesIterator)
|
2015-06-14 03:12:30 -04:00
|
|
|
|
errorPublisher.subscribe(sub)
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
for (pubMap ← publishersStack; pub ← pubMap.asScala.valuesIterator)
|
2015-06-14 03:12:30 -04:00
|
|
|
|
pub.subscribe(new CancellingSubscriber)
|
|
|
|
|
|
|
|
|
|
|
|
throw cause
|
2015-06-22 18:02:42 +02:00
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
|
protected def mergeAttributes(parent: Attributes, current: Attributes): Attributes =
|
2015-01-28 14:19:50 +01:00
|
|
|
|
parent and current
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
private val matValSrc: ju.Map[MaterializedValueNode, List[MaterializedValueSource[Any]]] = new ju.HashMap
|
|
|
|
|
|
def registerSrc(ms: MaterializedValueSource[Any]): Unit = {
|
|
|
|
|
|
if (MaterializerSession.Debug) println(s"registering source $ms")
|
|
|
|
|
|
matValSrc.get(ms.computation) match {
|
|
|
|
|
|
case null ⇒ matValSrc.put(ms.computation, ms :: Nil)
|
|
|
|
|
|
case xs ⇒ matValSrc.put(ms.computation, ms :: xs)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
|
protected def materializeModule(module: Module, effectiveAttributes: Attributes): Any = {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
val materializedValues: ju.Map[Module, Any] = new ju.HashMap
|
2015-03-30 14:22:12 +02:00
|
|
|
|
|
2015-01-28 14:19:50 +01:00
|
|
|
|
for (submodule ← module.subModules) {
|
|
|
|
|
|
val subEffectiveAttributes = mergeAttributes(effectiveAttributes, submodule.attributes)
|
2015-03-30 14:22:12 +02:00
|
|
|
|
submodule match {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
case GraphStageModule(shape, attributes, mv: MaterializedValueSource[_]) ⇒
|
|
|
|
|
|
val copy = mv.copySrc.asInstanceOf[MaterializedValueSource[Any]]
|
|
|
|
|
|
registerSrc(copy)
|
|
|
|
|
|
materializeAtomic(copy.module, subEffectiveAttributes, materializedValues)
|
2015-03-30 14:22:12 +02:00
|
|
|
|
case atomic if atomic.isAtomic ⇒
|
2015-12-14 17:02:00 +01:00
|
|
|
|
materializeAtomic(atomic, subEffectiveAttributes, materializedValues)
|
2015-04-23 13:32:07 +02:00
|
|
|
|
case copied: CopiedModule ⇒
|
|
|
|
|
|
enterScope(copied)
|
|
|
|
|
|
materializedValues.put(copied, materializeModule(copied, subEffectiveAttributes))
|
|
|
|
|
|
exitScope(copied)
|
2015-03-30 14:22:12 +02:00
|
|
|
|
case composite ⇒
|
|
|
|
|
|
materializedValues.put(composite, materializeComposite(composite, subEffectiveAttributes))
|
|
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
2015-03-30 14:22:12 +02:00
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
if (MaterializerSession.Debug) {
|
|
|
|
|
|
println("RESOLVING")
|
|
|
|
|
|
println(s" module = $module")
|
|
|
|
|
|
println(s" computation = ${module.materializedValueComputation}")
|
|
|
|
|
|
println(s" matValSrc = $matValSrc")
|
|
|
|
|
|
println(s" matVals = $materializedValues")
|
|
|
|
|
|
}
|
|
|
|
|
|
resolveMaterialized(module.materializedValueComputation, materializedValues, " ")
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-06-23 17:32:55 +02:00
|
|
|
|
protected def materializeComposite(composite: Module, effectiveAttributes: Attributes): Any = {
|
2015-01-28 14:19:50 +01:00
|
|
|
|
materializeModule(composite, effectiveAttributes)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
protected def materializeAtomic(atomic: Module, effectiveAttributes: Attributes, matVal: ju.Map[Module, Any]): Unit
|
2015-01-28 14:19:50 +01:00
|
|
|
|
|
2015-12-14 17:02:00 +01:00
|
|
|
|
private def resolveMaterialized(matNode: MaterializedValueNode, matVal: ju.Map[Module, Any], indent: String): Any = {
|
|
|
|
|
|
if (MaterializerSession.Debug) println(indent + matNode)
|
|
|
|
|
|
val ret = matNode match {
|
|
|
|
|
|
case Atomic(m) ⇒ matVal.get(m)
|
|
|
|
|
|
case Combine(f, d1, d2) ⇒ f(resolveMaterialized(d1, matVal, indent + " "), resolveMaterialized(d2, matVal, indent + " "))
|
|
|
|
|
|
case Transform(f, d) ⇒ f(resolveMaterialized(d, matVal, indent + " "))
|
2016-01-20 10:00:37 +02:00
|
|
|
|
case Ignore ⇒ NotUsed
|
2015-12-14 17:02:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
if (MaterializerSession.Debug) println(indent + s"result = $ret")
|
|
|
|
|
|
matValSrc.remove(matNode) match {
|
|
|
|
|
|
case null ⇒ // nothing to do
|
|
|
|
|
|
case srcs ⇒
|
|
|
|
|
|
if (MaterializerSession.Debug) println(indent + s"triggering sources $srcs")
|
|
|
|
|
|
srcs.foreach(_.setValue(ret))
|
|
|
|
|
|
}
|
|
|
|
|
|
ret
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final protected def assignPort(in: InPort, subscriber: Subscriber[Any]): Unit = {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
subscribers.put(in, subscriber)
|
2015-04-23 13:32:07 +02:00
|
|
|
|
// Interface (unconnected) ports of the current scope will be wired when exiting the scope
|
|
|
|
|
|
if (!currentLayout.inPorts(in)) {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
val publisher = publishers.get(currentLayout.upstreams(in))
|
2015-06-16 15:34:54 +02:00
|
|
|
|
if (publisher ne null) publisher.subscribe(subscriber)
|
2015-04-23 13:32:07 +02:00
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final protected def assignPort(out: OutPort, publisher: Publisher[Any]): Unit = {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
publishers.put(out, publisher)
|
2015-04-23 13:32:07 +02:00
|
|
|
|
// Interface (unconnected) ports of the current scope will be wired when exiting the scope
|
|
|
|
|
|
if (!currentLayout.outPorts(out)) {
|
2015-12-14 17:02:00 +01:00
|
|
|
|
val subscriber = subscribers.get(currentLayout.downstreams(out))
|
2015-06-16 15:34:54 +02:00
|
|
|
|
if (subscriber ne null) publisher.subscribe(subscriber)
|
2015-04-23 13:32:07 +02:00
|
|
|
|
}
|
2015-01-28 14:19:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|