Java Code Examples for org.eclipse.emf.ecore.EPackage#getEClassifier()

The following examples show how to use org.eclipse.emf.ecore.EPackage#getEClassifier() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetamodelTest.java    From xtext-core with Eclipse Public License 2.0 7 votes vote down vote up
@Test public void testDerivedModel() {
	EPackage pack = DatatypeRulesTestLanguagePackage.eINSTANCE;
	EClass model = (EClass) pack.getEClassifier("Model");
	assertNotNull(model);
	EStructuralFeature feature = model.getEStructuralFeature("id");
	assertNotNull(feature);
	assertEquals(EcorePackage.Literals.ESTRING, feature.getEType());
	feature = model.getEStructuralFeature("value");
	assertNotNull(feature);
	assertEquals(EcorePackage.Literals.EBIG_DECIMAL, feature.getEType());
	feature = model.getEStructuralFeature("vector");
	assertNotNull(feature);
	assertEquals(EcorePackage.Literals.ESTRING, feature.getEType());
	feature = model.getEStructuralFeature("dots");
	assertNotNull(feature);
	assertEquals(EcorePackage.Literals.ESTRING, feature.getEType());
}
 
Example 2
Source File: AbstractTemplateVariableResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected EClassifier getEClassifierForGrammar(String fqnClassName,
		Grammar grammar) {
	int dotIndex = fqnClassName.indexOf('.');
	String packageName = null;
	String className = fqnClassName;
	if (dotIndex > 0) {
		packageName = fqnClassName.substring(0, dotIndex);
		className = fqnClassName.substring(dotIndex + 1);
	}
	List<AbstractMetamodelDeclaration> allMetamodelDeclarations = GrammarUtil
			.allMetamodelDeclarations(grammar);
	for (AbstractMetamodelDeclaration decl : allMetamodelDeclarations) {
		EPackage pack = decl.getEPackage();
		if (packageName == null || pack.getName().equals(packageName)) {
			EClassifier eClassifier = pack.getEClassifier(className);
			if (eClassifier != null) {
				return eClassifier;
			}
		}
	}
	return null;
}
 
Example 3
Source File: EMFGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private void putMappingData(Map<EObject, EObject> result, EPackage usedEPackage, EPackage loadedEPackage) {
	if (loadedEPackage != null && usedEPackage != loadedEPackage) {
		result.put(usedEPackage, loadedEPackage);
		for(EClassifier usedClassifier: usedEPackage.getEClassifiers()) {
			EClassifier loadedClassifier = loadedEPackage.getEClassifier(usedClassifier.getName());
			if (loadedClassifier == null)
				throw new RuntimeException(
						"Cannot find classifier '" + usedClassifier.getName() + "' in loaded EPackage from " + loadedEPackage.eResource().getURI());
			result.put(usedClassifier, loadedClassifier);
		}
		for(EPackage usedNestedPackage: usedEPackage.getESubpackages()) {
			for(EPackage loadedNestedPackage: loadedEPackage.getESubpackages()) {
				if (usedNestedPackage.getName().equals(loadedNestedPackage.getName())) {
					putMappingData(result, usedNestedPackage, loadedNestedPackage);
					break;
				}
			}
		}
	}
}
 
Example 4
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_07() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/7'\n" +
		"Model: enumValue=MyEnum;\n" +
		"enum MyEnum: Value | Value;";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/7", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(1, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("Value", value.getLiteral());
}
 
Example 5
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_08() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/8'\n" +
		"Model: enumValue=MyEnum;\n" +
		"enum MyEnum: Value | Value = 'foo';";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/8", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(1, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("Value", value.getLiteral());
}
 
Example 6
Source File: UimaTypeSystem2Ecore.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Lookup E classifier for type.
 *
 * @param aFullTypeName the a full type name
 * @return the e classifier
 */
private static EClassifier lookupEClassifierForType(String aFullTypeName) {
  // separate name into package name and class name
  String uimaNamespace, shortTypeName;
  int lastDot = aFullTypeName.lastIndexOf('.');
  if (lastDot <= 0) {
    uimaNamespace = null;
    shortTypeName = aFullTypeName;
  } else {
    uimaNamespace = aFullTypeName.substring(0, lastDot);
    shortTypeName = aFullTypeName.substring(lastDot + 1);
  }
  String nsUri = uimaNamespace2NamespaceUri(uimaNamespace);

  // does EPackage already exist for this URI?
  EPackage ePackage = EPackage.Registry.INSTANCE.getEPackage(nsUri);
  if (ePackage == null) {
    return null;
  }
  return ePackage.getEClassifier(shortTypeName);
}
 
