Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1ed61606d50d72b3862c1f406010ef8a218dcb87 (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
/**
 * Copyright (c) 2015 ESEO.
 * 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:
 *     ESEO - initial API and implementation
 *
 * $Id$
 */
package org.eclipse.gmt.tcs.metadata.adhoc;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmt.tcs.extractor.ModelAdapter;
import org.eclipse.m2m.atl.drivers.emf4atl.ASMEMFModelElement;
import org.eclipse.m2m.atl.engine.emfvm.StackFrame;
import org.eclipse.m2m.atl.engine.emfvm.lib.ASMModule;
import org.eclipse.m2m.atl.engine.emfvm.lib.EnumLiteral;
import org.eclipse.m2m.atl.engine.emfvm.lib.ExecEnv;
import org.eclipse.m2m.atl.engine.emfvm.lib.OclUndefined;
import org.eclipse.m2m.atl.engine.emfvm.lib.Operation;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMEnumLiteral;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMModelElement;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMOclUndefined;
import org.eclipse.m2m.atl.engine.vm.nativelib.ASMString;

/**
 * This ModelAdapter delegates to another ModelAdapter but catches access to non-existing properties, which it virtualizes.
 * 
 * @author Frédéric Jouault
 */
public class VirtualExtractorModelAdapter implements ModelAdapter {

	private ModelAdapter actualModelAdapter;

	private ExecEnv execEnv;
	private ASMModule asmModule;

	public VirtualExtractorModelAdapter(ModelAdapter actualModelAdapter, ExecEnv execEnv, ASMModule asmModule) {
		this.actualModelAdapter = actualModelAdapter;
		this.execEnv = execEnv;
		this.asmModule = asmModule;
	}

	public Object get(Object ame, String propertyName) {
		if(ame instanceof ASMEMFModelElement) {
			ame = ((ASMEMFModelElement)ame).getObject();
		}
		Operation op = execEnv.getOperation(((EObject)ame).eClass(), "get");
		StackFrame stackFrame = new StackFrame(this.execEnv, this.asmModule, op);
		Object localVars[] = new Object[op.getMaxLocals()];
		localVars[0] = ame;
		localVars[1] = propertyName;
		stackFrame.setLocalVars(localVars);
		Object ret = op.exec(stackFrame);
		if(ret instanceof OclUndefined || ret instanceof ASMOclUndefined) {
			ret = null;
		}
		return ret;
	}

	public String getString(Object me, String propName) {
		Object ret = this.get(me, propName);
		if(ret instanceof EnumLiteral) {
			ret = this.getEnumLiteralName(ret);
		}
		return (String)ret;
	}

	public boolean getBool(Object me, String propName) {
		return ((Boolean)this.get(me, propName)).booleanValue();
	}

	public boolean getBoolUndefinedIsFalse(Object me, String propName) {
		Boolean ret = (Boolean)this.get(me, propName);
		return (ret == null) ? false : ret.booleanValue();
	}

	public int getInt(Object me, String propName) {
		return ((Number)this.get(me, propName)).intValue();
	}

	public Iterator getCol(Object me, String propName) {
		return ((Collection)this.get(me, propName)).iterator();
	}

	public Object getME(Object me, String propName) {
		return this.get(me, propName);
	}

	public String getName(Object me) {
		return this.getString(me, "name");
	}

	public String getEnumLiteralName(Object me) {
		if(me instanceof ASMEnumLiteral) {
			return ((ASMEnumLiteral)me).getName();
		} else {
			return (String)((EnumLiteral)me).get(null, "name");
		}
	}

	public Set getElementsByType(Object model, String typeName) {
		return this.actualModelAdapter.getElementsByType(model, typeName);
	}

	public String getTypeName(Object me) {
		if(me instanceof EObject) {
			return ((EObject)me).eClass().getName();
		} else {
			return this.actualModelAdapter.getTypeName(me);
		}
	}

	public Object getMetaobject(Object me) {
		if(me instanceof EObject) {
			return ((EObject)me).eClass();
		}
		return this.actualModelAdapter.getMetaobject(me);
	}

	public Object refImmediateComposite(Object me) {
		return this.actualModelAdapter.refImmediateComposite(me);
	}

	public boolean isAModelElement(Object o) {
		return o instanceof EObject || o instanceof ASMModelElement;
	}

	public Object getPropertyType(Object f, String propName) {
		if(f instanceof EClass) {
			return ((EClass)f).getEStructuralFeature(propName).getEType();
		}
		return this.actualModelAdapter.getPropertyType(f, propName);
	}

	public boolean isPrimitive(Object value) {
		return this.actualModelAdapter.isPrimitive(value);
	}

	public boolean isEnumLiteral(Object value) {
		return (value instanceof ASMEnumLiteral) || value instanceof EnumLiteral;
	}

	public String nextString(Iterator i) {
		Object ret = i.next();
		if(ret instanceof ASMString)
			ret = ((ASMString)ret).getSymbol();
		return (String)ret;
	}
}

Back to the top