use of com.avaloq.tools.ddk.xtext.valid.valid.NativeContext in project dsl-devkit by dsldevkit.
the class ValidScopingTest method testEStructuralFeatureScope.
/**
* Tests that validations may be declared on existing EClass and EStructuralFeature instances.
*/
@Test
public void testEStructuralFeatureScope() throws IOException {
final ValidModel validModel = (ValidModel) getTestSource().getModel();
final NativeContext context = getXtextTestUtil().getFirstInstanceOf(validModel, NativeContext.class);
// Check context feature reference
IScope scope = scopeProvider.getScope(context, ValidPackage.Literals.CONTEXT__CONTEXT_FEATURE);
IEObjectDescription name = scope.getSingleElement(QualifiedName.create("name"));
assertNotNull("Found valid EStructuralFeature \"name\"", name);
final EObject resolvedName = name.getEObjectOrProxy();
assertNotNull("Valid EStructuralFeature \"name\" can be resolved", resolvedName);
// Check context type reference
scope = scopeProvider.getScope(context, ValidPackage.Literals.CONTEXT__CONTEXT_TYPE);
assertEquals("Scope provider returns correct context type", context.getContextType(), scope.getSingleElement(QualifiedName.create("Model")).getEObjectOrProxy());
assertEquals("Container of \"name\" reference instance is \"Model\" instance", resolvedName.eContainer(), scope.getSingleElement(QualifiedName.create("Model")).getEObjectOrProxy());
// Check marker type reference
scope = scopeProvider.getScope(context, ValidPackage.Literals.NATIVE_CONTEXT__MARKER_TYPE);
assertEquals("Scope provider returns correct marker type", context.getMarkerType(), scope.getSingleElement(QualifiedName.create("Element")).getEObjectOrProxy());
// Check marker feature reference
scope = scopeProvider.getScope(context, ValidPackage.Literals.NATIVE_CONTEXT__MARKER_FEATURE);
assertEquals("Scope provider returns correct marker feature", context.getMarkerFeature(), scope.getSingleElement(QualifiedName.create("name")).getEObjectOrProxy());
}
use of com.avaloq.tools.ddk.xtext.valid.valid.NativeContext in project dsl-devkit by dsldevkit.
the class ValidJavaValidator method checkUniqueNativeContextName.
/**
* Check that, for each context type, the name of a context is unique inside a model.
*
* @param validModel
* the valid model
*/
@Check(CheckType.FAST)
public void checkUniqueNativeContextName(final ValidModel validModel) {
// for each name store the element of its first occurrence
final Map<String, NativeContext> firstOccurrenceOfName = new HashMap<String, NativeContext>();
// the set of duplicate names
final Set<String> duplicateNames = new HashSet<String>();
for (final Category category : validModel.getCategories()) {
for (final Rule rule : category.getRules()) {
if (rule instanceof NativeRule) {
for (final NativeContext context : ((NativeRule) rule).getContexts()) {
final String name = getName(context);
// if the name already occurred we have a duplicate name and hence an error
final NativeContext firstContext = firstOccurrenceOfName.get(name);
if (firstContext != null && firstContext.getContextType() == context.getContextType()) {
duplicateNames.add(name);
// note the second parameter t
// it is essential difference to the first example
error("Name " + name + " must be unique for a given context type (introduce an \"as\" clause)", context, ValidPackage.Literals.NATIVE_CONTEXT__GIVEN_NAME, UNIQUE_NATIVE_CONTEXT_NAME);
} else {
// otherwise store the name as first occurrence
firstOccurrenceOfName.put(name, context);
}
}
}
}
}
// now create the error for the first occurrence of a duplicate name
for (final String duplicate : duplicateNames) {
error("Name " + duplicate + " must be unique for a given context (introduce an \"as\" clause)", firstOccurrenceOfName.get(duplicate), ValidPackage.Literals.NATIVE_CONTEXT__GIVEN_NAME, UNIQUE_NATIVE_CONTEXT_NAME);
}
}
Aggregations