Newer
Older
package org.etsi.mts.tdl.scoping;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.Scopes;
import org.eclipse.xtext.scoping.impl.AbstractDeclarativeScopeProvider;
import org.etsi.mts.tdl.Assignment;
import org.etsi.mts.tdl.Behaviour;
import org.etsi.mts.tdl.Block;
import org.etsi.mts.tdl.ComponentInstance;
import org.etsi.mts.tdl.Connection;
import org.etsi.mts.tdl.DataElementMapping;
import org.etsi.mts.tdl.DataInstanceUse;
import org.etsi.mts.tdl.DataUse;
import org.etsi.mts.tdl.FormalParameterUse;
import org.etsi.mts.tdl.FunctionCall;
import org.etsi.mts.tdl.GateInstance;
import org.etsi.mts.tdl.GateReference;
Philip Makedonski
committed
import org.etsi.mts.tdl.LocalExpression;
import org.etsi.mts.tdl.Member;
import org.etsi.mts.tdl.MemberAssignment;
import org.etsi.mts.tdl.MemberReference;
import org.etsi.mts.tdl.Parameter;
import org.etsi.mts.tdl.ParameterBinding;
import org.etsi.mts.tdl.ParameterMapping;
import org.etsi.mts.tdl.StructuredDataInstance;
import org.etsi.mts.tdl.StructuredDataType;
import org.etsi.mts.tdl.Target;
import org.etsi.mts.tdl.TestConfiguration;
import org.etsi.mts.tdl.TestDescription;
import org.etsi.mts.tdl.Variable;
import org.etsi.mts.tdl.VariableUse;
import com.google.common.base.Function;
import com.google.inject.Inject;
/**
* This class contains custom scoping description.
*
* see : http://www.eclipse.org/Xtext/documentation/latest/xtext.html#scoping on
* how and when to use it
*
*/
public class TDLan2ScopeProvider extends AbstractDeclarativeScopeProvider {
@Inject
private IQualifiedNameConverter qualifiedNameConverter;
@Override
public IScope getScope(EObject context, EReference reference) {
if (reference.getEType().getInstanceClass() == GateInstance.class) {
if (context instanceof GateReference) {
GateReference gate = (GateReference) context;
IScope scope = Scopes.scopeFor(gate.getComponent().getType().getGateInstance());
return scope;
}
} else if (reference.getEType().getInstanceClass() == ComponentInstance.class) {
if (context instanceof Behaviour || context instanceof Block || context instanceof LocalExpression || context instanceof VariableUse) {
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
TestDescription testDescription = getTestDescription((Element) context);
if (testDescription!=null) {
TestConfiguration configuration = testDescription.getTestConfiguration();
IScope scope = Scopes.scopeFor(configuration.getComponentInstance());
return scope;
}
} else if (context instanceof GateReference) {
TestConfiguration configuration = getTestConfiguration((Element) context);
EList<ComponentInstance> components = configuration.getComponentInstance();
IScope scope = Scopes.scopeFor(components);
return scope;
}
} else if (reference.getEType().getInstanceClass() == GateReference.class) {
//TODO: safety checks in case invalid configuration is specified
//TODO: only suggest connected gates as targets?
if (context instanceof Behaviour || context instanceof Target || context instanceof Block) {
TestConfiguration configuration = getTestDescription((Element) context).getTestConfiguration();
EList<EObject> elements = new BasicEList<>();
for (Connection c : configuration.getConnection()) {
elements.addAll(c.getEndPoint());
}
IScope scope = Scopes.scopeFor(elements, new Function<EObject, QualifiedName>() {
@Override
public QualifiedName apply(EObject o) {
GateReference r = (GateReference) o;
String n = r.getComponent().getName()+"."+r.getGate().getName();
QualifiedName qualifiedName = qualifiedNameConverter.toQualifiedName(n);
return qualifiedName;
}
}, IScope.NULLSCOPE);
return scope;
}
} else if (Parameter.class.isAssignableFrom(reference.getEType().getInstanceClass())) {
if (context instanceof MemberAssignment) {
if (context.eContainer() instanceof StructuredDataInstance) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((StructuredDataInstance)context.eContainer()).getDataType()).getMember());
return scope;
}
} else if (context instanceof ParameterMapping) {
if (context.eContainer() instanceof DataElementMapping) {
if (((DataElementMapping)context.eContainer()).getMappableDataElement() instanceof StructuredDataType) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((DataElementMapping)context.eContainer()).getMappableDataElement()).getMember());
return scope;
}
}
} else if (context instanceof ParameterBinding) {
if (context.eContainer() instanceof DataInstanceUse) {
if (((DataInstanceUse)context.eContainer()).getDataInstance() instanceof StructuredDataInstance) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((StructuredDataInstance)((DataInstanceUse)context.eContainer()).getDataInstance()).getDataType()).getMember());
return scope;
}
} else if (context.eContainer() instanceof FunctionCall) {
// if (((FunctionCall)context.eContainer()).getFunction().get instanceof StructuredDataInstance) {
IScope scope = Scopes.scopeFor(((FunctionCall)context.eContainer()).getFunction().getFormalParameter());
return scope;
// }
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
} else if (context instanceof MemberReference) {
if (((DataUse)context.eContainer()).getReduction().indexOf(context)>0) {
EObject targetContext = ((DataUse)context.eContainer()).getReduction().get(((DataUse)context.eContainer()).getReduction().indexOf(context)-1);
if (((MemberReference)targetContext).getMember().getDataType() instanceof StructuredDataType) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((MemberReference)targetContext).getMember().getDataType()).getMember());
return scope;
}
} else
if (context.eContainer() instanceof DataInstanceUse) {
if (((DataInstanceUse)context.eContainer()).getDataInstance() instanceof StructuredDataInstance) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((StructuredDataInstance)((DataInstanceUse)context.eContainer()).getDataInstance()).getDataType()).getMember());
return scope;
}
} else if (context.eContainer() instanceof FunctionCall) {
if (((FunctionCall)context.eContainer()).getFunction().getReturnType() instanceof StructuredDataType) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((FunctionCall)context.eContainer()).getFunction().getReturnType()).getMember());
return scope;
}
} else if (context.eContainer() instanceof FormalParameterUse) {
if (((FormalParameterUse)context.eContainer()).getParameter().getDataType() instanceof StructuredDataType) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((FormalParameterUse)context.eContainer()).getParameter().getDataType()).getMember());
return scope;
}
} else if (context.eContainer() instanceof VariableUse) {
if (((VariableUse)context.eContainer()).getVariable().getDataType() instanceof StructuredDataType) {
IScope scope = Scopes.scopeFor(((StructuredDataType)((VariableUse)context.eContainer()).getVariable().getDataType()).getMember());
return scope;
}
} else {
}
}
} else if (reference.getEType().getInstanceClass() == Variable.class) {
if (context instanceof Assignment) {
if (((Assignment)context).getComponentInstance()!=null) {
IScope scope = Scopes.scopeFor(((Assignment)context).getComponentInstance().getType().getVariable());
return scope;
Philip Makedonski
committed
}
}
if (context instanceof VariableUse) {
if (((VariableUse)context).getComponentInstance()!=null) {
IScope scope = Scopes.scopeFor(((VariableUse)context).getComponentInstance().getType().getVariable());
return scope;
}
}
Philip Makedonski
committed
else if (context instanceof Target) {
if (((Target)context).getTargetGate().getComponent()!=null) {
IScope scope = Scopes.scopeFor(((Target)context).getTargetGate().getComponent().getType().getVariable());
return scope;
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
if (context instanceof Block) {
TestDescription testDescription = getTestDescription((Element) context);
if (testDescription!=null) {
TestConfiguration configuration = testDescription.getTestConfiguration();
IScope scope = Scopes.scopeFor(configuration.getComponentInstance());
return scope;
}
}
return super.getScope(context, reference);
}
private TestConfiguration getTestConfiguration(Element self) {
if (self.eContainer()!=null) {
if (!(self.eContainer() instanceof TestConfiguration)) {
return getTestConfiguration((Element) self.eContainer());
} else {
return (TestConfiguration)self.eContainer();
}
}
return null;
}
private TestDescription getTestDescription(Element self) {
if (self.eContainer()!=null) {
if (!(self.eContainer() instanceof TestDescription)) {
return getTestDescription((Element) self.eContainer());
} else {
return (TestDescription)self.eContainer();
}
}
return null;
}
}