org.eclipse.emf.ecore.EEnumLiteral Java Examples

The following examples show how to use org.eclipse.emf.ecore.EEnumLiteral. 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: IfcXmlDeserializer.java    From IfcPlugins with GNU Affero General Public License v3.0 6 votes vote down vote up
private Object parsePrimitive(EClassifier eType, String text) throws DeserializeException {
	if (eType == EcorePackage.eINSTANCE.getEString()) {
		return text;
	} else if (eType == EcorePackage.eINSTANCE.getEInt()) {
		return Integer.parseInt(text);
	} else if (eType == EcorePackage.eINSTANCE.getELong()) {
		return Long.parseLong(text);
	} else if (eType == EcorePackage.eINSTANCE.getEDouble()) {
		return Double.parseDouble(text);
	} else if (eType == EcorePackage.eINSTANCE.getEBoolean()) {
		return Boolean.parseBoolean(text);
	} else if (eType instanceof EEnum) {
		EEnumLiteral eEnumLiteral = ((EEnum) eType).getEEnumLiteral(text.toUpperCase());
		if (eEnumLiteral == null) {
			if (text.equals("unknown")) {
				return null;
			} else {
				throw new DeserializeException(DeserializerErrorCode.UNSPECIFIED_IFCXML_ERROR, "Unknown enum literal " + text + " in enum " + ((EEnum) eType).getName());
			}
		}
		return eEnumLiteral.getInstance();
	} else {
		throw new DeserializeException(DeserializerErrorCode.UNSPECIFIED_IFCXML_ERROR, "Unimplemented primitive type: " + eType.getName());
	}
}
 
Example #2
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testEnum_01() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/1'\n" +
		"Model: enumValue=MyEnum;\n" +
		"enum 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/1", 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 #3
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 #4
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 #5
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 #6
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 #7
Source File: ByteBufferVirtualObject.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
private void writeEnum(EStructuralFeature feature, Object value) {
	if (value == null) {
		ensureCapacity(buffer.position(), 4);
		buffer.putInt(-1);
	} else {
		EEnum eEnum = (EEnum) feature.getEType();
		EEnumLiteral eEnumLiteral = eEnum.getEEnumLiteralByLiteral(((Enum<?>) value).toString());
		ensureCapacity(buffer.position(), 4);
		if (eEnumLiteral != null) {
			buffer.putInt(eEnumLiteral.getValue());
		} else {
			LOGGER.error(((Enum<?>) value).toString() + " not found");
			buffer.putInt(-1);
		}
	}
}
 
Example #8
Source File: Schema.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
public EEnumLiteral createEEnumLiteral(EEnum eEnum, String name) {
	EEnumLiteral eEnumLiteral = EcoreFactory.eINSTANCE.createEEnumLiteral();
	if (eEnum.getELiterals().size() == 0) {
		eEnumLiteral.setValue(0);
	} else {
		int largestValue = Integer.MIN_VALUE;
		for (EEnumLiteral existingLiteral : eEnum.getELiterals()) {
			if (existingLiteral.getValue() > largestValue) {
				largestValue = existingLiteral.getValue();
			}
		}
		eEnumLiteral.setValue(largestValue + 1);
	}
	eEnum.getELiterals().add(eEnumLiteral);
	eEnumLiteral.setName(name);
	return eEnumLiteral;
}
 
Example #9
Source File: Express2EMF.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
private void createTristate() {
	tristate = eFactory.createEEnum();
	tristate.setName("Tristate");
	EEnumLiteral trueLiteral = eFactory.createEEnumLiteral();
	trueLiteral.setName("TRUE");
	trueLiteral.setValue(0);
	EEnumLiteral falseLiteral = eFactory.createEEnumLiteral();
	falseLiteral.setName("FALSE");
	falseLiteral.setValue(1);
	EEnumLiteral undefinedLiteral = eFactory.createEEnumLiteral();
	undefinedLiteral.setName("UNDEFINED");
	undefinedLiteral.setValue(2);
	tristate.getELiterals().add(trueLiteral);
	tristate.getELiterals().add(falseLiteral);
	tristate.getELiterals().add(undefinedLiteral);
	schemaPack.getEClassifiers().add(tristate);
}
 
