Commit b80aa333 authored by Marco Konersmann's avatar Marco Konersmann

Translates component hierarchy in code2tm for the running example (shown as prototype)

parent 4120c4cc
......@@ -121,6 +121,7 @@ public abstract class JavaBasedImplementationLanguageDefinition extends Implemen
t.getIALHolder().setFoundationalIALElement(foundationalIALElement);
t.getIALHolder().setPriorFoundationalIALElement(priorFoundationalIALElement);
t.getIALHolder().setIALCodeElement(codeElement);
t.getIALHolder().setIALRoots(newTMRoots);
if (findTranslatedElements == null)
findTranslatedElements = new FindTranslatedElements();
t.setWhereToFindTranslatedElements(findTranslatedElements);
......@@ -169,6 +170,10 @@ public abstract class JavaBasedImplementationLanguageDefinition extends Implemen
t.setIDRegistry(idRegistry);
t.setModelElement(imElement);
t.getIALHolder().setFoundationalIALElement(tmElement);
t.getIALHolder().setIALRoots(tmRoots);
if (findTranslatedElements == null)
findTranslatedElements = new FindTranslatedElements();
t.setWhereToFindTranslatedElements(findTranslatedElements);
JavaToModelTransformation executor = new JavaToModelTransformation(projects, idRegistry,
(AbstractModelCodeTransformation<?, ?>) t, null);
executor.execute(monitor);
......
......@@ -180,9 +180,11 @@ public class ProfilesUtils {
private static StereotypeApplication applyStereotype(Stereotype stereotype, Profile profile, EObject eObject,
Resource profileApplicationResource) throws IOException {
Extension extension = getDefaultExtension(stereotype, eObject, profileApplicationResource);
StereotypeApplication application = createProfileFacadeInstance(profile, profileApplicationResource)
.apply(stereotype, eObject, extension);
application.setExtension(null);
final StereotypeApplication application = (StereotypeApplication) stereotype
.getEPackage().getEFactoryInstance().create(stereotype);
application.setExtension(extension);
application.setAppliedTo(eObject);
eObject.eResource().getContents().add(application);
application.eResource().save(null);
return application;
}
......
package de.mkonersmann.advert.java.embed.transformation.references;
import java.util.List;
import org.codeling.utils.errorcontainers.MayHaveIssues;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.core.IJavaElement;
......@@ -14,6 +16,9 @@ public class IALHolder extends MayHaveIssues {
// Code Element
private IJavaElement ialCodeElement;
// IAL roots for adding stereotype applications
private List<EObject> ialRoots;
public EObject getPriorFoundationalIALElement() {
return priorFoundationalIALElement;
......@@ -38,4 +43,12 @@ public class IALHolder extends MayHaveIssues {
public void setIALCodeElement(IJavaElement ialCodeElement) {
this.ialCodeElement = ialCodeElement;
}
public List<EObject> getIALRoots() {
return ialRoots;
}
public void setIALRoots(List<EObject> ialRoots) {
this.ialRoots = ialRoots;
}
}
......@@ -78,7 +78,9 @@ public class EjbWithStateMachineLanguage extends JavaBasedImplementationLanguage
@Override
public List<String> getSelectedProfiles() {
return new HenshinTGGBasedLanguageDefinitionHelper().getSelectedProfiles(henshinTGGFileURI);
List<String> result = new HenshinTGGBasedLanguageDefinitionHelper().getSelectedProfiles(henshinTGGFileURI);
result.add(Profiles.COMPONENTS_HIERARCHY_SCOPED.getNsURI()); // Can also handle scoped hierarchies via the MIC
return result;
}
}
package org.codeling.lang.ejbWithStatemachine.transformation.component_feature;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;
import org.codeling.mechanisms.transformations.ClassMechanismTransformation;
import org.codeling.mechanisms.transformations.ReferenceMechanismTransformation;
import org.codeling.mechanisms.transformations.references.AnnotatedMemberReferenceTransformation;
import org.codeling.utils.CodelingException;
import org.codeling.utils.Models;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
......@@ -40,7 +42,7 @@ public class ChildTransformation extends ReferenceMechanismTransformation<Stereo
private void addChildAnnotation(IField field) {
try {
JDTUtils.addImportIfNecessary(codeElement,
"org.codeling.lang.ejbWithStatemachine.ial.mm.componentType.Child", null);
"org.codeling.lang.ejbWithStatemachine.ial.mm.componenttype_feature.Child", null);
ASTUtils.addAnnotation(field, "Child", null);
} catch (JavaModelException e) {
e.printStackTrace();
......@@ -118,6 +120,56 @@ public class ChildTransformation extends ReferenceMechanismTransformation<Stereo
@Override
public StereotypeApplication transformToModel() throws CodelingException {
resolveCodeElement();
IField[] fields;
try {
fields = codeElement.getFields();
} catch (JavaModelException e) {
throw new CodelingException(MessageFormat.format("Could not find fields of code element  [{0}]",
codeElement.getHandleIdentifier()), e);
}
for (IField field : fields) {
if (hasExpectedAnnotation(field)) {
IType targetType = null;
try {
targetType = ASTUtils.getFieldType(field);
} catch (JavaModelException e1) {
addError(MessageFormat.format("Could not resolve type of field [{0}]", field.getHandleIdentifier()),
e1);
continue;
}
String id = idRegistry.getIDFromCodeElement(targetType);
if (id == null)
continue; // This field is not part of any model notation
try {
StereotypeApplication application = ProfilesUtils.findOrApplyStereotype(
"HierarchicalComponentTypeScoped", Profiles.COMPONENTS_HIERARCHY_SCOPED.load(),
modelElement, holder.getFoundationalIALElement().eResource());
EObject target = idRegistry.resolveTranslationModelElement(id,
EcoreUtil.getRootContainer(holder.getFoundationalIALElement()));
@SuppressWarnings("unchecked")
List<EObject> targets = (List<EObject>) application.eGet(eReference);
if (targets == null) {
targets = new LinkedList<EObject>();
targets.add(target);
application.eSet(eReference, targets);
} else {
targets.add(target);
}
} catch (IOException e) {
addError("Could not find or apply stereotype \"HierarchicalComponentTypeScoped\" to a target.");
continue;
}
}
}
// This should also add the scoped hierarchy stereotype to the architecture and
// ensure that all root component types are referenced as system instance
// Always return null. Cannot return multiple StereotypeApplications
return null;
}
......
......@@ -137,6 +137,9 @@ public class IDRegistry {
}
public String getIDFromImplementationModelElement(EObject implementationModelElement) {
if (implementationModelElement == null)
throw new IllegalArgumentException("Implementation Model Element must not be null.");
final String implementationModelPath = getPath(implementationModelElement);
for (final Entry<String, String[]> e : id2paths.entrySet()) {
......@@ -145,12 +148,13 @@ public class IDRegistry {
}
}
throw new IllegalArgumentException(String.format(
"Could not get ID from implementation model element '%s'. No implementation model element with that path found.",
implementationModelPath));
return null;
}
public String getIDFromTranslationModelElement(EObject translationModelElement) {
if (translationModelElement == null)
throw new IllegalArgumentException("Translation Model must not be null.");
final String translationModelPath = getPath(translationModelElement);
for (final Entry<String, String[]> e : id2paths.entrySet()) {
......@@ -162,6 +166,21 @@ public class IDRegistry {
return null;
}
public String getIDFromCodeElement(IJavaElement codeElement) {
if (codeElement == null)
throw new IllegalArgumentException("Code Element must not be null.");
final String codePath = codeElement.getHandleIdentifier();
for (final Entry<String, String[]> e : id2paths.entrySet()) {
if (e.getValue()[0] != null && e.getValue()[0].equals(codePath)) {
return e.getKey();
}
}
return null;
}
public EObject resolveImplementationModelElement(EObject translationModelElement,
EObject implementationModelRootElement) {
final String translationModelPath = getPath(translationModelElement);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment