Exemplo n.º 1
0
        private void piPropertiesInspector_SelectedObjectChanged(object sender, EventArgs e)
        {
            object o = this.piPropertiesInspector.SelectedObject;

            if (o is Place)
            {
                if (!(o is PlaceInput))
                {
                    if (this.pnd.PetriNetType == PetriNetType.TimeInvariant)
                    {
                        this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new TimeInvariantAttribute() });
                    }
                    else
                    {
                        this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new CommonPropertiesAttribute() });
                    }
                }
                else
                {
                    PlaceInput pi = (PlaceInput)o;

                    if (pi.InputType == InputType.Fixed)
                    {
                        this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new TimeInvariantAttribute(), new PeriodicInputTypeAttribute(), new StohasticInputTypeAttribute() });
                    }
                    else if (pi.InputType == InputType.Periodic)
                    {
                        this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new TimeInvariantAttribute(), new PeriodicInputTypeAttribute() });
                    }
                    else if (pi.InputType == InputType.Stohastic)
                    {
                        this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new TimeInvariantAttribute(), new StohasticInputTypeAttribute() });
                    }
                }
            }
            else
            {
                this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new CommonPropertiesAttribute() });
            }

            if (o is SelectableAndMovableControl)
            {
                SelectableAndMovableControl s = (SelectableAndMovableControl)o;
                s.PerformActivation();
            }
            else if (o is Connection)
            {
                Connection cn = (Connection)o;
                if (cn.IsVirtual == true)
                {
                    this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new CommonPropertiesAttribute() });
                }
                else
                {
                    this.piPropertiesInspector.BrowsableAttributes = new AttributeCollection(new Attribute[] { new ConnectionPropertiesAttribute() });
                }

                cn.PerformActivation();
            }
        }
Exemplo n.º 2
0
        public override object Clone()
        {
            PlaceInput pi = new PlaceInput();

            pi.Location = this.Location;
            pi.NameID   = this.NameID;
            pi.Tokens   = this.Tokens;
            return(pi);
        }
