private void updatePackage(EA.Package sourcePackage, EA.Package targetPackage) { foreach (EA.Element sourceClass in sourcePackage.Elements) { //Tool.log(repository, sourceClass.Name); //Tool.log(repository, "" + targetPackage.Elements.Count); EA.Element targetClass = null; try { targetClass = targetPackage.Elements.GetByName(sourceClass.Name); } catch (System.Runtime.InteropServices.COMException) { Tool.log(repository, "Model update warning: no " + sourceClass.Name + " have been found in fesapi/" + targetPackage.Name + "!"); continue; } // handling notes if (sourceClass.Notes != "") { targetClass.Notes = sourceClass.Notes; if (!(targetClass.Update())) { Tool.showMessageBox(repository, targetClass.GetLastError()); continue; } } // handling methods updateMethods(sourceClass, targetClass); // handling attributes updateAttributes(sourceClass, targetClass); } }
/// <summary> /// Updated the energistics model (common and resqml model) according to the input fesapi model. /// </summary> public void updateEnergisticsModel() { // looking for: // - fesapi classes that exists in the energistics model (according to their name). For // that purpose, toUpdateClassF2E makes a correspondance between a fesapi class (key) and // corresponding Energistics class (value). These classes will then be updated into // the energistics model // - fesapi classes that does not exist in the Energistics model. These classes are collected // into toAddClassList. These classes will then be added to the Energistics model Dictionary <EA.Element, EA.Element> toUpdateClassF2E = new Dictionary <EA.Element, EA.Element>(); List <EA.Element> toAddClassList = new List <EA.Element>(); foreach (EA.Element fesapiClass in fesapiClassList) { EA.Element energisticsClass = energisticsClassList.Find(c => c.Name.Equals(fesapiClass.Name)); if (energisticsClass != null) { toUpdateClassF2E.Add(fesapiClass, energisticsClass); } else { toAddClassList.Add(fesapiClass); } } // console output Tool.log(repository, "classes to update:"); foreach (KeyValuePair <EA.Element, EA.Element> entry in toUpdateClassF2E) { Tool.log(repository, " " + entry.Value.Name); } Tool.log(repository, "classes to add:"); foreach (EA.Element c in toAddClassList) { Tool.log(repository, " " + c.Name); } // handling classes that exist into the Energistics model foreach (KeyValuePair <EA.Element, EA.Element> entry in toUpdateClassF2E) { EA.Element fesapiClass = entry.Key; EA.Element energisticsClass = entry.Value; // adding a fesapiGeneration tag to true EA.TaggedValue t = energisticsClass.TaggedValues.AddNew(Constants.fesapiGenerationTagName, "true"); if (!(t.Update())) { Tool.showMessageBox(repository, t.GetLastError()); continue; } energisticsClass.TaggedValues.Refresh(); // adding a fesapiNamespace tag. The tag value is set to the name of // package containing the fesapi class. This tag will be used to determine: // - the name space of the generated class // - the output directory of the generated class EA.Package p = repository.GetPackageByID(fesapiClass.PackageID); t = energisticsClass.TaggedValues.AddNew(Constants.fesapiNamespaceTagName, p.Name); if (!(t.Update())) { Tool.showMessageBox(repository, t.GetLastError()); continue; } energisticsClass.TaggedValues.Refresh(); // updating the ENergistics class according to the corresponding // fesapi class updateEnergisticsClass(energisticsClass, fesapiClass); } // handling classes that does not exist into the Energistics model foreach (EA.Element c in toAddClassList) { // getting the package carrying the fesapi class EA.Package sourcePackagePackage = repository.GetPackageByID(c.PackageID); // getting the package where to create the corresponding Energistics class EA.Package targetPackage = null; if (sourcePackagePackage.Name.Contains("common")) { targetPackage = Tool.getOrCreatePackage(repository, commonModel, "fesapi"); } else if (sourcePackagePackage.Name.Contains("resqml")) { targetPackage = Tool.getOrCreatePackage(repository, resqmlModel, "fesapi"); } if (targetPackage == null) { Tool.showMessageBox(repository, "The fesapi class " + c.Name + " is not handled. It will be skipped!"); continue; } // creating the new class EA.Element newClass = Tool.copyElement(repository, c, targetPackage); if (newClass == null) { Tool.showMessageBox(repository, "Not able to create a " + c.Name + " class in the Energistics model"); continue; } // adding a fesapiGeneration tag to true EA.TaggedValue t = newClass.TaggedValues.AddNew(Constants.fesapiGenerationTagName, "true"); if (!(t.Update())) { Tool.showMessageBox(repository, t.GetLastError()); continue; } newClass.TaggedValues.Refresh(); // adding a fesapiNamespace tag. The tag value is set to the name of // package containing the fesapi class. This tag will be used to determine: // - the name space of the generated class // - the output directory of the generated class t = newClass.TaggedValues.AddNew(Constants.fesapiNamespaceTagName, sourcePackagePackage.Name); if (!(t.Update())) { Tool.showMessageBox(repository, t.GetLastError()); continue; } newClass.TaggedValues.Refresh(); } // looking in the Energistics common model for a newly created EpcDocument class // if such class exists, we create its easy access vector members EA.Package commonFesapiPackage; try { commonFesapiPackage = commonModel.Packages.GetByName("fesapi"); } catch (Exception) { commonFesapiPackage = null; } if (commonFesapiPackage != null) { EA.Element epcDocumentClass = commonFesapiPackage.Elements.GetByName("EpcDocument"); if (epcDocumentClass != null) { createEpcDocumentEasyAccessVector(epcDocumentClass, toUpdateClassF2E); } } // make sure the model view is up to date in the Enterprise Architect GUI repository.RefreshModelView(0); }