Example 7
Source File: SlotEntry.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected Set<EClass> findEClasses(ResourceSet resourceSet, String nsURI2, String typeName2) {
	if (typeName2 == null)
		return Collections.emptySet();
	Set<EClass> result = Sets.newHashSet();
	Set<String> keySet = getNsUris();
	for (String string : keySet) {
		try {
			EPackage ePackage = resourceSet.getPackageRegistry().getEPackage(string);
			if (ePackage != null) {
				EClassifier classifier = ePackage.getEClassifier(typeName2);
				if (classifier instanceof EClass)
					result.add((EClass) classifier);
			}
		} catch(NoClassDefFoundError e) {
			throw new NoClassDefFoundError("NoClassDefFoundError while loading ePackage: " + string + " - " + e.getMessage());
		}
	}
	if (result.isEmpty()) {
		throw new WorkflowInterruptedException("Couldn't find EClass for name '" + typeName2 + "'.");
	}
	return result;
}
 
Example 8
Source File: GrammarUtil.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private static EClassifier findEClassifierByName(Grammar grammar, String nsURI, String name) {
	if (grammar != null) {
		for(AbstractMetamodelDeclaration declaration: allMetamodelDeclarations(grammar)) {
			if (declaration instanceof ReferencedMetamodel) {
				EPackage referencedPackage = declaration.getEPackage();
				if (referencedPackage != null && !referencedPackage.eIsProxy()) {
					if (nsURI.equals(referencedPackage.getNsURI())) {
						EClassifier result = referencedPackage.getEClassifier(name);
						if (result != null)
							return result;
					}
				}
			}
		}
	}
	return null;
}
 
Example 9
Source File: Xtext2EcoreTransformerTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testAddingFeatureTwice() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("grammar test with org.eclipse.xtext.common.Terminals generate test \'http://test\' RuleA returns TypeA: featureA=ID; RuleB returns TypeA: featureA=STRING;");
  final String grammar = _builder.toString();
  EPackage ePackage = this.getEPackageFromGrammar(grammar);
  Assert.assertEquals(1, 
    ePackage.getEClassifiers().size());
  EClassifier _eClassifier = ePackage.getEClassifier("TypeA");
  EClass typeA = ((EClass) _eClassifier);
  Assert.assertNotNull(typeA);
  Assert.assertEquals(1, typeA.getEAttributes().size());
  this.assertAttributeConfiguration(typeA, 0, 
    "featureA", 
    "EString");
}
 
Example 10
Source File: HiddensTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	with(HiddenTerminalsTestLanguageStandaloneSetup.class);
	EPackage pack = HiddenTerminalsTestLanguagePackage.eINSTANCE;
	withoutHiddens = (EClass) pack.getEClassifier("WithoutHiddens");
	withHiddens = (EClass) pack.getEClassifier("WithHiddens");
	datatypeHiddens = (EClass) pack.getEClassifier("DatatypeHiddens");
	overridingHiddens = (EClass) pack.getEClassifier("OverridingHiddens");
	overridingHiddensCall = (EClass) pack.getEClassifier("OverridingHiddensCall");
	inheritingHiddens = (EClass) pack.getEClassifier("InheritingHiddens");
	inheritingHiddensCall = (EClass) pack.getEClassifier("InheritingHiddensCall");
	model = (EClass) pack.getEClassifier("Model");
	spacesWithoutHiddens = withoutHiddens.getEStructuralFeature("spaces");
	valid = model.getEStructuralFeature("valid");
	overridingValid = overridingHiddensCall.getEStructuralFeature("valid");
	inheritingValid = inheritingHiddensCall.getEStructuralFeature("valid");
	inheritingCall = inheritingHiddens.getEStructuralFeature("called");
	overridingCall = overridingHiddens.getEStructuralFeature("called");
}
 
Example 11
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_03() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3'\n" +
		"Model: enumValue=MyEnumRule;\n" +
		"enum MyEnumRule returns MyEnum: Value1 = 'value';";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/3", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(1, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value1", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("value", value.getLiteral());
}
 
Example 12
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_02() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/2'\n" +
		"Model: enumValue=MyEnumRule;\n" +
		"enum MyEnumRule returns MyEnum: Value1;";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/2", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(1, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value1", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("Value1", value.getLiteral());
}
 
