Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a4745fa49a944a432752852c39f60292ebad5a58 (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
176
177
/*****************************************************************************
 * Copyright (c) 2017 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.migration.rhapsody.xmi;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.m2m.internal.qvt.oml.library.Context;
import org.eclipse.papyrus.m2m.qvto.TraceHelper;
import org.eclipse.papyrus.migration.rhapsody.importer.utils.RpyUtil;
import org.eclipse.uml2.uml.ConnectorEnd;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 * @author VL222926
 * 
 * This class allows to reuse Rhapsody id when possible end and calls helper to generate "standart" XMI_ID when we can't reuse Rhapsody ones.
 *
 */
public class PreserveRhapsodySemanticIDHelper {

	/**
	 * The context of the QVTo transformation
	 */
	@SuppressWarnings("restriction")
	private Context context;

	/**
	 * the name of the feature representing the Rhapsody ID
	 */
	private static final String ID_FEATURE_NAME = "id"; //$NON-NLS-1$
	/**
	 * The helper to use to manipulate QVTo trace
	 */
	private TraceHelper helper = new TraceHelper();

	/**
	 * 
	 * Constructor.
	 *
	 * @param context
	 *            the context of the QVTo transformation
	 */
	public PreserveRhapsodySemanticIDHelper(@SuppressWarnings("restriction") final Context context) {
		this.context = context;
	}

	public void keepIdForUMLResource(final XMIResource res) {

		// a set owning the known id to avoid duplicate
		final Set<String> knownIds = new HashSet<String>();

		// the object to manage in the second run
		final List<EObject> eobjectForSecondRun = new ArrayList<>();

		// we mainly set the stereotype application in this list
		final List<EObject> eobjectForThirdRun = new ArrayList<>();

		// we iterate on all elements of the UML model
		final TreeIterator<EObject> iter = res.getAllContents();
		while (iter.hasNext()) {
			final EObject current = iter.next();
			if (isStereotypeApplication(current)) {
				eobjectForThirdRun.add(current);
				continue;
			}
			if (requiredToBeManagedInASecondRun(current)) {
				eobjectForSecondRun.add(current);
				continue;
			}


			// looking for the Rhapsody object in the QVto trace
			final Object result = helper.traceFrom(context, current, null);
			String rpyId = getRhapsodyId(result);

			if (isValidId(rpyId) && false == knownIds.contains(rpyId)) {
				knownIds.add(rpyId);
				res.setID(current, rpyId);
			} else {
				eobjectForSecondRun.add(current);
			}
		}

		// second run (duplicated and null id in the first run)
		for (final EObject current : eobjectForSecondRun) {
			final String value = XMI_ID_Helper.calculateXMI_ID(current);
			if (isValidId(value) && false == knownIds.contains(value)) {
				knownIds.add(value);
				res.setID(current, value);
			} else {
				System.out.println("It is not possible to found an id for " + current.toString()); //$NON-NLS-1$
			}
		}

		// third run (stereotype)
		for (final EObject current : eobjectForThirdRun) {
			final String value = XMI_ID_Helper.calculateXMI_ID(current);
			if (isValidId(value) && false == knownIds.contains(value)) {
				knownIds.add(value);
				res.setID(current, value);
			} else {
				System.out.println("It is not possible to found an id for " + current.toString()); //$NON-NLS-1$
			}
		}
	}

	/**
	 * 
	 * @param eobject
	 *            an eobject
	 * @return
	 * 		<code>true</code> if the object must be manage during the second run
	 */
	protected boolean requiredToBeManagedInASecondRun(final EObject eobject) {
		return eobject instanceof ConnectorEnd;
	}

	/**
	 * 
	 * @param object
	 *            a rhapsody object
	 * @return
	 * 		the id of this object or <code>null</code> if not found
	 */
	protected String getRhapsodyId(final Object object) {
		if (object instanceof EObject) {
			final EObject eobject = (EObject) object;
			final EStructuralFeature idFeature = eobject.eClass().getEStructuralFeature(ID_FEATURE_NAME);
			if (null != idFeature) {
				final String value = (String) eobject.eGet(idFeature);
				return value;
			}
		}
		return null;
	}

	/**
	 * 
	 * @param eobject
	 *            an eobject
	 * @return
	 * 		<code>true</code> when the element represents a stereotype application
	 */
	protected boolean isStereotypeApplication(final EObject eobject) {
		return null != UMLUtil.getBaseElement(eobject);
	}

	/**
	 * 
	 * @param id
	 *            an id
	 * @return
	 * 		<code>true</code> when the string represents a valid id : so it must contains GUID or OLDID
	 */
	public static final boolean isValidId(final String id) {
		return null != id && (id.contains(RpyUtil.GUID_STRING) || id.contains(RpyUtil.OLDID_STRING));
	}

}

Back to the top