/// <summary>
        /// Contruye un nuevo ConditionalChartElement.
        /// </summary>
        /// <param name="index">El parametro no es relevante para este metodo.</param>
        /// <returns>El nuevo ConditionalChartElement.</returns>
        public override ChartElement Build(int index)
        {
            ConditionalChartElement element = new ConditionalChartElement
            {
                PinSize        = new Size(5, 5),
                BackColor      = Color.LightBlue,
                Display        = new Rectangle(Center.X - 100, Center.Y - 25, 200, 50),
                ShowInputPins  = false,
                ShowOutputPins = false,
                Condition      = ""
            };

            return(element);
        }
Пример #2
0
        /// <summary>
        /// Ejecuta un algoritmo determinado, dado por un ChartElementCollection.
        /// </summary>
        /// <param name="instructionCollection">El conjunto de instrucciones a ejecutar.</param>
        public void ExecuteAlgorithm(ChartElementCollection instructionCollection)
        {
            ChartElement current = null;

            try
            {
                ChartElement first = instructionCollection.First; // Obtiene el elemento "Begin" si existe.

                if (first == null || first.Connections == null)
                {
                    throw new Exception("Invalid algorithm to execute.");                                         // Si no existe el "Begin", el algoritmo no es valido.
                }
                foreach (var connection in first.Connections)
                {
                    if (connection.To != null)
                    {
                        current = connection.To.ChartElement;                   // Obtiene la primera instruccion a ejecutar.
                    }
                }
                while (!(current is EndChartElement) && !shouldStop)
                {
                    if (current == null)
                    {
                        throw new InvalidOperationException("Unconnected chart element. Check your chart connections.");
                    }

                    if (current is ConditionalChartElement)
                    {
                        ConditionalChartElement conditionalChart = (ConditionalChartElement)current;
                        if (conditionalChart.Condition == "")
                        {
                            throw new InvalidOperationException("Invalid conditional chart.");
                        }
                        string preExpression    = ParseSensorData(conditionalChart.Condition).ToLower();
                        string evaluationResult = Evaluator.Evaluate(preExpression);
                        bool   found            = false;
                        foreach (var connection in current.Connections)
                        {
                            if (evaluationResult.Equals(connection.Label))
                            {
                                if (connection.To != null)
                                {
                                    current = connection.To.ChartElement;
                                    found   = true;
                                }
                                else
                                {
                                    throw new Exception("Invalid connection");
                                }
                                break;
                            }
                        }
                        if (!found)
                        {
                            throw new InvalidOperationException("Invalid conditional expression.");
                        }
                    }
                    else
                    {
                        if (current is CommandChartElement)
                        {
                            ExecuteCommand((CommandChartElement)current);
                        }

                        else if (current is ActivityChartElement)
                        {
                            string activityName = ((ActivityChartElement)current).ActivityName;
                            if (ActivityList.ContainsKey(activityName))
                            {
                                ExecuteAlgorithm(ActivityList[activityName]);
                            }
                            else
                            {
                                throw new InvalidOperationException("Invalid activity.");
                            }
                        }

                        else if (current is AssignationChartElement)
                        {
                            AssignationChartElement assignChart = (AssignationChartElement)current;
                            if (assignChart.Name == "" || assignChart.Expression == "")
                            {
                                throw new InvalidOperationException("Invalid assignation chart element.");
                            }
                            string where = ParseSensorData(assignChart.Name);
                            string preExpression = ParseSensorData(((AssignationChartElement)current).Expression).ToLower();
                            string what          = Evaluator.Evaluate(preExpression);

                            Evaluator.Assign(where, what);
                        }

                        foreach (var connection in current.Connections)
                        {
                            if (connection.To != null)
                            {
                                current = connection.To.ChartElement;
                            }
                            else
                            {
                                throw new InvalidOperationException("Unconnected chart element. Check your chart connections.");
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                if (!(exception is ThreadAbortException))
                {
                    OnError(exception); // Lanza el evento Error con el mensaje incluido.
                    if (current != null)
                    {
                        current.BackColor = Color.Red; // Cambia el color del chartElement actual para permitir al usuario conocer la fuente del error.
                    }
                    Thread.CurrentThread.Abort();      // Termina la ejecucion del hilo actual.
                }
            }
        }