Example #1
0
        public void ShowProperties(PetriNetNode selected)
        {
            lblID.Visibility      = Visibility.Visible;
            lblIDValue.Visibility = Visibility.Visible;

            lblName.Visibility   = Visibility.Visible;
            tbName.Visibility    = Visibility.Visible;
            btnOkName.Visibility = Visibility.Visible;

            lblIngoingArcs.Visibility    = Visibility.Visible;
            textBoxIngoinArcs.Visibility = Visibility.Visible;

            lblOutgoinArcs.Visibility      = Visibility.Visible;
            textBoxOutgoingArcs.Visibility = Visibility.Visible;

            lblIDValue.Content = selected.Id;
            tbName.Text        = selected.Label;
            VPlace place = selected as VPlace;

            if (place != null)
            {
                ShowOnlyPlaceProperties();
                tbTokenNumber.Text = place.NumberOfTokens.ToString();
            }
            else
            //HideOnlyPlaceProperties();
            {
                VTransition transition = selected as VTransition;
                ShowOnlyTransitionProperties();
                tbPriority.Text = transition.Priority.ToString();
            }

            textBoxIngoinArcs.Clear();
            textBoxOutgoingArcs.Clear();
            foreach (VArc a in selected.ThisArcs)
            {
                VArc arc = a;
                if (a.To != selected)
                {
                    textBoxOutgoingArcs.Text += arc.To.Id + "\n";
                }
                else
                {
                    textBoxIngoinArcs.Text += arc.From.Id + "\n";
                }
            }
        }
Example #2
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);
            }
        }
Example #3
0
 private static string GenerateLatexTransition(VTransition transition)
 {
     return(@"\node [transition,label=-90:" + transition.Label + "] (" + transition.Id + ") at (" + Convert.ToString((transition.CoordX / koefX),
                                                                                                                     CultureInfo.GetCultureInfo("en-US")) + "," +
            Convert.ToString((-(transition.CoordY / koefY)), CultureInfo.GetCultureInfo("en-US")) + ") {};");
 }
Example #4
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);
        }
