]> gerrit.simantics Code Review - simantics/platform.git/blob
e86cdc54703559214895694478c386a6cf80e9f3
[simantics/platform.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.eclipse.jface.resource.ResourceManager;
22 import org.simantics.browsing.ui.BuiltinKeys.CheckedStateKey;
23 import org.simantics.browsing.ui.BuiltinKeys.ImageDecoratorKey;
24 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
25 import org.simantics.browsing.ui.BuiltinKeys.LabelDecoratorKey;
26 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
27 import org.simantics.browsing.ui.BuiltinKeys.ViewpointKey;
28 import org.simantics.browsing.ui.CheckedState;
29 import org.simantics.browsing.ui.NodeContext;
30 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
31 import org.simantics.browsing.ui.Tester;
32 import org.simantics.browsing.ui.common.EvaluatorData;
33 import org.simantics.browsing.ui.common.EvaluatorData.Evaluator;
34 import org.simantics.browsing.ui.common.EvaluatorData.EvaluatorTree;
35 import org.simantics.browsing.ui.common.EvaluatorDataImpl;
36 import org.simantics.browsing.ui.common.EvaluatorImpl;
37 import org.simantics.browsing.ui.common.exception.InvalidBrowserIdException;
38 import org.simantics.browsing.ui.common.extension.CheckedStateContributorBindingExtensionManager;
39 import org.simantics.browsing.ui.common.extension.ComparableContextContributorBindingExtensionManager;
40 import org.simantics.browsing.ui.common.extension.EvaluatorBindingExtensionManager;
41 import org.simantics.browsing.ui.common.extension.EvaluatorFactory;
42 import org.simantics.browsing.ui.common.extension.ImageDecoratorContributorBindingExtensionManager;
43 import org.simantics.browsing.ui.common.extension.ImagerContributorBindingExtensionManager;
44 import org.simantics.browsing.ui.common.extension.LabelDecoratorContributorBindingExtensionManager;
45 import org.simantics.browsing.ui.common.extension.LabelerContributorBindingExtensionManager;
46 import org.simantics.browsing.ui.common.extension.ViewpointContributionContributorBindingExtensionManager;
47 import org.simantics.browsing.ui.content.CheckedStateFactory;
48 import org.simantics.browsing.ui.content.ComparableContextFactory;
49 import org.simantics.browsing.ui.content.ContributorBinding;
50 import org.simantics.browsing.ui.content.ImageDecorator;
51 import org.simantics.browsing.ui.content.ImageDecoratorFactory;
52 import org.simantics.browsing.ui.content.Imager;
53 import org.simantics.browsing.ui.content.ImagerFactory;
54 import org.simantics.browsing.ui.content.LabelDecorator;
55 import org.simantics.browsing.ui.content.LabelDecoratorFactory;
56 import org.simantics.browsing.ui.content.Labeler;
57 import org.simantics.browsing.ui.content.LabelerFactory;
58 import org.simantics.browsing.ui.content.Viewpoint;
59 import org.simantics.browsing.ui.content.ViewpointContributionFactory;
60 import org.simantics.browsing.ui.content.ViewpointFactory;
61 import org.simantics.browsing.ui.model.InvalidContribution;
62 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
63 import org.simantics.db.ReadGraph;
64 import org.simantics.db.RequestProcessor;
65 import org.simantics.db.Resource;
66 import org.simantics.db.common.request.ReadRequest;
67 import org.simantics.db.exception.DatabaseException;
68 import org.simantics.db.exception.ResourceNotFoundException;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71
72 /**
73  * An entry point for loading an EvaluatorData in a data-oriented fashion. Also
74  * adds contributions made by extensions of the related extension points.
75  * 
76  * @author Antti Villberg
77  */
78 public class Evaluators {
79
80     private static final Logger LOGGER = LoggerFactory.getLogger(Evaluators.class);
81     public static final boolean DEBUG = false;
82
83     public static void create(EvaluatorData data, final ResourceManager resourceManager,
84             final Map<String, Collection<ContributorBinding<ViewpointContributionFactory>>> viewpointContributions,
85             final Set<ContributorBinding<ComparableContextFactory>> comparableContextContributions,
86             final Set<ContributorBinding<LabelerFactory>> labelerContributions,
87             final Set<ContributorBinding<CheckedStateFactory>> checkStateContributions,
88             final Set<ContributorBinding<LabelDecoratorFactory>> labelDecoratorContributions,
89             final Set<ContributorBinding<ImagerFactory>> imagerContributions,
90             final Set<ContributorBinding<ImageDecoratorFactory>> imageDecoratorContributions) {
91
92         Map<Class<?>, Evaluator> evaluators = new HashMap<Class<?>, Evaluator>();
93
94         HashMap<String, ContributionViewpointFactory> viewpointFactories = new HashMap<String, ContributionViewpointFactory>();
95
96         for(final Map.Entry<String, Collection<ContributorBinding<ViewpointContributionFactory>>> entry : viewpointContributions.entrySet()) {
97             Collection<ViewpointContributionFactory> factories = new ArrayList<ViewpointContributionFactory>();
98             for(ContributorBinding<ViewpointContributionFactory> binding : entry.getValue()) factories.add(binding.getContributor().getFactory());
99             viewpointFactories.put(entry.getKey(), new ContributionViewpointFactory(entry.getKey(), factories));
100         }
101
102         for(final Map.Entry<String, Collection<ContributorBinding<ViewpointContributionFactory>>> entry : viewpointContributions.entrySet()) {
103             ContributionViewpointFactory factory = viewpointFactories.get(entry.getKey());
104             for(ContributorBinding<ViewpointContributionFactory> binding : entry.getValue()) {
105                 Tester test = binding.getContributor().getNodeContextTester();
106                 Class<?> clazz = binding.getContributor().getInputClass();
107                 Evaluator evaluator = evaluators.get(clazz);
108                 if(evaluator == null) {
109                     evaluator = data.newEvaluator();
110                     evaluators.put(clazz, evaluator);
111                 }
112                 if(test == null) {
113                     if (DEBUG)
114                         System.out.println("add root viewpoint " + factory + " to class " + clazz.getSimpleName());
115                     evaluator.addViewpoint(factory, binding.getPreference());
116                 } else {
117                     if (DEBUG)
118                         System.out.println("add viewpoint " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
119                     EvaluatorTree<ViewpointFactory> branch = ((EvaluatorImpl)evaluator).getViewpointTree().addBranch(test);
120                     branch.addFactory(factory, binding.getPreference());
121                 }
122             }
123         }
124
125         for(final ContributorBinding<ComparableContextFactory> binding : comparableContextContributions) {
126
127             ComparableContextFactory factory = binding.getContributor().getFactory();
128             Tester test = binding.getContributor().getNodeContextTester();
129             Class<?> clazz = binding.getContributor().getInputClass();
130             Evaluator evaluator = evaluators.get(clazz);
131             if(evaluator == null) {
132                 evaluator = data.newEvaluator();
133                 evaluators.put(clazz, evaluator);
134             }
135             if(test == null) {
136                 if (DEBUG)
137                     System.out.println("add root comparable context " + factory + " to class " + clazz.getSimpleName());
138                 evaluator.addComparableContext(factory, binding.getPreference());
139             } else {
140                 if (DEBUG)
141                     System.out.println("add comparable context " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
142                 EvaluatorTree<ComparableContextFactory> branch = ((EvaluatorImpl)evaluator).getComparableContextTree().addBranch(test);
143                 branch.addFactory(factory, binding.getPreference());
144             }
145         }
146
147         for(final ContributorBinding<LabelerFactory> binding : labelerContributions) {
148
149             LabelerFactory factory = binding.getContributor().getFactory();
150             Tester test = binding.getContributor().getNodeContextTester();
151             Class<?> clazz = binding.getContributor().getInputClass();
152             Evaluator evaluator = evaluators.get(clazz);
153             if(evaluator == null) {
154                 evaluator = data.newEvaluator();
155                 evaluators.put(clazz, evaluator);
156             }
157             if(test == null) {
158                 if (DEBUG)
159                     System.out.println("add root labeler " + factory + " to class " + clazz.getSimpleName());
160                 evaluator.addLabeler(factory, binding.getPreference());
161             } else {
162                 if (DEBUG)
163                     System.out.println("add labeler " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
164                 EvaluatorTree<LabelerFactory> branch = ((EvaluatorImpl)evaluator).getLabelerTree().addBranch(test);
165                 branch.addFactory(factory, binding.getPreference());
166             }
167         }
168
169         for(final ContributorBinding<CheckedStateFactory> binding : checkStateContributions) {
170
171             CheckedStateFactory factory = binding.getContributor().getFactory();
172             Tester test = binding.getContributor().getNodeContextTester();
173             Class<?> clazz = binding.getContributor().getInputClass();
174             Evaluator evaluator = evaluators.get(clazz);
175             if(evaluator == null) {
176                 evaluator = data.newEvaluator();
177                 evaluators.put(clazz, evaluator);
178             }
179             if(test == null) {
180                 if (DEBUG)
181                     System.out.println("add root check state " + factory + " to class " + clazz.getSimpleName());
182                 evaluator.addCheckState(factory, binding.getPreference());
183             } else {
184                 if (DEBUG)
185                     System.out.println("add check state " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
186                 EvaluatorTree<CheckedStateFactory> branch = ((EvaluatorImpl)evaluator).getCheckStateTree().addBranch(test);
187                 branch.addFactory(factory, binding.getPreference());
188             }
189         }
190
191         for(final ContributorBinding<LabelDecoratorFactory> binding : labelDecoratorContributions) {
192
193             LabelDecoratorFactory factory = binding.getContributor().getFactory();
194             Tester test = binding.getContributor().getNodeContextTester();
195             Class<?> clazz = binding.getContributor().getInputClass();
196             Evaluator evaluator = evaluators.get(clazz);
197             if(evaluator == null) {
198                 evaluator = data.newEvaluator();
199                 evaluators.put(clazz, evaluator);
200             }
201             if(test == null) {
202                 if (DEBUG)
203                     System.out.println("add root label decorator " + factory + " to class " + clazz.getSimpleName());
204                 evaluator.addLabelDecorator(factory, binding.getPreference());
205             } else {
206                 if (DEBUG)
207                     System.out.println("add label decorator " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
208                 EvaluatorTree<LabelDecoratorFactory> branch = ((EvaluatorImpl)evaluator).getLabelDecoratorTree().addBranch(test);
209                 branch.addFactory(factory, binding.getPreference());
210             }
211         }
212
213         for(final ContributorBinding<ImagerFactory> binding : imagerContributions) {
214
215             ImagerFactory factory = binding.getContributor().getFactory();
216             Tester test = binding.getContributor().getNodeContextTester();
217             Class<?> clazz = binding.getContributor().getInputClass();
218             Evaluator evaluator = evaluators.get(clazz);
219             if(evaluator == null) {
220                 evaluator = data.newEvaluator();
221                 evaluators.put(clazz, evaluator);
222             }
223             if(test == null) {
224                 if (DEBUG)
225                     System.out.println("add root imager " + factory + " to class " + clazz.getSimpleName());
226                 evaluator.addImager(factory, binding.getPreference());
227             } else {
228                 if (DEBUG)
229                     System.out.println("add imager " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
230                 EvaluatorTree<ImagerFactory> branch = ((EvaluatorImpl)evaluator).getImagerTree().addBranch(test);
231                 branch.addFactory(factory, binding.getPreference());
232             }
233         }
234
235         for(final ContributorBinding<ImageDecoratorFactory> binding : imageDecoratorContributions) {
236
237             ImageDecoratorFactory factory = binding.getContributor().getFactory();
238             Tester test = binding.getContributor().getNodeContextTester();
239             Class<?> clazz = binding.getContributor().getInputClass();
240             Evaluator evaluator = evaluators.get(clazz);
241             if(evaluator == null) {
242                 evaluator = data.newEvaluator();
243                 evaluators.put(clazz, evaluator);
244             }
245             if(test == null) {
246                 if (DEBUG)
247                     System.out.println("add root image decorator " + factory + " to class " + clazz.getSimpleName());
248                 evaluator.addImageDecorator(factory, binding.getPreference());
249             } else {
250                 if (DEBUG)
251                     System.out.println("add image decorator " + factory + " to class " + clazz.getSimpleName() + " with tester " + test);
252                 EvaluatorTree<ImageDecoratorFactory> branch = ((EvaluatorImpl)evaluator).getImageDecoratorTree().addBranch(test);
253                 branch.addFactory(factory, binding.getPreference());
254             }
255         }
256
257         for(Map.Entry<Class<?>, Evaluator> entry : evaluators.entrySet()) {
258             data.addEvaluator(entry.getKey(), entry.getValue());
259         }
260
261     }
262
263     /**
264      * @param processor
265      * @param browseContexts
266      * @param resourceManager
267      * @param data
268      * @return the URIs of the actual set of browse contexts loaded by this
269      *         method. If an included browse context resource has no URI, it
270      *         will not be included in this set.
271      */
272     public static Set<String> loadModelled(
273             RequestProcessor processor, 
274             final Set<String> browseContexts, 
275             final ResourceManager resourceManager,
276             final EvaluatorDataImpl data,
277             final boolean useNodeBrowseContexts,
278             final boolean useNodeActionContexts) {
279
280         final Set<String> allBrowseContexts = new HashSet<String>(browseContexts);
281
282         // Create evaluator
283         final EvaluatorImpl eval = new EvaluatorImpl();
284         data.addEvaluator(Object.class, eval);  
285         
286         try {
287
288             // Load data for evaluator
289             processor.syncRequest(new ReadRequest() {
290                 
291                 @Override
292                 public void run(ReadGraph graph) throws DatabaseException {
293
294                     // Map browse context names to resources
295                     Collection<Resource> browseContextResources = new ArrayList<Resource>(browseContexts.size());
296                     for(String browseContext : browseContexts) {
297                         try {
298                             browseContextResources.add(graph.getResource(browseContext));
299                         } catch(DatabaseException e) {
300                             LOGGER.error("Didn't find " + browseContext + " while loading model browser.", e);
301                         }
302                     }
303
304                     // Load browse context
305                     try {
306                         final BrowseContext browseContext = 
307                             BrowseContext.create(graph, browseContextResources);
308                         
309                         data.setBrowseContext(browseContext);
310
311                         // Get URI's for all found browse contexts to fill return value
312                         for (Resource context : BrowseContext.findSubcontexts(graph, browseContextResources)) {
313                             String uri = graph.getPossibleURI(context);
314                             if (uri != null)
315                                 allBrowseContexts.add(uri);
316                         }
317
318                         // Fill evaluator
319                         eval.addLabeler(new LabelerFactory() {
320                             @Override
321                             public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context,
322                                     LabelerKey key) {
323                                 return new EvaluatorLabeler(updater, context, key, browseContext, useNodeBrowseContexts);
324                             }
325                         }, 0.0);
326
327                         eval.addImager(new ImagerFactory() {
328                             @Override
329                             public Imager create(PrimitiveQueryUpdater updater, NodeContext context,
330                                     ImagerKey key) {
331                                 return new EvaluatorImager(updater, context, key, browseContext, useNodeBrowseContexts);
332                             }
333                         }, 0.0);
334
335                         eval.addCheckState(new CheckedStateFactory() {
336
337                             @Override
338                             public CheckedState create(PrimitiveQueryUpdater updater,
339                                     NodeContext context, CheckedStateKey key) {
340                                 return new EvaluatorCheckedState(updater, context, key, browseContext, useNodeBrowseContexts).getState();
341                             }
342                         }, 0.0);
343
344                         eval.addLabelDecorator(new LabelDecoratorFactory() {
345                             @Override
346                             public LabelDecorator create(PrimitiveQueryUpdater updater,
347                                     NodeContext context, LabelDecoratorKey key) {
348                                 return new EvaluatorLabelDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
349                             }
350                         }, 0.0);
351
352                         eval.addImageDecorator(new ImageDecoratorFactory() {
353                             @Override
354                             public ImageDecorator create(PrimitiveQueryUpdater updater,
355                                     NodeContext context, ImageDecoratorKey key) {
356                                 return new EvaluatorImageDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
357                             }
358                         }, 0.0);
359
360                         eval.addViewpoint(new ViewpointFactory() {
361                             @Override
362                             public Viewpoint create(PrimitiveQueryUpdater updater, 
363                                     final NodeContext context,
364                                     ViewpointKey key) {
365                                 return new EvaluatorViewpoint(updater, context, key, browseContext, useNodeBrowseContexts, useNodeActionContexts);
366                             }
367                         }, 0.0);
368                     } catch (InvalidContribution e) {
369                         e.printStackTrace();
370                         return;
371                     }
372
373                 }
374
375             });
376
377         } catch (DatabaseException e) {
378
379             e.printStackTrace();
380
381         }
382
383         return allBrowseContexts;
384     }
385
386     public static void loadExtensions(RequestProcessor processor, final Set<String> browseContexts, final ResourceManager resourceManager,
387             final Map<String, Collection<ContributorBinding<ViewpointContributionFactory>>> viewpointContributions,
388             final Set<ContributorBinding<ComparableContextFactory>> comparableContextContributions,
389             final Set<ContributorBinding<LabelerFactory>> labelerContributions,
390             final Set<ContributorBinding<CheckedStateFactory>> checkStateContributions,
391             final Set<ContributorBinding<LabelDecoratorFactory>> labelDecoratorContributions,
392             final Set<ContributorBinding<ImagerFactory>> imagerContributions,
393             final Set<ContributorBinding<ImageDecoratorFactory>> imageDecoratorContributions) {
394
395         for(ContributorBinding<ViewpointContributionFactory> binding : ViewpointContributionContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
396             String viewpointId = binding.getContributor().getFactory().getViewpointId();
397             Collection<ContributorBinding<ViewpointContributionFactory>> viewpoints = viewpointContributions.get(viewpointId);
398             if(viewpoints == null) {
399                 viewpoints = new ArrayList<ContributorBinding<ViewpointContributionFactory>>();
400                 viewpointContributions.put(viewpointId, viewpoints);
401             }
402             viewpoints.add(binding);
403         }
404
405         for(final ContributorBinding<ComparableContextFactory> binding : ComparableContextContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
406             comparableContextContributions.add(binding);
407         }
408
409         for(final ContributorBinding<LabelerFactory> binding : LabelerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
410             labelerContributions.add(binding);
411         }
412
413         for(final ContributorBinding<CheckedStateFactory> binding : CheckedStateContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
414             checkStateContributions.add(binding);
415         }
416
417         for(final ContributorBinding<LabelDecoratorFactory> binding : LabelDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
418             labelDecoratorContributions.add(binding);
419         }
420
421         for(final ContributorBinding<ImagerFactory> binding : ImagerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
422             imagerContributions.add(binding);
423         }
424
425         for(final ContributorBinding<ImageDecoratorFactory> binding : ImageDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
426             imageDecoratorContributions.add(binding);
427         }
428
429     }
430
431     public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager) throws InvalidBrowserIdException {
432         return load(processor, browseContexts, resourceManager, false, false);
433     }
434
435     public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager, boolean useNodeBrowseContexts, boolean useNodeActionContexts) throws InvalidBrowserIdException {
436
437         Map<String, Collection<ContributorBinding<ViewpointContributionFactory>>> viewpointContributions = new HashMap<String, Collection<ContributorBinding<ViewpointContributionFactory>>>();
438         Set<ContributorBinding<ComparableContextFactory>> comparableContextContributions = new HashSet<ContributorBinding<ComparableContextFactory>>();
439         Set<ContributorBinding<LabelerFactory>> labelerContributions = new HashSet<ContributorBinding<LabelerFactory>>();
440         Set<ContributorBinding<CheckedStateFactory>> checkStateContributions = new HashSet<ContributorBinding<CheckedStateFactory>>();
441         Set<ContributorBinding<LabelDecoratorFactory>> labelDecoratorContributions = new HashSet<ContributorBinding<LabelDecoratorFactory>>();
442         Set<ContributorBinding<ImagerFactory>> imagerContributions = new HashSet<ContributorBinding<ImagerFactory>>();
443         Set<ContributorBinding<ImageDecoratorFactory>> imageDecoratorContributions = new HashSet<ContributorBinding<ImageDecoratorFactory>>();
444
445         EvaluatorDataImpl data = new EvaluatorDataImpl();
446
447         // Load modelled contributions
448         Set<String> allBrowseContexts = loadModelled(processor, browseContexts, resourceManager, data, useNodeBrowseContexts, useNodeActionContexts);
449
450         // Load extension point contributions
451         loadExtensions(processor, allBrowseContexts, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
452
453         // Create contributed evaluators
454         create(data, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
455
456         // Create plugin evaluators for the contexts
457         for(EvaluatorFactory factory : EvaluatorBindingExtensionManager.getInstance().getBoundFactories(allBrowseContexts)) {
458             data.addEvaluator(factory.getClazz(), factory.create(allBrowseContexts));
459         }
460
461         return data;
462
463     }
464     
465 }