Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 57be9f6a65da23acef3be33421afb3e66102c360 (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
/*
 * Copyright (c) 2007, 2009 Borland Software Corporation
 * 
 * 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:
 *    Dmitry Stadnik (Borland) - initial API and implementation
 */

«IMPORT 'http://www.eclipse.org/gmf/2009/GenModel'»

«DEFINE OCLExpressionFactory FOR gmfgen::GenExpressionInterpreter-»
«EXPAND xpt::Common::copyright FOR container.editorGen-»
package «container.expressionsPackageName»;

«EXPAND xpt::Common::generatedClassComment»
public class «className» {

	«EXPAND xpt::Common::generatedMemberComment»
	private «className»() {
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public static «container.getAbstractExpressionQualifiedClassName()» getExpression(
			String body, org.eclipse.emf.ecore.EClassifier context, java.util.Map environment) {
		return new Expression(body, context, environment);
	}

	«EXPAND xpt::Common::generatedMemberComment»
	public static «container.getAbstractExpressionQualifiedClassName()» getExpression(
			String body, org.eclipse.emf.ecore.EClassifier context) {
		return getExpression(body, context, java.util.Collections.EMPTY_MAP);
	}

	«EXPAND xpt::Common::generatedMemberComment»
	private static class Expression extends «container.getAbstractExpressionQualifiedClassName()» {

		«EXPAND xpt::Common::generatedMemberComment»
		private java.lang.ref.WeakReference queryRef;

		«EXPAND xpt::Common::generatedMemberComment»
		private final org.eclipse.ocl.ecore.OCL oclInstance;

		«EXPAND xpt::Common::generatedMemberComment»
		public Expression(String body, org.eclipse.emf.ecore.EClassifier context, java.util.Map environment) {
			super(body, context);
			oclInstance = org.eclipse.ocl.ecore.OCL.newInstance();
			initCustomEnv(oclInstance.getEnvironment(), environment);
		}

		«EXPAND xpt::Common::generatedMemberComment»
		protected org.eclipse.ocl.Query getQuery() {
			org.eclipse.ocl.Query oclQuery = null;
			if (this.queryRef != null) {
				oclQuery = (org.eclipse.ocl.Query) this.queryRef.get();
			}
			if (oclQuery == null) {
				org.eclipse.ocl.helper.OCLHelper oclHelper = oclInstance.createOCLHelper();
				oclHelper.setContext(context());
				try {
					org.eclipse.ocl.expressions.OCLExpression oclExpression = oclHelper.createQuery(body());
					oclQuery = oclInstance.createQuery(oclExpression);
					this.queryRef = new java.lang.ref.WeakReference(oclQuery);
					setStatus(org.eclipse.core.runtime.IStatus.OK, null, null);
				} catch (org.eclipse.ocl.ParserException e) {
					setStatus(org.eclipse.core.runtime.IStatus.ERROR, e.getMessage(), e);
				}
			}
			return oclQuery;
		}

		«EXPAND xpt::Common::generatedMemberComment»
		protected Object doEvaluate(Object context, java.util.Map env) {
			org.eclipse.ocl.Query oclQuery = getQuery();
			if (oclQuery == null) {
				return null;
			}
			org.eclipse.ocl.EvaluationEnvironment evalEnv = oclQuery.getEvaluationEnvironment();
			// init environment
			for (java.util.Iterator it = env.entrySet().iterator(); it.hasNext();) {
				java.util.Map.Entry nextEntry = (java.util.Map.Entry) it.next();
				evalEnv.replace((String) nextEntry.getKey(), nextEntry.getValue());
			}
			try {
				initExtentMap(context);
				Object result = oclQuery.evaluate(context);
				return (result != oclInstance.getEnvironment().getOCLStandardLibrary().getOclInvalid()) ? result : null;
			} finally {
				evalEnv.clear();
				oclQuery.getExtentMap().clear();
			}
		}

		«EXPAND xpt::Common::generatedMemberComment»
		private void initExtentMap(Object context) {
			if (!getStatus().isOK() || context == null) {
				return;
			}
			final org.eclipse.ocl.Query queryToInit = getQuery();
			final Object extentContext = context;
			queryToInit.getExtentMap().clear();
			if (queryToInit.queryText() != null
					&& queryToInit.queryText().indexOf(org.eclipse.ocl.utilities.PredefinedType.ALL_INSTANCES_NAME) >= 0) {
				org.eclipse.ocl.utilities.AbstractVisitor visitior = new org.eclipse.ocl.utilities.AbstractVisitor() {

					private boolean usesAllInstances  = false;

					public Object visitOperationCallExp(org.eclipse.ocl.expressions.OperationCallExp oc) {
						if (!usesAllInstances) {
							usesAllInstances = org.eclipse.ocl.utilities.PredefinedType.ALL_INSTANCES == oc.getOperationCode();
							if (usesAllInstances) {
								queryToInit.getExtentMap().putAll(
										oclInstance.getEvaluationEnvironment().createExtentMap(extentContext));
							}
						}
						return super.visitOperationCallExp(oc);
					}
				};
				queryToInit.getExpression().accept(visitior);
			}
		}

		«EXPAND xpt::Common::generatedMemberComment»
		private static void initCustomEnv(org.eclipse.ocl.Environment ecoreEnv, java.util.Map environment) {
			for (java.util.Iterator it = environment.keySet().iterator(); it.hasNext();) {
				String varName = (String) it.next();
				org.eclipse.emf.ecore.EClassifier varType = (org.eclipse.emf.ecore.EClassifier) environment.get(varName);
				ecoreEnv.addElement(varName, createVar(ecoreEnv, varName, varType), false);
			}
		}

		«EXPAND xpt::Common::generatedMemberComment»
		private static org.eclipse.ocl.expressions.Variable createVar(
				org.eclipse.ocl.Environment ecoreEnv, String name, org.eclipse.emf.ecore.EClassifier type) {
			org.eclipse.ocl.expressions.Variable var = org.eclipse.ocl.ecore.EcoreFactory.eINSTANCE.createVariable();
			var.setName(name);
			var.setType(ecoreEnv.getUMLReflection().getOCLType(type));
			return var;
		}
	}
	«EXPAND additions-»
}
«ENDDEFINE»

«DEFINE additions FOR gmfgen::GenExpressionInterpreter»«ENDDEFINE»

Back to the top