public Type TryResolveComponentName(string componentName)
        {
            Type result;

            if (!mResolvedTypes.TryGetValue(componentName, out result))
            {
                result         = ComponentFinder.FindByName(componentName);
                mResolvedTypes = mResolvedTypes.Add(componentName, result);
            }
            return(result);
        }
Esempio n. 2
0
        private void InstantiateProcesses()
        {
            foreach (var node in graph.Nodes)
            {
                var component = Activator.CreateInstance(ComponentFinder.FindByName(node.Type)) as Component;
                if (component == null)
                {
                    throw new ArgumentException($"Could not create process for component '{node.Type}'");
                }
                var process = new Process(node, component);
                nodeToProcess = nodeToProcess.Add(node, process);

                if (process.FillInitialData())
                {
                    runnableProcesses = runnableProcesses.Enqueue(process);
                }
            }
        }
Esempio n. 3
0
        // https://noflojs.org/documentation/graphs/

        public static Graph Read(string fbpFullFileName)
        {
            string fbpContent = System.IO.File.ReadAllText(fbpFullFileName);
            var    result     = Parse(fbpContent);

            var nodes = ImmutableList <Node> .Empty;

            for (var i = 0; i < result.Components.Count; i++)
            {
                var component = result.Components[i];

                var componentType = ComponentFinder.FindByName(component.Type);
                if (componentType != null)
                {
                    var inputAttributes  = ComponentFinder.GetInputAttributes(componentType);
                    var outputAttributes = ComponentFinder.GetOutputAttributes(componentType);
                    //var inputAttributeNames = inputAttributes.Select((x) => x.Name).ToImmutableArray();
                    var inputAttributeNames  = inputAttributes.ToDictionary((x) => x.Name);
                    var outputAttributeNames = outputAttributes.ToDictionary((x) => x.Name);

                    // 1. Add missing inputs and outputs: the fbp format serializes only connected ports
                    foreach (var ia in inputAttributes)
                    {
                        if (!component.InputPorts.Contains(ia.Name))
                        {
                            component.InputPorts = component.InputPorts.Add(ia.Name);
                        }
                    }
                    foreach (var ia in outputAttributes)
                    {
                        if (!component.OutputPorts.Contains(ia.Name))
                        {
                            component.OutputPorts = component.OutputPorts.Add(ia.Name);
                        }
                    }

                    // 2. Sort the ports
                    component.InputPorts  = component.InputPorts.Sort((string a, string b) => inputAttributeNames[a].Index.CompareTo(inputAttributeNames[b].Index));
                    component.OutputPorts = component.OutputPorts.Sort((string a, string b) => outputAttributeNames[a].Index.CompareTo(outputAttributeNames[b].Index));

                    // Other possibile validations that will be done as part of GraphTypeChecker:
                    // 1. Ports that are not present in the type
                    // 2. Component type not found
                    // 3. Port types not matching
                    // 4. Connections that are invalid due to mismatched types
                }

                var inputs = ImmutableArray <NodeInput> .Empty.AddRange(from x in component.InputPorts
                                                                        select new NodeInput(x, component.InputPortInitialDatas.GetValueOrDefault(x, null)));

                var outputs = ImmutableArray <NodeOutput> .Empty.AddRange(from x in component.OutputPorts
                                                                          select new NodeOutput(x, false));

                var pos = new Point2(Double.Parse(component.Metadata.GetValueOrDefault("x", "0")),
                                     Double.Parse(component.Metadata.GetValueOrDefault("y", "0")));

                var node = new Node(component.Name, component.Type,
                                    pos, inputs, outputs);
                nodes = nodes.Add(node);
            }

            var connections = ImmutableList <Connection> .Empty;

            for (var i = 0; i < result.Components.Count; i++)
            {
                var component = result.Components[i];
                var fromNode  = nodes.Where((x) => x.Name == component.Name).First();

                foreach (var conn in component.OutputPortConnections)
                {
                    var fromNodeOutput = fromNode.Outputs.Where((x) => x.Name == conn.Item1).First();
                    var toNode         = nodes.Where((x) => x.Name == conn.Item2.Name).First();
                    var toNodeInput    = toNode.Inputs.Where((x) => x.Name == conn.Item3).First();
                    connections = connections.Add(new Connection(fromNode, fromNodeOutput, toNode, toNodeInput));
                }
            }

            return(new Graph(nodes, connections));
        }