Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8e286a6792e75949344e64fc55eb88931d49fb1d (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
178
179
180
181
182
183
184
185
186
187
188
package org.eclipse.papyrus.qompass.modellibs.tracing;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.FCM.ConfigOption;
import org.eclipse.papyrus.FCM.ContainerRule;
import org.eclipse.papyrus.FCM.RuleApplication;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.infra.services.tracepoints.ITraceMechanism;
import org.eclipse.papyrus.infra.services.tracepoints.MarkerUtils;
import org.eclipse.papyrus.infra.services.tracepoints.TraceActions.TAOperation;
import org.eclipse.papyrus.infra.services.tracepoints.TracepointConstants;
import org.eclipse.papyrus.qompass.designer.core.Description;
import org.eclipse.papyrus.qompass.designer.core.Log;
import org.eclipse.papyrus.qompass.designer.core.Utils;
import org.eclipse.papyrus.uml.tools.utils.PackageUtil;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Property;

public class QompassTraceMechanism implements ITraceMechanism {

	public static final String EC3M_TRACING_URI = "pathmap://QML_TRACE/tracing.uml"; //$NON-NLS-1$

	public static final URI tracingURI = URI.createURI(EC3M_TRACING_URI);

	@Override
	public EList<String> getTraceMechanismIDs(EObject eObj) {
		EList<String> ids = new BasicEList<String>();
		// obtain list of all available container rules via Utils. Restrict to those doing tracing.
		EList<ContainerRule> containerRules = getContainerRules(eObj);
		for (ContainerRule containerRule : containerRules) {
			if (isForTracing(containerRule)) {
				Class clazz = containerRule.getBase_Class();
				ids.add(clazz.getName());
			}
		}
		if (ids.size() == 0) {
			ids.add("dummy qompass rule");
		}
		return ids;
	}

	@Override
	public String getTraceMechanismDescription(EObject eObj, String id) {
		EList<ContainerRule> containerRules = getContainerRules(eObj);
		for (ContainerRule containerRule : containerRules) {
			if (isForTracing(containerRule)) {
				Class clazz = containerRule.getBase_Class();
				if (clazz.getName().equals(id)) {
					return Description.getDescription(clazz);
				}
			}
		}
		return null;
	}

	/**
	 * Apply the trace mechanism, i.e. set or unset the appropriate ContainerRule for tracing.
	 * Currently handles trace on class.
	 *
	 * Basic idea: always apply same container rule. But container rule expansion is different in function of set tracepoints.
	 * Need to unapply, if there is no longer a trace needing it.
	 */
	@Override
	public boolean applyTraceMechanism(EObject eObj, String id, int traceOption) {
		Class clazzContext = getClassContext(eObj);
		if (clazzContext == null) {
			return false;
		}
		EList<ContainerRule> containerRules = getContainerRules(eObj);
		for (ContainerRule containerRule : containerRules) {
			if (isForTracing(containerRule)) {
				Class clazz = containerRule.getBase_Class();

				if (clazz.getName().equals(id)) {
					if (traceOption == TAOperation.OnlyCall.ordinal()) {
						// yes => what do we then (i.e. how is mapping done??)
					}
					RuleApplication ruleApplication = StereotypeUtil.applyApp(clazzContext, RuleApplication.class);
					if ((ruleApplication != null) && !ruleApplication.getContainerRule().contains(containerRule)) {
						ruleApplication.getContainerRule().add(containerRule);
						return true;
					}
				}
			}
		}
		return false;
	}

	// QompassTraceMechanism

	public boolean isForTracing(ContainerRule rule) {
		for (ConfigOption co : rule.getForConfig()) {
			// TODO: not very clean to used fixed string
			if (co.getBase_Class().getName().equals("Trace")) { //$NON-NLS-1$
				return true;
			}
		}
		return false;
	}

	/**
	 * Return the class (component) for which we need to apply a container rule
	 * to enable the tracing of the passed eObject. This eObject may be
	 * (1) A class, in this case it could be returned directly
	 * (2) An operation. In this case, the owning class is returned (caveat: operation might belong to an interface)
	 * (3) A property of the class (including ports).
	 *
	 * @param eObj
	 *            see description above
	 * @return the class to a container rule may be applied
	 */
	public Class getClassContext(EObject eObj) {
		if (eObj instanceof Class) {
			return (Class) eObj;
		} else if (eObj instanceof Operation) {
			return ((Operation) eObj).getClass_();
		} else if (eObj instanceof Property) {
			return ((Property) eObj).getClass_();
		} else {
			return null;
		}
	}

	public EList<ContainerRule> getContainerRules(EObject eObj) {
		if (eObj == null) {
			// load rules of registered Tracing model library
			try {
				ModelSet ms = ServiceUtilsForActionHandlers.getInstance().getModelSet();
				Resource rs = ms.getResource(tracingURI, true);
				EList<EObject> contents = rs.getContents();
				if ((contents.size() > 0) && (contents.get(0) instanceof Package)) {
					return Utils.getAllRules((Package) contents.get(0));
				}
			} catch (ServiceException e) {
				Log.log(IStatus.ERROR, Log.TRAFO_CONTAINER, e.getMessage());
			}
			return new BasicEList<ContainerRule>();
		} else {
			Package top = PackageUtil.getRootPackage((Element) eObj);
			return Utils.getAllRules(top);
		}
	}

	@Override
	public boolean configureTraceMechanisms() {
		String config = ""; //$NON-NLS-1$
		// TODO: config is never evaluated
		try {
			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

			if (root != null) {
				Object tracePoints[] = root.findMarkers(TracepointConstants.tpOrbpMarker, true, IResource.DEPTH_INFINITE);
				for (Object tracePointObj : tracePoints) {
					if (tracePointObj instanceof IMarker) {
						IMarker tracePoint = (IMarker) tracePointObj;
						EObject eobj = MarkerUtils.getEObjectOfMarker(tracePoint);
						if (MarkerUtils.isActive(tracePoint)) {
							if (eobj instanceof NamedElement) {
								config += ((NamedElement) eobj).getQualifiedName();
							}
						}
					}
				}
			}
		} catch (CoreException e) {
			Log.log(IStatus.ERROR, Log.TRAFO_CONTAINER, e.getMessage());
		}
		return true;
	}

}

Back to the top