1 package org.andromda.core.engine;
2
3 import java.util.Collection;
4 import org.andromda.core.configuration.Configuration;
5 import org.andromda.core.metafacade.ModelValidationMessage;
6
7 /**
8 * The <em>engine</em> of AndroMDA. Handles the configuration of AndroMDA and
9 * loading/processing of models by plugins. Basically a wrapper around the {@link ModelProcessor}
10 * that takes a configuration file in order to configure AndroMDA.
11 *
12 * @see ModelProcessor
13 * @author Chad Brandon
14 * @author Bob Fields
15 */
16 public final class Engine
17 {
18 /**
19 * Create a new Engine instance.
20 *
21 * @return the new instance of Engine.
22 */
23 public static Engine newInstance()
24 {
25 return new Engine();
26 }
27
28 /**
29 * The model processor for this engine.
30 */
31 private ModelProcessor modelProcessor;
32
33 private Engine()
34 {
35 // do not allow instantiation
36 this.modelProcessor = ModelProcessor.newInstance();
37 }
38
39 /**
40 * Initializes Engine (discovers all plugins, etc) with the
41 * given configuration. This configuration is overridden (if changed)
42 * when calling {@link #run(Configuration, boolean, String)}.
43 * @param configuration
44 */
45 public void initialize(final Configuration configuration)
46 {
47 this.modelProcessor.initialize(configuration);
48 }
49
50 /**
51 * Checks to see if any of the models in the given configuration
52 * should be loaded (based on whether or not they've been modified),
53 * and if so, performs the load. This way the
54 * models are loaded for the next run of the model processor.
55 *
56 * @param configuration the AndroMDA configuration the contains the repositories containing
57 * the models to load.
58 * @return messages from modelProcessor.loadIfNecessary
59 */
60 public ModelValidationMessage[] loadModelsIfNecessary(final Configuration configuration)
61 {
62 ModelValidationMessage[] messages = null;
63 if (configuration != null)
64 {
65 final Collection<ModelValidationMessage> messagesList =
66 this.modelProcessor.loadIfNecessary(configuration.getRepositories());
67 messages =
68 messagesList.toArray(
69 new ModelValidationMessage[messagesList.size()]);
70 }
71 return messages == null ? new ModelValidationMessage[0] : messages;
72 }
73
74 /**
75 * Runs Engine with the given configuration.
76 *
77 * @param configuration the String that contains the configuration
78 * contents for configuring Engine.
79 * @param lastModifiedCheck Check for model modifications after last generation
80 * @param historyDir Overrides model lastModifiedCheck globally
81 * @return the new instance of Engine.
82 */
83 public ModelValidationMessage[] run(final Configuration configuration,
84 boolean lastModifiedCheck, final String historyDir)
85 {
86 ModelValidationMessage[] messages = null;
87 if (configuration != null)
88 {
89 this.modelProcessor.setHistoryDir(historyDir);
90 this.modelProcessor.setLastModifiedCheck(lastModifiedCheck);
91 messages = this.modelProcessor.process(configuration);
92 }
93 return messages == null ? new ModelValidationMessage[0] : messages;
94 }
95
96 /**
97 * Shuts down this instance.
98 */
99 public void shutdown()
100 {
101 this.modelProcessor.shutdown();
102 }
103 }