private void RemoveTokensFromCanvas(VPlace place)
 {
     foreach (var token in place.TokensList)
     {
         MainModelCanvas.Children.Remove(token);
     }
 }
        public void AddFourTokens(VPlace marked)
        {
            RemoveTokens(marked);
            Ellipse token1 = new Ellipse();
            Ellipse token2 = new Ellipse();
            Ellipse token3 = new Ellipse();
            Ellipse token4 = new Ellipse();

            marked.TokensList.Add(token1);
            marked.TokensList.Add(token2);
            marked.TokensList.Add(token3);
            marked.TokensList.Add(token4);
            SetPropertiesForTokens(marked, 7);
            Canvas.SetLeft(token1, marked.CoordX + 6.5);
            Canvas.SetTop(token1, marked.CoordY + 8);
            MainModelCanvas.Children.Add(token1);
            Canvas.SetLeft(token2, marked.CoordX + 16.5);
            Canvas.SetTop(token2, marked.CoordY + 8);
            MainModelCanvas.Children.Add(token2);
            Canvas.SetLeft(token3, marked.CoordX + 6.5);
            Canvas.SetTop(token3, marked.CoordY + 16);
            MainModelCanvas.Children.Add(token3);
            Canvas.SetLeft(token4, marked.CoordX + 16.5);
            Canvas.SetTop(token4, marked.CoordY + 16);
            MainModelCanvas.Children.Add(token4);
        }
Beispiel #3
0
 private static string GenerateLatexPlace(VPlace place)
 {
     return(@"\node [place,label=-90:" + place.Label + ", tokens=" + ((place.NumberOfTokens > 4) ? 0 : place.NumberOfTokens) + "] (" + place.Id + ") at (" + Convert.ToString((place.CoordX / koefX),
                                                                                                                                                                              CultureInfo.GetCultureInfo("en-US")) + "," +
            Convert.ToString((-(place.CoordY / koefY)), CultureInfo.GetCultureInfo("en-US")) + ") {" + ((place.NumberOfTokens <= 4) ? "" :
                                                                                                        place.NumberOfTokens.ToString()) + "}" + AddAllArcs(place) + ";");
 }
 public void AddFiveTokens(VPlace marked, Label number)
 {
     RemoveTokens(marked);
     MainModelCanvas.Children.Remove(number);
     number.Content = marked.NumberOfTokens;
     Canvas.SetLeft(number, marked.CoordX + 7);
     Canvas.SetTop(number, marked.CoordY + 2);
     MainModelCanvas.Children.Add(number);
 }
 public void SetPropertiesForTokens(VPlace placeWithTokens, int width)
 {
     foreach (Ellipse ellipse in placeWithTokens.TokensList)
     {
         ellipse.Width      = width;
         ellipse.Height     = width;
         ellipse.Fill       = Brushes.Black;
         ellipse.Visibility = Visibility.Visible;
     }
 }
        public void AddOneToken(VPlace place)
        {
            RemoveTokens(place);
            Ellipse token = new Ellipse();

            place.TokensList.Add(token);
            SetPropertiesForTokens(place, 10);
            Canvas.SetLeft(token, place.CoordX + 10);
            Canvas.SetTop(token, place.CoordY + 10);
            MainModelCanvas.Children.Add(token);
        }
Beispiel #7
0
        public void RemoveNode(Node node)
        {
            VPlace place = node as VPlace;

            if (place != null)
            {
                PNEditorControl.Net.places.Remove(place);
            }
            else
            {
                PNEditorControl.Net.transitions.Remove(node as VTransition);
            }
        }
        public void AddTwoTokens(VPlace place)
        {
            RemoveTokens(place);
            Ellipse token1 = new Ellipse();
            Ellipse token2 = new Ellipse();

            place.TokensList.Add(token1);
            place.TokensList.Add(token2);
            SetPropertiesForTokens(place, 7);
            Canvas.SetLeft(token1, place.CoordX + 6.5);
            Canvas.SetTop(token1, place.CoordY + 11.5);
            MainModelCanvas.Children.Add(token1);
            Canvas.SetLeft(token2, place.CoordX + 16.5);
            Canvas.SetTop(token2, place.CoordY + 11.5);
            MainModelCanvas.Children.Add(token2);
        }
 public void AddFrom6To999Tokens(VPlace marked, Label number)
 {
     RemoveTokens(marked);
     MainModelCanvas.Children.Remove(number);
     number.Content = marked.NumberOfTokens;
     if (marked.NumberOfTokens < 10)
     {
         Canvas.SetLeft(number, marked.CoordX + 6);
         Canvas.SetTop(number, marked.CoordY + 2);
     }
     else
     {
         Canvas.SetLeft(number, marked.CoordX + 1);
         Canvas.SetTop(number, marked.CoordY + 2);
     }
     MainModelCanvas.Children.Add(number);
 }
Beispiel #10
0
        private static string AddAllArcs(VPlace place)
        {
            string s = "";

            foreach (var i in place.ThisArcs)
            {
                if (i.From.Id == place.Id)
                {
                    s += "\n\t edge[post] node[auto,swap] {" + ((i.Weight == "1") ? "" : i.Weight) + "} (" + i.To.Id + ")";
                }
                else
                {
                    s += "\n\t edge[pre] node[auto,swap] {" + ((i.Weight == "1") ? "" : i.Weight) + "} (" + i.From.Id + ")";
                }
            }

            return(s);
        }
