Exemple #1
0
        private void CleanUpTempMgaProject(string outputFile)
        {
            var  newProject = new MgaProject();
            bool ro_mode;

            // open the copied temporary mga file
            newProject.Open("MGA=" + outputFile, out ro_mode);
            IMgaTerritory terr = null;

            try
            {
                // create a new transaction
                terr = newProject.BeginTransactionInNewTerr();

                // get the sot object on which the job manager will operate
                var sot = newProject.GetObjectByID(sotConfig.SoTID);

                if (sot != null)
                {
                    // if the sot object exists in the project
                    GME.MGA.Meta.objtype_enum objtype;
                    MgaObject parent;

                    // get the parent folder of which this SoT belongs to.
                    sot.GetParent(out parent, out objtype);

                    if (objtype == GME.MGA.Meta.objtype_enum.OBJTYPE_FOLDER)
                    {
                        // if it is really a folder cast it
                        var parentSoTFolder = parent as MgaFolder;

                        // get all SoT objects in the folder except the one we operate on
                        // mark them for deletion
                        var sotsToDelete = parentSoTFolder
                                           .ChildFCOs
                                           .Cast <MgaFCO>()
                                           .Where(x => x.ID != sotConfig.SoTID)
                                           .ToList();

                        try
                        {
                            // destroy all SoTs that are not used by this sot run
                            sotsToDelete.ForEach(x => x.DestroyObject());
                        }
                        catch
                        {
                            // ok
                        }

                        // get all not null test bench objects that this sot run uses.
                        var referredTBsInSot = sot
                                               .ChildObjects
                                               .OfType <MgaReference>()
                                               .Where(x => x.Meta.Name == typeof(CyPhy.TestBenchRef).Name && x.Referred != null)
                                               .Select(x => x.Referred)
                                               .Cast <MgaModel>()
                                               .ToList();

                        foreach (var tb in referredTBsInSot)
                        {
                            GME.MGA.Meta.objtype_enum tbParentObjtype;
                            MgaObject tbParent;

                            // get the parent folder where the referenced test bench located
                            tb.GetParent(out tbParent, out tbParentObjtype);
                            if (tbParentObjtype == GME.MGA.Meta.objtype_enum.OBJTYPE_FOLDER)
                            {
                                var parentTBFolder = tbParent as MgaFolder;

                                // get all test benches that are not used by the sot
                                // WARNING: sometimes COM object comparison can fail this code is not the safest.
                                var tbsToDelete = parentTBFolder
                                                  .ChildFCOs
                                                  .OfType <MgaModel>()
                                                  .Where(x => referredTBsInSot.Contains(x) == false)
                                                  .ToList();

                                try
                                {
                                    // delete test benches that sot does not use
                                    tbsToDelete.ForEach(x => x.DestroyObject());
                                }
                                catch
                                {
                                    // ok
                                }
                            }
                        }
                    }
                }

                // commit changes if everything is successful
                newProject.CommitTransaction();
            }
            catch
            {
                // abort all changes if exception occured
                newProject.AbortTransaction();
            }
            finally
            {
                if (terr != null)
                {
                    // destroy the territory we were working in if it is not null
                    terr.Destroy();
                }
            }

            // close the project AND save all changes
            newProject.Close();
        }
        public T GetGMEObjectFromIdentification <T>(MgaProject project, string identification) where T : IMgaObject
        {
            T result = default(T);

            // is project in transaction already?
            bool projectWasNotInTransaction = (project.ProjectStatus & 8) == 0;

            try
            {
                if (projectWasNotInTransaction)
                {
                    // open a transaction if it is not in a transaction already.
                    project.BeginTransactionInNewTerr();
                }

                // regexp for GME ID - case insensitive
                string idPattern = @"(id-006[0-9a-f]{1}-[0-9a-f]{8})"; // hexadecimal char: [0-9a-f]

                System.Text.RegularExpressions.Regex regexId =
                    new System.Text.RegularExpressions.Regex(idPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                bool isId = regexId.IsMatch(identification);

                // regexp for GUID - case insensitive
                string guidPattern = @"(\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\})";

                System.Text.RegularExpressions.Regex regexGuid =
                    new System.Text.RegularExpressions.Regex(guidPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                bool isGuid = regexGuid.IsMatch(identification);

                if (isId)
                {
                    // assume it is an id
                    identification = identification.ToLowerInvariant();

                    result = (T)project.GetObjectByID(identification);
                }
                else if (isGuid)
                {
                    // FIXME: does not work for folders

                    // assume it is a GUID
                    identification = identification.ToLowerInvariant();

                    // this may take time, no better method on the GME API at this point.
                    foreach (MgaFCO fco in project.AllFCOs(project.CreateFilter()))
                    {
                        if (fco.GetGuidDisp() == identification)
                        {
                            result = (T)fco;
                            break;
                        }
                    }
                }
                else if (identification.StartsWith("/"))
                {
                    // assume it is an AbsPath
                    if (identification.StartsWith("/@") == false)
                    {
                        // inject @ signs for the user
                        // FIXME: what if the name has / for any objects?
                        identification = identification.Replace("/", "/@");
                    }

                    result = (T)project.ObjectByPath[identification];
                }
                else
                {
                    throw new FormatException(string.Format("Identification must be a GME ID 'id-006X-YYYYYYYY' or a GUID '{{guid}}' or an AbsPath '/@...' : given value:'{0}'", identification));
                }
            }
            finally
            {
                if (projectWasNotInTransaction)
                {
                    project.AbortTransaction();
                }
            }

            return(result);
        }