Пример #1
0
  /**
   * Create a new empty model from an existing model that applies the same profiles and has the same
   * imports
   *
   * @param existingModel
   * @return
   */
  public ModelManagement createTargetModel(Model existingModel, String name, boolean copyImports)
      throws TransformationException {
    ModelManagement mm = new ModelManagement();
    Model newModel = mm.getModel();
    newModel.setName(name);

    try {
      // copy profile application
      for (Profile profile : existingModel.getAppliedProfiles()) {
        // reload profile in resource of new model
        monitor.subTask(Messages.InstantiateDepPlan_InfoApplyProfile + profile.getQualifiedName());

        if (profile.eResource() == null) {
          String profileName = profile.getQualifiedName();
          if (profileName == null) {
            if (profile instanceof MinimalEObjectImpl.Container) {
              URI uri = ((MinimalEObjectImpl.Container) profile).eProxyURI();
              if (uri != null) {
                throw new TransformationException(
                    String.format(Messages.InstantiateDepPlan_CheckInputModelProfileNoRes, uri));
              }
            }
            throw new TransformationException(
                Messages.InstantiateDepPlan_CheckInputModelProfileNoResNoName);
          }
          throw new TransformationException(
              String.format(Messages.InstantiateDepPlan_CheckInputModelProfile3, profileName));
        }

        Resource profileResource = null;
        try {
          profileResource =
              ModelManagement.getResourceSet().getResource(profile.eResource().getURI(), true);
        } catch (WrappedException e) {
          // read 2nd time (some diagnostic errors are raised only
          // once)
          Log.log(
              IStatus.WARNING,
              Log.DEPLOYMENT,
              "Warning: exception in profile.eResource() " + e.getMessage()); // $NON-NLS-1$
          profileResource =
              ModelManagement.getResourceSet().getResource(profile.eResource().getURI(), true);
        }
        Profile newProfileTop = (Profile) profileResource.getContents().get(0);
        Profile newProfile;
        String qname = profile.getQualifiedName();
        if ((qname != null) && qname.contains("::")) { // $NON-NLS-1$
          // profile is a sub-profile within same resource
          // TODO: should Copy class copy profile applications?
          // Should be handled in shallowContainer class.
          // if we put profile/newProfile pair into copy map, copy
          // would find (and copy profile
          // applications in sub-folders
          qname = qname.substring(qname.indexOf("::") + 2); // $NON-NLS-1$
          newProfile = (Profile) Utils.getQualifiedElement(newProfileTop, qname);
        } else {
          newProfile = newProfileTop;
        }
        newProfile.getMember("dummy"); // force profile loading //$NON-NLS-1$
        newModel.applyProfile(newProfile);
      }
    } catch (IllegalArgumentException e) {
      throw new TransformationException(
          Messages.InstantiateDepPlan_IllegalArgumentDuringCopy + e.toString());
    }

    // copy imports (and load resources associated - TODO: might not be
    // necessary)
    // While this is useful in general, it implies that code for imported
    // models
    // has been generated and compiled (for the right target) into a
    // library. This may be
    // quite tedious, unless automatically managed.
    // Therefore we do not activate this option in a first pass of the model
    // transformations.
    if (copyImports) {
      for (Package importedPackage : existingModel.getImportedPackages()) {
        if (importedPackage == null) {
          throw new TransformationException(Messages.InstantiateDepPlan_CheckInputImportPkg);
        }
        if (importedPackage.eResource() == null) {
          String errorMsg = Messages.InstantiateDepPlan_CheckInputImportPkgNoRes;
          if (importedPackage instanceof MinimalEObjectImpl.Container) {
            URI uri = ((MinimalEObjectImpl.Container) importedPackage).eProxyURI();
            if (uri != null) {
              errorMsg += " - URI: " + uri.devicePath(); // $NON-NLS-1$
            }
          }
          throw new TransformationException(errorMsg);
        }
        newModel.createPackageImport(importedPackage);
        monitor.subTask(
            String.format(
                Messages.InstantiateDepPlan_InfoImportPackage, importedPackage.getName()));

        try {
          importedPackage.eResource().load(null);
          newModel.getMember("dummy"); // force loading of model //$NON-NLS-1$
        } catch (IOException e) {
          throw new TransformationException(e.getMessage());
        }
      }
    }

    StUtils.copyStereotypes(existingModel, newModel);

    return mm;
  }
Пример #2
0
 private void printAndDisplayErrorMessage(
     Exception e, final String title, final String message, final boolean consultConsole) {
   e.printStackTrace();
   displayError(title, message);
   Log.log(IStatus.ERROR, Log.DEPLOYMENT, "", e); // $NON-NLS-1$
 }