Example #5
0
        public bool SimulateOneStep()
        {
            isSomethingChanged = true;

            if (isItFirstStep)
            {
                weights.Clear();

                foreach (var place in Net.places)
                {
                    weights.Add(place.Id, place.NumberOfTokens);
                }
                isItFirstStep = false;
            }

            List <VPlace> initialPlaces = new List <VPlace>();

            foreach (VPlace place in Net.places)
            {
                int numberOfOutgoingArcs = place.ThisArcs.Count(t => place != t.To);

                //todo вот здесь если токены не кружками, то будет плохо
                if (place.TokensList.Count != 0 && numberOfOutgoingArcs != 0)
                {
                    initialPlaces.Add(place);
                }
                foreach (Ellipse ellipse in place.TokensList)
                {
                    ellipse.Fill = Brushes.Black;
                }
            }

            var outgoingTransitions = new List <VTransition>();

            foreach (var place in initialPlaces)
            {
                foreach (var arc1 in place.ThisArcs)
                {
                    if (arc1.From != place)
                    {
                        continue;
                    }

                    foreach (var arc2 in arc1.To.ThisArcs)
                    {
                        var mayBeEnabled = true;

                        if (arc2.To != arc1.To)
                        {
                            continue;
                        }

                        foreach (var arc in arc1.To.ThisArcs)
                        {
                            if (arc.To != arc1.To)
                            {
                                continue;
                            }
                            int numberOfRequiredTokens;
                            int.TryParse(arc.Weight, out numberOfRequiredTokens);
                            var numberOfExistingTokens = (arc.From as VPlace).NumberOfTokens;

                            if (numberOfRequiredTokens <= numberOfExistingTokens)
                            {
                                continue;
                            }
                            mayBeEnabled = false;
                            break;
                        }
                        if (!outgoingTransitions.Contains(arc1.To as VTransition) && mayBeEnabled)
                        {
                            outgoingTransitions.Add(arc1.To as VTransition);
                        }
                    }
                }
            }

            if (outgoingTransitions.Count != 0)
            {
                foreach (VTransition transition in outgoingTransitions)
                {
                    (GetKeyByValueForFigures(transition)
                     as Shape).Stroke = Brushes.Black;
                }


                if (_isTransitionSelected == false)
                {
                    if (_modeChoice == Choice.Forced)
                    {
                        if (outgoingTransitions.Count > 1)
                        {
                            _leftMouseButtonMode = LeftMouseButtonMode.ChooseTransition;
                            foreach (VTransition transition in outgoingTransitions)
                            {
                                SolidColorBrush brush = new SolidColorBrush();
                                brush.Color = Color.FromRgb(255, 0, 51);
                                (GetKeyByValueForFigures(transition) as Shape).Stroke = brush;
                            }
                            return(false);
                        }
                        else
                        {
                            enabledTransition = outgoingTransitions[0];
                        }
                    }
                    else
                    {
                        var transitionsWithTopPriority = new List <VTransition>();
                        outgoingTransitions.Sort(new Comparison <VTransition>((VTransition a, VTransition b) => (a.Priority - b.Priority)));

                        var transitionWithTopPriority = outgoingTransitions.Find(new Predicate <VTransition>((VTransition t) => t.Priority > 0));

                        int topPriority = 0;
                        if (transitionWithTopPriority != null)
                        {
                            topPriority = transitionWithTopPriority.Priority;
                        }

                        outgoingTransitions = outgoingTransitions.FindAll(new Predicate <VTransition>((VTransition a) => (a.Priority == topPriority || a.Priority == 0)));

                        int indexOfEnabledTransition = MainRandom.Next(0, outgoingTransitions.Count);
                        enabledTransition = outgoingTransitions[indexOfEnabledTransition];
                    }
                }
                _isTransitionSelected = false;
                _leftMouseButtonMode  = LeftMouseButtonMode.Select;

                var isFirstRemove = true;

                foreach (var arc in enabledTransition.ThisArcs)
                {
                    if (arc.From == enabledTransition)
                    {
                        continue;
                    }

                    if (colored.Contains(arc.From))
                    {
                        colored.Remove(arc.From);
                        if (isFirstRemove)
                        {
                            numberOfLevels--;
                        }
                        isFirstRemove = false;
                    }
                    colored.Add(arc.From);
                }
                numberOfLevels++;

                if (colored.Contains(enabledTransition))
                {
                    colored.Remove(enabledTransition);
                    numberOfLevels--;
                }
                colored.Add(enabledTransition);

                numberOfLevels++;
                isFirstRemove = true;
                foreach (VArc arc in enabledTransition.ThisArcs)
                {
                    VPlace changedPlace;
                    if (arc.From != enabledTransition)
                    {
                        changedPlace = (arc.From as VPlace);
                        if (changedPlace.NumberOfTokens != 0)
                        {
                            if (changedPlace.NumberOfTokens < 5)
                            {
                                RemoveTokens(changedPlace);
                            }
                            else
                            {
                                MainModelCanvas.Children.Remove(changedPlace.NumberOfTokensLabel);
                            }
                        }
                        int delta;
                        int.TryParse(arc.Weight, out delta);
                        changedPlace.NumberOfTokens -= delta;
                    }
                    else
                    {
                        changedPlace = (arc.To as VPlace);
                        if (changedPlace.NumberOfTokens != 0)
                        {
                            if (changedPlace.NumberOfTokens < 5)
                            {
                                RemoveTokens(changedPlace);
                            }
                            else
                            {
                                MainModelCanvas.Children.Remove(changedPlace.NumberOfTokensLabel);
                            }
                        }
                        int delta;
                        int.TryParse(arc.Weight, out delta);
                        changedPlace.NumberOfTokens += delta;
                        if (colored.Contains(changedPlace))
                        {
                            colored.Remove(changedPlace);
                            if (isFirstRemove)
                            {
                                numberOfLevels--;
                            }
                            isFirstRemove = false;
                        }
                        colored.Add(changedPlace);
                    }



                    AddTokens(changedPlace);
                    if (arc.From != enabledTransition)
                    {
                        continue;
                    }
                    var placeToColor = (arc.To as VPlace);
                    foreach (var ellepse in placeToColor.TokensList)
                    {
                        var brush = new SolidColorBrush {
                            Color = Color.FromRgb(153, 255, 102)
                        };
                        ellepse.Fill   = brush;
                        ellepse.Stroke = Brushes.Black;
                    }
                }
                numberOfLevels++;

                if (simulationMode == SimulationMode.wave)
                {
                    ColorFigures();
                }


                if (marking.Count <= 0)
                {
                    return(false);
                }
                oneStepMarking = new int[Net.places.Count];
                for (int j = 0; j < Net.places.Count; j++)
                {
                    oneStepMarking[j] = Net.places[j].NumberOfTokens;
                }

                marking.Push(oneStepMarking);
                maxMarking++;

                textBoxSimulationCurrentMarking.Text += "M_" + (maxMarking - 1) + " = { ";
                for (int j = 0; j < oneStepMarking.Length - 1; j++)
                {
                    textBoxSimulationCurrentMarking.Text += oneStepMarking[j] + " | ";
                }
                textBoxSimulationCurrentMarking.Text += oneStepMarking[oneStepMarking.Length - 1] + " }\n";
                textBoxSimulationCurrentMarking.ScrollToEnd();
                return(false);
            }
            else
            {
                return(true);
            }
        }
        private void btnPaste_Click(object sender, RoutedEventArgs e)
        {
            _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();
        }