Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08a784972e0902cc622d752a56b9469141f1f51b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*****************************************************************************
 * Copyright (c) 2015 Christian W. Damus and others.
 * 
 * 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.aof.sync.examples.uml.internal.util;

import static org.eclipse.papyrus.aof.sync.examples.uml.internal.util.NamedElements.shallowCopy;
import static org.eclipse.papyrus.infra.tools.util.StreamUtil.asStream;
import static org.eclipse.papyrus.infra.tools.util.StreamUtil.select;

import java.util.Objects;
import java.util.stream.Stream;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import org.eclipse.uml2.uml.Behavior;
import org.eclipse.uml2.uml.BehavioredClassifier;
import org.eclipse.uml2.uml.RedefinableElement;
import org.eclipse.uml2.uml.Region;
import org.eclipse.uml2.uml.State;
import org.eclipse.uml2.uml.StateMachine;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.Vertex;

/**
 * Static utilities for resolving the redefining element of some element in a given context.
 */
public class RedefinitionUtil {
	private RedefinitionUtil() {
		super();
	}

	/**
	 * Finds the behavior in a given {@code context} that redefines the specified {@code behavior},
	 * or creates a new one if it doesn't yet exist.
	 */
	public static Behavior getRedefiningBehavior(Behavior behavior, BehavioredClassifier context) {
		// This is the reference that is actually stored and read from serialization
		return getInverseReferencers(behavior, UMLPackage.Literals.STATE_MACHINE__EXTENDED_STATE_MACHINE, Behavior.class)
				.filter(b -> b.getContext() == context)
				.findFirst().orElseGet(() -> shallowCopy(behavior));
	}

	private static <T extends EObject> Stream<T> getInverseReferencers(EObject object, EReference reference, Class<T> type) {
		return ECrossReferenceAdapter.getCrossReferenceAdapter(object).getInverseReferences(object).stream()
				.filter(s -> (s.getEStructuralFeature() == reference) && type.isInstance(s.getEObject()))
				.map(s -> type.cast(s.getEObject()));
	}

	/**
	 * Finds the region in a given {@code context} that redefines the specified {@code region},
	 * or creates a new one if it doesn't yet exist.
	 */
	public static Region getRedefiningRegion(Region region, StateMachine context) {
		return getInverseReferencers(region, UMLPackage.Literals.REGION__EXTENDED_REGION, Region.class)
				.filter(r -> r.getStateMachine() == context)
				.findFirst().orElseGet(() -> shallowCopy(region));
	}

	/**
	 * Finds the region in a given {@code context} that redefines the specified {@code region},
	 * or creates a new one if it doesn't yet exist.
	 */
	public static Region getRedefiningCompositeStateRegion(Region region, State context) {
		return getInverseReferencers(region, UMLPackage.Literals.REGION__EXTENDED_REGION, Region.class)
				.filter(r -> r.getState() == context)
				.findFirst().orElseGet(() -> shallowCopy(region));
	}

	/**
	 * Finds the state in a given {@code context} that redefines the specified {@code state},
	 * or creates a new one if it doesn't yet exist.
	 */
	public static State getRedefiningState(State state, Region context) {
		return getInverseReferencers(state, UMLPackage.Literals.STATE__REDEFINED_STATE, State.class)
				.filter(s -> s.getContainer() == context)
				.findFirst().orElseGet(() -> shallowCopy(state));
	}

	/**
	 * Finds the vertex in a given {@code context} that redefines the specified {@code vertex},
	 * or creates a new one if it doesn't yet exist.
	 */
	public static <V extends Vertex> V getRedefiningVertex(V vertex, Region context) {
		@SuppressWarnings("unchecked")
		final Class<V> type = (Class<V>) vertex.getClass();

		V result;

		if (vertex instanceof State) {
			result = type.cast(getRedefiningState((State) vertex, context));
		} else {
			result = context.getSubvertices().stream()
					.filter(type::isInstance).map(type::cast)
					.filter(v -> Objects.equals(v.getName(), vertex.getName()) && v.eClass() == vertex.eClass())
					.findFirst().orElseGet(() -> shallowCopy(vertex));
		}

		return result;
	}

	/**
	 * Finds the transition in a given {@code context} that redefines the specified {@code transition},
	 * or creates a new one if it doesn't yet exist.
	 */
	public static Transition getRedefiningTransition(Transition transition, Region context) {
		return getInverseReferencers(transition, UMLPackage.Literals.TRANSITION__REDEFINED_TRANSITION, Transition.class)
				.filter(t -> t.getContainer() == context)
				.findFirst().orElseGet(() -> shallowCopy(transition));
	}

	/**
	 * This is used to resolve the matching transition when mapping the notation of a diagram.
	 * Does not create new transitions: the correspnoding transition element is expected to exist, already
	 * (notation mappings naturally fire after semantic mappings).
	 */
	public static Transition getRedefiningTransition(Transition transition, StateMachine context) {
		return getInverseReferencers(transition, UMLPackage.Literals.TRANSITION__REDEFINED_TRANSITION, Transition.class)
				.filter(t -> t.containingStateMachine() == context)
				.findFirst().orElse(null);
	}

	/**
	 * Finds the end of a given {@code context} that redefines the specified {@code vertex}.
	 */
	public static <V extends Vertex> V getRedefiningEnd(V vertex, Transition context) {
		V result = null;

		// Vertex is null when computing default result of an active operation
		if (vertex != null) {
			@SuppressWarnings("unchecked")
			final Class<V> type = (Class<V>) vertex.getClass();

			result = select(asStream(context.containingStateMachine().eAllContents()), type)
					.filter(v -> redefines(v, vertex))
					.findFirst()
					.orElse(null);
		}

		return result;
	}

	/**
	 * Queries whether two elements are in a redefinition relationship.
	 * 
	 * @return whether {@code element} redefines {@code other}, or vice versa
	 */
	public static boolean redefines(EObject element, EObject other) {
		boolean result = false;

		if ((element instanceof RedefinableElement) && (other instanceof RedefinableElement)) {
			result = ((RedefinableElement) element).getRedefinedElements().contains(other);
		} else if ((element instanceof Vertex) && (other instanceof Vertex)) {
			Vertex v1 = (Vertex) element;
			Vertex v2 = (Vertex) other;

			// Pseudostates correspond by name
			result = redefines(v1.getContainer(), v2.getContainer())
					&& Objects.equals(v1.getName(), v2.getName());
		}

		return result;
	}
}

Back to the top