001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.uml14.maven.plugin;
009
010import java.io.File;
011import java.util.ArrayList;
012import java.util.List;
013
014import javax.jmi.reflect.RefPackage;
015
016import org.apache.maven.plugin.AbstractMojo;
017import org.apache.maven.plugin.MojoExecutionException;
018import org.apache.maven.plugins.annotations.Execute;
019import org.apache.maven.plugins.annotations.LifecyclePhase;
020import org.apache.maven.plugins.annotations.Mojo;
021import org.apache.maven.plugins.annotations.Parameter;
022
023import net.mdatools.modelant.core.api.diff.Export;
024import net.mdatools.modelant.core.api.diff.ModelComparisonResult;
025import net.mdatools.modelant.core.api.match.ConsideredEqual;
026import net.mdatools.modelant.core.operation.model.CompareModels;
027import net.mdatools.modelant.repository.api.ModelFactory;
028import net.mdatools.modelant.repository.api.ModelRepository;
029import net.mdatools.modelant.repository.api.ModelRepositoryFactory;
030import net.mdatools.modelant.uml14.metamodel.CompareUml14Models;
031
032/**
033 * Compare two UML 1.4 models and report the differences, that are needed to convert the source model into the target one
034 * @author Rusi Popov (popovr@mdatools.net)
035 */
036@Mojo(name="compare-uml14-models",
037defaultPhase=LifecyclePhase.COMPILE
038)
039@Execute(phase=LifecyclePhase.COMPILE)
040public class CompareModelsMojo extends AbstractMojo {
041
042  /**
043   * The name of the file with the source (original) model
044   */
045  @Parameter(property="project.build.sourceDirectory", required=true)
046  private File sourceModel;
047
048  /**
049   * The name of the file with the target (changed) model
050   */
051  @Parameter(required=true)
052  private File targetModel;
053
054  /**
055   * The directory where the temporary internal files are located
056   */
057  @Parameter(property="project.build.directory", required=true)
058  private File workDirectory;
059
060  /**
061   * Pairs of <metaclass>, <metapackage> as pairs of source and target metamodel classes,
062   * that should be considered equal.
063   */
064  @Parameter
065  private List<ConsideredEqual> equals;
066
067  /**
068   * The mechanism to export the result of models comparison. Default: print the string representation
069   */
070  @Parameter
071  private Export export = Export.DEFAULT;
072
073  public void execute() throws MojoExecutionException {
074    ModelRepository repository;
075    ModelFactory metamodelFactory;
076    CompareModels compare;
077    RefPackage sourceExtent;
078    RefPackage targetExtent;
079    ModelComparisonResult result;
080    List<ConsideredEqual> bindings;
081
082    // lookup the implementation in the plugin's classpath, as it should be a dependency of this plugin
083    repository = ModelRepositoryFactory.construct(workDirectory);
084    try {
085      metamodelFactory = repository.loadMetamodel("UML14");
086      sourceExtent = metamodelFactory.instantiate("SOURCE");
087      targetExtent = metamodelFactory.instantiate("TARGET");
088
089      repository.readIntoExtent( sourceExtent, sourceModel );
090      repository.readIntoExtent( targetExtent, targetModel );
091
092      if (equals==null) {
093        bindings = new ArrayList<>();
094      } else {
095        bindings = equals;
096      }
097      compare = new CompareUml14Models(bindings, sourceExtent);
098
099      result = compare.execute( targetExtent );
100
101      export.export(result);
102
103    } catch (Exception ex) {
104      throw new MojoExecutionException( "The target failed with:", ex);
105
106    } finally {
107      repository.shutdown();
108    }
109  }
110}