Search in sources :

Example 1 with Category

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);
    }
}
Also used : Category(com.avaloq.tools.ddk.xtext.valid.valid.Category) NativeRule(com.avaloq.tools.ddk.xtext.valid.valid.NativeRule) NativeContext(com.avaloq.tools.ddk.xtext.valid.valid.NativeContext) HashMap(java.util.HashMap) Rule(com.avaloq.tools.ddk.xtext.valid.valid.Rule) PredefinedRule(com.avaloq.tools.ddk.xtext.valid.valid.PredefinedRule) NativeRule(com.avaloq.tools.ddk.xtext.valid.valid.NativeRule) HashSet(java.util.HashSet) Check(org.eclipse.xtext.validation.Check)

Aggregations

Category (com.avaloq.tools.ddk.xtext.valid.valid.Category)1 NativeContext (com.avaloq.tools.ddk.xtext.valid.valid.NativeContext)1 NativeRule (com.avaloq.tools.ddk.xtext.valid.valid.NativeRule)1 PredefinedRule (com.avaloq.tools.ddk.xtext.valid.valid.PredefinedRule)1 Rule (com.avaloq.tools.ddk.xtext.valid.valid.Rule)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Check (org.eclipse.xtext.validation.Check)1