Esempio n. 1
0
        /// <summary>
        /// Return the <see cref="StructuralInfoModule"/> for this Graph without any fusing
        /// </summary>
        /// <typeparam name="TShape">TBD</typeparam>
        /// <typeparam name="TMat"></typeparam>
        /// <param name="graph"></param>
        /// <param name="attributes"></param>
        /// <returns></returns>
        public static StructuralInfoModule StructuralInfo <TShape, TMat>(IGraph <TShape, TMat> graph, Attributes attributes) where TShape : Shape
        {
            var structuralInfo = new BuildStructuralInfo();

            // First perform normalization by descending the module tree and recording
            // information in the BuildStructuralInfo instance.

            try
            {
                var materializedValue = Descend <TMat>(graph.Module, Attributes.None, structuralInfo,
                                                       structuralInfo.CreateGroup(0), 0);

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

                // Extract the full topological information from the builder
                return(structuralInfo.ToInfo(shape, materializedValue.Select(pair => Tuple.Create(pair.Key, pair.Value)).ToList(), attributes));
            }
            catch (Exception)
            {
                if (IsDebug)
                {
                    structuralInfo.Dump();
                }

                throw;
            }
        }
Esempio n. 2
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));
        }