Skip to main content
summaryrefslogtreecommitdiffstats
blob: 670cfa133a98d0f919ed9264d38f5a1ff5e050a4 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
 * Copyright (c) 2008 INRIA.
 * 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:
 *     INRIA - initial API and implementation
 *
 * $Id: WorkspaceLanguage.java,v 1.9 2008/06/24 14:27:32 fjouault Exp $
 */
package org.eclipse.gmt.tcs.metadata.adhoc;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.gmt.tcs.extractor.TCSExtractor;
import org.eclipse.gmt.tcs.metadata.Language;
import org.eclipse.gmt.tcs.metadata.LanguageSource;
import org.eclipse.gmt.tcs.metadata.ModelFactory;
import org.eclipse.gmt.tcs.metadata.VMLauncher;
import org.eclipse.m2m.atl.dsls.DSLResourceProvider;
import org.eclipse.m2m.atl.dsls.textsource.IFileTextSource;
import org.eclipse.m2m.atl.dsls.textsource.TextSource;
import org.eclipse.m2m.atl.engine.AtlEMFModelHandler;
import org.eclipse.m2m.atl.engine.AtlModelHandler;
import org.eclipse.m2m.atl.engine.vm.ASM;
import org.eclipse.m2m.atl.engine.vm.ASMXMLReader;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMModel;

/**
 * 
 * @author Frédéric Jouault
 */
public class WorkspaceLanguage implements Language, LanguageSource {

	private static class ModelCacheFromIFile extends ModelCache {
		private String metamodelName;
		private URI metamodelURI;
		private String modelName;
		private IFile modelFile;
		private long timestamp;

		public ModelCacheFromIFile(String metamodelName, URI metamodelURI, String modelName, IFile modelFile) {
			this.metamodelName = metamodelName;
			this.metamodelURI = metamodelURI;
			this.modelName = modelName;
			this.modelFile = modelFile;
		}

		protected Object loadModel(ModelFactory factory) {
			Object metamodel = factory.loadMetamodel(metamodelName, metamodelURI);
			Object ret = null;
			try {
				ret = factory.loadModel(modelName + "-" + metamodelName, metamodel, modelFile.getContents());
				this.timestamp = modelFile.getModificationStamp();
			} catch (CoreException e) {
				throw new RuntimeException(e);
			}
			return ret;
		}

		protected boolean shouldReload() {
			return modelFile.getModificationStamp() != timestamp;
		}
	}

	private static final String DEFAULT_PARSER_GENERATOR_NAME = "antlr3";

	private IProject project;
	private String name;
	private String extension;
	// TODO: properties timestamp
	private long propertiesTimestamp;
	private IFile propertiesFile;
	private Properties properties;
	private IFile editorFile;
	private ModelCache editorCache;
	private IFile outlineFile;
	private ModelCache outlineCache;
	private IFile ecoreFile;
	private ModelCache metamodelCache;
	private IFile compilerFile;

	private IFile jarFile;
	private IFile km3File;
	private IFile annotationFile;
	private IFile tcsFile;
	private IFile antlrFile;
	private IFile acgFile;

	public WorkspaceLanguage(IProject project) {
		this.project = project;
		propertiesFile = project.getFile("build.properties");

		if(!propertiesFile.exists()) {
			throw new RuntimeException("Missing property file");
		}

		try {
			properties = new Properties();
			InputStream in = propertiesFile.getContents();
			properties.load(in);
			in.close();
			propertiesTimestamp = propertiesFile.getModificationStamp();
		} catch(IOException e) {
			throw new RuntimeException("Error reading properties file", e);
		} catch(CoreException e) {
			throw new RuntimeException("Error reading properties file", e);
		}

		name = properties.getProperty("dsl.name");
		extension = properties.getProperty("dsl.ext");
		if(extension == null) {
			throw new RuntimeException("Missing extension");
		}

		if(name == null) {
			throw new RuntimeException("Missing name");
		}

		km3File = project.getFile("Metamodel/" + name + ".km3");
		annotationFile = project.getFile("Metamodel/" + name + ".ann");
		tcsFile = project.getFile("Syntax/" + name + ".tcs");

		if(!km3File.exists()) {
			throw new RuntimeException("Missing KM3 file");
		}

		if(!tcsFile.exists()) {
			throw new RuntimeException("Missing TCS file");
		}

		editorFile = project.getFile("TGE/" + name + "-Editor.xmi");
		editorCache = new ModelCacheFromIFile("Editor", DSLResourceProvider.getDefault().getResource("Editor/Metamodel/Editor.ecore").asEMFURI(), name, editorFile);

		outlineFile = project.getFile("TGE/" + name + "-Outline.xmi");
		outlineCache = new ModelCacheFromIFile("Outline", DSLResourceProvider.getDefault().getResource("Outline/Metamodel/Outline.ecore").asEMFURI(), name, outlineFile);

		ecoreFile = project.getFile("Metamodel/" + name + ".ecore");
		metamodelCache = new ModelCache() {
			private long timestamp;

			protected Object loadModel(ModelFactory factory) {
				try {
					Object ret = factory.loadMetamodel(name, ecoreFile.getContents());
					this.timestamp = ecoreFile.getModificationStamp();
					return ret;
				} catch (CoreException e) {
					throw new RuntimeException(e);
				}
			}			

			protected boolean shouldReload() {
				return ecoreFile.getModificationStamp() != timestamp;
			}
		};
		
		antlrFile = project.getFile("Syntax/" + name + "_ANTLR3.g");
		jarFile = project.getFile("Syntax/" + name + "-parser.jar");

		acgFile = project.getFile("Compiler/" + name + ".acg");
		compilerFile = project.getFile("Compiler/" + name + ".asm");
	}