Exemplo n.º 3
0
        public ToolBox()
        {
            // This call is required by the Windows Form Designer.
            InitializeComponent();

            #region Initialize TreeView
            TreeNode tnSimpleRoot = new TreeNode("Simple  ", 10, 10);
            tnSimpleRoot.NodeFont  = new Font(this.Font, FontStyle.Bold);
            tnSimpleRoot.BackColor = SystemColors.ControlDark;
            tnSimpleRoot.ForeColor = Color.White;

            TreeNode tn = new TreeNode("Input", 0, 0);
            Place    p  = new PlaceInput();
            PetriNetEditorMergeModule pnemm = new PetriNetEditorMergeModule(p);
            tn.Tag = pnemm;
            tnSimpleRoot.Nodes.Add(tn);

            tn     = new TreeNode("Operation", 1, 1);
            p      = new PlaceOperation();
            pnemm  = new PetriNetEditorMergeModule(p);
            tn.Tag = pnemm;
            tnSimpleRoot.Nodes.Add(tn);

            tn     = new TreeNode("Resource", 2, 2);
            p      = new PlaceResource();
            pnemm  = new PetriNetEditorMergeModule(p);
            tn.Tag = pnemm;
            tnSimpleRoot.Nodes.Add(tn);

            tn     = new TreeNode("Control", 3, 3);
            p      = new PlaceControl();
            pnemm  = new PetriNetEditorMergeModule(p);
            tn.Tag = pnemm;
            tnSimpleRoot.Nodes.Add(tn);

            tn     = new TreeNode("Output", 4, 4);
            p      = new PlaceOutput();
            pnemm  = new PetriNetEditorMergeModule(p);
            tn.Tag = pnemm;
            tnSimpleRoot.Nodes.Add(tn);

            tn = new TreeNode("Transition", 5, 5);
            Transition t = new Transition();
            pnemm  = new PetriNetEditorMergeModule(t);
            tn.Tag = pnemm;
            tnSimpleRoot.Nodes.Add(tn);

            tn = new TreeNode("Label", 6, 6);
            DescriptionLabel dl = new DescriptionLabel();
            pnemm  = new PetriNetEditorMergeModule(dl);
            tn.Tag = pnemm;
            tnSimpleRoot.Nodes.Add(tn);

            TreeNode tnAdvancedRoot = new TreeNode("Advanced  ", 10, 10);
            tnAdvancedRoot.NodeFont  = new Font(this.Font, FontStyle.Bold);
            tnAdvancedRoot.BackColor = SystemColors.ControlDark;
            tnAdvancedRoot.ForeColor = Color.White;

            tn = new TreeNode("Subsystem block", 7, 7);
            Subsystem s = new Subsystem();
            pnemm  = new PetriNetEditorMergeModule(s);
            tn.Tag = pnemm;
            tnAdvancedRoot.Nodes.Add(tn);

            tn = new TreeNode("In", 8, 8);
            Input i = new Input();
            pnemm  = new PetriNetEditorMergeModule(i);
            tn.Tag = pnemm;
            tnAdvancedRoot.Nodes.Add(tn);

            tn = new TreeNode("Out", 9, 9);
            Output o = new Output();
            pnemm  = new PetriNetEditorMergeModule(o);
            tn.Tag = pnemm;
            tnAdvancedRoot.Nodes.Add(tn);

            tn = new TreeNode("Resource-operation", 2, 2);
            Transition t1 = new Transition();
            pnemm = new PetriNetEditorMergeModule();
            pnemm.Add(t1);
            PlaceOperation po = new PlaceOperation();
            po.Location = new Point(150, 0);
            pnemm.Add(po);
            PlaceResource pr = new PlaceResource();
            pr.Tokens   = 1;
            pr.Location = new Point(150, 100);
            pnemm.Add(pr);
            Transition t2 = new Transition();
            t2.Location = new Point(300, 0);
            pnemm.Add(t2);
            Connection cn = new Connection(t1, po, 1, 1, new Point(t1.Width, (int)(t1.Height / 2)), new Point(0, (int)(po.Height / 2)), 1, true);
            pnemm.Add(cn);
            cn = new Connection(po, t2, 1, 1, new Point(po.Width, (int)(po.Height / 2)), new Point(0, (int)(t2.Height / 2)), 1, true);
            pnemm.Add(cn);
            cn = new Connection(t2, pr, 1, 1, new Point(t2.Width, (int)(3 * t2.Height / 4)), new Point(pr.Width, (int)(pr.Height / 2)), 1, true);
            pnemm.Add(cn);
            cn = new Connection(pr, t1, 1, 1, new Point(0, (int)(pr.Height / 2)), new Point(0, (int)(3 * t1.Height / 4)), 1, true);
            pnemm.Add(cn);
            tn.Tag = pnemm;
            tnAdvancedRoot.Nodes.Add(tn);

            tvToolBox.BeginUpdate();
            tvToolBox.Nodes.Add(tnSimpleRoot);
            tvToolBox.Nodes.Add(tnAdvancedRoot);

            tvToolBox.ExpandAll();

            tvToolBox.EndUpdate();
            tvToolBox.Refresh();
            #endregion

            tvToolBox.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.tvToolBox_BeforeSelect);
            tvToolBox.Click        += new EventHandler(tvToolBox_Click);
            tvToolBox.MouseDown    += new MouseEventHandler(tvToolBox_MouseDown);

            //Workaround HScroll bug
            TreeViewHelper.DisableHScroller(this.tvToolBox.Handle);
            TreeViewHelper.EnableHScroller(this.tvToolBox.Handle);
        }
