Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8274b285d332fe7a5f1253ea7250efa0c7589d5a (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
/*****************************************************************************
 * Copyright (c) 2013 Cedric Dumoulin.
 *
 *    
 * 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.layers.stackmodel.exprmatcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.query.conditions.eobjects.EObjectCondition;
import org.eclipse.emf.query.ocl.conditions.BooleanOCLCondition;
import org.eclipse.emf.query.statements.FROM;
import org.eclipse.emf.query.statements.IQueryResult;
import org.eclipse.emf.query.statements.SELECT;
import org.eclipse.emf.query.statements.WHERE;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.ocl.ParserException;
import org.eclipse.ocl.ecore.OCL;
import org.eclipse.papyrus.layers.stackmodel.LayersException;
import org.eclipse.papyrus.layers.stackmodel.util.Collections3;
import org.eclipse.papyrus.layers.stackmodel.util.ObservableListView;

/**
 * This class evaluate its associated expression against the associated models.
 * It provide a list of elements matching the expression in the model.
 * The list of matching elements is synchronized by the matcher. The list can be provided  at construction 
 * time. The ExpressinMatcher takes care to minimize the number of write to the underlying list of matching elements.
 * Usually, there is two writes (see {@link Collections3#resetListTo(Collection, Collection)}.
 * <br>
 * It is possible to be inform of changes in the underlying list by wrapping it in an {@link ObservableListView}.
 * 
 * 
 * @author cedric dumoulin
 *
 */
public class ExpressionMatcher implements IValueChangedEventListener {

	protected String expression="";
	
	/**
	 * List of element matching the expression.
	 * This class maintains the list.
	 */
	protected List<View> matchingElements;
	
	/**
	 * List of element used as starting point for search.
	 */
	protected List<EObject> searchRoots;
	
	/**
	 * OCL Condition computed from the expr.
	 */
	protected EObjectCondition  condition;
	protected OCL ocl;
	

	/**
	 * 
	 * Constructor.
	 *
	 */
	public ExpressionMatcher() {
		this.expression = "";
		this.searchRoots = Collections.emptyList();
		// init matchingElements
		matchingElements = new ArrayList<View>();
	}
	
	/**
	 * 
	 * Constructor.
	 *
	 * @param searchRoots
	 * @throws LayersException
	 */
	public ExpressionMatcher(List<View> matchingElementsList) {
		this.expression = "";
		this.searchRoots = Collections.emptyList();
		// init matchingElements
		matchingElements = matchingElementsList;
	}
	
	/**
	 * 
	 * Constructor.
	 *
	 * @param searchRoots
	 * @throws LayersException
	 */
//	public ExpressionMatcher(List<EObject> searchRoots) {
//		this.expression = "";
//		setSearchRoots(searchRoots);
//		// init matchingElements
//		matchingElements = new ObservableListView<View>(new ArrayList<View>());
//	}
	
	/**
	 * 
	 * Constructor.
	 *
	 * @param searchRoot
	 * @throws LayersException
	 */
	public ExpressionMatcher(EObject searchRoot) {
		this.expression = "";
		setSearchRoots(Collections.singletonList(searchRoot));
		// init matchingElements
		matchingElements = new ArrayList<View>();
	}
	
	/**
	 * Constructor.
	 *
	 * @param expression
	 * @param searchRoots
	 * @throws LayersException If the Condition can't be computed from the expression.
	 */
	public ExpressionMatcher(String expression, List<EObject> searchRoots) throws LayersException {
		this.searchRoots = searchRoots;
		matchingElements = new ArrayList<View>();
		
		// compute expr
		setExpression(expression);
	}

	/**
	 * Constructor.
	 *
	 * @param expression
	 * @param searchRoots
	 * @throws LayersException If the Condition can't be computed from the expression.
	 */
	public ExpressionMatcher(String expression, List<View> matchingElementsList, List<EObject> searchRoots) throws LayersException {
		this.searchRoots = searchRoots;
		matchingElements = matchingElementsList;
		
		// compute expr
		setExpression(expression);
	}

	/**
	 * Constructor.
	 *
	 * @param expression
	 * @param searchRoots
	 * @throws LayersException If the Condition can't be computed from the expression.
	 */
	public ExpressionMatcher(String expression, EObject searchRoot) throws LayersException {
		this(expression, Collections.singletonList(searchRoot));
	}

	/**
	 * Constructor.
	 *
	 * @param expression
	 * @param searchRoots
	 * @throws LayersException If the Condition can't be computed from the expression.
	 */
	public ExpressionMatcher(String expression, List<View> matchingElementsList, EObject searchRoot) throws LayersException {
		this(expression, matchingElementsList, Collections.singletonList(searchRoot));
	}

	/**
	 * Compute the condition from the expr.
	 */
	private void computeCondition() throws LayersException {
		// silently fails if the expr is not set.
		if( getExpression() == null || getExpression().length() == 0) {
			return;
		}
		
		if( ocl == null) {
		  ocl = OCL.newInstance();
		}
		// Create the condition
		try {
			// If the 3rd args is null, this is a context free condition.
			
			condition = new BooleanOCLCondition<EClassifier, EClass, EObject>(
				    ocl.getEnvironment(),
//			    "self.oclIsKindOf(Shape)",
//			    "self.oclIsKindOf(Shape) and self.oclAsType(Shape).visible = true",
//				    "self.oclAsType(Shape).visible = true",
				    getExpression(),
				    NotationPackage.Literals.VIEW
//				    null
				    );
		} catch (ParserException e) {
			// TODO Auto-generated catch block
			condition = null;
			throw new LayersException("Can't parse expression : " + e.getMessage(), e);
		}
		
	}

	/**
	 * Recompute the matching elements.
	 * This lead to firing Events (added and removed)
	 */
	public void refreshMatchingElements() {
		

		if( condition == null) {
			// If the condition is not set, the list should be empty
			if( !getMatchingElements().isEmpty()) {
				resetMatchingElements( Collections.EMPTY_LIST);
			}
			return;
		}
		
		// Create the OCL statement
		SELECT statement = new SELECT(SELECT.UNBOUNDED, false,
			new FROM(getSearchRoots()), new WHERE(condition),
			new NullProgressMonitor());

		// Execute the OCL statement
		IQueryResult results = statement.execute();

		/**
		 * Reset the matching elements with the new result.
		 */
		resetMatchingElements( results);
	}
	
	/**
	 * Reset the {@link #matchingElements} and let it contain the specified collection.
	 * This fire added and removed events.
	 * 
	 * @param results
	 */
	@SuppressWarnings("unchecked")
	private void resetMatchingElements(Collection<?> newElements) {
		
		Collections3.resetListTo(matchingElements, (Collection<View>)newElements);
//		matchingElements.resetTo((Collection<View>)newElements);
		
//		// Compute views to add
//		// This are views in the newElements, but not in the actual list of matchingElement
//		// viewsToAdd = results - getViews()
//		List<View> viewsToAdd = new ArrayList<View>();
//		for( Object o : newElements ) {
//			View v = (View)o;
//			if( !getMatchingElements().contains(v)) {
//				viewsToAdd.add(v);
//			}
//		}
//		
//		// Compute views to remove
//		// Their is two ways to compute it:
//		//    - viewsToremove = diagramViews - results
//		//    - or viewsToremove = getViews() - result  
//		// Use the cheaper one.
//		// The computed viewsToRemove list contains also views that are not in the layer,
//		// But this is cheaper than checking for the existence.
//		
////		List<View> viewsToRemove = new ArrayList<View>();
////		for( View v : (views.size()<getViews().size()?views:getViews()) ) {
////			if( !results.contains(v)) {
////				viewsToRemove.add(v);
////			}
////		}
//		
//		// Do operations
//		getMatchingElements().retainAll(newElements);
////		getViews().removeAll(viewsToRemove);
//		getMatchingElements().addAll(viewsToAdd);
		
	}

	/**
	 * @return the expression
	 */
	public String getExpression() {
		return expression;
	}


	/**
	 * @param expression the expression to set
	 * @throws LayersException If the Condition can't be computed from the expression.
	 */
	public void setExpression(String expression) throws LayersException {
		
		if( expression == null || expression.length() == 0 ) {
			// standardize noop expr
			expression = "";
		}
		if( expression.equals(this.expression)) {
			return;
		}
		
		this.expression = expression;
		
		computeCondition();
		refreshMatchingElements();
	}


	/**
	 * @return the matchingElements
	 */
	public List<View> getMatchingElements() {
		return matchingElements;
	}


	/**
	 * @return the searchRoots
	 */
	public List<EObject> getSearchRoots() {
		return searchRoots;
	}

	/**
	 * 
	 * @param searchRoots
	 */
	public void setSearchRoots(List<EObject> searchRoots) {
		
		// Remove any existing observers
			removeSearchRootsObservers();
		
		if( searchRoots == null) {
			searchRoots = Collections.emptyList();
		}
		this.searchRoots = searchRoots;
		// add observers on roots changes
			addSearchRootsObservers();
		
		// Do not refresh. Let user do it.
	}

	/**
	 * 
	 * @param searchRoots
	 */
	public void setSearchRoots(EObject searchRoot) {
		if( searchRoot == null) {
			// Remove any existing observers
			removeSearchRootsObservers();
			searchRoots = Collections.emptyList();
			return;
		}

		setSearchRoots( Collections.singletonList(searchRoot) );
	}

	/**
	 * Observes all searchRoots for changes. If a change occurs, refresh the matching elements.
	 * 
	 */
	protected void addSearchRootsObservers() {
		
		if( searchRoots == null) {
			return;
		}

		
		for( EObject root : searchRoots) {
		  ValueChangedEventNotifier notifier = ValueChangedEventNotifierFactory.instance.adapt(root);
		  notifier.addEventListener(this);
		}
	}
	
	/**
	 * Observes all searchRoots for changes. If a change occurs, refresh the matching elements.
	 * 
	 */
	protected void removeSearchRootsObservers() {
		
		if( searchRoots == null) {
			return;
		}
		
		for( EObject root : searchRoots) {
		  ValueChangedEventNotifier notifier = ValueChangedEventNotifierFactory.instance.adapt(root);
		  notifier.removeEventListener(this);
		}
	}

	/** 
	 * Called when a value change in one of the elements of the observed  roots.
	 * @param msg
	 */
	@Override
	public void valueChanged(Notification msg) {
		refreshMatchingElements();
	}
	
	
}

Back to the top