diff options
author | Maxime Porhel | 2020-10-14 08:29:56 +0000 |
---|---|---|
committer | Maxime Porhel | 2020-10-23 13:59:17 +0000 |
commit | 9cf51c4b29d4b1ae61ef34e2b986ab5928df3675 (patch) | |
tree | ee9df3f8f27114bd7935d524c1939a9864d46eee /plugins | |
parent | 6cdbdb469a12dff87cfba17b6d44c2bccd9be725 (diff) | |
download | org.eclipse.sirius-9cf51c4b29d4b1ae61ef34e2b986ab5928df3675.tar.gz org.eclipse.sirius-9cf51c4b29d4b1ae61ef34e2b986ab5928df3675.tar.xz org.eclipse.sirius-9cf51c4b29d4b1ae61ef34e2b986ab5928df3675.zip |
[567517] Add a cache for EventEndHelper.findEndsFromSemanticOrdering
- This allows to have a EventEnds cache for each diagram.
- The cache is cleared when the eventEnds list of the ordering changes
- The adapter is created on the first call to
findEndsFromSemanticOrdering
Bug: 567517
Change-Id: I8756ab466e367a2e35f4732e443083f4bd0d2b10
Signed-off-by: Nathalie Lepine <nathalie.lepine@obeo.fr>
Signed-off-by: Maxime Porhel <maxime.porhel@obeo.fr>
Diffstat (limited to 'plugins')
2 files changed, 94 insertions, 32 deletions
diff --git a/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/operation/RefreshSemanticOrderingsOperation.java b/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/operation/RefreshSemanticOrderingsOperation.java index 08c784f366..420954c57b 100644 --- a/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/operation/RefreshSemanticOrderingsOperation.java +++ b/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/operation/RefreshSemanticOrderingsOperation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2015 THALES GLOBAL SERVICES and others. + * Copyright (c) 2010, 2020 THALES GLOBAL SERVICES and others. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -44,9 +44,8 @@ import com.google.common.collect.Iterables; import com.google.common.collect.Lists; /** - * An operation to re-compute the global semantic orderings of events and - * instance roles in a sequence diagram according to the user-specified - * criteria. + * An operation to re-compute the global semantic orderings of events and instance roles in a sequence diagram according + * to the user-specified criteria. * * <pre> * Semantic Model + User-specified Ordering Expression ---> SemanticMessageOrdering @@ -56,8 +55,7 @@ import com.google.common.collect.Lists; */ public class RefreshSemanticOrderingsOperation extends AbstractModelChangeOperation<Boolean> { /** - * The name of the variable used to pass event ends to sort to - * user-specified expressions. + * The name of the variable used to pass event ends to sort to user-specified expressions. */ private static final String EVENT_ENDS_TO_SORT_VARIABLE = "eventEnds"; //$NON-NLS-1$ @@ -100,16 +98,17 @@ public class RefreshSemanticOrderingsOperation extends AbstractModelChangeOperat } /** - * Returns all the event ends of the current Sequence diagram. + * Returns all the event ends of the current Sequence diagram. * - * The default implementation does the computation on each call, subclasses may override this method to change this behavior. + * The default implementation does the computation on each call, subclasses may override this method to change this + * behavior. * * @return an Iterable with all event ends. */ protected Iterable<? extends EventEnd> getAllEventEnds() { return RefreshOrderingHelper.getAllEventEnds(sequenceDDiagram); } - + private List<EventEnd> computeEventEndsOrdering(EventEndsOrdering semanticOrdering, Iterable<? extends EventEnd> allEnds) { Map<EObject, EventEnd> index = new HashMap<>(); for (EventEnd eventEnd : allEnds) { diff --git a/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/ordering/EventEndHelper.java b/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/ordering/EventEndHelper.java index 9910bf3a95..213e13baf4 100644 --- a/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/ordering/EventEndHelper.java +++ b/plugins/org.eclipse.sirius.diagram.sequence/src/org/eclipse/sirius/diagram/sequence/business/internal/ordering/EventEndHelper.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 THALES GLOBAL SERVICES. + * Copyright (c) 2010, 2020 THALES GLOBAL SERVICES. * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 * which accompanies this distribution, and is available at @@ -15,33 +15,40 @@ package org.eclipse.sirius.diagram.sequence.business.internal.ordering; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.emf.common.notify.Notification; +import org.eclipse.emf.common.notify.impl.AdapterImpl; import org.eclipse.emf.ecore.EObject; import org.eclipse.sirius.diagram.sequence.SequenceDDiagram; import org.eclipse.sirius.diagram.sequence.business.internal.elements.ISequenceEvent; import org.eclipse.sirius.diagram.sequence.business.internal.elements.SequenceDiagram; import org.eclipse.sirius.diagram.sequence.ordering.CompoundEventEnd; import org.eclipse.sirius.diagram.sequence.ordering.EventEnd; +import org.eclipse.sirius.diagram.sequence.ordering.EventEndsOrdering; +import org.eclipse.sirius.diagram.sequence.ordering.OrderingPackage; import org.eclipse.sirius.diagram.sequence.ordering.SingleEventEnd; import org.eclipse.sirius.ext.base.Option; import com.google.common.base.Function; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; /** - * Helper class to factor common code for semantic and graphical orders - * refreshing. + * Helper class to factor common code for semantic and graphical orders refreshing. * * @author mporhel */ public final class EventEndHelper { /** - * Function to find and returns the EventEnds corresponding to the given - * part. + * Function to find and returns the EventEnds corresponding to the given part. */ public static final Function<ISequenceEvent, List<EventEnd>> EVENT_ENDS = new Function<ISequenceEvent, List<EventEnd>>() { + @Override public List<EventEnd> apply(ISequenceEvent from) { return EventEndHelper.findEndsFromSemanticOrdering(from); } @@ -51,26 +58,27 @@ public final class EventEndHelper { * A function which compute the semantic end element from a event end. */ public static final Function<EventEnd, EObject> SEMANTIC_END = new Function<EventEnd, EObject>() { + @Override public EObject apply(EventEnd from) { return from.getSemanticEnd(); } }; /** - * A predicate which check that the given {@link SingleEventEnd} is a - * starting event. + * A predicate which check that the given {@link SingleEventEnd} is a starting event. */ public static final Predicate<SingleEventEnd> IS_START = new Predicate<SingleEventEnd>() { + @Override public boolean apply(SingleEventEnd from) { return from.isStart(); } }; /** - * A predicate which check that the given {@link EventEnd} is a punctual - * compound event end. + * A predicate which check that the given {@link EventEnd} is a punctual compound event end. */ public static final Predicate<EventEnd> PUNCTUAL_COMPOUND_EVENT_END = new Predicate<EventEnd>() { + @Override public boolean apply(EventEnd input) { return input instanceof CompoundEventEnd && EventEndHelper.getSemanticEvents(input).size() == 1; } @@ -126,9 +134,8 @@ public final class EventEndHelper { } /** - * Finds and returns the EventEnds corresponding to the given part, using - * the semantic ordering instead of the graphical ordering used by the plain - * {@link #findEnds(ISequenceEventEditPart)}. + * Finds and returns the EventEnds corresponding to the given part, using the semantic ordering instead of the + * graphical ordering used by the plain {@link #findEnds(ISequenceEventEditPart)}. * * @param part * the part to look for @@ -139,11 +146,32 @@ public final class EventEndHelper { SequenceDiagram sdep = part.getDiagram(); SequenceDDiagram seqDiag = (SequenceDDiagram) sdep.getNotationDiagram().getElement(); Option<EObject> semanticEvent = part.getSemanticTargetElement(); - for (EventEnd ee : seqDiag.getSemanticOrdering().getEventEnds()) { - if (semanticEvent.some() && EventEndHelper.getSemanticEvents(ee).contains(semanticEvent.get())) { - ends.add(ee); + if (semanticEvent.some()) { + EObject eObject = semanticEvent.get(); + + EventEndsOrdering semanticOrdering = seqDiag.getSemanticOrdering(); + Optional<EventEndsCache> eventEndsCache = semanticOrdering.eAdapters().stream().filter(EventEndsCache.class::isInstance).map(EventEndsCache.class::cast).findFirst(); + if (eventEndsCache.isPresent()) { + ends = eventEndsCache.get().getEventEndsFromCache(eObject); + } else { + EventEndsCache cache = new EventEndsCache(); + semanticOrdering.eAdapters().add(cache); + eventEndsCache = Optional.of(cache); + } + + if (ends == null) { + ends = new ArrayList<EventEnd>(); + + for (EventEnd ee : semanticOrdering.getEventEnds()) { + if (EventEndHelper.getSemanticEvents(ee).contains(eObject)) { + ends.add(ee); + } + } + eventEndsCache.get().putEventEndsInCache(eObject, ends); } + ends = Lists.newArrayList(ends); } + return ends; } @@ -160,9 +188,10 @@ public final class EventEndHelper { public static Iterable<ISequenceEvent> getIndependantEvents(ISequenceEvent self, Collection<ISequenceEvent> childrenEvents) { final List<EventEnd> parentEnds = EventEndHelper.findEndsFromSemanticOrdering(self); Predicate<ISequenceEvent> isValidSubEvent = new Predicate<ISequenceEvent>() { + @Override public boolean apply(ISequenceEvent input) { List<EventEnd> inputEnds = EventEndHelper.findEndsFromSemanticOrdering(input); - boolean res = inputEnds.removeAll(parentEnds); + boolean res = inputEnds.stream().anyMatch(element -> parentEnds.contains(element)); return !res; } }; @@ -170,15 +199,13 @@ public final class EventEndHelper { } /** - * Returns the list of direct compound-events of this given event, in - * chronological (and thus also graphical) order. This includes events with - * same semantic ends + * Returns the list of direct compound-events of this given event, in chronological (and thus also graphical) order. + * This includes events with same semantic ends * * @param self * The given {@link ISequenceEventEditPart}. * - * @return the list of direct compound-events of this event, in - * chronological order. + * @return the list of direct compound-events of this event, in chronological order. */ public static List<ISequenceEvent> getCompoundEvents(ISequenceEvent self) { List<ISequenceEvent> compoundEvents = new ArrayList<>(); @@ -200,8 +227,7 @@ public final class EventEndHelper { } /** - * Finds and returns the ISequenceEvent corresponding to the given - * SingleEventEnd. + * Finds and returns the ISequenceEvent corresponding to the given SingleEventEnd. * * @param end * the end to look for @@ -218,4 +244,41 @@ public final class EventEndHelper { } return null; } + + private static final class EventEndsCache extends AdapterImpl { + + private Map<EObject, List<EventEnd>> eventEndCache = new ConcurrentHashMap<>(); + + @Override + public void notifyChanged(Notification notification) { + if (!notification.isTouch() && OrderingPackage.eINSTANCE.getEventEndsOrdering_EventEnds().equals(notification.getFeature())) { + eventEndCache.clear(); + } + + } + + /** + * Return event ends for eObject. + * + * @param eObject + * EObject + * @return event ends for eObject. + */ + public List<EventEnd> getEventEndsFromCache(EObject eObject) { + return eventEndCache.get(eObject); + } + + /** + * Put event ends for eObject. + * + * @param eObject + * EObject + * @param ends + * List<EventEnd> + */ + public void putEventEndsInCache(EObject eObject, List<EventEnd> ends) { + eventEndCache.put(eObject, ends); + } + + } } |