Example 13
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testEnum_04() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/4'\n" +
		"Model: enumValue=ExistingEnum;\n" +
		"enum ExistingEnum: SameName;";
	Grammar grammar = (Grammar) getModel(modelAsString);
	assertTrue(grammar.eResource().getErrors().toString(), grammar.eResource().getErrors().isEmpty());
	checkEnums(grammar);
	EPackage pack = grammar.getMetamodelDeclarations().get(0).getEPackage();
	assertEquals("http://www.eclipse.org/2009/tmf/xtext/EnumRulesTestLanguage/imported", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("ExistingEnum");
	assertNotNull(eEnum);
	assertEquals(3, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals(ExistingEnum.SAME_NAME.getName(), value.getName());
	assertEquals(ExistingEnum.SAME_NAME.getValue(), value.getValue());
	assertEquals(ExistingEnum.SAME_NAME.getLiteral(), value.getLiteral());
	
	EnumRule rule = (EnumRule) grammar.getRules().get(1);
	assertEquals(eEnum, rule.getType().getClassifier());
	EnumLiteralDeclaration decl = (EnumLiteralDeclaration) rule.getAlternatives();
	assertEquals(value, decl.getEnumLiteral());
	assertNotNull(decl.getLiteral());
	assertEquals(value.getLiteral(), decl.getLiteral().getValue());
}
 
Example 14
Source File: Database.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public EClass getEClassForName(String packageName, String className) {
	EPackage ePackage = emfPackages.get(packageName);
	if (ePackage.getEClassifier(className) != null) {
		return (EClass) ePackage.getEClassifier(className);
	}
	return null;
}
 
Example 15
Source File: ParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	with(DatatypeRulesTestLanguageStandaloneSetup.class);
	EPackage pack = DatatypeRulesTestLanguagePackage.eINSTANCE;
	EClass model = (EClass) pack.getEClassifier("Model");
	idFeature = model.getEStructuralFeature("id");
	valueFeature = model.getEStructuralFeature("value");
	vectorFeature = model.getEStructuralFeature("vector");
	dotsFeature = model.getEStructuralFeature("dots");
	EClass compositeModel = (EClass) pack.getEClassifier("CompositeModel");
	modelFeature = compositeModel.getEStructuralFeature("model");
}
 
Example 16
Source File: TransformUtil.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Looks up an EClass visible in the context of a grammar.
 *
 * @param grammar
 *          context grammar
 * @param packageName
 *          package name
 * @param className
 *          class name
 * @return EClass or null
 */
public static EClass lookupEClass(final Grammar grammar, final String packageName, final String className) {
  EPackage ePackage = lookupEPackage(grammar, packageName);
  if (ePackage != null) {
    EClassifier eClassifier = ePackage.getEClassifier(className);
    if (eClassifier instanceof EClass) {
      return (EClass) eClassifier;
    }
  }
  LOG.info("no class found with name " + packageName + "::" + className);
  return null;
}
 
Example 17
Source File: ParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	with(KeywordsTestLanguageStandaloneSetup.class);
	EPackage pack = KeywordsTestLanguagePackage.eINSTANCE;
	EClass clazz = (EClass) pack.getEClassifier("Model");
	first = clazz.getEStructuralFeature("first");
	second = clazz.getEStructuralFeature("second");
	third = clazz.getEStructuralFeature("third");
	forth = clazz.getEStructuralFeature("forth");
	fifth = clazz.getEStructuralFeature("fifth");
	sixth = clazz.getEStructuralFeature("sixth");
	seventh = clazz.getEStructuralFeature("seventh");
	eighth = clazz.getEStructuralFeature("eighth");
}
 
Example 18
Source File: ParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
	super.setUp();
	with(TerminalRulesTestLanguageStandaloneSetup.class);
	EPackage pack = TerminalRulesTestLanguagePackage.eINSTANCE;
	EClass model = (EClass) pack.getEClassifier("Model");
	idFeature = model.getEStructuralFeature("idValue");
	intFeature = model.getEStructuralFeature("intValue");
	stringFeature = model.getEStructuralFeature("stringValue");
	richStringFeature = model.getEStructuralFeature("richStringValue");
	mlCommentFeature = model.getEStructuralFeature("mlCommentValue");
	slCommentFeature = model.getEStructuralFeature("slCommentValue");
	wsFeature = model.getEStructuralFeature("wsValue");
	anyOtherFeature = model.getEStructuralFeature("anyValue");
}
 
Example 19
Source File: Schema.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public EClass getEClass(String packageName, String name) {
	EPackage ePackage = packages.get(packageName);
	if (ePackage == null) {
		throw new RuntimeException("Package with name " + name + " not found");
	}
	EClass eClassifier = (EClass) ePackage.getEClassifier(name);
	if (eClassifier == null) {
		throw new RuntimeException("Class " + name + " not found in " + packageName);
	}
	return eClassifier;
}
 
Example 20
Source File: ParserTest.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Test public void testSetup() {
	EPackage pack = TerminalRulesTestLanguagePackage.eINSTANCE;
	EClass model = (EClass) pack.getEClassifier("Model");
	assertEquals(model.getEStructuralFeatures().size(), ParserTest.class.getDeclaredFields().length);
}