1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
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;
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.
76 * @author Antti Villberg
78 public class Evaluators {
80 private static final Logger LOGGER = LoggerFactory.getLogger(Evaluators.class);
81 public static final boolean DEBUG = false;
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) {
92 Map<Class<?>, Evaluator> evaluators = new HashMap<Class<?>, Evaluator>();
94 HashMap<String, ContributionViewpointFactory> viewpointFactories = new HashMap<String, ContributionViewpointFactory>();
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));
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);
114 System.out.println("add root viewpoint " + factory + " to class " + clazz.getSimpleName());
115 evaluator.addViewpoint(factory, binding.getPreference());
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());
125 for(final ContributorBinding<ComparableContextFactory> binding : comparableContextContributions) {
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);
137 System.out.println("add root comparable context " + factory + " to class " + clazz.getSimpleName());
138 evaluator.addComparableContext(factory, binding.getPreference());
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());
147 for(final ContributorBinding<LabelerFactory> binding : labelerContributions) {
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);
159 System.out.println("add root labeler " + factory + " to class " + clazz.getSimpleName());
160 evaluator.addLabeler(factory, binding.getPreference());
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());
169 for(final ContributorBinding<CheckedStateFactory> binding : checkStateContributions) {
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);
181 System.out.println("add root check state " + factory + " to class " + clazz.getSimpleName());
182 evaluator.addCheckState(factory, binding.getPreference());
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());
191 for(final ContributorBinding<LabelDecoratorFactory> binding : labelDecoratorContributions) {
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);
203 System.out.println("add root label decorator " + factory + " to class " + clazz.getSimpleName());
204 evaluator.addLabelDecorator(factory, binding.getPreference());
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());
213 for(final ContributorBinding<ImagerFactory> binding : imagerContributions) {
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);
225 System.out.println("add root imager " + factory + " to class " + clazz.getSimpleName());
226 evaluator.addImager(factory, binding.getPreference());
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());
235 for(final ContributorBinding<ImageDecoratorFactory> binding : imageDecoratorContributions) {
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);
247 System.out.println("add root image decorator " + factory + " to class " + clazz.getSimpleName());
248 evaluator.addImageDecorator(factory, binding.getPreference());
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());
257 for(Map.Entry<Class<?>, Evaluator> entry : evaluators.entrySet()) {
258 data.addEvaluator(entry.getKey(), entry.getValue());
265 * @param browseContexts
266 * @param resourceManager
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.
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) {
280 final Set<String> allBrowseContexts = new HashSet<String>(browseContexts);
283 final EvaluatorImpl eval = new EvaluatorImpl();
284 data.addEvaluator(Object.class, eval);
288 // Load data for evaluator
289 processor.syncRequest(new ReadRequest() {
292 public void run(ReadGraph graph) throws DatabaseException {
294 // Map browse context names to resources
295 Collection<Resource> browseContextResources = new ArrayList<Resource>(browseContexts.size());
296 for(String browseContext : browseContexts) {
298 browseContextResources.add(graph.getResource(browseContext));
299 } catch(DatabaseException e) {
300 LOGGER.error("Didn't find " + browseContext + " while loading model browser.", e);
304 // Load browse context
306 final BrowseContext browseContext =
307 BrowseContext.create(graph, browseContextResources);
309 data.setBrowseContext(browseContext);
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);
315 allBrowseContexts.add(uri);
319 eval.addLabeler(new LabelerFactory() {
321 public Labeler create(PrimitiveQueryUpdater updater, final NodeContext context,
323 return new EvaluatorLabeler(updater, context, key, browseContext, useNodeBrowseContexts);
327 eval.addImager(new ImagerFactory() {
329 public Imager create(PrimitiveQueryUpdater updater, NodeContext context,
331 return new EvaluatorImager(updater, context, key, browseContext, useNodeBrowseContexts);
335 eval.addCheckState(new CheckedStateFactory() {
338 public CheckedState create(PrimitiveQueryUpdater updater,
339 NodeContext context, CheckedStateKey key) {
340 return new EvaluatorCheckedState(updater, context, key, browseContext, useNodeBrowseContexts).getState();
344 eval.addLabelDecorator(new LabelDecoratorFactory() {
346 public LabelDecorator create(PrimitiveQueryUpdater updater,
347 NodeContext context, LabelDecoratorKey key) {
348 return new EvaluatorLabelDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
352 eval.addImageDecorator(new ImageDecoratorFactory() {
354 public ImageDecorator create(PrimitiveQueryUpdater updater,
355 NodeContext context, ImageDecoratorKey key) {
356 return new EvaluatorImageDecorator(updater, context, key, browseContext, useNodeBrowseContexts);
360 eval.addViewpoint(new ViewpointFactory() {
362 public Viewpoint create(PrimitiveQueryUpdater updater,
363 final NodeContext context,
365 return new EvaluatorViewpoint(updater, context, key, browseContext, useNodeBrowseContexts, useNodeActionContexts);
368 } catch (InvalidContribution e) {
377 } catch (DatabaseException e) {
383 return allBrowseContexts;
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) {
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);
402 viewpoints.add(binding);
405 for(final ContributorBinding<ComparableContextFactory> binding : ComparableContextContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
406 comparableContextContributions.add(binding);
409 for(final ContributorBinding<LabelerFactory> binding : LabelerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
410 labelerContributions.add(binding);
413 for(final ContributorBinding<CheckedStateFactory> binding : CheckedStateContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
414 checkStateContributions.add(binding);
417 for(final ContributorBinding<LabelDecoratorFactory> binding : LabelDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
418 labelDecoratorContributions.add(binding);
421 for(final ContributorBinding<ImagerFactory> binding : ImagerContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
422 imagerContributions.add(binding);
425 for(final ContributorBinding<ImageDecoratorFactory> binding : ImageDecoratorContributorBindingExtensionManager.getInstance().getBoundContributions(browseContexts)) {
426 imageDecoratorContributions.add(binding);
431 public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager) throws InvalidBrowserIdException {
432 return load(processor, browseContexts, resourceManager, false, false);
435 public static EvaluatorData load(RequestProcessor processor, Set<String> browseContexts, ResourceManager resourceManager, boolean useNodeBrowseContexts, boolean useNodeActionContexts) throws InvalidBrowserIdException {
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>>();
445 EvaluatorDataImpl data = new EvaluatorDataImpl();
447 // Load modelled contributions
448 Set<String> allBrowseContexts = loadModelled(processor, browseContexts, resourceManager, data, useNodeBrowseContexts, useNodeActionContexts);
450 // Load extension point contributions
451 loadExtensions(processor, allBrowseContexts, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
453 // Create contributed evaluators
454 create(data, resourceManager, viewpointContributions, comparableContextContributions, labelerContributions, checkStateContributions, labelDecoratorContributions, imagerContributions, imageDecoratorContributions);
456 // Create plugin evaluators for the contexts
457 for(EvaluatorFactory factory : EvaluatorBindingExtensionManager.getInstance().getBoundFactories(allBrowseContexts)) {
458 data.addEvaluator(factory.getClazz(), factory.create(allBrowseContexts));