Exemplo n.º 1
0
        private void simulation_Execute(object sender, ExecuteEventArgs e)
        {
            UnmarkSimulationParticpants();

            foreach (var tuple in e.Tuples)
            {
                int sourceId = tuple.Item1;
                int destId   = tuple.Item2;

                if (!NodeIdMap.ContainsKey(sourceId) || !NodeIdMap.ContainsKey(destId))
                {
                    continue;
                }

                Node source = null, dest = null;
                NodeIdMap.TryGetValue(sourceId, out source);
                NodeIdMap.TryGetValue(destId, out dest);

                Edge        connector = null;
                List <Edge> edges;
                Nodes.TryGetValue(source, out edges);
                foreach (var edge in edges)
                {
                    if (edge.SourceNode == dest || edge.DestinationNode == dest)
                    {
                        connector = edge;
                        break;
                    }
                }

                if (connector == null)
                {
                    MessageBox.Show(String.Format("There is no edge connecting nodes with ids \"{0}\" and \"{1}\"", sourceId, destId), "Error",
                                    System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    continue;
                }

                if (source == SimulationServerNode)
                {
                    MessageBox.Show("Server node can't send information!", "Error",
                                    System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    continue;
                }

                List <int> sourceData;
                SimulationData.TryGetValue(sourceId, out sourceData);
                if (sourceData.Count == 0)
                {
                    MessageBox.Show(String.Format("Node with id \"{0}\" doesn't have any information to send", sourceId), "Error",
                                    System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    continue;
                }

                List <int> destData;
                SimulationData.TryGetValue(destId, out destData);

                var data = sourceData[sourceData.Count - 1];
                sourceData.RemoveAt(sourceData.Count - 1);
                destData.Add(data);

                dest.Color      = MarkerColor;
                connector.Color = MarkerColor;
                SimulationMarkedNodes.Push(dest);
                SimulationMarkedEdges.Push(connector);

                // move data
            }
        }
        private void next_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentBlockIndex < BlockCount - 2)
            {
                // CurrentBlock is current from previous iteration
                HighlightBlock(CurrentBlock, ClearHighlightBrush);

                CurrentBlock = CurrentBlock.NextBlock;
                CurrentBlockIndex++;  // time slot


                List <Tuple <int, int> > tuples = new List <Tuple <int, int> >();

                String blockText        = GetBlockText(CurrentBlock);
                String regex            = "^([0-9]+):( [0-9]+ -> [0-9]+,)* [0-9]+ -> [0-9]+$";
                String tupleRegexString = " ?([0-9]+) -> ([0-9]+),?";

                bool   success      = true;
                String errorMessage = "";

                Match match = Regex.Match(blockText, regex);
                if (match.Success && match.Groups.Count > 1)
                {
                    bool timeMatched = false;
                    int  time;
                    Int32.TryParse(match.Groups[1].Value, out time);
                    if (time == CurrentBlockIndex)
                    {
                        timeMatched = true;
                    }

                    if (timeMatched == true)
                    {
                        Regex tupleRegex = new Regex(tupleRegexString, RegexOptions.Compiled);
                        foreach (Match tupleMatch in tupleRegex.Matches(blockText))
                        {
                            if (tupleMatch.Success && tupleMatch.Groups.Count >= 2)
                            {
                                int value1, value2;
                                Int32.TryParse(tupleMatch.Groups[1].Value, out value1);
                                Int32.TryParse(tupleMatch.Groups[2].Value, out value2);

                                int missingValue = -1;
                                if (!NodeList.Contains(value1))
                                {
                                    missingValue = value1;
                                }
                                else if (!NodeList.Contains(value2))
                                {
                                    missingValue = value2;
                                }

                                if (missingValue == -1)
                                {
                                    tuples.Add(new Tuple <int, int>(value1, value2));
                                }
                                else
                                {
                                    success      = false;
                                    errorMessage = String.Format("Node with id \"{0}\" does not exist", missingValue);
                                    break;
                                }
                            }
                        }
                    }
                    else
                    {
                        success      = false;
                        errorMessage = String.Format("Failed to parse time id \"{0}\".\nTime slots should be numbered sequentialy starting from 0.", time);
                    }
                }
                else
                {
                    success = false;
                }

                if (!success)
                {
                    SetupErrorState();
                    ShowErrorMessage(errorMessage);
                    return;
                }

                HighlightBlock(CurrentBlock, GreenHighlightBrush);

                ExecuteEventArgs args = new ExecuteEventArgs(tuples);
                Execute(this, args);
            }

            if (CurrentBlockIndex == BlockCount - 2)
            {
                nextBtn.IsEnabled = false;
            }
        }