Commit 8709250f authored by Simon Barner's avatar Simon Barner
Browse files

- Add getOrCreateAllocationTable(), a method that retrieves the {@link...

- Add getOrCreateAllocationTable(), a method that retrieves the {@link AllocationTable} specialization of the given type, or that creates it, if it does not exist yet.
refs 3067
parent 3e353998
AllocationModelElementFactory.java 0d13ccc9da944dd9a25db262eb1fa47d1a9582e9 YELLOW
AllocationUtils.java b0d7acc0e4da9cea2298f4abdc82d3f2a84a11e7 YELLOW
AllocationUtils.java 2ab0769710bb0bb0f8c05e2f2b23e9493d75b0cc YELLOW
......@@ -15,11 +15,17 @@
+--------------------------------------------------------------------------*/
package org.fortiss.af3.allocation.utils;
import static org.eclipse.emf.ecore.util.EcoreUtil.create;
import static org.fortiss.af3.allocation.utils.AllocationModelElementFactory.createAllocationEntry;
import static org.fortiss.af3.allocation.utils.AllocationModelElementFactory.createAllocationTableCollection;
import static org.fortiss.tooling.common.util.LambdaUtils.asStream;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getEClassForClass;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.pickInstanceOf;
import static org.fortiss.tooling.kernel.utils.KernelModelElementUtils.computeFullyQualifiedName;
import static org.fortiss.tooling.kernel.utils.KernelModelElementUtils.getRootElements;
import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
import org.eclipse.emf.ecore.EObject;
import org.fortiss.af3.allocation.AF3AllocationActivator;
import org.fortiss.af3.allocation.model.AllocationEntry;
import org.fortiss.af3.allocation.model.AllocationTable;
......@@ -31,10 +37,15 @@ import org.fortiss.af3.allocation.model.OneToManyAllocationEntry;
import org.fortiss.af3.allocation.model.OneToOneAllocationEntry;
import org.fortiss.af3.allocation.model.ParameterEntry;
import org.fortiss.af3.allocation.model.ParameterTable;
import org.fortiss.af3.project.model.FileProject;
import org.fortiss.af3.project.utils.ProjectUtils;
import org.fortiss.tooling.base.annotation.AnnotationEntry;
import org.fortiss.tooling.base.annotation.IAnnotationValueService;
import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
import org.fortiss.tooling.base.model.element.IModelElement;
import org.fortiss.tooling.kernel.model.INamedElement;
import org.fortiss.tooling.kernel.model.IProjectRootElement;
import org.fortiss.tooling.kernel.service.IElementCompositorService;
/**
* Utility methods for working with allocations ({@link AllocationTableCollection}s,
......@@ -356,6 +367,57 @@ public class AllocationUtils {
return null;
}
/**
* Retrieves the {@link AllocationTable} specialization of the given {@code allocationTableType}
* that references the given {@code sourceView} and {@code targetView} (at most one of these
* references may be {@code null} in which case it is ignored). In case such
* {@link AllocationTable} specialization does not exit yet, it is created (with the given
* {@code name}).
*
* @param allocationTableType
* Desired {@link AllocationTable} type.
* @param sourceView
* Source model that must be referenced by the returned {@link AllocationTable}
* (ignored when {@code null}).
* @param targetView
* Target model that must be referenced by the returned {@link AllocationTable}
* (ignored when {@code null}).
* @param name
* Name of allocationTable specialization in case it has to be created.
* @return {@link AllocationTable} specialization of the given {@code allocationTableType} that
* references the given {@code sourceView} and {@code targetView}.
*/
public static <T extends AllocationTable & INamedElement> T getOrCreateAllocationTable(
Class<T> allocationTableType, IProjectRootElement sourceView,
IProjectRootElement targetView, String name) {
// Try to retrieve existing AllocationTable specialization
EObject modelRef = sourceView != null ? sourceView : targetView;
for(AllocationTableCollection atc : getRootElements(modelRef,
AllocationTableCollection.class)) {
for(T at : pickInstanceOf(allocationTableType, atc.getAllocationTables())) {
if((sourceView == null || at.getSourceView() == sourceView) &&
(targetView == null || at.getTargetView() == targetView)) {
return at;
}
}
}
// Create and initialize new one
AllocationTableCollection atc = createAllocationTableCollection("Allocations");
FileProject fileProject = ProjectUtils.getFileProject(modelRef);
IElementCompositorService cs = IElementCompositorService.getInstance();
cs.compose(fileProject, atc, null);
@SuppressWarnings("unchecked") T at = (T)create(getEClassForClass(allocationTableType));
at.setName(name);
at.setSourceView(sourceView);
at.setTargetView(targetView);
atc.getContainedElements().add(at);
return at;
}
/** Returns the {@link AnnotationEntry} for the given allocation parameter. */
private static AnnotationEntry getAnnotationEntry(ParameterTable parameterTable,
IModelElement sourceElement, IModelElement targetElement,
......
Supports Markdown
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