Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fb70a68f00fb3da411892f91cb9050c48aa22d8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.java;

import java.util.Iterator;

import org.eclipse.emf.common.EMFPlugin;
import org.eclipse.etrice.core.etmap.ETMapStandaloneSetup;
import org.eclipse.etrice.core.etmap.util.ETMapUtil;
import org.eclipse.etrice.core.etphys.ETPhysStandaloneSetup;
import org.eclipse.etrice.core.genmodel.etricegen.Root;
import org.eclipse.etrice.generator.base.AbstractGenerator;
import org.eclipse.etrice.generator.base.IDataConfiguration;
import org.eclipse.etrice.generator.java.gen.GlobalSettings;
import org.eclipse.etrice.generator.java.gen.MainGen;
import org.eclipse.etrice.generator.java.gen.Validator;
import org.eclipse.etrice.generator.java.setup.GeneratorModule;
import org.eclipse.xtext.scoping.impl.ImportUriResolver;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class Main extends AbstractGenerator {
	
	public static final String OPTION_GEN_PERSIST = "-persistable";
	public static final String OPTION_GEN_STORE_DATA_OBJ = "-storeDataObj";
	
	/**
	 * print usage message to output/console
	 */
	protected void printUsage() {
		output.println(this.getClass().getName()+getCommonOptions()
				+" ["+OPTION_GEN_PERSIST+"]"
				+" ["+OPTION_GEN_STORE_DATA_OBJ+"]"
				+" <list of model file paths>");
		output.println(getCommonOptionDescriptions());
		output.println("      -persistable                       # if specified make actor classes persistable");
		output.println("      -storeDataObj                      # if specified equip actor classes with store/restore using POJOs");
	}

	/**
	 * The main method delegates to {@link #createAndRunGenerator(com.google.inject.Module, String[])}
	 * and calls {@link System#exit(int)} if an error occurred and {@link #isTerminateOnError()}.
	 * 
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		int ret = createAndRunGenerator(new GeneratorModule(), args);
		if (isTerminateOnError() && ret!=GENERATOR_OK)
			System.exit(ret);
	}

	@Inject
	private MainGen mainGenerator;

	@Inject
	protected org.eclipse.etrice.generator.doc.gen.MainGen mainDocGenerator; 
	
	@Inject
	private Validator validator;
	
	@Inject
	protected IDataConfiguration dataConfig;
	
	@Inject
	protected ImportUriResolver uriResolver;
	
	/**
	 * This method recognizes the option {@value #OPTION_GEN_PERSIST} and delegates all
	 * other cases to super.
	 * 
	 * @see org.eclipse.etrice.generator.base.AbstractGenerator#parseOption(java.util.Iterator)
	 */
	@Override
	protected boolean parseOption(String arg, Iterator<String> it) {
		if (arg.equals(OPTION_GEN_PERSIST)) {
			getSettings().setGeneratePersistenceInterface(true);
			return true;
		}
		else if (arg.equals(OPTION_GEN_STORE_DATA_OBJ)) {
			getSettings().setGenerateStoreDataObj(true);
			return true;
		}
		
		return super.parseOption(arg, it);
	}

	/**
	 * @return the unique {@link GlobalSettings}
	 */
	public static GlobalSettings getSettings() {
		return (GlobalSettings) getInstance().getGeneratorSettings();
	}

	/**
	 * setup the eTrice mapping model plug-in
	 */
	protected void setupMappingModel() {
		if (!EMFPlugin.IS_ECLIPSE_RUNNING) {
			ETMapStandaloneSetup.doSetup();
		}
	}

	/**
	 * setup the eTrice mapping model plug-in
	 */
	protected void setupPhysicalModel() {
		if (!EMFPlugin.IS_ECLIPSE_RUNNING)
			ETPhysStandaloneSetup.doSetup();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.generator.base.AbstractGenerator#runGenerator()
	 */
	protected int runGenerator() {
		setupRoomModel();
		dataConfig.doSetup();
		setupMappingModel();
		setupPhysicalModel();
		
		try {
			activateModelLocator();
			
			if (!loadModels(getSettings().getInputModelURIs())) {
				logger.logInfo("loading of models failed");
				logger.logError("-- terminating", null);
				return GENERATOR_ERROR;
			}
			
			if (!validateModels()) {
				logger.logInfo("validation failed");
				logger.logError("-- terminating", null);
				return GENERATOR_ERROR;
			}
			
			if (!dataConfig.setResources(getResourceSet(), logger)) {
				logger.logInfo("configuration errors");
				logger.logError("-- terminating", null);
				return GENERATOR_ERROR;
			}
			
			Root genModel = createGeneratorModel(getSettings().isGenerateAsLibrary(), getSettings().getGeneratorModelPath());
			if (diagnostician.isFailed() || genModel==null) {
				logger.logInfo("errors during build of generator model");
				logger.logError("-- terminating", null);
				return GENERATOR_ERROR;
			}
			
			if (!validator.validate(genModel)) {
				logger.logInfo("validation failed during build of generator model");
				logger.logError("-- terminating", null);
				return GENERATOR_ERROR;
			}
			
			ETMapUtil.processModels(genModel, getResourceSet(), diagnostician);
			if (getSettings().isDebugMode()) {
				logger.logInfo("-- begin dump of mappings");
				logger.logInfo(ETMapUtil.dumpMappings());
				logger.logInfo("-- end dump of mappings");
			}
			if (diagnostician.isFailed() || genModel==null) {
				logger.logInfo("errors in mapping");
				logger.logError("-- terminating", null);
				return GENERATOR_ERROR;
			}
			
			logger.logInfo("-- starting code generation");
			mainGenerator.doGenerate(genModel.eResource());
			
			if (getSettings().isGenerateDocumentation()) {
				mainDocGenerator.doGenerate(genModel.eResource());
			}
			
			if (diagnostician.isFailed()) {
				logger.logInfo("errors during code generation");
				logger.logError("-- terminating", null);
				return GENERATOR_ERROR;
			}
			logger.logInfo("-- finished code generation");
		}
		finally {
			deactivateModelLocator();
		}
		
		return GENERATOR_OK;
	}
}

Back to the top