Search in sources :

Example 1 with NativeContext

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());
}
Also used : ValidModel(com.avaloq.tools.ddk.xtext.valid.valid.ValidModel) NativeContext(com.avaloq.tools.ddk.xtext.valid.valid.NativeContext) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) AbstractScopingTest(com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest) Test(org.junit.Test)

Example 2 with NativeContext

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);
    }
}
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

NativeContext (com.avaloq.tools.ddk.xtext.valid.valid.NativeContext)2 AbstractScopingTest (com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest)1 Category (com.avaloq.tools.ddk.xtext.valid.valid.Category)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 ValidModel (com.avaloq.tools.ddk.xtext.valid.valid.ValidModel)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 EObject (org.eclipse.emf.ecore.EObject)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 IScope (org.eclipse.xtext.scoping.IScope)1 Check (org.eclipse.xtext.validation.Check)1 Test (org.junit.Test)1