Example #10
Source File: PackageMetaData.java    From BIMserver with GNU Affero General Public License v3.0 6 votes vote down vote up
public EEnumLiteral getEEnumLiteral(String enumName, String literalName) {
	EClassifier eClassifier = ePackage.getEClassifier(enumName);
	if (eClassifier == null) {
		throw new RuntimeException("Classifier " + enumName + " not found in package " + ePackage.getName());
	}
	if (eClassifier instanceof EEnum) {
		EEnum eEnum = (EEnum)eClassifier;
		EEnumLiteral literal = eEnum.getEEnumLiteral(literalName);
		if (literal == null) {
			throw new RuntimeException("No enum literal " + literalName + " found on " + ePackage.getName() + "." + enumName);
		}
		return literal;
	} else {
		throw new RuntimeException("Classifier " + enumName + " is not of type enum");
	}
}
 
Example #11
Source File: MetamodelImpl.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public void refreshCaches() {
	for (final EPackage ePackage : getEPackages()) {
		for (final Iterator<EObject> i = ePackage.eAllContents(); i.hasNext();) {
			final EObject element = i.next();
			if (element instanceof EStructuralFeatureImpl) {
				final EStructuralFeatureImpl feature = (EStructuralFeatureImpl) element;
				feature.setSettingDelegate(null);
			}
			if (element instanceof EEnumLiteral) {
				final EEnumLiteral literal = (EEnumLiteral) element;
				literal.setInstance(literal);
			}
		}
	}
}
 
Example #12
Source File: MaterializingBackwardConverter.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected Enumerator resolveLiteral(Object literal) {
	if (EEnumLiteral.class.isInstance(literal)) {
		final EEnum sourceEnum = EEnumLiteral.class.cast(literal).getEEnum();
		final String value = sourceEnum.getEPackage().getEFactoryInstance()
			.convertToString(sourceEnum, literal);
		final EEnum targetEnum = (EEnum) resolveEClassifier(sourceEnum);
		return (Enumerator) targetEnum.getEPackage().getEFactoryInstance()
			.createFromString(targetEnum, value);
	} else if (Enumerator.class.isInstance(literal)) {
		return Enumerator.class.cast(literal);
	} else if (literal == null) {
		return null;
	}
	throw new IllegalArgumentException(MessageFormat.format(
		"Unexpected literal {0} of type {1} cannot be converted to an Enumerator", literal, literal.getClass())); //$NON-NLS-1$
}
 
Example #13
Source File: EnumTemplateVariableResolver.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public List<String> resolveValues(TemplateVariable variable,
		XtextTemplateContext castedContext) {
	String enumerationName = variable.getVariableType()
			.getParams().iterator().next();
	Grammar grammar = getGrammar(castedContext);
	if (grammar == null)
		return Collections.emptyList();
	EEnum enumeration = (EEnum) getEClassifierForGrammar(enumerationName, grammar);
	if (enumeration == null) {
		return Collections.emptyList();
	}
	return Lists.transform(enumeration.getELiterals(), new Function<EEnumLiteral, String>() {
		@Override
		public String apply(EEnumLiteral enumLiteral) {
			return enumLiteral.getLiteral();
		}
	});
}
 
Example #14
Source File: EnumLiteralDeclarationImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void setEnumLiteral(EEnumLiteral newEnumLiteral)
{
  EEnumLiteral oldEnumLiteral = enumLiteral;
  enumLiteral = newEnumLiteral;
  if (eNotificationRequired())
    eNotify(new ENotificationImpl(this, Notification.SET, XtextTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, oldEnumLiteral, enumLiteral));
}
 
Example #15
Source File: Express2EMF.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addEnumerations() {
	Iterator<DefinedType> typeIter = schema.getTypes().iterator();
	while (typeIter.hasNext()) {
		DefinedType type = typeIter.next();
		if (type instanceof EnumerationType) {
			EEnum enumeration = eFactory.createEEnum();
			enumeration.setName(type.getName());

			EEnumLiteral nullValue = eFactory.createEEnumLiteral();
			nullValue.setName("NULL");
			nullValue.setLiteral("NULL");
			nullValue.setValue(0);
			enumeration.getELiterals().add(nullValue);

			int counter = 1;
			Iterator<String> values = ((EnumerationType) type).getElements().iterator();
			while (values.hasNext()) {
				String stringVal = values.next();
				if (!stringVal.equals("NULL")) {
					EEnumLiteral value = eFactory.createEEnumLiteral();
					value.setName(stringVal);
					value.setLiteral(stringVal);
					value.setValue(counter);
					counter++;
					enumeration.getELiterals().add(value);
				}
			}
			schemaPack.getEClassifiers().add(enumeration);
		}
	}
}
 
