Skip to content
Snippets Groups Projects
Commit 873218cd authored by Simon Barner's avatar Simon Barner
Browse files

Merge branch '4233' into 'master'

Added removeNonMatchingConnections() for refactoring reuse providers

See merge request !176
parents db08c239 98b9bdac
No related branches found
No related tags found
1 merge request!176Added removeNonMatchingConnections() for refactoring reuse providers
ReuseLibraryModelElementFactory.java 2e7e8f0b3b6a93225204bfbfc6223e28c2bd69b4 GREEN ReuseLibraryModelElementFactory.java 2e7e8f0b3b6a93225204bfbfc6223e28c2bd69b4 GREEN
ReuseLibraryUtils.java 0dce9ab39ccc7646c9cddad2bf882ec1e4cfd777 GREEN ReuseLibraryUtils.java 9ba005d6be2e2e2fc0c8635a9567c000b8dd9e9b GREEN
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
+--------------------------------------------------------------------------*/ +--------------------------------------------------------------------------*/
package org.fortiss.tooling.ext.reuse.utils; package org.fortiss.tooling.ext.reuse.utils;
import static org.eclipse.emf.ecore.util.EcoreUtil.delete;
import static org.eclipse.emf.ecore.util.EcoreUtil.replace; import static org.eclipse.emf.ecore.util.EcoreUtil.replace;
import static org.fortiss.tooling.ext.reuse.ToolingReuseActivator.getDefault; import static org.fortiss.tooling.ext.reuse.ToolingReuseActivator.getDefault;
import static org.fortiss.tooling.ext.reuse.service.ReuseProviderService.getPossibleExternalReferenceClasses; import static org.fortiss.tooling.ext.reuse.service.ReuseProviderService.getPossibleExternalReferenceClasses;
...@@ -25,7 +26,9 @@ import static org.fortiss.tooling.ext.reuse.storage.ReuseLibraryStorageManager.s ...@@ -25,7 +26,9 @@ import static org.fortiss.tooling.ext.reuse.storage.ReuseLibraryStorageManager.s
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseElementSpec; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseElementSpec;
import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseLibrary; import static org.fortiss.tooling.ext.reuse.utils.ReuseLibraryModelElementFactory.createReuseLibrary;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.copy; import static org.fortiss.tooling.kernel.utils.EcoreUtils.copy;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getAllReferencesSetting;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType; import static org.fortiss.tooling.kernel.utils.EcoreUtils.getChildrenWithType;
import static org.fortiss.tooling.kernel.utils.EcoreUtils.replaceEObjectReferences;
import static org.fortiss.tooling.kernel.utils.LoggingUtils.error; import static org.fortiss.tooling.kernel.utils.LoggingUtils.error;
import static org.fortiss.tooling.kernel.utils.LoggingUtils.showError; import static org.fortiss.tooling.kernel.utils.LoggingUtils.showError;
import static org.fortiss.tooling.kernel.utils.UniqueIDUtils.generateMissingIDs; import static org.fortiss.tooling.kernel.utils.UniqueIDUtils.generateMissingIDs;
...@@ -36,12 +39,17 @@ import java.io.IOException; ...@@ -36,12 +39,17 @@ import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.EList; import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.fortiss.tooling.base.model.element.IConnection;
import org.fortiss.tooling.base.model.element.IConnector;
import org.fortiss.tooling.base.model.element.IHierarchicElement;
import org.fortiss.tooling.base.model.element.IModelElement; import org.fortiss.tooling.base.model.element.IModelElement;
import org.fortiss.tooling.base.model.element.IModelElementSpecification; import org.fortiss.tooling.base.model.element.IModelElementSpecification;
import org.fortiss.tooling.ext.reuse.model.ReuseElementSpec; import org.fortiss.tooling.ext.reuse.model.ReuseElementSpec;
...@@ -309,6 +317,71 @@ public class ReuseLibraryUtils { ...@@ -309,6 +317,71 @@ public class ReuseLibraryUtils {
setReuseElementLastUpdate(element, new Date()); setReuseElementLastUpdate(element, new Date());
} }
/**
* Removes all {@link IConnection}s from/to the target element except the
* {@link IConnection} that belongs to a {@link IConnector} of the target
* element that can be matched to a {@link IConnector} of the source element. In
* this case, the {@link IConnection} will be updated to connect now to the
* source element instead of the target element. This method is useful when the
* target element should be replaced by the source element during reuse.
* Regarding the matching, a {@link Comparator} must be given that realizes a
* compare() method that returns 0 if both given {@link IConnector}s are
* matching, otherwise +/-1.
*
* @param target The target element (e.g. the replaced
* element)
* @param source The source element (e.g. the replacing
* element)
* @param connectorMatchComparator A comparator that will return 0 with
* compare() if both given {@link IConnector}s
* are matching
*/
public static void removeNonMatchingConnections(IHierarchicElement target, IHierarchicElement source,
Comparator<IConnector> connectorMatchComparator) {
IConnector matchedConnector = null;
for (IConnector targetConnector : target.getConnectors()) {
// check if a matching connector can be found
boolean saveConnections = false;
for (IConnector sourceConnector : source.getConnectors()) {
// matching is done via custom compare method (individual comparators possible)
if (connectorMatchComparator.compare(targetConnector, sourceConnector) == 0) {
matchedConnector = sourceConnector;
saveConnections = true;
replaceEObjectReferences(targetConnector, matchedConnector);
break;
}
}
// remove all connections or replace one end of them if a match was found
List<IConnection> connectionsToBeRemoved = new ArrayList<IConnection>();
for (IConnection incoming : targetConnector.getIncoming()) {
if (saveConnections) {
incoming.setTarget(matchedConnector);
} else {
connectionsToBeRemoved.add(incoming);
}
}
for (IConnection outgoing : targetConnector.getOutgoing()) {
if (saveConnections) {
outgoing.setSource(matchedConnector);
} else {
connectionsToBeRemoved.add(outgoing);
}
}
for (IConnection connection : connectionsToBeRemoved) {
delete(connection);
}
// delete all external references
if (!saveConnections) {
for (Setting setting : getAllReferencesSetting(targetConnector)) {
delete(setting.getEObject());
}
}
}
}
/** /**
* Returns all {@link ReuseElementSpec}s of the given element to a * Returns all {@link ReuseElementSpec}s of the given element to a
* {@link ReuseLibrary}. If none exist, an empty list will be returned. * {@link ReuseLibrary}. If none exist, an empty list will be returned.
...@@ -800,5 +873,4 @@ public class ReuseLibraryUtils { ...@@ -800,5 +873,4 @@ public class ReuseLibraryUtils {
} }
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment