blob: dd12594a6af05c5684a119dfc612ec3ae0f24b15 [file] [log] [blame]
pre {
var genModel = new GenModel!GenModel;
var topEPackage = Ecore!EPackage.all.first();
genModel.complianceLevel = topEPackage.getEnumerationAnnotation("complianceLevel", genModel, GenModel!GenJDKLevel#JDK60);
genModel.copyrightFields = topEPackage.getBooleanAnnotation("copyrightFields", false);
genModel.modelPluginId = topEPackage.getAnnotation("modelPluginID", pluginName);
genModel.modelDirectory = topEPackage.getAnnotation("modelDirectory", "/" + pluginName + "/src");
genModel.modelName = topEPackage.getAnnotation("modelName", topEPackage.name.firstToUpperCase());
genModel.importerID = topEPackage.getAnnotation("importerID", "org.eclipse.emf.importer.ecore");
genModel.foreignModel.add(foreignModel);
copyAnnotations(topEPackage, genModel);
for (usedGenPackage in usedGenPackages.values()) {
genModel.usedGenPackages.add(usedGenPackage);
}
}
rule EPackage2GenPackage
transform s : Ecore!EPackage
to t : GenModel!GenPackage {
if (not s.eSuperPackage.isDefined()) {
genModel.genPackages.add(t);
}
t.ecorePackage = s;
t.disposableProviderFactory = s.getBooleanAnnotation("disposableProviderFactory", true);
t.prefix = s.getAnnotation("prefix", s.name.firstToUpperCase());
t.nestedGenPackages ::= s.eSubpackages;
copyAnnotations(s, t);
}
rule EClass2GenClass
transform s : Ecore!EClass
to t : GenModel!GenClass {
s.ePackage.equivalent().genClasses.add(t);
t.image = s.getBooleanAnnotation("icon", not s.isAbstract);
t.ecoreClass = s;
copyAnnotations(s, t);
}
@greedy
rule EStructuralFeature2GenFeature
transform s : Ecore!EStructuralFeature
to t : GenModel!GenFeature {
guard : s.eContainer().isTypeOf(Ecore!EClass)
s.eContainer().equivalent().genFeatures.add(t);
t.ecoreFeature = s;
var defaultProperty;
if (s.isTypeOf(Ecore!EReference)) {
if (not s.container and not s.containment) {
if (s.changeable) {
defaultProperty = GenModel!GenPropertyKind#Editable;
}
else {
defaultProperty = GenModel!GenPropertyKind#Readonly;
}
}
else {
defaultProperty = GenModel!GenPropertyKind#None;
}
t.children = s.getBooleanAnnotation("children", s.isContainment);
t.createChild = s.getBooleanAnnotation("createChild", t.isChildren and s.isChangeable);
t.notify = s.getBooleanAnnotation("notify", t.isChildren);
}
else {
if (s.changeable) {
defaultProperty = GenModel!GenPropertyKind#Editable;
}
else {
defaultProperty = GenModel!GenPropertyKind#Readonly;
}
t.children = s.getBooleanAnnotation("children", false);
t.createChild = s.getBooleanAnnotation("createChild", false);
t.notify = s.getBooleanAnnotation("notify", true);
}
t.propertySortChoices = s.getBooleanAnnotation("propertySortChoices", s.isTypeOf(Ecore!EReference) and defaultProperty = GenModel!GenPropertyKind#Editable);
t.property = s.getEnumerationAnnotation("property", t, defaultProperty);
copyAnnotations(s, t);
}
rule EOperation2GenOperation
transform s: Ecore!EOperation
to t : GenModel!GenOperation {
guard : s.eContainer().isTypeOf(Ecore!EClass)
s.eContainer().equivalent().genOperations.add(t);
t.ecoreOperation = s;
copyAnnotations(s, t);
}
rule EParameter2EGenParameter
transform s : Ecore!EParameter
to t : GenModel!GenParameter {
guard : s.eContainer().equivalent().isDefined()
s.eContainer().equivalent().genParameters.add(t);
t.ecoreParameter = s;
copyAnnotations(s, t);
}
rule EEnum2GenEnum
transform s : Ecore!EEnum
to t : GenModel!GenEnum {
s.ePackage.equivalent().genEnums.add(t);
t.typeSafeEnumCompatible = s.getBooleanAnnotation("typeSafeEnumCompatible", false);
t.ecoreEnum = s;
copyAnnotations(s, t);
}
rule EEnumLiteral2GenEnumLiteral
transform s : Ecore!EEnumLiteral
to t : GenModel!GenEnumLiteral {
s.eContainer().equivalent().genEnumLiterals.add(t);
t.ecoreEnumLiteral = s;
copyAnnotations(s, t);
}
rule EDataType2GenDataType
transform s : Ecore!EDataType
to t : GenModel!GenDataType {
s.ePackage.equivalent().genDataTypes.add(t);
t.ecoreDataType = s;
copyAnnotations(s, t);
}
operation copyAnnotations(source : Any, target : Any) {
if (source.isDefined() and not target.isTypeOf(GenModel!GenModel)) {
for (eAnnotation in source.eAnnotations.select(a|a.source="emf.gen.annotation")) {
var genAnnotation : new GenModel!GenAnnotation;
genAnnotation.source = eAnnotation.getDetail("source");
genAnnotation.details.put(eAnnotation.getDetail("key"), eAnnotation.getDetail("value"));
target.genAnnotations.add(genAnnotation);
}
}
for (stringFeature in target.eClass().eAllStructuralFeatures.select(sf|sf.eType.name = "String" or sf.eType.name = "EString")) {
if (source.hasAnnotation(stringFeature.name)) {
var annotationValue = source.getAnnotation(stringFeature.name, "");
if (stringFeature.many) {
var parts = annotationValue.split(",").collect(s|s.trim());
target.eGet(stringFeature).addAll(parts);
}
else {
target.eSet(stringFeature, annotationValue);
}
}
}
for (booleanFeature in target.eClass().eAllStructuralFeatures.select(sf|not sf.isMany and (sf.eType.name = "Boolean" or sf.eType.name = "EBoolean"))) {
if (source.hasAnnotation(booleanFeature.name)) {
//booleanFeature.name.println();
//"Setting boolean feature".println();
target.eSet(booleanFeature, source.getBooleanAnnotation(booleanFeature.name, target.eGet(booleanFeature)));
}
}
}
operation Ecore!EModelElement hasAnnotation(label : String) : Boolean {
var ann := self.eAnnotations.selectOne(a|a.source = "emf.gen");
if (ann.isDefined()) {
return ann.details.exists(d|d.key = label);
}
return false;
}
operation Ecore!EAnnotation getDetail(key : String) {
var detail = self.details.selectOne(d|d.key=key);
if (detail.isDefined()) {
return detail.value;
}
else {
return "";
}
}
operation Ecore!EModelElement getAnnotation(label : String, defaultValue : Any) : Any {
// GenModel -> "http://www.eclipse.org/emf/2002/GenModel"
var ann := self.eAnnotations.selectOne(a|a.source = "emf.gen");
var det;
if (ann.isDefined()) {
det = ann.details.selectOne(d|d.key = label);
}
if (det.isDefined()) {
return det.value;
}
else {
return defaultValue;
}
}
operation Ecore!EModelElement getEnumerationAnnotation(label : String, eObject : Any, defaultValue : Any) {
var result = self.getAnnotation(label, defaultValue);
if (result.isTypeOf(String)) {
var eClass = eObject.eClass();
var eAttribute = eClass.getEStructuralFeature(label);
var eEnum = eAttribute.getEType();
var eEnumLiteral = eEnum.getELiterals().selectOne(l|l.name = result);
if (eEnumLiteral.isDefined()) {
return eEnumLiteral.getInstance();
}
else {
printWarning("Value of " + label + " was expected to be " +
eEnum.getELiterals().collect(l|l.name).concat("/") +
" but was " + result + " instead");
return defaultValue;
}
}
else {
return defaultValue;
}
}
operation Ecore!EModelElement getBooleanAnnotation(label : String, defaultValue : Boolean) : Boolean {
var result = self.getAnnotation(label, defaultValue);
if (result.isTypeOf(String)) {
if (result == "true") return true;
else if (result == "false") return false;
else {
printWarning("Value of " + label + " was expected to be true/false but was " + result + " instead");
return defaultValue;
}
}
else {
return defaultValue;
}
}
operation printWarning(str : String) {
("WARNING: " + str).println();
}