001package org.andromda.core.engine;
002
003import java.util.Collection;
004import org.andromda.core.configuration.Configuration;
005import org.andromda.core.metafacade.ModelValidationMessage;
006
007/**
008 * The <em>engine</em> of AndroMDA. Handles the configuration of AndroMDA and
009 * loading/processing of models by plugins. Basically a wrapper around the {@link ModelProcessor}
010 * that takes a configuration file in order to configure AndroMDA.
011 *
012 * @see ModelProcessor
013 * @author Chad Brandon
014 * @author Bob Fields
015 */
016public final class Engine
017{
018    /**
019     * Create a new Engine instance.
020     *
021     * @return the new instance of Engine.
022     */
023    public static Engine newInstance()
024    {
025        return new Engine();
026    }
027
028    /**
029     * The model processor for this engine.
030     */
031    private ModelProcessor modelProcessor;
032
033    private Engine()
034    {
035        // do not allow instantiation
036        this.modelProcessor = ModelProcessor.newInstance();
037    }
038
039    /**
040     * Initializes Engine (discovers all plugins, etc) with the
041     * given configuration.  This configuration is overridden (if changed)
042     * when calling {@link #run(Configuration, boolean, String)}.
043     * @param configuration
044     */
045    public void initialize(final Configuration configuration)
046    {
047        this.modelProcessor.initialize(configuration);
048    }
049
050    /**
051     * Checks to see if any of the models in the given configuration
052     * should be loaded (based on whether or not they've been modified),
053     * and if so, performs the load.  This way the
054     * models are loaded for the next run of the model processor.
055     *
056     * @param configuration the AndroMDA configuration the contains the repositories containing
057     *        the models to load.
058     * @return messages from modelProcessor.loadIfNecessary
059     */
060    public ModelValidationMessage[] loadModelsIfNecessary(final Configuration configuration)
061    {
062        ModelValidationMessage[] messages = null;
063        if (configuration != null)
064        {
065            final Collection<ModelValidationMessage> messagesList =
066                    this.modelProcessor.loadIfNecessary(configuration.getRepositories());
067            messages =
068                messagesList.toArray(
069                    new ModelValidationMessage[messagesList.size()]);
070        }
071        return messages == null ? new ModelValidationMessage[0] : messages;
072    }
073
074    /**
075     * Runs Engine with the given configuration.
076     *
077     * @param configuration the String that contains the configuration
078     *        contents for configuring Engine.
079     * @param lastModifiedCheck Check for model modifications after last generation
080     * @param historyDir Overrides model lastModifiedCheck globally
081     * @return the new instance of Engine.
082     */
083    public ModelValidationMessage[] run(final Configuration configuration,
084        boolean lastModifiedCheck, final String historyDir)
085    {
086        ModelValidationMessage[] messages = null;
087        if (configuration != null)
088        {
089            this.modelProcessor.setHistoryDir(historyDir);
090            this.modelProcessor.setLastModifiedCheck(lastModifiedCheck);
091            messages = this.modelProcessor.process(configuration);
092        }
093        return messages == null ? new ModelValidationMessage[0] : messages;
094    }
095
096    /**
097     * Shuts down this instance.
098     */
099    public void shutdown()
100    {
101        this.modelProcessor.shutdown();
102    }
103}