Ejemplo n.º 1
0
        public static Operant Calculate(Command command, List <Operant> operants)
        {
            var    left   = operants.FirstOrDefault(x => x.Order == OperantOrder.Left)?.Value;
            var    right  = operants.FirstOrDefault(x => x.Order == OperantOrder.Right)?.Value;
            double?result = null;

            switch (command.Type)
            {
            case CommandType.Add:
                result = left + right;
                break;

            case CommandType.Minus:
                result = -left;
                break;

            case CommandType.Subtr:
                result = left - right;
                break;

            case CommandType.Mult:
                result = left * right;
                break;

            case CommandType.Divide:
                result = left / right;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            if (!result.HasValue)
            {
                throw new InvalidOperationException("Result is null");
            }
            var operant = new Operant()
            {
                NextCommandId = command.NextCommandId,
                Order         = command.Order,
                Value         = result.Value
            };

            return(operant);
        }
Ejemplo n.º 2
0
        private static void BuildTables(DataFlowSystem system, Node node, int nextCommandId, OperantOrder order)
        {
            if (node is NodeBinary nodeBinary)
            {
                var command = new Command()
                {
                    Id            = system._lastCommandId++,
                    IsActive      = false,
                    NextCommandId = nextCommandId,
                    Order         = order,
                    Type          = (CommandType)nodeBinary.Operation
                };
                system.CommandsMemory.NotActiveCommands.Add(command);
                BuildTables(system, nodeBinary.Right, command.Id, OperantOrder.Right);
                BuildTables(system, nodeBinary.Left, command.Id, OperantOrder.Left);
            }

            if (node is NodeUnary nodeUnary)
            {
                var command = new Command()
                {
                    Id            = system._lastCommandId++,
                    IsActive      = false,
                    NextCommandId = nextCommandId,
                    Order         = order,
                    Type          = (CommandType)nodeUnary.Operation
                };
                system.CommandsMemory.NotActiveCommands.Add(command);
                BuildTables(system, nodeUnary.Right, command.Id, OperantOrder.Right);
            }

            if (node is NodeNumber nodeNumber)
            {
                var operant = new Operant {
                    NextCommandId = nextCommandId,
                    Order         = order,
                    Value         = nodeNumber.Number
                };
                system.Distributor.DistributeOperants(new List <Operant> {
                    operant
                });
            }
        }