Exemplo n.º 4
0
        private void Simulate()
        {
            #region if (this.pnd.PetriNetType == PetriNetType.TimeInvariant)
            if (this.pnd.PetriNetType == PetriNetType.TimeInvariant)
            {
                // TODO : Deselect all selected controls in pnd

                SaveDataForCharting(0);
                while (true)
                {
                    this.Step();

                    SaveDataForCharting(this.pnd.StepCounter);

                    // Find is there any transition that can fire

                    if (this.pnd.FireableTransitions.Count != 0 ||
                        this.ignoreLackOfFireableTransition)
                    {
                        Thread.Sleep(this.sleepBetweenStep);    // 1000 by default..
                        Application.DoEvents();
                    }
                    else
                    {
                        break;
                    }
                }

                this.bRunning = false;

                // Raise SimulationFinished event
                if (this.SimulationFinished != null)
                {
                    this.SimulationFinished(this, new EventArgs());
                }
            }
            #endregion

            #region else if (this.pnd.PetriNetType == PetriNetType.PTimed)
            else if (this.pnd.PetriNetType == PetriNetType.PTimed)
            {
                // TODO : Deselect all selected controls in pnd

                // Initialize
                IntMatrix imk = new IntMatrix(this.pnd.GroupedPlaces.Count, 1);

                IntMatrix imF = this.pnd.F;
                IntMatrix Ft  = this.pnd.F.Transpose();
                IntMatrix Wt  = this.pnd.W.Transpose();

                ArrayList alInputPlaces     = this.pnd.InputPlaces;
                ArrayList alOperationPlaces = this.pnd.OperationPlaces;
                ArrayList alResourcePlaces  = this.pnd.ResourcePlaces;
                ArrayList alControlPlaces   = this.pnd.ControlPlaces;
                ArrayList alOutputPlaces    = this.pnd.OutputPlaces;
                ArrayList alGroupedPlaces   = this.pnd.GroupedPlaces;
                ArrayList alTransitions     = this.pnd.Transitions;

                IntMatrix Sv = this.pnd.Sv;
                IntMatrix Sr = this.pnd.Sr;
                IntMatrix Sd = this.pnd.Sd;
                IntMatrix Sy = this.pnd.Sy;

                IntMatrix Tv0 = this.pnd.Tv0;
                IntMatrix Tr0 = this.pnd.Tr0;
                IntMatrix Td0 = this.pnd.Td0;
                IntMatrix Ty0 = this.pnd.Ty0;

                IntMatrix[] Tv = new IntMatrix[1];
                Tv[0] = new IntMatrix(Tv0.Dimensions.Height, Tv0.Dimensions.Width);

                IntMatrix[] Tr = new IntMatrix[1];
                Tr[0] = new IntMatrix(Tr0.Dimensions.Height, Tr0.Dimensions.Width);

                IntMatrix[] Td = new IntMatrix[1];
                Td[0] = new IntMatrix(Td0.Dimensions.Height, Td0.Dimensions.Width);

                IntMatrix[] Ty = new IntMatrix[1];
                Ty[0] = new IntMatrix(Ty0.Dimensions.Height, Ty0.Dimensions.Width);

                // Initial condition
                for (int i = 0; i < alGroupedPlaces.Count; i++)
                {
                    if (ht.Count != 0)
                    {
                        imk[i, 0] = (int)ht[alGroupedPlaces[i]];
                    }
                    else
                    {
                        imk[i, 0] = ((Place)alGroupedPlaces[i]).Tokens;
                    }
                }

                // Get mks
                IntMatrix imks = imk;

                // Get index of first control place
                int iIndexOfFirstControlPlace = -1;
                if (alControlPlaces.Count != 0)
                {
                    iIndexOfFirstControlPlace = alGroupedPlaces.IndexOf(alControlPlaces[0]);
                }

                #region Set inputs
                // Set inputs
                Hashtable htInputTokens = new Hashtable();
                foreach (PlaceInput pi in pnd.InputPlaces)
                {
                    if (pi.InputType == InputType.Periodic)
                    {
                        ArrayList alInputTimes = new ArrayList();

                        int iTime = 0;
                        while (iTime < this.pnd.EndTime / this.pnd.Td)
                        {
                            iTime += pi.Interval;
                            alInputTimes.Add(iTime);
                        }

                        htInputTokens.Add(pi, alInputTimes);
                    }
                    else if (pi.InputType == InputType.Stohastic)
                    {
                        ArrayList alInputTimes = new ArrayList();
                        Random    r            = new Random(Environment.TickCount);
                        int       iTime        = 0;
                        while (iTime < this.pnd.EndTime / this.pnd.Td)
                        {
                            int i = r.Next(1, pi.RandomInterval);
                            iTime += i;
                            alInputTimes.Add(iTime);
                        }

                        htInputTokens.Add(pi, alInputTimes);
                    }
                }
                #endregion

                SaveDataForCharting(0);
                // Simulate
                for (int k = 1; k <= this.pnd.EndTime / this.pnd.Td; k++)
                {
                    foreach (Transition t in alTransitions)
                    {
                        t.RefreshMT();
                    }

                    Thread.Sleep(this.pnd.Td);
                    Application.DoEvents();


                    #region Set control vector
                    // Set control vector

                    bool isPhytonExecutedOK = script.ScriptSingleStep();
                    Tv0 = this.pnd.Tv0; // Refresh duration information..

                    if (alControlPlaces.Count != 0)
                    {
                        if (isPhytonExecutedOK)
                        {
                            for (int i = 0; i < alControlPlaces.Count; i++)
                            {
                                imk[iIndexOfFirstControlPlace + i, 0] = ((Place)alControlPlaces[i]).Tokens;
                            }
                        }
                        else
                        {
                            // Set all control places tokens to 1 if they don't have parents
                            for (int i = 0; i < alControlPlaces.Count; i++)
                            {
                                if (((Place)alControlPlaces[i]).Parents.Count == 0)
                                {
                                    imk[iIndexOfFirstControlPlace + i, 0] = 1;
                                }
                            }
                        }

                        this.pnd.StepCounter++;

                        foreach (Rule r in this.pnd.Rules)
                        {
                            int[] iaTokensVector = new int[alGroupedPlaces.Count];

                            for (int i = 0; i < iaTokensVector.Length; i++)
                            {
                                iaTokensVector[i] = (int)imk[i, 0];
                            }

                            char[] ca = r.ControlVector(alGroupedPlaces, alControlPlaces, iaTokensVector);

                            if (ca != null)
                            {
                                for (int i = 0; i < ca.Length; i++)
                                {
                                    if (ca[i] != 'x')
                                    {
                                        if (((Place)this.pnd.ControlPlaces[i]).Parents.Count == 0)
                                        {
                                            imk[iIndexOfFirstControlPlace + i, 0] = int.Parse(ca[i].ToString());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #endregion

                    #region Find transitions that can fire
                    // Find transitions that can fire
                    IntMatrix imT = new IntMatrix(alTransitions.Count, 1);

                    for (int i = 0; i < alTransitions.Count; i++)
                    {
                        Transition t = (Transition)alTransitions[i];
                        ArrayList  alPlaceParents = t.PlaceParents;

                        int iCountMetConditions = 0;
                        for (int j = 0; j < alPlaceParents.Count; j++)
                        {
                            Place p = (Place)alPlaceParents[j];

                            if ((int)imk[alGroupedPlaces.IndexOf(p), 0] >= imF[i, alGroupedPlaces.IndexOf(p)])
                            {
                                iCountMetConditions++;
                            }
                        }

                        if (iCountMetConditions == alPlaceParents.Count && alPlaceParents.Count != 0)
                        {
                            imT[i, 0] = 1;
                        }
                    }
                    #endregion

                    // Calculate mks+1
                    imks = imks + Wt * imT;

                    #region Determine maximum tokens in one place
                    // Determine maximum tokens in one place
                    int       iMaxTokens  = 0;
                    IntMatrix imOpsAndRes = new IntMatrix(this.pnd.OperationPlaces.Count + this.pnd.ResourcePlaces.Count, 1);
                    for (int z = 0; z < this.pnd.OperationPlaces.Count; z++)
                    {
                        imOpsAndRes[z, 0] = imks[this.pnd.InputPlaces.Count + z, 0];
                    }
                    for (int z = 0; z < this.pnd.ResourcePlaces.Count; z++)
                    {
                        imOpsAndRes[this.pnd.OperationPlaces.Count + z, 0] = imks[this.pnd.InputPlaces.Count + this.pnd.OperationPlaces.Count + z, 0];
                    }

                    iMaxTokens = imOpsAndRes.Max();
                    #endregion

                    #region Adjust sizes of Tx matrixes
                    // Adjust sizes of Tx matrixes
                    if (iMaxTokens > Tv.Length)
                    {
                        IntMatrix[] Tvx = new IntMatrix[iMaxTokens];
                        for (int i = 0; i < iMaxTokens; i++)
                        {
                            Tvx[i] = new IntMatrix(Tv0.Dimensions.Height, Tv0.Dimensions.Width);
                        }
                        Tv.CopyTo(Tvx, 0);
                        Tv = Tvx;

                        IntMatrix[] Trx = new IntMatrix[iMaxTokens];
                        for (int i = 0; i < iMaxTokens; i++)
                        {
                            Trx[i] = new IntMatrix(Tr0.Dimensions.Height, Tr0.Dimensions.Width);
                        }
                        Tr.CopyTo(Trx, 0);
                        Tr = Trx;

                        IntMatrix[] Tdx = new IntMatrix[iMaxTokens];
                        for (int i = 0; i < iMaxTokens; i++)
                        {
                            Tdx[i] = new IntMatrix(Td0.Dimensions.Height, Td0.Dimensions.Width);
                        }
                        Td.CopyTo(Tdx, 0);
                        Td = Tdx;

                        IntMatrix[] Tyx = new IntMatrix[iMaxTokens];
                        for (int i = 0; i < iMaxTokens; i++)
                        {
                            Tyx[i] = new IntMatrix(Ty0.Dimensions.Height, Ty0.Dimensions.Width);
                        }
                        Ty.CopyTo(Tyx, 0);
                        Ty = Tyx;
                    }
                    #endregion

                    if (this.pnd.Editor.PauseBeforeFiring == true && k != 1)
                    {
                        if (imT.Max() == 1)
                        {
                            if (this.SimulationProcessFinished != null)
                            {
                                this.SimulationProcessFinished(this, EventArgs.Empty);
                            }
                        }
                    }

                    // Calculate Tx_temp matrixes
                    IntMatrix Tv_temp = IntMatrix.MulTim(Tv0, imT);
                    IntMatrix Tr_temp = IntMatrix.MulTim(Tr0, imT);
                    IntMatrix Td_temp = IntMatrix.MulTim(Td0, imT);
                    IntMatrix Ty_temp = IntMatrix.MulTim(Ty0, imT);

                    // Adjust all Connections
                    foreach (Connection cn in this.pnd.ConnectionsAll)
                    {
                        cn.TokenPositions = new ArrayList(Tv.Length);
                        for (int h = 0; h < Tv.Length; h++)
                        {
                            cn.TokenPositions.Add(0);
                        }
                    }

                    try
                    {
                        #region Adjust Tv matrix
                        // Put new values in table
                        for (int m = 0; m < Tv0.Dimensions.Height; m++)
                        {
                            for (int l = 0; l < Tv0.Dimensions.Width; l++)
                            {
                                if (Tv_temp[m, l] > 0)
                                {
                                    // Find first available position in Tv matrix
                                    int iAvailable = -1;
                                    for (int z = 0; z < Tv.Length; z++)
                                    {
                                        if (Tv[z][m, l] == 0)
                                        {
                                            iAvailable = z;
                                            break;
                                        }
                                    }

                                    if (iAvailable == -1)
                                    {
                                        throw new ArgumentOutOfRangeException("No free layer. Max tokens number in one place has been exceeded.");
                                    }

                                    Tv[iAvailable][m, l] = Tv_temp[m, l];
                                    Tv_temp[m, l]        = 0;

                                    ((PlaceOperation)alOperationPlaces[m]).MaxFillAngle = Tv[iAvailable][m, l];
                                    ((PlaceOperation)alOperationPlaces[m]).FillAngle    = Tv[iAvailable][m, l];
                                }
                            }
                        }

                        // Decrement all values by 1
                        for (int ctn = 0; ctn < Tv.Length; ctn++)
                        {
                            for (int m = 0; m < Tv0.Dimensions.Height; m++)
                            {
                                for (int l = 0; l < Tv0.Dimensions.Width; l++)
                                {
                                    if (Tv[ctn][m, l] > 0)
                                    {
                                        if (Tv[ctn][m, l] == 1)
                                        {
                                            imk[m + alInputPlaces.Count, 0] = imk[m + alInputPlaces.Count, 0] + Sv[m, l];
                                        }

                                        Tv[ctn][m, l] = Tv[ctn][m, l] - 1;

                                        if (Tv[ctn][m, l] < ((PlaceOperation)alOperationPlaces[m]).FillAngle || ((PlaceOperation)alOperationPlaces[m]).FillAngle == 0)
                                        {
                                            ((PlaceOperation)alOperationPlaces[m]).FillAngle = Tv[ctn][m, l];
                                        }

                                        if (this.pnd.TokenGameAnimation == true)
                                        {
                                            // Get connection
                                            Connection cn = Connection.GetConnectionBetweenControls((ConnectableControl)alTransitions[l], (ConnectableControl)alOperationPlaces[m]);

                                            int i = (int)((((PlaceOperation)alOperationPlaces[m]).MaxFillAngle - Tv[ctn][m, l]) * 100f / (float)((PlaceOperation)alOperationPlaces[m]).MaxFillAngle);

                                            if (i < 100)
                                            {
                                                cn.TokenPositions[ctn] = i;
                                            }
                                            else
                                            {
                                                cn.TokenPositions[ctn] = 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        #endregion

                        #region Adjust Tr matrix
                        // Put new values in table
                        for (int m = 0; m < Tr0.Dimensions.Height; m++)
                        {
                            for (int l = 0; l < Tr0.Dimensions.Width; l++)
                            {
                                if (Tr_temp[m, l] > 0)
                                {
                                    // Find first available position in Tr matrix
                                    int iAvailable = -1;
                                    for (int z = 0; z < Tr.Length; z++)
                                    {
                                        if (Tr[z][m, l] == 0)
                                        {
                                            iAvailable = z;
                                            break;
                                        }
                                    }

                                    if (iAvailable == -1)
                                    {
                                        throw new ArgumentOutOfRangeException("No free layer. Max tokens number in one place has been exceeded.");
                                    }

                                    Tr[iAvailable][m, l] = Tr_temp[m, l];
                                    Tr_temp[m, l]        = 0;

                                    ((PlaceResource)alResourcePlaces[m]).MaxFillAngle = Tr[iAvailable][m, l];
                                    ((PlaceResource)alResourcePlaces[m]).FillAngle    = Tr[iAvailable][m, l];
                                }
                            }
                        }

                        // Decrement all values by 1
                        for (int ctn = 0; ctn < Tr.Length; ctn++)
                        {
                            for (int m = 0; m < Tr0.Dimensions.Height; m++)
                            {
                                for (int l = 0; l < Tr0.Dimensions.Width; l++)
                                {
                                    if (Tr[ctn][m, l] > 0)
                                    {
                                        if (Tr[ctn][m, l] == 1)
                                        {
                                            imk[m + alInputPlaces.Count + alOperationPlaces.Count, 0] = imk[m + alInputPlaces.Count + alOperationPlaces.Count, 0] + Sr[m, l];
                                        }

                                        Tr[ctn][m, l] = Tr[ctn][m, l] - 1;

                                        if (Tr[ctn][m, l] < ((PlaceResource)alResourcePlaces[m]).FillAngle || ((PlaceResource)alResourcePlaces[m]).FillAngle == 0)
                                        {
                                            ((PlaceResource)alResourcePlaces[m]).FillAngle = Tr[ctn][m, l];
                                        }

                                        if (this.pnd.TokenGameAnimation == true)
                                        {
                                            // Get connection
                                            Connection cn = Connection.GetConnectionBetweenControls((ConnectableControl)alTransitions[l], (ConnectableControl)alResourcePlaces[m]);

                                            int i = (int)((((PlaceResource)alResourcePlaces[m]).MaxFillAngle - Tr[ctn][m, l]) * 100f / (float)((PlaceResource)alResourcePlaces[m]).MaxFillAngle);

                                            if (i < 100)
                                            {
                                                cn.TokenPositions[ctn] = i;
                                            }
                                            else
                                            {
                                                cn.TokenPositions[ctn] = 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        #endregion

                        #region Adjust Td matrix
                        for (int ctn = 0; ctn < iMaxTokens; ctn++)
                        {
                            for (int m = 0; m < Td0.Dimensions.Height; m++)
                            {
                                for (int l = 0; l < Td0.Dimensions.Width; l++)
                                {
                                    if (Td_temp[m, l] > 0)
                                    {
                                        if (Td[ctn][m, l] > 0)
                                        {
                                            Td[ctn + 1][m, l] = Td_temp[m, l];
                                            Td_temp[m, l]     = 0;
                                        }
                                        else
                                        {
                                            Td[ctn][m, l] = Td_temp[m, l];
                                            Td_temp[m, l] = 0;
                                        }
                                    }

                                    if (Td[ctn][m, l] == 1)
                                    {
                                        imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count, 0] = imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count, 0] + Sd[m, l];
                                        Td[ctn][m, l] = Td[ctn][m, l] - 1;
                                    }
                                }
                            }
                        }
                        #endregion

                        #region Adjust Ty matrix
                        for (int ctn = 0; ctn < iMaxTokens; ctn++)
                        {
                            for (int m = 0; m < Ty0.Dimensions.Height; m++)
                            {
                                for (int l = 0; l < Ty0.Dimensions.Width; l++)
                                {
                                    if (Ty_temp[m, l] > 0)
                                    {
                                        if (Ty[ctn][m, l] > 0)
                                        {
                                            Ty[ctn + 1][m, l] = Ty_temp[m, l];
                                            Ty_temp[m, l]     = 0;
                                        }
                                        else
                                        {
                                            Ty[ctn][m, l] = Ty_temp[m, l];
                                            Ty_temp[m, l] = 0;
                                        }
                                    }

                                    if (Ty[ctn][m, l] > 0)
                                    {
                                        imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count + alControlPlaces.Count, 0] = imk[m + alInputPlaces.Count + alOperationPlaces.Count + alResourcePlaces.Count + alControlPlaces.Count, 0] + Sy[m, l];
                                        Ty[ctn][m, l] = Ty[ctn][m, l] - 1;
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        MessageBox.Show(e.ToString());
                        break;
                    }

                    // Calculate mk+1
                    imk = imk - Ft * imT;

                    #region Set Inputs
                    // Set Inputs
                    if (pnd.InputPlaces.Count != 0)
                    {
                        for (int i = 0; i < pnd.InputPlaces.Count; i++)
                        {
                            PlaceInput pi = (PlaceInput)pnd.InputPlaces[i];
                            if (pi.InputType != InputType.Fixed)
                            {
                                if (((ArrayList)htInputTokens[pi]).Contains(k))
                                {
                                    imk[i, 0]  += 1;
                                    imks[i, 0] += 1;
                                }
                            }
                        }
                    }
                    #endregion

                    this.pnd.Editor.RefreshMT();

                    // Raise SimulationStepFinished event
                    if (this.SimulationStepFinished != null)
                    {
                        this.SimulationStepFinished(this, new EventArgs());
                    }

                    for (int j = 0; j < this.pnd.Places.Count; j++)
                    {
                        ((Place)this.pnd.GroupedPlaces[j]).Tokens = imk[j, 0];
                    }
                    SaveDataForCharting(this.pnd.StepCounter + 1);
                }

                this.bRunning = false;

                // Raise SimulationFinished event
                if (this.SimulationFinished != null)
                {
                    this.SimulationFinished(this, new EventArgs());
                }
            }
            #endregion
        }