Beispiel #11
0
        /// <summary>
        // Нормирование
        /// </summary>
        public static void Normalize()
        {
            TrList.Clear();
            PlList.Clear();
            //foreach (Transition transition in Transition.transitions)
            foreach (VTransition transition in PNEditorControl.Net.transitions)
            {
                minX = Math.Min(minX, transition.CoordX);
                minY = Math.Min(minY, transition.CoordY);
                maxX = Math.Max(maxX, transition.CoordX);
                maxY = Math.Max(maxY, transition.CoordY);
            }
            //foreach (Place place in Place.places)
            foreach (VPlace place in PNEditorControl.Net.places)
            {
                minX = Math.Min(minX, place.CoordX);
                minY = Math.Min(minY, place.CoordY);
                maxX = Math.Max(maxX, place.CoordX);
                maxY = Math.Max(maxY, place.CoordY);
            }

            //foreach (Place p in Place.places)
            foreach (VPlace p in PNEditorControl.Net.places)
            {
                //Place place = new Place(p.CoordX - minX, p.CoordY - minY - 10);
                var place = VPlace.Create(p.CoordX - minX, p.CoordY - minY - 10);
                place.Id             = p.Id;
                place.Label          = p.Label;
                place.NumberOfTokens = p.NumberOfTokens;
                place.ThisArcs       = p.ThisArcs;
                PlList.Add(place);
            }

            //foreach (Transition t in Transition.transitions)
            foreach (VTransition t in PNEditorControl.Net.transitions)
            {
                //Transition transition = new Transition(t.CoordX - minX, t.CoordY - minY);
                var transition = VTransition.Create(t.CoordX - minX, t.CoordY - minY);
                transition.Id       = t.Id;
                transition.Label    = t.Label;
                transition.ThisArcs = t.ThisArcs;
                TrList.Add(transition);
            }
        }
Beispiel #12
0
        public void RemoveNode(PetriNetNode node)
        {
            node.IsSelect = false;
            VPlace place = node as VPlace;

            if (place != null)
            {
                //Net.places.Remove(place);
                VPlace selPlace = place;
                MainModelCanvas.Children.Remove(selPlace.NumberOfTokensLabel);
                while (selPlace.TokensList.Count != 0)
                {
                    MainModelCanvas.Children.Remove(selPlace.TokensList[0]);
                    selPlace.TokensList.RemoveAt(0);
                }
            }
            // else
            //   Net.transitions.Remove(selected as Transition);

            DeleteArcs(node.ThisArcs);
            //selected.ThisArcs.Clear();

            var objectToRemove = GetKeyByValueForFigures(node);

            _allFiguresObjectReferences.Remove(objectToRemove);

            Shape shapeToRemove = objectToRemove as Shape;

            if (shapeToRemove != null)
            {
                shapeToRemove.Stroke = Brushes.Black;
                MainModelCanvas.Children.Remove(shapeToRemove);
            }

            Label labelForRemove;

            NodesToLabelsInCanvas.TryGetValue(node, out labelForRemove);
            MainModelCanvas.Children.Remove(labelForRemove);

            Net.RemoveNode(node);
        }
Beispiel #13
0
 public void AddMoreThan1000Tokens(VPlace marked, Label number)
 {
     RemoveTokens(marked);
     MainModelCanvas.Children.Remove(number);
     if (marked.NumberOfTokens % 1000 == 0)
     {
         number.Content = marked.NumberOfTokens / 1000 + "k";
         Canvas.SetLeft(number, marked.CoordX + 2);
         Canvas.SetTop(number, marked.CoordY + 2);
     }
     else if (marked.NumberOfTokens < 10000)
     {
         double temp = marked.NumberOfTokens % 100;
         if (temp < 50)
         {
             number.Content = "~" + (marked.NumberOfTokens - temp) / 1000 + "k";
         }
         else
         {
             number.Content = "~" + (marked.NumberOfTokens - temp + 100) / 1000 + "k";
         }
         Canvas.SetLeft(number, marked.CoordX - 4);
         Canvas.SetTop(number, marked.CoordY + 2);
     }
     else
     {
         if (marked.NumberOfTokens % 1000 < 500)
         {
             number.Content = "~" + marked.NumberOfTokens / 1000 + "k";
         }
         else
         {
             number.Content = "~" + (marked.NumberOfTokens + 1000) / 1000 + "k";
         }
         Canvas.SetLeft(number, marked.CoordX - 2);
         Canvas.SetTop(number, marked.CoordY + 2);
     }
     MainModelCanvas.Children.Add(number);
 }
