Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3c02069aaad29a9fbe0efe4e57f64591b238757a (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * 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.uml.search.ui.query;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.resource.NotFoundException;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.services.viewersearch.impl.ViewerSearchService;
import org.eclipse.papyrus.uml.search.ui.Activator;
import org.eclipse.papyrus.uml.search.ui.Messages;
import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeAttribute;
import org.eclipse.papyrus.uml.search.ui.providers.ParticipantTypeElement;
import org.eclipse.papyrus.uml.search.ui.results.PapyrusSearchResult;
import org.eclipse.papyrus.uml.search.ui.validator.ParticipantValidator;
import org.eclipse.papyrus.uml.tools.model.UmlModel;
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil;
import org.eclipse.papyrus.views.search.regex.PatternHelper;
import org.eclipse.papyrus.views.search.results.AbstractResultEntry;
import org.eclipse.papyrus.views.search.results.AttributeMatch;
import org.eclipse.papyrus.views.search.results.ModelElementMatch;
import org.eclipse.papyrus.views.search.results.ModelMatch;
import org.eclipse.papyrus.views.search.results.ViewerMatch;
import org.eclipse.papyrus.views.search.scope.ScopeEntry;
import org.eclipse.search.ui.ISearchResult;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.util.UMLUtil;

/**
 *
 * Papyrus specific search query
 *
 */
public class PapyrusAdvancedQuery extends AbstractPapyrusQuery {

	@Override
	public boolean isCaseSensitive() {
		return isCaseSensitive;
	}

	@Override
	public boolean isRegularExpression() {
		return isRegularExpression;
	}

	private String searchQueryText;

	private boolean isCaseSensitive;

	private boolean isRegularExpression;

	private Collection<ScopeEntry> scopeEntries;

	private Object[] participantsChecked;

	private PapyrusSearchResult results;

	private HashMap<EObject, List<EAttribute>> participantsList;

	protected Set<AbstractResultEntry> fResults = null;

	private HashMap<Stereotype, ArrayList<Property>> stereotypeList;

	private List<EAttribute> attributesList;

	private List<Property> propertyList;

	private boolean searchForAllSter;
	
	private boolean searchForAnySter;

	public PapyrusAdvancedQuery(String searchQueryText, boolean isCaseSensitive, boolean isRegularExpression, Collection<ScopeEntry> scopeEntries, Object[] participantsChecked, boolean searchForAllSter, boolean searchForAnySter) {
		this.propertyList = new ArrayList<Property>();
		this.searchQueryText = searchQueryText;
		this.isCaseSensitive = isCaseSensitive;
		this.isRegularExpression = isRegularExpression;
		this.scopeEntries = scopeEntries;
		this.participantsChecked = participantsChecked;
		this.searchForAllSter = searchForAllSter;
		this.searchForAnySter = searchForAnySter;
		results = new PapyrusSearchResult(this);

		participantsList = new HashMap<EObject, List<EAttribute>>();
		stereotypeList = new HashMap<Stereotype, ArrayList<Property>>();
		for (Object participant : this.participantsChecked) {
			if (participant instanceof ParticipantTypeElement) {
				if (((ParticipantTypeElement) participant).getElement() instanceof ENamedElement) {
					List<EAttribute> attributesChecked = new ArrayList<EAttribute>();
					for (Object attributesFound : this.participantsChecked) {
						if (attributesFound instanceof ParticipantTypeAttribute) {
							if (((ParticipantTypeAttribute) attributesFound).getParent() == participant) {
								attributesChecked.add((EAttribute) ((ParticipantTypeAttribute) attributesFound).getElement());
							}
						}
					}
					participantsList.put(((ParticipantTypeElement) participant).getElement(), attributesChecked);

				} else if (((ParticipantTypeElement) participant).getElement() instanceof Stereotype) {

					ArrayList<Property> attributesChecked = new ArrayList<Property>();
					for (Object attributesFound : this.participantsChecked) {
						if (attributesFound instanceof ParticipantTypeAttribute) {
							if (((ParticipantTypeAttribute) attributesFound).getParent() == participant) {
								attributesChecked.add((Property) ((ParticipantTypeAttribute) attributesFound).getElement());
							}
						}
					}
					stereotypeList.put((Stereotype) ((ParticipantTypeElement) participant).getElement(), attributesChecked);
				}
			}
		}

		fResults = new HashSet<AbstractResultEntry>();
	}

	public IStatus run(IProgressMonitor monitor) throws OperationCanceledException {
		progressMonitor = SubMonitor.convert(monitor, scopeEntries.size() * 4);
		progressMonitor.subTask("Searching");
		
		results.removeAll();
		fResults.clear();

		for (ScopeEntry scopeEntry : scopeEntries) {
			try {
				if (scopeEntry.getModelSet() != null) {
					UmlModel umlModel = (UmlModel) scopeEntry.getModelSet().getModelChecked(UmlModel.MODEL_ID);

					EObject root = umlModel.lookupRoot();
					EList<EObject> contents = umlModel.getResource().getContents();

					Collection<EObject> participants = ParticipantValidator.getInstance().getParticipants(root, participantsList.keySet().toArray());
					
					progressMonitor.worked(1);
					
					Collection<EObject> stereotypedParticipants = ParticipantValidator.getInstance().getParticipantsStereotype(contents, stereotypeList.keySet().toArray());

					progressMonitor.worked(1);
					
					if (searchForAllSter) {
						if (participantsList.keySet().size() == 0) {
							stereotypedParticipants = getElementsWithAllSter(stereotypedParticipants);
							evaluate(stereotypedParticipants, scopeEntry);
							if (!searchQueryText.equals("")) {
								evaluateStereotypes(stereotypedParticipants, scopeEntry);
							}
						} else {
							participants = getElementsWithAllSter(participants);
							evaluate(participants, scopeEntry);
							if (!searchQueryText.equals("")) {
								evaluateStereotypes(participants, scopeEntry);
							}
						}
					} else if (searchForAnySter) {
						if (participantsList.keySet().size() == 0) {
							stereotypedParticipants = getElementsWithAnySter(stereotypedParticipants);
							evaluate(stereotypedParticipants, scopeEntry);
							if (!searchQueryText.equals("")) {
								evaluateStereotypes(stereotypedParticipants, scopeEntry);
							}
						} else {
							participants = getElementsWithAnySter(participants);
							evaluate(participants, scopeEntry);
							if (!searchQueryText.equals("")) {
								evaluateStereotypes(participants, scopeEntry);
							}
						}
					} else {
						evaluate(participants, scopeEntry);
						evaluateStereotypes(stereotypedParticipants, scopeEntry);
					}
					
					progressMonitor.worked(1);
				}
			} catch (NotFoundException e) {
				Activator.log.error(Messages.PapyrusQuery_0 + scopeEntry.getModelSet(), e);
			}
		}
		
		//progressMonitor.done();

		return Status.OK_STATUS;
	}

	private Collection<EObject> getElementsWithAllSter(Collection<EObject> initialParticipants) {
		Collection<EObject> participantsToKeep = new ArrayList<EObject>();
		for (EObject participants : initialParticipants) {
			if (participants instanceof Element) {
				int numberOfStereotypeToBeApplied = stereotypeList.size();
				int numberOfStereotypeMatching = 0;
				if (numberOfStereotypeToBeApplied == ((Element) participants).getAppliedStereotypes().size()) {
					for (Stereotype stereotypeToBeApplied : stereotypeList.keySet()) {
						for (Stereotype stereotypeApplied : ((Element) participants).getAppliedStereotypes()) {
							if (EcoreUtil.getURI(stereotypeToBeApplied).equals(EcoreUtil.getURI(stereotypeApplied))) {
								numberOfStereotypeMatching++;
							}
						}
					}
					
					if (numberOfStereotypeMatching == numberOfStereotypeToBeApplied) {
						participantsToKeep.add(participants);
					}
				}
			}
		}
		return participantsToKeep;
	}
	
	private Collection<EObject> getElementsWithAnySter(Collection<EObject> initialParticipants) {
		Collection<EObject> participantsToKeep = new ArrayList<EObject>();
		for (EObject participants : initialParticipants) {
			if (participants instanceof Element) {
				boolean added = false;
				
				for (Stereotype stereotypeToBeApplied : stereotypeList.keySet()) {	
					for (Stereotype stereotypeApplied : ((Element) participants).getAppliedStereotypes()) {
						if (EcoreUtil.getURI(stereotypeToBeApplied).equals(EcoreUtil.getURI(stereotypeApplied))) {
							participantsToKeep.add(participants);
							added = true;
							break;
						}
					}

					if (added) {
						break;
					}
				}
			}
		}
		
		return participantsToKeep;
	}

	/**
	 * Evaluate if the value matches the pattern
	 *
	 * @param value
	 *            the value to evaluate
	 * @param attribute
	 *            the attribute has the value
	 * @param pattern
	 *            the pattern that is searched
	 * @param participant
	 *            the element that contains the value
	 * @param scopeEntry
	 *            the scopeEntry that contains the participant
	 */
	protected void evaluateAndAddToResult(String value, Object attribute, Pattern pattern, Object participant, ScopeEntry scopeEntry, Stereotype stereotype) {

		value = value != null ? value : ""; //$NON-NLS-1$

		Matcher m = pattern.matcher(value);

		if (isRegularExpression) {
			if (m.matches()) {
				int offset = m.start();
				int length = m.end() - m.start();
				
				ModelMatch match = new AttributeMatch(offset, length, participant, scopeEntry, attribute, stereotype);

				fResults.add(match);
			}
		} else {
			while (m.find()) {
				int offset = m.start();
				int length = m.end() - m.start();
				
				AttributeMatch match = new AttributeMatch(offset, length, participant, scopeEntry, attribute, stereotype);
				
				fResults.add(match);
			}
		}

		// if(PatternHelper.getInstance().evaluate(m, isRegularExpression)) {
		// int start = m.start();
		// int end = m.end();
		// ModelMatch match = new AttributeMatch(start, end, participant, scopeEntry, attribute);
		//
		// fResults.add(match);
		// }
	}

	/**
	 * Try to find elements that match in the participants
	 *
	 * @param participants
	 * @param scopeEntry
	 */


	protected void evaluate(Collection<EObject> participants, ScopeEntry scopeEntry) {
		for (EObject participant : participants) {

			if (searchQueryText.equals("")) { //$NON-NLS-1$
				fResults.add(new ModelElementMatch(participant, scopeEntry));
			} else {
				String query = searchQueryText;
				if (searchQueryText.equals("")) { //$NON-NLS-1$
					query = ".*"; //$NON-NLS-1$
				}

				Pattern pattern = PatternHelper.getInstance().createPattern(query, isCaseSensitive, isRegularExpression);

				if (pattern != null) {
					if (participantsList.get(participant.eClass()).size() == 0) {
						attributesList = participant.eClass().getEAllAttributes();
					} else {
						attributesList = participantsList.get(participant.eClass());
					}
					
					for (EAttribute attribute : attributesList) {

						Object value = participant.eGet(attribute);

						if (value instanceof String) {
							String stringValue = (String) value;
							evaluateAndAddToResult(stringValue, attribute, pattern, participant, scopeEntry, null);
						} else {
							String stringValue = String.valueOf(value);
							evaluateAndAddToResult(stringValue, attribute, pattern, participant, scopeEntry, null);
						}
					}
				}


			}
		}
		
		findInDiagram(scopeEntry);
	}


	protected void evaluateStereotypes(Collection<EObject> participants, ScopeEntry scopeEntry) {

		for (EObject participant : participants) {

			if (searchQueryText.equals("")) { //$NON-NLS-1$
				fResults.add(new ModelElementMatch(participant, scopeEntry));
			} else {
				String query = searchQueryText;
				if (searchQueryText.equals("")) { //$NON-NLS-1$
					query = ".*"; //$NON-NLS-1$
				}

				Pattern pattern = PatternHelper.getInstance().createPattern(query, isCaseSensitive, isRegularExpression);

				if (pattern != null) {
					EList<Stereotype> stereotypesApplied = ((Element) participant).getAppliedStereotypes();
					for (Stereotype stereotype : stereotypesApplied) {
						for (Stereotype stereotypeSelected : stereotypeList.keySet()) {
							if (EcoreUtil.getURI(stereotype).equals(EcoreUtil.getURI(stereotypeSelected))) {
								propertyList = this.getStereotypesAttributes(stereotype);
								
								for (Property property : propertyList) {
									if (stereotypeList.get(stereotypeSelected).size() == 0) {
										String value = "[" + StereotypeUtil.displayPropertyValue(stereotype, property, (Element) participant, ";") + "]";
										evaluateAndAddToResult(value, property, pattern, participant, scopeEntry, stereotype);
									} else {
										for (Property property2 : (stereotypeList.get(stereotypeSelected))) {
											if (EcoreUtil.getURI(property).equals(EcoreUtil.getURI(property2))) { // We loop through all selected attributes of all stereotypes, therefore this test is necessary to compare to currently searched stereotype's attributes
												String value = StereotypeUtil.displayPropertyValueOnly(stereotype, property, (Element) participant, "");
												evaluateAndAddToResult(value, property, pattern, participant, scopeEntry, stereotype);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}

		findInDiagram(scopeEntry);

	}

	private String getStringValue(Object value) {
		if (value == null) {
			return "";
		}
		
		if (value instanceof String) { // Primitive types will hit this case
			return (String) value;
		} else if (value instanceof EnumerationLiteral) {
			return ((EnumerationLiteral) value).getName();
		} else if (value instanceof NamedElement) {
			return ((NamedElement) value).getName();
		} else if (value instanceof EObject) { // Ref to an element in a model
			Element baseElement = UMLUtil.getBaseElement((EObject) value);
			return getStringValue(baseElement);
		} else {
			return String.valueOf(value);
		}
	}
	
	private EList<String> getStringValuesOfProperty(Element element, Stereotype stereotype, Property property) {
		BasicEList<String> results = new BasicEList<String>();
		
		Object values = element.getValue(stereotype, property.getName());
		if (values instanceof EList) {
			for (Object val : (EList) values) {
				results.add(getStringValue(val));
			}
		} else {
			results.add(getStringValue(values));
		}
		
		return results;
	}

	public List<Property> getStereotypesAttributes(Object parentElement) {
		List<Property> result = new ArrayList<Property>();
		if (parentElement instanceof Stereotype) {

			for (Property property : ((Stereotype) parentElement).getAllAttributes()) {

				if (!property.getName().startsWith("base_")) { //$NON-NLS-1$
					if (property.getType() instanceof Element) {
						result.add(property);
					}
				}
			}

		}

		return result;
	}

	protected void findInDiagram(ScopeEntry scopeEntry) {
		// Find diagrams that contain the elements that were found
		Set<AbstractResultEntry> viewResults = new HashSet<AbstractResultEntry>();
		for (AbstractResultEntry match : fResults) {
			Object source = match.getSource();

			if (source instanceof Element) {
				List<View> views = getViews((Element) source);

				if (views != null && !views.isEmpty()) {
					for (View view : views) {
						ViewerMatch viewMatch = new ViewerMatch(view, scopeEntry, source);
						viewResults.add(viewMatch);
					}
				}
			}
		}
		
		fResults.addAll(viewResults);
	}

	public String getLabel() {
		return Messages.PapyrusQuery_6;
	}

	public boolean canRerun() {
		return true;
	}

	public boolean canRunInBackground() {
		return true;
	}

	public ISearchResult getSearchResult() {
		if (progressMonitor != null) {
			progressMonitor.setWorkRemaining(fResults.size());
			progressMonitor.subTask("Displaying Results");
		}
		
		
		for (AbstractResultEntry match : fResults) {
			results.addMatch(match);

			if (progressMonitor != null) {
				progressMonitor.worked(1);
			}
		}
		
		if (progressMonitor != null) {
			progressMonitor.done();
		}
		
		return results;
	}

	/**
	 * Getter for the text query
	 *
	 * @return the the query text
	 */
	@Override
	public String getSearchQueryText() {
		return searchQueryText;
	}

}

Back to the top