use of com.avaloq.tools.ddk.xtext.valid.valid.Category 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