Beispiel #14
0
        public static List <VArc> OpenGraphMl(XmlDocument graphmlDoc, out bool exc)
        {
            var returnArcs = new List <VArc>();

            keys.Clear();
            exc = false;
            XmlNode graph = null;

            if (graphmlDoc.DocumentElement.LastChild.Name == "graph")
            {
                graph = graphmlDoc.DocumentElement.LastChild;
            }

            for (var i = 0; i < graphmlDoc.DocumentElement.ChildNodes.Count; i++)
            {
                if (graphmlDoc.DocumentElement.ChildNodes[i].Name != "key")
                {
                    continue;
                }
                string keyId = null, keyValue = null;
                for (var j = 0; j < graphmlDoc.DocumentElement.ChildNodes[i].Attributes.Count; j++)
                {
                    switch (graphmlDoc.DocumentElement.ChildNodes[i].Attributes[j].Name)
                    {
                    case "id":
                        keyId = graphmlDoc.DocumentElement.ChildNodes[i].Attributes[j].Value;
                        break;

                    case "attr.name":
                        keyValue = graphmlDoc.DocumentElement.ChildNodes[i].Attributes[j].Value;
                        break;
                    }
                }
                if (!keys.ContainsKey(keyId))
                {
                    keys.Add(keyId, keyValue);
                }
            }

            bool isArcsDirectedByDefault = false;

            if (graph == null)
            {
                MessageBox.Show("Node 'graph' is missed, check the file");
                exc = true;
                return(returnArcs);
            }
            else if (graph.LastChild.Name == "edgedefault")
            {
                if (graph.LastChild.Value == "directed")
                {
                    isArcsDirectedByDefault = true;
                }
            }
            XmlNodeList graphObjects = graph.ChildNodes;

            if (graphObjects.Count == 0)
            {
                MessageBox.Show("There are no nodes in the graph, check the file");
                exc = true;
                return(returnArcs);
            }

            foreach (XmlNode node in graphObjects)
            {
                var id = "";

                if (node.Name == "node")
                {
                    for (int i = 0; i < node.Attributes.Count; i++)
                    {
                        if (node.Attributes[i].Name == "id")
                        {
                            id = node.Attributes[i].Value;
                        }
                    }

                    VPlace place = VPlace.Create(0, 0);
                    place.Id = id;
                    PNEditorControl.Net.places.Add(place);
                    //SetOfFigures.Figures.Add(place);
                }
                else if (node.Name == "edge")
                {
                }
                else
                {
                    MessageBox.Show("The file contains element with foreign name, it will be ignored, check the file");
                }
            }

            if (PNEditorControl.Net.Nodes.Count == 0)
            {
                MessageBox.Show("There are no nodes in the graph, check the file");
                exc = true;
                return(returnArcs);
            }
            else
            {
                foreach (XmlNode node in graphObjects)
                {
                    if (node.Name != "edge")
                    {
                        continue;
                    }
                    PetriNetNode from = null, to = null;
                    string       fromId = null, toId = null, id = null, directed = null;

                    for (int i = 0; i < node.Attributes.Count; i++)
                    {
                        switch (node.Attributes[i].Name)
                        {
                        case "source":
                            fromId = node.Attributes[i].Value;
                            break;

                        case "target":
                            toId = node.Attributes[i].Value;
                            break;

                        case "id":
                            id = node.Attributes[i].Value;
                            break;

                        case "directed":
                            directed = node.Attributes[i].Value;
                            break;
                        }
                    }
                    double weight = 0;
                    if (node.FirstChild != null)
                    {
                        var data = node.FirstChild;
                        for (var i = 0; i < data.Attributes.Count; i++)
                        {
                            if (data.Attributes[i].Name != "key")
                            {
                                continue;
                            }
                            string weightValue;
                            keys.TryGetValue(data.Attributes[i].Value, out weightValue);
                            if (weightValue == "weight")
                            {
                                double.TryParse(data.InnerText.Replace('.', ','), out weight);
                            }
                        }
                    }

                    foreach (var figure in PNEditorControl.Net.Nodes)
                    {
                        if (figure.Id == fromId)
                        {
                            from = figure;
                        }
                        else if (figure.Id == toId)
                        {
                            to = figure;
                        }
                    }

                    var arc = new VArc(from, to)
                    {
                        Weight = weight > 1 ? weight.ToString(CultureInfo.InvariantCulture) : "1",
                        Id     = id
                    };
                    if (directed != null)
                    {
                        arc.IsDirected = directed == "true";
                    }
                    else
                    {
                        arc.IsDirected = isArcsDirectedByDefault;
                    }

                    returnArcs.Add(arc);
                }
            }
            return(returnArcs);
        }
Beispiel #15
0
 public AddTokensCommand(VPlace marked, int oldNum, int newNum)
 {
     markedPlace = marked;
     oldNumber   = oldNum;
     newNumber   = newNum;
 }