Example #16
Source File: EnumLiteralDeclarationImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eSet(int featureID, Object newValue)
{
  switch (featureID)
  {
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)newValue);
      return;
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)newValue);
      return;
  }
  super.eSet(featureID, newValue);
}
 
Example #17
Source File: EnumLiteralDeclarationImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eUnset(int featureID)
{
  switch (featureID)
  {
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)null);
      return;
    case XtextTestPackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)null);
      return;
  }
  super.eUnset(featureID);
}
 
Example #18
Source File: Ecore2UimaTypeSystem.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Eenum 2 uima type.
 *
 * @param aEEnum the a E enum
 * @param aUimaNamespace the a uima namespace
 * @param aOptions the a options
 * @return the type description
 * @throws URISyntaxException the URI syntax exception
 */
private static TypeDescription eenum2UimaType(EEnum aEEnum, String aUimaNamespace, Map aOptions)
        throws URISyntaxException {
  TypeDescription type = uimaFactory.createTypeDescription();
  // set name
  if (aUimaNamespace != null) {
    type.setName(aUimaNamespace + "." + aEEnum.getName());
  } else {
    type.setName(aEEnum.getName());
  }
  // set supetype to String
  type.setSupertypeName(CAS.TYPE_NAME_STRING);
  // try to get desecription from EAnnotation
  EAnnotation eannot = aEEnum.getEAnnotation("http://uima.apache.org");
  if (eannot != null) {
    type.setDescription((String) eannot.getDetails().get("description"));
  }
  // set allowed values
  EList literals = aEEnum.getELiterals();
  AllowedValue[] vals = new AllowedValue[literals.size()];
  for (int i = 0; i < literals.size(); i++) {
    EEnumLiteral literal = (EEnumLiteral) literals.get(i);
    vals[i] = uimaFactory.createAllowedValue();
    vals[i].setString(literal.getName());
    EAnnotation literalAnnot = literal.getEAnnotation("http://uima.apache.org");
    if (literalAnnot != null) {
      vals[i].setDescription((String) literalAnnot.getDetails().get("description"));
    }
  }
  type.setAllowedValues(vals);
  return type;
}
 
Example #19
Source File: PackageJsonUtils.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private static <T extends Enumerator> T parseEnumLiteral(EEnum emfEnumType, Class<T> javaEnumType,
		String enumLiteralStr) {
	EEnumLiteral emfLit = enumLiteralStr != null ? emfEnumType.getELiterals().stream()
			.filter(lit -> lit.getName().equalsIgnoreCase(enumLiteralStr))
			.findFirst().orElse(null) : null;
	if (emfLit == null) {
		return null;
	}
	final Enumerator javaLit = emfLit.getInstance();
	@SuppressWarnings("unchecked")
	T javaLitCasted = javaEnumType.isInstance(javaLit) ? (T) javaLit : null;
	return javaLitCasted;
}
 
Example #20
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 #21
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testEnum_05() 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/5'\n" +
		"Model: enumValue=ExistingEnum;\n" +
		"enum ExistingEnum: SameName = '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/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", decl.getLiteral().getValue());
}
 
Example #22
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testEnum_06() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.common.Terminals\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/6'\n" +
		"Model: enumValue=MyEnum;\n" +
		"enum MyEnum: Value1 | Value2='value' | Value3;";
	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/6", pack.getNsURI());
	EEnum eEnum = (EEnum) pack.getEClassifier("MyEnum");
	assertNotNull(eEnum);
	assertEquals(3, eEnum.getELiterals().size());
	EEnumLiteral value = eEnum.getELiterals().get(0);
	assertEquals("Value1", value.getName());
	assertEquals(0, value.getValue());
	assertEquals("Value1", value.getLiteral());
	
	value = eEnum.getELiterals().get(1);
	assertEquals("Value2", value.getName());
	assertEquals(1, value.getValue());
	assertEquals("value", value.getLiteral());
	
	value = eEnum.getELiterals().get(2);
	assertEquals("Value3", value.getName());
	assertEquals(2, value.getValue());
	assertEquals("Value3", value.getLiteral());
}
 
