All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cdc.mf.html.MfToPumlGenerator Maven / Gradle / Ivy

The newest version!
package cdc.mf.html;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupDir;

import cdc.mf.html.adaptors.MfElementAdaptor;
import cdc.mf.model.MfElement;
import cdc.mf.model.MfPackage;
import cdc.mf.model.MfQNameItem;
import cdc.mf.model.MfType;
import cdc.util.files.Resources;

/**
 * Generator of diagrams using ST4 and PlantUML.
 *
 * @author Damien Carbonne
 */
public final class MfToPumlGenerator extends AbstractGenerator {
    public MfToPumlGenerator(MfHtmlGenerationArgs args) {
        super(args);
    }

    public void generate() throws ExecutionException {
        final URL templates = Resources.getResource("cdc/mf/html/templates");
        final STGroupDir stg = new STGroupDir(templates);

        stg.registerModelAdaptor(MfElement.class, new MfElementAdaptor(args));

        addDictionaries(stg);

        final List tasks = new ArrayList<>();
        addModelOverviewTask(stg, tasks);
        addPackageOverviewTasks(stg, tasks);
        addTypeOverviewTasks(stg, tasks);
        final List pumlFiles = invokeTasks("PlantUML generation", tasks);

        generateImages(pumlFiles);
    }

    /**
     * Creates a task to generate model overview diagram.
     *
     * @param stg The ST group.
     * @param tasks The list of tasks.
     */
    private void addModelOverviewTask(STGroup stg,
                                      List tasks) {
        final ST st = stg.getInstanceOf("mf-puml/generateModelOverviewImage");

        st.add(MfParams.Names.MODEL, args.getModel());

        tasks.add(new STWorker(st, MfParams.Files.getImgOverviewFile(args.getModel(), ImageType.PUML)));
    }

    /**
     * Creates tasks to generate package overview diagrams.
     *
     * @param stg The ST group.
     * @param tasks The list of tasks.
     */
    private void addPackageOverviewTasks(STGroup stg,
                                         List tasks) {
        final List packages = args.getModel().collect(MfPackage.class);
        packages.sort(MfQNameItem.QNAME_COMPARATOR);

        for (final MfPackage pack : packages) {
            final ST st = stg.getInstanceOf("mf-puml/generatePackageOverviewImage");

            st.add(MfParams.Names.PACKAGE, pack);
            st.add(MfParams.Names.LTOR, MfParams.getItemsCount(pack, args) > args.getLtorThreshold());

            tasks.add(new STWorker(st, MfParams.Files.getImgPackageOverviewFile(pack, ImageType.PUML)));
        }
    }

    /**
     * Creates tasks to generate type overview diagrams.
     *
     * @param stg The ST group.
     * @param tasks The list of tasks.
     */
    private void addTypeOverviewTasks(STGroup stg,
                                      List tasks) {
        final List types = args.getModel().collect(MfType.class);
        types.sort(MfQNameItem.QNAME_COMPARATOR);

        for (final MfType type : types) {
            final ST st = stg.getInstanceOf("mf-puml/generateTypeOverviewImage");

            st.add(MfParams.Names.TYPE, type);
            st.add(MfParams.Names.LTOR, MfParams.getItemsCount(type, args) > args.getLtorThreshold());

            tasks.add(new STWorker(st, MfParams.Files.getImgTypeOverviewFile(type, ImageType.PUML)));
        }
    }

    /**
     * Creates task to generate images and generates them.
     *
     * @param pumlFiles The PlantUML files to convert to images.
     * @throws ExecutionException When an exception occurs.
     */
    private void generateImages(List pumlFiles) throws ExecutionException {
        final List> tasks = new ArrayList<>();

        for (final File file : pumlFiles) {
            final PumlToAny.MainArgs margs = new PumlToAny.MainArgs();
            margs.outputDir = new File(args.getBaseDir(), MfParams.Dirs.IMAGES.getPath());
            margs.input = file;
            if (args.getHints().contains(MfHtmlGenerationHint.VERBOSE)) {
                margs.features.add(PumlToAny.MainArgs.Feature.VERBOSE);
            }
            if (args.getHints().contains(MfHtmlGenerationHint.FORCE)) {
                margs.features.add(PumlToAny.MainArgs.Feature.FORCE);
            }
            if (args.getHints().contains(MfHtmlGenerationHint.IMG_SVG)) {
                margs.features.add(PumlToAny.MainArgs.Feature.SVG);
            }
            tasks.add(() -> {
                PumlToAny.execute(margs);
                return null;
            });
        }

        invokeTasks("Images generation", tasks);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy