Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
263 lines (218 sloc) 14.2 KB
/*******************************************************************************
* Copyright (c) 2011 University of Mannheim: Chair for Software Engineering
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ralph Gerbig - initial API and implementation and initial documentation
*******************************************************************************/
/*
*
* Fix for Initializers with Sequence{Integer}
*
*/
«IMPORT 'http://www.eclipse.org/gmf/2009/GenModel'»
«IMPORT 'http://www.eclipse.org/emf/2002/GenModel'»
«EXTENSION xpt::providers::ElementInitializers»
«EXTENSION xpt::GenModelUtils»
«EXTENSION gmf::GenModelUtils»
// XXX should generate this class only when there is initialization logic defined in the model
«DEFINE ElementInitializers FOR gmfgen::GenDiagram-»
«EXPAND xpt::Common::copyright FOR editorGen-»
package «getElementInitializersPackageName()»;
«EXPAND xpt::Common::generatedClassComment»
public class «getElementInitializersClassName()» {
protected «getElementInitializersClassName()»() {
// use #getInstance to access cached instance
}
«EXPAND Initializers»
«EXPAND JavaSupport»
«EXPAND additions-»
«EXPAND xpt::Common::generatedMemberComment»
public static «getElementInitializersClassName()» getInstance() {
«getElementInitializersClassName()» cached = «EXPAND plugin::Activator::instanceAccess FOR editorGen».getElementInitializers();
if (cached == null) {
«EXPAND plugin::Activator::instanceAccess FOR editorGen».setElementInitializers(cached = new «getElementInitializersClassName()»());
}
return cached;
}
}
«ENDDEFINE»
«DEFINE Initializers FOR gmfgen::GenDiagram-»
«EXPAND initMethod FOREACH getAllNodes()-»
«EXPAND initMethod FOREACH links-»
«ENDDEFINE»
«DEFINE JavaSupport FOR gmfgen::GenDiagram-»
«IF editorGen.expressionProviders <> null and editorGen.expressionProviders.providers[gmfgen::GenJavaExpressionProvider]->notEmpty()-»
«EXPAND javaMethod FOREACH getAllNodes()-»
«EXPAND javaMethod FOREACH links-»
«ENDIF-»
«ENDDEFINE»
«DEFINE additions FOR gmfgen::GenDiagram»«ENDDEFINE»
«REM»
Definitions of initializer objects.
«ENDREM»
«DEFINE initMethod FOR gmfgen::GenNode»«EXPAND initMethod(self) FOR modelFacet»«ENDDEFINE»
«DEFINE initMethod FOR gmfgen::GenLink»«EXPAND initMethod(self) FOR modelFacet»«ENDDEFINE»
«DEFINE initMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::ModelFacet»«ENDDEFINE»
«DEFINE initMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::TypeModelFacet-»
«EXPAND initMethod(diagramElement) FOR modelElementInitializer-»
«ENDDEFINE»
«DEFINE initMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::GenElementInitializer»«ERROR 'No idea how to init using ' + self.repr()»«ENDDEFINE»
«AROUND additions FOR gmfgen::GenDiagram»
private static final String REASONING_SERVICE_ID = "org.melanee.core.reasoning.service";
«ENDAROUND»
«REM»
1 - Do DSL Element initialization
2 - Pause refactoring service during element setup
«ENDREM»
«DEFINE initMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::GenFeatureSeqInitializer-»
«EXPAND xpt::Common::generatedMemberComment»
public void init_«diagramElement.getUniqueIdentifier()»(«EXPAND MetaModel::QualifiedClassName FOR elementClass» instance) {
try {
«FOREACH initializers AS i»«EXPAND performInit(diagramElement, 'instance', elementClass, getSuffixes((initializers->indexOf(i) - 1))) FOR i»«ENDFOREACH-»
«REM»
*********************************************************
* Customization for automatic instance setup
***********************************************************«ENDREM»
«IF diagramElement.getUniqueIdentifier().startsWith('Entity_')
or diagramElement.getUniqueIdentifier().startsWith('Connection_')»
org.melanee.core.modeleditor.part.PLMDiagramEditor editor = (org.melanee.core.modeleditor.part.PLMDiagramEditor)org.eclipse.ui.PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
org.eclipse.gef.ui.palette.PaletteViewer viewer = editor.getPalette();
//We create an instance of a DSL element
if (viewer.getActiveTool().getId().startsWith("dsl.")) {
String activeToolID = viewer.getActiveTool().getId();
boolean instantiateContent = activeToolID.endsWith(org.melanee.core.modeleditor.custom.ModelEditorConstants.INSTANTIATE_WITH_CONTENT);
String typeName = null;
if(instantiateContent)
typeName = activeToolID.substring(activeToolID.indexOf(".") + 1).replace(org.melanee.core.modeleditor.custom.ModelEditorConstants.INSTANTIATE_WITH_CONTENT, "");
else
typeName = activeToolID.substring(activeToolID.indexOf(".") + 1).replace(org.melanee.core.modeleditor.custom.ModelEditorConstants.INSTANTIATE_WITHOUT_CONTENT, "");
org.eclipse.emf.common.util.TreeIterator<org.eclipse.emf.ecore.EObject> iter = org.eclipse.emf.ecore.util.EcoreUtil.getRootContainer(instance).eAllContents();
while (iter.hasNext()) {
org.eclipse.emf.ecore.EObject current = iter.next();
if (org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil.getProxyID(current).equals(typeName)) {
org.melanee.core.models.plm.PLM.impl.PLMFactoryImpl.eINSTANCE.configureClabject((org.melanee.core.models.plm.PLM.Clabject) current, instance, null, false, true);
java.util.Map<org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject> type2Instance = new java.util.HashMap<org.eclipse.emf.ecore.EObject, org.eclipse.emf.ecore.EObject>();
type2Instance.put(current, instance);
if(!instantiateContent)
break;
//************************************
//Instantiate the content
//************************************
org.eclipse.emf.common.util.TreeIterator<org.eclipse.emf.ecore.EObject> contentIterator = current.eAllContents();
while(contentIterator.hasNext()){
org.eclipse.emf.ecore.EObject next = contentIterator.next();
//Only Clabjects are instantiated
if(! (next instanceof org.melanee.core.models.plm.PLM.Clabject))
continue;
org.melanee.core.models.plm.PLM.Clabject contentInstance;
contentInstance = org.melanee.core.models.plm.PLM.PLMFactory.eINSTANCE.createClabjectFromTemplate((org.melanee.core.models.plm.PLM.Clabject)next, (org.melanee.core.models.plm.PLM.Element)type2Instance.get(next.eContainer()), true, true);
type2Instance.put(next, contentInstance);
}
break;
}
}
}
«ENDIF»
} catch(RuntimeException e) {
«diagramElement.getDiagram().editorGen.plugin.getActivatorQualifiedClassName()».getInstance().logError("Element initialization failed", e); //$NON-NLS-1$
}
}
«ENDDEFINE»
«DEFINE performInit(diagramElement : gmfgen::GenCommonBase, instanceVar : String, instanceClass : genmodel::GenClass, counters : Sequence(Integer)) FOR gmfgen::GenFeatureInitializer»«ENDDEFINE»
«REM»
FIXME: need cleaner appoach to provider's language switch (not to mix if == literal and polymorphism)
«ENDREM»
«DEFINE performInit(diagramElement : gmfgen::GenCommonBase, instanceVar : String, instanceClass : genmodel::GenClass, counters : Sequence(Integer)) FOR gmfgen::GenFeatureValueSpec-»
«IF value.provider.getLanguage() = gmfgen::GenLanguage::_literal-»
«EXPAND MetaModel::modifyFeature(instanceVar, instanceClass, value.body) FOR feature»
«ELSE-»
«LET getVariableName('value', counters) AS expressionVarName-»
Object «expressionVarName» = «EXPAND evaluateExpr(diagramElement, self, instanceVar) FOR value.provider»;
«IF isListType(feature)-»
if («expressionVarName» instanceof java.util.Collection) {
«EXPAND MetaModel::getFeatureValue(instanceVar, instanceClass, true) FOR feature».clear();
«IF getTypeGenClassifier(feature).expressionResultNeedsCast()-»
for (java.util.Iterator it = ((java.util.Collection) «expressionVarName»).iterator(); it.hasNext(); ) {
Object next = «diagramElement.getDiagram().editorGen.expressionProviders.getAbstractExpressionQualifiedClassName()».performCast(it.next(), «EXPAND MetaModel::MetaClass FOR getTypeGenClassifier(feature)»);
«EXPAND MetaModel::getFeatureValue(instanceVar, instanceClass, true) FOR feature».add((«EXPAND MetaModel::QualifiedClassName /*XXX sorta hack, better would be MM::setFeatureValue that supports lists*/FOR getTypeGenClassifier(feature)») next);
}
«ELSE-»
«EXPAND MetaModel::getFeatureValue(instanceVar, instanceClass, true) FOR feature».addAll(((java.util.Collection) «expressionVarName»));
«ENDIF-»
} else {
«IF getTypeGenClassifier(feature).expressionResultNeedsCast()-»
«expressionVarName» = «diagramElement.getDiagram().editorGen.expressionProviders.getAbstractExpressionQualifiedClassName()».performCast(«expressionVarName», «EXPAND MetaModel::MetaClass FOR getTypeGenClassifier(feature)»);
«ENDIF-»
«EXPAND MetaModel::getFeatureValue(instanceVar, instanceClass, true) FOR feature».add((«EXPAND MetaModel::QualifiedClassName /*XXX sorta hack, better would be MM::setFeatureValue that supports lists*/FOR getTypeGenClassifier(feature)») «expressionVarName»);
}
«ELSE-»
«IF getTypeGenClassifier(feature).expressionResultNeedsCast()»
«expressionVarName» = «diagramElement.getDiagram().editorGen.expressionProviders.getAbstractExpressionQualifiedClassName()».performCast(«expressionVarName», «EXPAND MetaModel::MetaClass FOR getTypeGenClassifier(feature)»);
«ENDIF-»
«EXPAND MetaModel::setFeatureValue(instanceVar, instanceClass, expressionVarName, true) FOR feature»;
«ENDIF /*isListType*/-»
«ENDLET-»
«ENDIF /*is literal expression*/-»
«ENDDEFINE»
«DEFINE performInit(diagramElement : gmfgen::GenCommonBase, instanceVar : String, instanceClass : genmodel::GenClass, counters : Sequence(Integer)) FOR gmfgen::GenReferenceNewElementSpec-»
«FOREACH newElementInitializers AS newElemInit-»
«LET getSuffixes(counters, (newElementInitializers->indexOf(newElemInit) - 1)) AS initializerCounters-»
«LET getVariableName('newInstance', initializerCounters) AS newInstanceVar-»
«EXPAND MetaModel::NewInstance(newInstanceVar) FOR newElemInit.elementClass-»
«EXPAND MetaModel::modifyFeature(instanceVar, instanceClass, newInstanceVar) FOR feature»
«FOREACH newElemInit.initializers AS i»«EXPAND performInit(diagramElement, newInstanceVar, newElemInit.elementClass, getSuffixes(initializerCounters, (newElemInit.initializers->indexOf(i) - 1))) FOR i»«ENDFOREACH»
«ENDLET-»
«ENDLET-»
«ENDFOREACH-»
«ENDDEFINE»
/////////////////////////////////
«DEFINE evaluateExpr(diagramElement : gmfgen::GenCommonBase, valueExpr : gmfgen::GenFeatureValueSpec, instanceVar : String) FOR gmfgen::GenExpressionProviderBase»«ENDDEFINE»
«DEFINE evaluateExpr(gmfgen::GenCommonBase diagramElement, gmfgen::GenFeatureValueSpec valueExpr, String instanceVar) FOR gmfgen::GenExpressionInterpreter-»
«EXPAND xpt::expressions::getExpression::getExpression(valueExpr.value, valueExpr.featureSeqInitializer.elementClass)».evaluate(«instanceVar»)«-»
«ENDDEFINE»
«REM»
XXX revisit: if emf java merge doesn't support genereated NOT methods with modified
return type, there's no much sense to keep Object value = invokeJavaMethodWithSpecificReturnType,
as client won't benefit from such code (he can't modify return type and thus would get duplicated methods on regeneration)
However, if merge does ignore method return type when merging, allowing Object as return type may help.
«ENDREM»
«DEFINE evaluateExpr(diagramElement : gmfgen::GenCommonBase, valueExpr : gmfgen::GenFeatureValueSpec, instanceVar : String) FOR gmfgen::GenJavaExpressionProvider-»
«javaMethodName(diagramElement, valueExpr)»(«instanceVar»)«-»
«ENDDEFINE»
/////////////////////////////////
«DEFINE javaMethod FOR gmfgen::GenNode»«EXPAND javaMethod(self) FOR modelFacet»«ENDDEFINE»
«DEFINE javaMethod FOR gmfgen::GenLink»«EXPAND javaMethod(self) FOR modelFacet»«ENDDEFINE»
«DEFINE javaMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::ModelFacet»«ENDDEFINE»
«DEFINE javaMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::TypeModelFacet-»
«EXPAND javaMethod(diagramElement) FOR modelElementInitializer-»
«ENDDEFINE»
«DEFINE javaMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::GenElementInitializer»«ERROR 'No idea how to handle ' + self.repr()»«ENDDEFINE»
«DEFINE javaMethod(diagramElement : gmfgen::GenCommonBase) FOR gmfgen::GenFeatureSeqInitializer-»
«FOREACH recurseCollectValueSpec(self) AS vs»«EXPAND javaMethod(diagramElement, vs) FOR vs.value.provider»«ENDFOREACH-»
«ENDDEFINE»
«DEFINE javaMethod(diagramElement : gmfgen::GenCommonBase, vs : gmfgen::GenFeatureValueSpec) FOR gmfgen::GenExpressionProviderBase-»«ENDDEFINE»
«DEFINE javaMethod(diagramElement : gmfgen::GenCommonBase, vs : gmfgen::GenFeatureValueSpec) FOR gmfgen::GenJavaExpressionProvider-»
«EXPAND xpt::Common::generatedMemberComment»
private «EXPAND MetaModel::featureTargetType FOR vs.feature» «javaMethodName(diagramElement, vs)»(«EXPAND MetaModel::QualifiedClassName FOR vs.featureSeqInitializer.elementClass» self) {
«IF injectExpressionBody and (vs.value.body <> null and vs.value.body.size() <> 0)-»
«vs.value.body»
«ELSEIF throwException or (injectExpressionBody and (vs.value.body = null or vs.value.body.size() = 0))-»
// TODO: implement this method to return value
// for «EXPAND MetaModel::MetaFeature FOR vs.feature»
// Ensure that you remove @generated or mark it @generated NOT
throw new java.lang.UnsupportedOperationException("No user java implementation provided in '«javaMethodName(diagramElement, vs)»' operation");«EXPAND xpt::Common::nonNLS»
«ELSE-»
return null;
«ENDIF-»
}
«ENDDEFINE»
// GenLink or GenNode
«DEFINE initMethodCall(gmfgen::TypeModelFacet modelFacet, String newElementVar) FOR gmfgen::GenCommonBase-»
«IF modelFacet.modelElementInitializer <> null -»
«getDiagram().getElementInitializersPackageName()».«getDiagram().getElementInitializersClassName()».getInstance().init_«getUniqueIdentifier()»(«newElementVar»);
«ENDIF-»
«ENDDEFINE»