	public Object getEditorModel(ModelFactory factory) {
		return editorCache.getModel(factory);
	}

	public Object getOutlineModel(ModelFactory factory) {
		return outlineCache.getModel(factory);
	}

	public Object getMetamodel(ModelFactory factory) {
		return metamodelCache.getModel(factory);
	}

	public LanguageSource getSource() {
		return this;
	}

	public Object inject(ModelFactory factory, Object model, TextSource source, Map params) {
		try {
			params.put("name", name);
			// TODO: support multiple parserGenerators
			params.put("parserGenerator", getParserGenerator());
			Object metamodel = getMetamodel(factory);
			Object problemModel = params.get("problems");	// if we get it after the injection, it may have been replaced by a model adapter
			Object ret = TCSInjection.inject(factory, model, metamodel, source, params, jarFile.getLocationURI().toURL());
			if(problemModel != null) {
				// TODO: if no parsing error
				VMLauncher vmLauncher = (VMLauncher)params.get("vmLauncher");
				if(vmLauncher != null) {
					IFile wfrFile = project.getFile("WFR/" + name + ".asm");
					if(wfrFile.exists()) {
						Map libraries = new HashMap();
						String libNameAndPathsCSV = getProperty("wfr.libraries");
						if(libNameAndPathsCSV != null) {
							String libNameAndPaths[] = libNameAndPathsCSV.split(",");
							for(int i = 0 ; i < libNameAndPaths.length ; i++) {
								String parts[] = libNameAndPaths[i].split("=");
								libraries.put(parts[0], project.getFile(parts[1]).getLocationURI().toURL());
							}
						}
						Map models = new HashMap();
						models.put(name, metamodel);
						models.put("IN", ret);
						models.put("Problem", factory.metamodelOf(problemModel));
						models.put("OUT", problemModel);
						vmLauncher.launch(wfrFile.getLocationURI().toURL(), models, libraries);
					}
				}
			}
			return ret;
		} catch (MalformedURLException e) {
			throw new RuntimeException("Could not inject", e);
		}
	}

	public void extract(ModelFactory factory, Object model, OutputStream out, Map params) {
		AtlEMFModelHandler amh = (AtlEMFModelHandler)AtlModelHandler.getDefault(AtlModelHandler.AMH_EMF);
		ASMModel tcsMetamodel = amh.loadModel("TCS", amh.getMof(), DSLResourceProvider.getDefault().getResource("TCS/Metamodel/TCS.ecore").asEMFURI());
		ASMModel format = amh.newModel("syntax", "model.xmi", tcsMetamodel);
		Map oparams = new HashMap();
		oparams.put("name", "TCS");
		TCSInjection.inject(factory, format, tcsMetamodel, new IFileTextSource(tcsFile), oparams, DSLResourceProvider.getDefault().getResource("TCS/Syntax/TCS-parser.jar").asURL());
		params.put("format", format);
		new TCSExtractor().extract((ASMModel)model, out, params);
	}

	public String getExtension() {
		return extension;
	}

	public String getName() {
		return name;
	}

	public ASM getCompiler() {
		ASM ret = null;
		if(compilerFile != null && compilerFile.exists()) {
			try {
				ret = new ASMXMLReader().read(new BufferedInputStream(compilerFile.getContents()));
			} catch (CoreException e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	public String getProperty(String key) {
		if(propertiesFile.getModificationStamp() != propertiesTimestamp) {
			properties = new Properties();
			try {
				properties.load(propertiesFile.getContents());
			} catch (IOException e) {
				throw new RuntimeException("Error reading properties file", e);
			} catch (CoreException e) {
				throw new RuntimeException("Error reading properties file", e);
			}
			propertiesTimestamp = propertiesFile.getModificationStamp();
			if(!name.equals(properties.getProperty("dsl.name"))) {
				throw new RuntimeException("Language name changed, restart Eclipse");
			}
			if(!extension.equals(properties.getProperty("dsl.ext"))) {
				throw new RuntimeException("language extension changed, restart Eclipse");
			}
		}
		return properties.getProperty(key);
	}

	public IFile getEditorFile() {
		return editorFile;
	}

	public IFile getKM3SourceFile() {
		return km3File;
	}

	public IFile getAnnotationSourceFile() {
		return annotationFile;
	}

	public IFile getOutlineFile() {
		return outlineFile;
	}

	public IFile getTCSSourceFile() {
		return tcsFile;
	}

	public IFile getMetamodelFile() {
		return ecoreFile;
	}

	public IFile getGrammarFile() {
		return antlrFile;
	}

	public String getParserGenerator() {
		String ret = getProperty("parser.generator.name");
		if(ret == null) {
			ret = DEFAULT_PARSER_GENERATOR_NAME;
		}
		return ret;
	}

	public IFile getParserFile() {
		return jarFile;
	}

	public IFile getACGSourceFile() {
		return acgFile;
	}

	public IFile getCompilerFile() {
		return compilerFile;
	}
}

Back to the top