Example #23
Source File: BackwardConverter.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
/** Resolve the literal value of an enumeration. */
protected Enumerator resolveLiteral(Object literal) {
    if (EEnumLiteral.class.isInstance(literal)) {
        return EEnumLiteral.class.cast(literal);
    } else if (Enumerator.class.isInstance(literal)) {
        return Enumerator.class.cast(literal);
    }
    throw new IllegalArgumentException(MessageFormat.format(
            "Unexpected literal {0} of type {1} cannot be converted to an Enumerator", literal, literal.getClass())); //$NON-NLS-1$
}
 
Example #24
Source File: GrammarParserTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testEnum_10() throws Exception {
	String modelAsString =
		"grammar TestLanguage with org.eclipse.xtext.enumrules.EnumRulesTestLanguage\n" +
		"import 'classpath:/org/eclipse/xtext/enumrules/enums.ecore'\n" +
		"generate testLanguage 'http://www.eclipse.org/2009/tmf/xtext/AbstractEnumRulesTest/TestEnum/10'\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("SameName", decl.getLiteral().getValue());
}
 
Example #25
Source File: SchemaChecker.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private void compareEEnum(EEnum eEnum1, EEnum eEnum2) throws SchemaCompareException {
	if (eEnum1.getELiterals().size() != eEnum2.getELiterals().size()) {
		throw new SchemaCompareException("Not the same amount of literals");
	}
	Iterator<EEnumLiteral> iterator1 = eEnum1.getELiterals().iterator();
	Iterator<EEnumLiteral> iterator2 = eEnum2.getELiterals().iterator();
	while (iterator1.hasNext()) {
		EEnumLiteral enumLiteral1 = iterator1.next();
		EEnumLiteral enumLiteral2 = iterator2.next();
		if (!enumLiteral1.getName().equals(enumLiteral2.getName())) {
			throw new SchemaCompareException("Literal not the same");
		}
	}
}
 
Example #26
Source File: Schema.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public EEnumLiteral createEEnumLiteral(EEnum eEnum, String name, int value) {
	EEnumLiteral eEnumLiteral = EcoreFactory.eINSTANCE.createEEnumLiteral();
	eEnumLiteral.setValue(value);
	eEnum.getELiterals().add(eEnumLiteral);
	eEnumLiteral.setName(name);
	return eEnumLiteral;
}
 
Example #27
Source File: EnumLiteralDeclarationImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eUnset(int featureID)
{
  switch (featureID)
  {
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)null);
      return;
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)null);
      return;
  }
  super.eUnset(featureID);
}
 
Example #28
Source File: EnumLiteralDeclarationImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void eSet(int featureID, Object newValue)
{
  switch (featureID)
  {
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL:
      setEnumLiteral((EEnumLiteral)newValue);
      return;
    case XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__LITERAL:
      setLiteral((Keyword)newValue);
      return;
  }
  super.eSet(featureID, newValue);
}
 
Example #29
Source File: EnumLiteralDeclarationImpl.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void setEnumLiteral(EEnumLiteral newEnumLiteral)
{
  EEnumLiteral oldEnumLiteral = enumLiteral;
  enumLiteral = newEnumLiteral;
  if (eNotificationRequired())
    eNotify(new ENotificationImpl(this, Notification.SET, XtextTerminalsTestLanguagePackage.ENUM_LITERAL_DECLARATION__ENUM_LITERAL, oldEnumLiteral, enumLiteral));
}
 
Example #30
Source File: XtextScopeProvider.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected IScope createEnumLiteralsScope(EEnum eEnum) {
	return new SimpleScope(IScope.NULLSCOPE,Iterables.transform(eEnum.getELiterals(), new Function<EEnumLiteral, IEObjectDescription>() {
						@Override
						public IEObjectDescription apply(EEnumLiteral param) {
							return EObjectDescription.create(QualifiedName.create(param.getName()), param);
						}
					}));
}