Comparision Expression


Node Information
Compares two given values, if they are the same 'A==B' result will be TRUE, else result will be FALSE.
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
A IN Object ONE FALSE TRUE
B IN Object ONE FALSE TRUE
Result OUT Boolean MULTIPLE FALSE FALSE

Source
namespace Korduene.KGraph.Nodes.Conditions
{
    [IsNode("Comparision Expression")]
    public class ComparisonExpression : Node
    {
        Port a = new Port(PortType.IN, typeof(object), AcceptsLinks.ONE, "A");
        Port b = new Port(PortType.IN, typeof(object), AcceptsLinks.ONE, "B");
        Port result = new Port(PortType.OUT, typeof(bool), AcceptsLinks.MULTIPLE, "Result");
        public GeneralComparisonType Comparision
        {
            set
            {
                Settings[1] = value.ToString();
            }
            get
            {
                if (Settings[1] == string.Empty) { return GeneralComparisonType.Equal; }
                else
                {
                    return (GeneralComparisonType)Enum.Parse(typeof(GeneralComparisonType), Settings[1]);
                }
            }
        }
        public ComparisonExpression()
        {
            NodeType = KGraph.NodeType.Function;
            ReturnType = typeof(bool);
            this.Name = "Comparision Expression";
            this.AddPort(a);
            this.AddPort(b);
            this.AddPort(result);
            a.CustomAcception = Accepts;
            b.CustomAcception = Accepts;
            Information = "Compares two given values, if they are the same 'A==B' result will be TRUE, else result will be FALSE.";
        }
        private bool Accepts(Port port)
        {
            if (port == null)
            {
                return false;
            }
            if (a.IsConnected || b.IsConnected)
            {
                if (port.DataType == a.DataType)
                {
                    return true;
                }
            }
            else
            {
                ReturnType = port.DataType;
                a.DataType = port.DataType;
                a.Refresh();
                b.DataType = port.DataType;
                b.Refresh();
                return true;
            }
            return false;
        }
        public override string Code()
        {
            string oprtor = string.Empty;
            switch (Comparision)
            {
                case GeneralComparisonType.Equal:
                    oprtor = "==";
                    break;
                case GeneralComparisonType.NotEqual:
                    oprtor = "!=";
                    break;
                default:
                    break;
            }
            if (a.IsConnected)
            {
                return "if (" + GetValue(a) + " " + oprtor + " " + GetValue(b) + ") { return true; } else { return false; }";
            }
            return string.Empty;
        }
    }
}