Beispiel #16
0
        private void btnPaste_Click(object sender, RoutedEventArgs e)
        {
            btnUndo.IsEnabled = true;
            _copies.Clear();
            if (cuttedOrCopiedFigures.Count == 0)
            {
                nothingToPaste = true;
            }
            if (nothingToPaste == false)
            {
                double minX = cuttedOrCopiedFigures[0].CoordX, minY = cuttedOrCopiedFigures[0].CoordY;

                foreach (PetriNetNode figure in cuttedOrCopiedFigures)
                {
                    if (figure.CoordX < minX)
                    {
                        minX = figure.CoordX;
                    }
                    if (figure.CoordY < minY)
                    {
                        minY = figure.CoordY;
                    }
                }

                if (cutOrCopy == cutOrCopyMode.copy)
                {
                    var copiedF = new List <PetriNetNode>();
                    var copiedA = new List <VArc>();

                    UnselectFigures();//(cuttedOrCopiedFigures, cuttedOrCopiedArcs);

                    foreach (PetriNetNode copiedFigure in cuttedOrCopiedFigures)
                    {
                        var x = copiedFigure.CoordX - minX + ScrollViewerForMainModelCanvas.HorizontalOffset + 40;
                        var y = copiedFigure.CoordY - minY + ScrollViewerForMainModelCanvas.VerticalOffset + 40;

                        PetriNetNode newFigure;
                        if (copiedFigure is VPlace)
                        {
                            newFigure = VPlace.Create(x, y);
                            Net.places.Add((VPlace)newFigure);
                        }
                        else
                        {
                            newFigure = VTransition.Create(x, y);
                            Net.transitions.Add((VTransition)newFigure);
                        }
                        //SetOfFigures.Figures.Add(newFigure);
                        copiedF.Add(newFigure);

                        newFigure.Label = copiedFigure.Label;
                        VPlace place = newFigure as VPlace;
                        if (place != null)
                        {
                            place.NumberOfTokens = (copiedFigure as VPlace).NumberOfTokens;
                        }
                        newFigure.IsSelect = true;

                        DrawFigure(newFigure);
                        _copies.Add(copiedFigure, newFigure);
                        MakeSelected(newFigure);
                    }


                    foreach (PetriNetNode fig in cuttedOrCopiedFigures)
                    {
                        foreach (var arc in fig.ThisArcs)
                        {
                            if (!cuttedOrCopiedArcs.Contains(arc))
                            {
                                continue;
                            }

                            PetriNetNode fromF;
                            _copies.TryGetValue(arc.From, out fromF);
                            PetriNetNode toF;
                            _copies.TryGetValue(arc.To, out toF);
                            var arcs = from ar in Net.arcs where ar.From == fromF && ar.To == toF select ar;

                            if (arcs.Any())
                            {
                                continue;
                            }

                            var newArc = new VArc(fromF, toF)
                            {
                                IsDirected = arc.IsDirected,
                                Weight     = arc.Weight
                            };
                            newArc.AddToThisArcsLists();
                            DrawArc(newArc);

                            if (newArc.IsDirected)
                            {
                                var lineVisible = GetKeyByValueForArcs(newArc, DictionaryForArcs);
                                DrawArrowHeads(lineVisible);
                                GetKeyByValueForArcs(newArc, DictionaryForArrowHeads1).MouseDown += MouseArcDown;
                                GetKeyByValueForArcs(newArc, DictionaryForArrowHeads2).MouseDown += MouseArcDown;
                            }

                            newArc.IsSelect = true;
                            _selectedArcs.Add(newArc);
                            ColorArrow(newArc);
                            Net.arcs.Add(newArc);
                            copiedA.Add(newArc);
                        }
                    }
                    var newCommand = new PasteCommand(copiedF, copiedA);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (cutOrCopy == cutOrCopyMode.cut && _numberOfPastes == 0)
                {
                    _numberOfPastes++;
                    foreach (var figure in cuttedOrCopiedFigures)
                    {
                        foreach (var arc in figure.ThisArcs)
                        {
                            if (!cuttedOrCopiedArcs.Contains(arc))
                            {
                                cuttedOrCopiedArcs.Add(arc);
                            }
                        }
                        figure.CoordX += -minX + ScrollViewerForMainModelCanvas.HorizontalOffset;
                        figure.CoordY += -minY + ScrollViewerForMainModelCanvas.VerticalOffset;
                    }

                    var toDelete = PasteFiguresAndArcs(cuttedOrCopiedFigures, cuttedOrCopiedArcs);
                    foreach (var arc in toDelete)
                    {
                        cuttedOrCopiedArcs.Remove(arc);
                        arc.IsSelect = false;
                        _selectedArcs.Remove(arc);
                        Net.arcs.Remove(arc);
                    }

                    var figures = new List <PetriNetNode>();
                    var arcs1   = new List <VArc>();
                    figures.AddRange(cuttedOrCopiedFigures);
                    arcs1.AddRange(cuttedOrCopiedArcs);

                    var newCommand = new PasteCommand(figures, arcs1);
                    Command.ExecutedCommands.Push(newCommand);
                }
                Command.CanceledCommands.Clear();
            }
            _figuresBeforeDrag = CopyListOfFigures(Net.Nodes);
            ReassignSelectedProperties();
            TurnOnSelectMode();
            btnPaste.Focusable = false;
        }
        private void btnRedo_Click(object sender, RoutedEventArgs e)
        {
            btnSelect.IsEnabled = true;
            if (Command.CanceledCommands.Count != 0)
            {
                Command lastCommand = Command.CanceledCommands.Pop();
                if (lastCommand is AddFigureCommand)
                {
                    PetriNetNode currentFigure = (lastCommand as AddFigureCommand).newFigure;
                    Shape        shape         = (lastCommand as AddFigureCommand).shape;

                    if (currentFigure.IsSelect)
                    {
                        currentFigure.IsSelect = false;
                    }
                    if ((lastCommand as AddFigureCommand).newFigure is VPlace)
                    {
                        //Place.places.Add(currentFigure as Place);
                        Net.places.Add(currentFigure as VPlace);
                    }
                    else
                    {
                        //Transition.transitions.Add(currentFigure as Transition);
                        Net.transitions.Add(currentFigure as VTransition);
                    }
                    //SetOfFigures.Figures.Add(currentFigure);

                    Canvas.SetLeft(shape, currentFigure.CoordX);
                    Canvas.SetTop(shape, currentFigure.CoordY);
                    MainModelCanvas.Children.Add(shape);

                    _allFiguresObjectReferences.Add(shape, currentFigure);

                    AddFigureCommand newCommand = new AddFigureCommand(currentFigure, shape);
                    Command.ExecutedCommands.Push(newCommand);
                    btnUndo.IsEnabled = true;
                }
                else if (lastCommand is AddArcCommand)
                {
                    VArc newArc = (lastCommand as AddArcCommand).newArc;
                    if (newArc.IsSelect)
                    {
                        newArc.IsSelect = false;
                    }
                    Net.arcs.Add(newArc);
                    newArc.AddToThisArcsLists();

                    DrawArc(newArc);
                    RedrawArrowHeads(newArc);

                    AddArcCommand newCommand = new AddArcCommand(newArc);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is AddTokensCommand)
                {
                    PetriNetNode figure = (lastCommand as AddTokensCommand).markedPlace;

                    VPlace place = (VPlace)figure;
                    place.NumberOfTokens = (lastCommand as AddTokensCommand).oldNumber;

                    if (place.NumberOfTokens == 0)
                    {
                        RemoveTokens(place);
                    }
                    if (place.NumberOfTokens >= 0 && place.NumberOfTokens < 5)
                    {
                        MainModelCanvas.Children.Remove(place.NumberOfTokensLabel);
                    }
                    AddTokens(place);

                    int temp = (lastCommand as AddTokensCommand).oldNumber;
                    (lastCommand as AddTokensCommand).oldNumber = (lastCommand as AddTokensCommand).newNumber;
                    (lastCommand as AddTokensCommand).newNumber = temp;

                    AddTokensCommand newCommand = new AddTokensCommand(place, (lastCommand as AddTokensCommand).oldNumber,
                                                                       (lastCommand as AddTokensCommand).newNumber);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is ChangeNameCommand)
                {
                    PetriNetNode namedFigure = (lastCommand as ChangeNameCommand).namedFigure;
                    namedFigure.Label = (lastCommand as ChangeNameCommand).oldName;

                    ChangeLabel(namedFigure, (lastCommand as ChangeNameCommand).oldName);

                    string temp = (lastCommand as ChangeNameCommand).oldName;
                    (lastCommand as ChangeNameCommand).oldName = (lastCommand as ChangeNameCommand).newName;
                    (lastCommand as ChangeNameCommand).newName = temp;

                    ChangeNameCommand newCommand = new ChangeNameCommand(namedFigure, (lastCommand as ChangeNameCommand).oldName,
                                                                         (lastCommand as ChangeNameCommand).newName);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is DragCommand)
                {
                    foreach (PetriNetNode figure in Net.Nodes)
                    {
                        foreach (PetriNetNode afterDragFigure in (lastCommand as DragCommand).figuresAfterDrag)
                        {
                            if (figure.Id == afterDragFigure.Id)
                            {
                                figure.CoordX = afterDragFigure.CoordX;
                                figure.CoordY = afterDragFigure.CoordY;
                            }
                        }
                    }

                    //List<PetriNetNode> newList = SetOfFigures.Figures;

                    foreach (PetriNetNode figure in Net.Nodes)//newList)
                    {
                        MoveFigure(figure);
                    }

                    DragCommand newCommand = new DragCommand((lastCommand as DragCommand).figuresBeforeDrag,
                                                             (lastCommand as DragCommand).figuresAfterDrag);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is DeleteCommand)
                {
                    List <PetriNetNode> figures = (lastCommand as DeleteCommand).deletedFigures;
                    List <VArc>         arcs    = (lastCommand as DeleteCommand).deletedArcs;
                    DeleteFigures(figures, arcs);
                    DeleteCommand newCommand = new DeleteCommand(figures, arcs);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is ChangeWeightCommand)
                {
                    VArc arc = (lastCommand as ChangeWeightCommand).arc;
                    MainModelCanvas.Children.Remove((lastCommand as ChangeWeightCommand).oldWeightLabel);

                    arc.Weight = (lastCommand as ChangeWeightCommand).newWeight;
                    (lastCommand as ChangeWeightCommand).newWeightLabel.Content = arc.Weight;

                    Canvas.SetLeft((lastCommand as ChangeWeightCommand).newWeightLabel, (arc.From.CoordX + arc.To.CoordX) / 2);
                    Canvas.SetTop((lastCommand as ChangeWeightCommand).newWeightLabel, (arc.From.CoordY + arc.To.CoordY) / 2 - 5);
                    MainModelCanvas.Children.Add((lastCommand as ChangeWeightCommand).newWeightLabel);

                    arc.WeightLabel = (lastCommand as ChangeWeightCommand).newWeightLabel;

                    ChangeWeightCommand newCommand = new ChangeWeightCommand(arc, (lastCommand as ChangeWeightCommand).oldWeight,
                                                                             arc.Weight, (lastCommand as ChangeWeightCommand).oldWeightLabel, arc.WeightLabel);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is CutCommand)
                {
                    cuttedOrCopiedArcs.Clear();
                    cuttedOrCopiedFigures.Clear();

                    List <PetriNetNode> figures  = (lastCommand as CutCommand).cuttedFigures;
                    List <PetriNetNode> old      = (lastCommand as CutCommand).oldFigures;
                    List <VArc>         expArcs  = (lastCommand as CutCommand).explicitlyCuttedArcs;
                    List <VArc>         implArcs = (lastCommand as CutCommand).implicitlyCuttedArcs;

                    cuttedOrCopiedFigures.AddRange(figures);
                    cuttedOrCopiedArcs.AddRange(expArcs);

                    List <PetriNetNode> cutF = new List <PetriNetNode>();
                    List <VArc>         cutA = new List <VArc>();
                    cutF.AddRange(cuttedOrCopiedFigures);
                    cutA.AddRange(implArcs);

                    CutFigures(cutF, cutA);
                    _selectedFigures.Clear();
                    _selectedArcs.Clear();
                    ReassignSelectedProperties();

                    CutCommand newCommand = new CutCommand(figures, old, expArcs, implArcs);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is PasteCommand)
                {
                    if (nothingToPaste == false)
                    {
                        List <PetriNetNode> figuresToPaste = (lastCommand as PasteCommand).pastedfigures;
                        List <VArc>         arcsToPaste    = (lastCommand as PasteCommand).pastedArcs;

                        double minX = figuresToPaste[0].CoordX, minY = figuresToPaste[0].CoordY;

                        foreach (PetriNetNode figure in figuresToPaste)
                        {
                            if (figure.CoordX < minX)
                            {
                                minX = figure.CoordX;
                            }
                            if (figure.CoordY < minY)
                            {
                                minY = figure.CoordY;
                            }
                        }

                        if (cutOrCopy == cutOrCopyMode.copy)
                        {
                            UnselectFigures();//(selectedFigures, selectedArcs);
                        }

                        foreach (PetriNetNode figure in figuresToPaste)
                        {
                            if (cutOrCopy == cutOrCopyMode.copy)
                            {
                                figure.DetectIdMatches(Net.Nodes);
                            }
                            figure.CoordX += -minX + ScrollViewerForMainModelCanvas.HorizontalOffset;
                            figure.CoordY += -minY + ScrollViewerForMainModelCanvas.VerticalOffset;
                        }
                        PasteCommand newCommand = new PasteCommand(figuresToPaste, arcsToPaste);
                        Command.ExecutedCommands.Push(newCommand);

                        if (cutOrCopy == cutOrCopyMode.cut)
                        {
                            arcsToPaste.RemoveAll(ShouldBeDeleted);
                        }

                        PasteFiguresAndArcs(figuresToPaste, arcsToPaste);
                    }
                }
            }

            EnableUndoRedoButtons();
            ReassignSelectedProperties();
            if (Command.CanceledCommands.Count == 0)
            {
                btnRedo.IsEnabled = false;
            }
            if (Command.ExecutedCommands.Count > 0)
            {
                btnUndo.IsEnabled = true;
            }
            TurnOnSelectMode();
        }
        private void btnUndo_Click(object sender, RoutedEventArgs e)
        {
            btnSelect.IsEnabled = true;
            if (Command.ExecutedCommands.Count != 0)
            {
                Command lastCommand = Command.ExecutedCommands.Peek();
                if (lastCommand is AddFigureCommand)
                {
                    PetriNetNode thisFigure = (lastCommand as AddFigureCommand).newFigure;
                    EnableAddButtons();

                    RemoveNode(thisFigure);

                    if (_selectedFigures.Contains(thisFigure))
                    {
                        _selectedFigures.Remove(thisFigure);
                        _selectedFigure = null;
                    }
                }
                else if (lastCommand is AddArcCommand)
                {
                    VArc thisArc = (lastCommand as AddArcCommand).newArc;
                    EnableAddButtons();

                    List <VArc> arcs = new List <VArc>();
                    arcs.Add(thisArc);
                    DeleteArcs(arcs);

                    if (_selectedArcs.Contains(thisArc))
                    {
                        _selectedArcs.Remove(thisArc);
                        _selectedArc = null;
                    }
                }
                else if (lastCommand is AddTokensCommand)
                {
                    PetriNetNode figure = (lastCommand as AddTokensCommand).markedPlace;
                    VPlace       place  = (VPlace)figure;
                    place.NumberOfTokens = (lastCommand as AddTokensCommand).oldNumber;


                    if (place.NumberOfTokens == 0)
                    {
                        RemoveTokens(place);
                    }
                    if (place.NumberOfTokens >= 0 && place.NumberOfTokens < 5)
                    {
                        MainModelCanvas.Children.Remove(place.NumberOfTokensLabel);
                    }

                    AddTokens(place);

                    int temp = (lastCommand as AddTokensCommand).oldNumber;
                    (lastCommand as AddTokensCommand).oldNumber = (lastCommand as AddTokensCommand).newNumber;
                    (lastCommand as AddTokensCommand).newNumber = temp;
                }
                else if (lastCommand is ChangeNameCommand)
                {
                    PetriNetNode namedFigure = (lastCommand as ChangeNameCommand).namedFigure;
                    namedFigure.Label = (lastCommand as ChangeNameCommand).oldName;

                    ChangeLabel(namedFigure, (lastCommand as ChangeNameCommand).oldName);

                    string temp = (lastCommand as ChangeNameCommand).oldName;
                    (lastCommand as ChangeNameCommand).oldName = (lastCommand as ChangeNameCommand).newName;
                    (lastCommand as ChangeNameCommand).newName = temp;
                }
                else if (lastCommand is DragCommand)
                {
                    foreach (PetriNetNode figure in Net.Nodes)
                    {
                        foreach (PetriNetNode f in (lastCommand as DragCommand).figuresBeforeDrag)
                        {
                            if (figure.Id == f.Id)
                            {
                                figure.CoordX = f.CoordX;
                                figure.CoordY = f.CoordY;
                            }
                        }
                    }

                    //List<PetriNetNode> newList = SetOfFigures.Figures;

                    foreach (PetriNetNode figure in Net.Nodes)//newlist
                    {
                        MoveFigure(figure);
                    }
                }
                else if (lastCommand is DeleteCommand)
                {
                    var restoredFigures = (lastCommand as DeleteCommand).deletedFigures;
                    var restoredArcs    = (lastCommand as DeleteCommand).deletedArcs;

                    //SetOfFigures.Figures.AddRange(restoredFigures);
                    foreach (PetriNetNode node in restoredFigures)
                    {
                        if (node is VPlace)
                        {
                            Net.places.Add(node as VPlace);
                        }
                        else
                        {
                            Net.transitions.Add(node as VTransition);
                        }
                    }
                    Net.arcs.AddRange(restoredArcs);

                    foreach (var figure in Net.Nodes)
                    {
                        if (restoredFigures.Contains(figure))
                        {
                            DrawFigure(figure);
                        }
                    }
                    foreach (var arc in Net.arcs)
                    {
                        if (restoredArcs.Contains(arc))
                        {
                            DisplayArc(arc);
                        }
                    }
                }
                else if (lastCommand is ChangeWeightCommand)
                {
                    VArc arc = (lastCommand as ChangeWeightCommand).arc;
                    MainModelCanvas.Children.Remove((lastCommand as ChangeWeightCommand).newWeightLabel);
                    arc.Weight      = (lastCommand as ChangeWeightCommand).oldWeight;
                    arc.WeightLabel = (lastCommand as ChangeWeightCommand).oldWeightLabel;


                    if (arc.Weight != "1")
                    {
                        (lastCommand as ChangeWeightCommand).oldWeightLabel.Content = arc.Weight;
                        Canvas.SetLeft((lastCommand as ChangeWeightCommand).oldWeightLabel,
                                       (arc.From.CoordX + arc.To.CoordX) / 2);
                        Canvas.SetTop((lastCommand as ChangeWeightCommand).oldWeightLabel,
                                      (arc.From.CoordY + arc.To.CoordY) / 2 - 5);
                        MainModelCanvas.Children.Add((lastCommand as ChangeWeightCommand).oldWeightLabel);
                    }
                }
                else if (lastCommand is CutCommand)
                {
                    List <PetriNetNode> cuttedFigures = (lastCommand as CutCommand).cuttedFigures;
                    List <PetriNetNode> old           = (lastCommand as CutCommand).oldFigures;
                    List <VArc>         cuttedArcs    = (lastCommand as CutCommand).implicitlyCuttedArcs;

                    //SetOfFigures.Figures.AddRange(cuttedFigures);
                    Net.arcs.AddRange(cuttedArcs);
                    foreach (PetriNetNode figure in cuttedFigures)//SetOfFigures.Figures)
                    {
                        //if (cuttedFigures.Contains(figure))
                        {
                            DrawFigure(figure);
                            figure.IsSelect = true;
                            foreach (PetriNetNode oldFigure in old)
                            {
                                if (figure.Id == oldFigure.Id)
                                {
                                    figure.CoordX = oldFigure.CoordX;
                                    figure.CoordY = oldFigure.CoordY;
                                    Shape shape = GetKeyByValueForFigures(figure) as Shape;
                                    Canvas.SetLeft(shape, figure.CoordX);
                                    Canvas.SetTop(shape, figure.CoordY);
                                    break;
                                }
                            }
                            MakeSelected(figure);
                        }
                    }
                    foreach (VArc arc in Net.arcs)
                    {
                        if (cuttedArcs.Contains(arc))
                        {
                            DisplayArc(arc);
                            _selectedArcs.Add(arc);
                            arc.IsSelect = true;
                            ColorArrow(arc);
                        }
                    }
                }
                else if (lastCommand is PasteCommand)
                {
                    List <PetriNetNode> figures = (lastCommand as PasteCommand).pastedfigures;
                    List <VArc>         arcs    = (lastCommand as PasteCommand).pastedArcs;

                    cuttedOrCopiedArcs.Clear();
                    cuttedOrCopiedFigures.Clear();

                    cuttedOrCopiedFigures.AddRange(figures);
                    cuttedOrCopiedArcs.AddRange(arcs);

                    CutFigures(cuttedOrCopiedFigures, cuttedOrCopiedArcs);

                    _selectedFigures.Clear();
                    _selectedArcs.Clear();
                    ReassignSelectedProperties();
                }
                Command.ExecutedCommands.Pop();
                Command.CanceledCommands.Push(lastCommand);
            }

            ReassignSelectedProperties();
            EnableUndoRedoButtons();
            if (Command.ExecutedCommands.Count == 0)
            {
                btnUndo.IsEnabled = false;
            }
            TurnOnSelectMode();
        }
Beispiel #19
0
        /// <summary>
        /// Read model from PNML
        /// </summary>
        /// <param name="pnmlDoc">pnml document</param>
        /// <param name="exc">shows whether any mistakes has been detected</param>
        public static List <VArc> OpenPnml(XmlDocument pnmlDoc, out bool exc)
        {
            var returnArcs = new List <VArc>();

            exc = false;
            XmlNode net = null;

            if (pnmlDoc.DocumentElement.FirstChild.Name == "net")
            {
                net = pnmlDoc.DocumentElement.FirstChild;
            }

            if (net == null)
            {
                MessageBox.Show("Node 'net' is missed, check the file");
                exc = true;
                return(returnArcs);
            }

            XmlNode page = null;

            for (var i = 0; i < net.ChildNodes.Count; i++)
            {
                if (net.ChildNodes[i].Name == "page")
                {
                    page = net.ChildNodes[i];
                }
            }

            if (page == null)
            {
                MessageBox.Show("Node 'page' is missed, check the file");
                exc = true;
                return(returnArcs);
            }

            var petriNetObjects = page.ChildNodes;

            if (petriNetObjects.Count == 0)
            {
                MessageBox.Show("There are no nodes in the net, check the file");
                exc = true;
                return(returnArcs);
            }

            foreach (XmlNode figure in petriNetObjects)
            {
                string id = "", name = "";
                int    numberOfTokens = 0;
                double x = -1, y = -1;

                if (figure.Name == "place" || figure.Name == "transition")
                {
                    for (var i = 0; i < figure.Attributes.Count; i++)
                    {
                        if (figure.Attributes[i].Name == "id")
                        {
                            id = figure.Attributes[i].Value;
                        }
                    }

                    var figureNodes = figure.ChildNodes;

                    //todo Какой-то очень стремный кусок кода. Нужно рефакторить

                    for (int i = 0; i < figureNodes.Count; i++)
                    {
                        if (figureNodes[i].Name == "name")
                        {
                            name = figureNodes[i].FirstChild.InnerText;
                        }
                        else if (figureNodes[i].Name == "graphics")
                        {
                            for (int j = 0; j < figureNodes[i].ChildNodes.Count; j++)
                            {
                                if (figureNodes[i].ChildNodes[j].Name == "position")
                                {
                                    for (int k = 0; k < figureNodes[i].ChildNodes[j].Attributes.Count; k++)
                                    {
                                        if (figureNodes[i].ChildNodes[j].Attributes[k].Name == "x")
                                        {
                                            if (double.TryParse(figureNodes[i].ChildNodes[j].Attributes[k].Value.Replace('.', ','), out x) == false || x < 0)
                                            {
                                                x = 0;
                                                MessageBox.Show("Node " + id + " has incorrect x-coordinate(it will be assumed as 0)");
                                            }
                                        }
                                        else if (figureNodes[i].ChildNodes[j].Attributes[k].Name == "y")
                                        {
                                            if (double.TryParse(figureNodes[i].ChildNodes[j].Attributes[k].Value.Replace('.', ','), out y) == false || y < 0)
                                            {
                                                y = 0;
                                                MessageBox.Show("Node " + id + " has incorrect y-coordinate(it will be assumed as 0)");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (figureNodes[i].Name == "initialMarking")
                        {
                            if (int.TryParse(figureNodes[i].FirstChild.InnerText, out numberOfTokens) == false)
                            {
                                numberOfTokens = 0;
                                MessageBox.Show("Place " + id + " has incorrect number of tokens(it will be assumed as 0)");
                            }
                        }
                    }

                    if (figure.Name == "place")
                    {
                        var place = VPlace.Create(x, y);
                        place.Id             = id;
                        place.Label          = name;
                        place.NumberOfTokens = numberOfTokens;
                        PNEditorControl.Net.places.Add(place);
                        //SetOfFigures.Figures.Add(place);
                    }
                    else
                    {
                        var transition = VTransition.Create(x, y);
                        transition.Id    = id;
                        transition.Label = name;
                        PNEditorControl.Net.transitions.Add(transition);
                        //SetOfFigures.Figures.Add(transition);
                    }
                }
                else if (figure.Name == "arc")
                {
                }
                else
                {
                    MessageBox.Show("The file contains element with foreign name, it will be ignored, check the file");
                }
            }
            if (PNEditorControl.Net.Nodes.Count == 0)
            {
                MessageBox.Show("There are no nodes in the net, check the file");
                exc = true;
                return(returnArcs);
            }
            else
            {
                foreach (XmlNode figure in petriNetObjects)
                {
                    if (figure.Name != "arc")
                    {
                        continue;
                    }

                    PetriNetNode from = null, to = null;
                    string       fromId = null, toId = null, id = null;

                    for (var i = 0; i < figure.Attributes.Count; i++)
                    {
                        switch (figure.Attributes[i].Name)
                        {
                        case "source":
                            fromId = figure.Attributes[i].Value;
                            break;

                        case "target":
                            toId = figure.Attributes[i].Value;
                            break;

                        case "id":
                            id = figure.Attributes[i].Value;
                            break;
                        }
                    }

                    var arcWeight = 1;
                    if (figure.FirstChild != null)
                    {
                        if (figure.FirstChild.Name == "name")
                        {
                            int.TryParse(figure.FirstChild.FirstChild.FirstChild.InnerText, out arcWeight);
                        }
                    }

                    foreach (var f in PNEditorControl.Net.Nodes)
                    {
                        if (f.Id == fromId)
                        {
                            from = f;
                        }
                        else if (f.Id == toId)
                        {
                            to = f;
                        }
                    }

                    var arc = new VArc(from, to)
                    {
                        Weight     = arcWeight.ToString(),
                        Id         = id,
                        IsDirected = true
                    };
                    returnArcs.Add(arc);
                }
            }
            return(returnArcs);
        }
Beispiel #20
0
 public void RemoveTokens(VPlace place)
 {
     RemoveTokensFromCanvas(place);
     place.TokensList.Clear();
 }