Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4bf44d70e6bddfba35992b9f9e187ee3eb946579 (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
/*****************************************************************************
 * Copyright (c) 2020, 2021 Christian W. Damus, CEA LIST, and others.
 *
 * All rights reserved. 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
 * http://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Christian W. Damus - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.toolsmiths.validation.elementtypes.internal.checkers;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.Switch;
import org.eclipse.papyrus.infra.emf.utils.ResourceUtils;
import org.eclipse.papyrus.infra.types.util.ElementTypesConfigurationsSwitch;
import org.eclipse.papyrus.uml.types.core.advices.applystereotype.StereotypeToApply;
import org.eclipse.papyrus.uml.types.core.advices.applystereotype.util.ApplyStereotypeAdviceSwitch;
import org.eclipse.papyrus.uml.types.core.advices.stereotypepropertyreferenceedgeadvice.StereotypePropertyReferenceEdgeAdviceConfiguration;
import org.eclipse.papyrus.uml.types.core.advices.stereotypepropertyreferenceedgeadvice.util.StereotypePropertyReferenceEdgeAdviceSwitch;
import org.eclipse.papyrus.uml.types.core.matchers.stereotype.StereotypeApplicationMatcherConfiguration;
import org.eclipse.papyrus.uml.types.core.matchers.stereotype.util.StereotypeApplicationMatcherSwitch;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;

/**
 * Computation of referenced UML profiles that need to be packaged in the <tt>build.properties</tt>
 * along with the <em>Element Types Configurations</em> model being validated.
 */
class ElementTypesBuildPropertiesDependencies {

	private final Resource resource;

	/**
	 * Initialize me with the resource to scan for element-types references to profiles (direct and indirect via stereotype names).
	 *
	 * @param resource
	 *            the element types model resource to scan
	 */
	ElementTypesBuildPropertiesDependencies(Resource resource) {
		super();

		this.resource = resource;
	}

	/**
	 * Scan my resource for referenced profiles and return the workspace resources containing those profiles,
	 * where they can be determined to be in the workspace.
	 *
	 * @return the workspace resources containing profiles that are referenced by the element types model
	 */
	Collection<IResource> getDependencies() {
		Set<IResource> result = new HashSet<>();
		LocalProfileIndex index = LocalProfileIndex.getInstance(resource.getResourceSet());

		Set<Profile> profiles = collectProfiles(index, resource.getContents());
		for (Profile next : profiles) {
			// If we found the profile, we must have loaded it from a resource that has an URI
			Resource resource = next.eResource();
			IFile profileFile = ResourceUtils.getFile(resource);
			if (profileFile != null) {
				result.add(profileFile);
			}
		}

		return result;
	}

	private Set<Profile> collectProfiles(LocalProfileIndex index, List<EObject> objects) {
		Set<Profile> result = new HashSet<>();

		Switch<Set<Profile>> applyStereotypeSwitch = new ApplyStereotypeAdviceSwitch<>() {
			@Override
			public Set<Profile> caseStereotypeToApply(StereotypeToApply object) {
				if (object.getStereotypeQualifiedName() != null) {
					Stereotype stereotype = index.getStereotype(object.getStereotypeQualifiedName(), object.getRequiredProfiles(), object);
					if (stereotype != null) {
						result.add(stereotype.containingProfile());
					}
				}

				return result;
			}
		};

		Switch<Set<Profile>> stereotypeMatcherSwitch = new StereotypeApplicationMatcherSwitch<>() {
			public Set<Profile> caseStereotypeApplicationMatcherConfiguration(StereotypeApplicationMatcherConfiguration object) {
				if (object.getProfileUri() != null) {
					Profile profile = index.getProfileByURI(object.getProfileUri(), object);
					if (profile != null) {
						result.add(profile);
					}
				} else {
					object.getStereotypesQualifiedNames().stream()
							.map(name -> index.getStereotype(name, object))
							.filter(Objects::nonNull)
							.map(Stereotype::containingProfile)
							.forEach(result::add);
				}

				return result;
			};
		};

		Switch<Set<Profile>> stereotypeReferenceSwitch = new StereotypePropertyReferenceEdgeAdviceSwitch<>() {
			public Set<Profile> caseStereotypePropertyReferenceEdgeAdviceConfiguration(StereotypePropertyReferenceEdgeAdviceConfiguration object) {
				if (object.getStereotypeQualifiedName() != null) {
					Stereotype stereotype = index.getStereotype(object.getStereotypeQualifiedName(), object);
					if (stereotype != null) {
						result.add(stereotype.containingProfile());
					}
				}

				return result;
			}
		};

		Switch<Set<Profile>> master = new ElementTypesConfigurationsSwitch<>() {

			public Set<Profile> defaultCase(EObject object) {
				object.eContents().forEach(this::doSwitch);
				return result;
			}

			public Set<Profile> doSwitch(EObject eObject) {
				Set<Profile> profiles = applyStereotypeSwitch.doSwitch(eObject);
				if (profiles == null) {
					profiles = stereotypeMatcherSwitch.doSwitch(eObject);
				}
				if (profiles == null) {
					profiles = stereotypeReferenceSwitch.doSwitch(eObject);
				}
				if (profiles == null) {
					profiles = defaultCase(eObject);
				}
				return profiles;
			}

		};

		objects.forEach(master::doSwitch);

		return result;
	}

}

Back to the top