Example #1
0
            /// <summary>
            /// INTERNAL API.
            /// This is only used by the materialization-importing apply methods of Source,
            /// Flow, Sink and Graph.
            /// </summary>
            internal TShape Add <TShape, TMat1, TMat2, TMat3>(IGraph <TShape> graph, Func <TMat1, TMat2, TMat3> combine) where TShape : Shape
            {
                if (StreamLayout.IsDebug)
                {
                    StreamLayout.Validate(graph.Module);
                }

                var copy = graph.Module.CarbonCopy();

                _moduleInProgress = _moduleInProgress.Compose(copy, combine);
                return((TShape)graph.Shape.CopyFromPorts(copy.Shape.Inlets, copy.Shape.Outlets));
            }
Example #2
0
            /// <summary>
            /// INTERNAL API.
            /// This is only used by the materialization-importing apply methods of Source,
            /// Flow, Sink and Graph.
            /// </summary>
            internal TShape Add <TShape, TMat, TMat2>(IGraph <TShape, TMat> graph, Func <TMat, TMat2> transform) where TShape : Shape
            {
                if (StreamLayout.IsDebug)
                {
                    StreamLayout.Validate(graph.Module);
                }

                var copy = graph.Module.CarbonCopy();

                _moduleInProgress = _moduleInProgress.Compose(copy.TransformMaterializedValue(transform));
                return((TShape)graph.Shape.CopyFromPorts(copy.Shape.Inlets, copy.Shape.Outlets));
            }
Example #3
0
            /// <summary>
            /// Import a graph into this module, performing a deep copy, discarding its
            /// materialized value and returning the copied Ports that are now to be connected.
            /// </summary>
            public TShape Add <TShape, TMat>(IGraph <TShape, TMat> graph)
                where TShape : Shape
            {
                if (StreamLayout.IsDebug)
                {
                    StreamLayout.Validate(graph.Module);
                }

                var copy = graph.Module.CarbonCopy();

                _moduleInProgress = _moduleInProgress.Compose(copy);
                return((TShape)graph.Shape.CopyFromPorts(copy.Shape.Inlets, copy.Shape.Outlets));
            }
Example #4
0
        /// <summary>
        /// TBD
        /// </summary>
        /// <typeparam name="TMat">TBD</typeparam>
        /// <param name="runnable">TBD</param>
        /// <param name="subFlowFuser">TBD</param>
        /// <param name="initialAttributes">TBD</param>
        /// <exception cref="IllegalStateException">TBD</exception>
        /// <returns>TBD</returns>
        public override TMat Materialize <TMat>(IGraph <ClosedShape, TMat> runnable, Func <GraphInterpreterShell, IActorRef> subFlowFuser, Attributes initialAttributes)
        {
            var runnableGraph = _settings.IsAutoFusing
                ? Fusing.Fusing.Aggressive(runnable)
                : runnable;

            if (_haveShutDown.Value)
            {
                throw new IllegalStateException("Attempted to call Materialize() after the ActorMaterializer has been shut down.");
            }

            if (StreamLayout.IsDebug)
            {
                StreamLayout.Validate(runnableGraph.Module);
            }

            var session = new ActorMaterializerSession(this, runnableGraph.Module, initialAttributes, subFlowFuser);

            var matVal = session.Materialize();

            return((TMat)matVal);
        }
Example #5
0
        /// <summary>
        /// Fuse everything that is not forbidden via AsyncBoundary attribute.
        /// </summary>
        /// <typeparam name="TShape">TBD</typeparam>
        /// <typeparam name="TMat">TBD</typeparam>
        /// <param name="graph">TBD</param>
        /// <returns>TBD</returns>
        public static Streams.Fusing.FusedGraph <TShape, TMat> Aggressive <TShape, TMat>(IGraph <TShape, TMat> graph)
            where TShape : Shape
        {
            if (graph is Streams.Fusing.FusedGraph <TShape, TMat> fusedGraph)
            {
                return(fusedGraph);
            }

            var structInfo = new BuildStructuralInfo();

            // First perform normalization by descending the module tree and recording information in the BuildStructuralInfo instance.
            LinkedList <KeyValuePair <IModule, IMaterializedValueNode> > materializedValue;

            try
            {
                materializedValue = Descend <TMat>(graph.Module, Attributes.None, structInfo,
                                                   structInfo.CreateGroup(0), 0);
            }
            catch
            {
                if (IsDebug)
                {
                    structInfo.Dump();
                }
                throw;
            }

            // Then create a copy of the original Shape with the new copied ports.
            var shape = graph.Shape.CopyFromPorts(structInfo.NewInlets(graph.Shape.Inlets),
                                                  structInfo.NewOutlets(graph.Shape.Outlets));

            // Extract the full topological information from the builder before removing assembly-internal (fused) wirings in the next step.
            var info = structInfo.ToInfo(shape,
                                         materializedValue.Select(pair => Tuple.Create(pair.Key, pair.Value)).ToList());

            // Perform the fusing of `structInfo.groups` into GraphModules (leaving them as they are for non - fusable modules).
            structInfo.RemoveInternalWires();
            structInfo.BreakUpGroupsByDispatcher();
            var modules = Fuse(structInfo);

            // Now we have everything ready for a FusedModule.
            var module = new FusedModule(
                modules,
                shape,
                ImmutableDictionary.CreateRange(structInfo.Downstreams),
                ImmutableDictionary.CreateRange(structInfo.Upstreams),
                materializedValue.First().Value,
                Attributes.None,
                info);

            if (StreamLayout.IsDebug)
            {
                StreamLayout.Validate(module);
            }
            if (IsDebug)
            {
                Console.WriteLine(module.ToString());
            }

            return(new Streams.Fusing.FusedGraph <TShape, TMat>(module, (TShape)shape));
        }