ForEach Loop


Node Information
Loops through each item in the given List.
General Information
• This node doesn't have other available options.
• Some of the ports of this node can change their types and accept types automatically.
Visual
Visual

Ports
Text Type DataType Accepts IsList CustomAcception
Loop IN RUN MULTIPLE FALSE FALSE
List IN List<Object> ONE TRUE TRUE
Break IN RUN MULTIPLE FALSE FALSE
Body OUT RUN MULTIPLE FALSE FALSE
Current OUT Object MULTIPLE FALSE FALSE
Complete OUT RUN MULTIPLE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Loops
{
    [IsNode("ForEach Loop")]
    public class ForEachLoop : Node
    {
        Port run = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE, "Loop");
        Port list = new Port(PortType.IN, typeof(List<object>), AcceptsLinks.ONE, "List");
        Port loopbreak = new Port(PortType.IN, typeof(RUN), AcceptsLinks.MULTIPLE, "Break");
        Port current = new Port(PortType.OUT, typeof(object), AcceptsLinks.MULTIPLE, "Current");
        Port runout = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE, "Body");
        Port complete = new Port(PortType.OUT, typeof(RUN), AcceptsLinks.MULTIPLE, "Complete");
        public ForEachLoop()
        {
            this.NodeType = NodeType.Method;
            this.Name = "ForEach Loop";
            AddPort(run);
            AddPort(list);
            AddPort(loopbreak);
            AddPort(runout);
            AddPort(current);
            AddPort(complete);
            list.EditorEnabled = false;
            loopbreak.IsVariable = true;
            loopbreak.VariableType = typeof(bool);
            current.IsVariable = true;
            current.VariableType = typeof(object);
            Information = "Loops through each item in the given List.";
            list.CustomAcception = Accepts;
        }
        public override string Code()
        {
            string code = string.Empty;
            loopbreak.Instructions = loopbreak.ID + "=true;";
            current.Instructions = current.ID;
            string ar = "new List<object>()";
            if (list.IsConnected)
            {
                ar = GetValue(list);
            }
            current.VariableType = current.DataType;
            code = "foreach (" + current.DataType.FullName + " item in " + ar + ")\r\n" +
            "{\r\n" +
             current.ID + "=item;\r\n" + GetValue(runout) +
            "if (" + loopbreak.ID + "){ break; }\r\n" + "}\r\n" + GetValue(complete);
            return code;
        }
        private bool Accepts(Port port)
        {
            if (port == null)
            {
                return false;
            }
            if (list.IsConnected)
            {
                return false;
            }
            if (port.DataType != null && port.IsList)
            {
                list.DataType = port.DataType;
                current.DataType = GraphCore.GetListSubType(port.DataType);
                current.VariableType = current.DataType;
                return true;
            }
            if (port.DataType == typeof(string))
            {
                list.DataType = port.DataType;
                current.DataType = typeof(char);
                current.VariableType = current.DataType;
                return true;
            }
            return false;
        }
        public override void UpdateResults()
        {
            current.Instructions = current.ID;
            if (list.ConnectedPorts.Count < 1)="" {="" status="NodeStatus.ERROR;" errormessage="No arrays attached." ;="" }="" else="" {="" status="NodeStatus.OK;" errormessage="string.Empty;" }="" code();="" }="" }="" }=""></object></object>