1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.project;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Arrays;
\r
16 import java.util.Collection;
\r
17 import java.util.Collections;
\r
18 import java.util.Comparator;
\r
19 import java.util.HashMap;
\r
20 import java.util.HashSet;
\r
21 import java.util.Map;
\r
22 import java.util.Set;
\r
23 import java.util.TreeSet;
\r
24 import java.util.regex.Matcher;
\r
25 import java.util.regex.Pattern;
\r
27 import org.eclipse.core.runtime.IStatus;
\r
28 import org.eclipse.core.runtime.Status;
\r
29 import org.simantics.databoard.Bindings;
\r
30 import org.simantics.db.ReadGraph;
\r
31 import org.simantics.db.Resource;
\r
32 import org.simantics.db.exception.DatabaseException;
\r
33 import org.simantics.project.exception.ProjectException;
\r
34 import org.simantics.project.features.registry.GroupReference;
\r
35 import org.simantics.project.features.registry.IProjectFeatureExtension;
\r
36 import org.simantics.project.features.registry.IProjectFeatureRegistry;
\r
37 import org.simantics.project.features.registry.InjectedDependency;
\r
38 import org.simantics.project.features.registry.ProjectFeatureReference;
\r
39 import org.simantics.project.internal.Activator;
\r
40 import org.simantics.project.ontology.ProjectResource;
\r
41 import org.simantics.utils.datastructures.MapSet;
\r
44 * A facade for handling project feature -related tasks.
\r
46 * @author Tuukka Lehtonen
\r
48 public class ProjectFeatures {
\r
51 * @return the singleton project feature registry service
\r
53 public static IProjectFeatureRegistry getRegistry() {
\r
54 return Activator.getDefault().getProjectFeatureRegistry();
\r
60 public static IProjectFeatureExtension[] getAllProjectFeatures() {
\r
61 return getRegistry().getExtensions();
\r
67 public static Collection<IProjectFeatureExtension> getPublishedProjectFeatures() {
\r
68 IProjectFeatureExtension[] extensions = getAllProjectFeatures();
\r
69 Collection<IProjectFeatureExtension> result = new ArrayList<IProjectFeatureExtension>();
\r
70 for (IProjectFeatureExtension ext : extensions) {
\r
71 if (ext.isPublished())
\r
80 public static Set<GroupReference> getInstallGroupsOfPublishedFeatures() {
\r
81 return getInstallGroups( getPublishedProjectFeatures() );
\r
88 public static Set<GroupReference> getInstallGroups(Collection<IProjectFeatureExtension> extensions) {
\r
89 Set<GroupReference> result = new TreeSet<GroupReference>();
\r
90 for (IProjectFeatureExtension ext : extensions)
\r
91 for (GroupReference grp : ext.installGroups())
\r
100 public static Collection<IProjectFeatureExtension> filterPublishedFeatures(GroupFilter filter) {
\r
101 return filterExtensions( getPublishedProjectFeatures(), filter );
\r
104 private static Set<IProjectFeatureExtension> filterExtensions(Collection<IProjectFeatureExtension> extensions,
\r
105 GroupFilter filter) {
\r
106 Set<IProjectFeatureExtension> included = new HashSet<IProjectFeatureExtension>();
\r
107 for (IProjectFeatureExtension ext : extensions)
\r
108 if (acceptExtension(ext, filter))
\r
113 private static boolean acceptExtension(IProjectFeatureExtension extension, GroupFilter filter) {
\r
114 for (GroupReference grp : extension.installGroups())
\r
115 if (filter.accept(grp))
\r
124 * @throws DatabaseException
\r
125 * @throws ProjectException
\r
127 public static Collection<IProjectFeatureExtension> getTopologicallySortedFeatures(GroupFilter filter) throws DatabaseException, ProjectException {
\r
128 return new FeatureWalker().getSortedFeatures(filter);
\r
135 * @throws DatabaseException
\r
136 * @throws ProjectException
\r
138 public static Collection<IProjectFeatureExtension> getTopologicallySortedFeatures(ReadGraph g, Resource project) throws DatabaseException, ProjectException {
\r
139 return new FeatureWalker().getSortedFeatures(g, project);
\r
142 private static class FeatureWalker {
\r
144 private Map<String, IProjectFeatureExtension> byId;
\r
145 private final MapSet<IProjectFeatureExtension, IProjectFeatureExtension> required = new MapSet.Hash<IProjectFeatureExtension, IProjectFeatureExtension>();
\r
147 private IProjectFeatureExtension getExtension(ProjectFeatureReference ref) {
\r
148 return byId.get(ref.id);
\r
151 public Collection<IProjectFeatureExtension> getSortedFeatures(ReadGraph g, Resource project) throws DatabaseException, ProjectException {
\r
152 Set<GroupReference> projectDefinedFeatures = loadFeatureReferences(g, project);
\r
153 return getSortedFeatures( GroupFilters.includesVersion( projectDefinedFeatures ) );
\r
156 public Collection<IProjectFeatureExtension> getSortedFeatures(GroupFilter filter) throws DatabaseException, ProjectException {
\r
157 // Create ID -> Extension cache
\r
158 IProjectFeatureExtension[] allExtensions = getAllProjectFeatures();
\r
159 byId = createIdMap(allExtensions);
\r
161 // Find which extensions are to be loaded for the feature contexts.
\r
162 Set<IProjectFeatureExtension> included = filterExtensions(Arrays.asList(allExtensions), filter);
\r
164 // Add all required extensions
\r
165 Collection<IProjectFeatureExtension> required = requiredExtensions(allExtensions, included);
\r
167 // Sort topologically
\r
168 return sortTopologically(required);
\r
171 private boolean deepRequires(Set<IProjectFeatureExtension> visited, IProjectFeatureExtension e1, IProjectFeatureExtension e2) {
\r
172 // Prevent eternal looping in cyclic cases
\r
173 // which are obviously erroneous definitions
\r
174 // but we don't want to get stuck anyway.
\r
175 if (visited.add(e1)) {
\r
176 // Check direct requires first.
\r
177 Set<IProjectFeatureExtension> req = required.getValues(e1);
\r
180 if (req.contains(e2))
\r
182 // Check transitively requirements
\r
183 for (IProjectFeatureExtension r : req) {
\r
184 if (deepRequires(visited, r, e2))
\r
191 private Collection<IProjectFeatureExtension> sortTopologically(Collection<IProjectFeatureExtension> toSort) {
\r
192 ArrayList<IProjectFeatureExtension> sorted = new ArrayList<IProjectFeatureExtension>(toSort);
\r
193 Collections.sort(sorted, new Comparator<IProjectFeatureExtension>() {
\r
194 Set<IProjectFeatureExtension> visited = new HashSet<IProjectFeatureExtension>();
\r
196 public int compare(IProjectFeatureExtension e1, IProjectFeatureExtension e2) {
\r
198 if (deepRequires(visited, e1, e2))
\r
201 if (deepRequires(visited, e2, e1))
\r
209 private Collection<IProjectFeatureExtension> requiredExtensions(IProjectFeatureExtension[] allExtensions, Collection<IProjectFeatureExtension> includedExtensions) throws ProjectException {
\r
210 Collection<IProjectFeatureExtension> result = new ArrayList<IProjectFeatureExtension>();
\r
211 Set<IProjectFeatureExtension> visited = new HashSet<IProjectFeatureExtension>();
\r
212 for (IProjectFeatureExtension ext : includedExtensions) {
\r
213 walkRequired(result, visited, ext);
\r
216 Set<String> includedProjectFeatureIds = new HashSet<String>();
\r
217 for (IProjectFeatureExtension ext : result) {
\r
218 includedProjectFeatureIds.add(ext.getId());
\r
221 // Find all injected dependencies!
\r
222 boolean changed = false;
\r
223 Set<IProjectFeatureExtension> injectionsProcessedFor = new HashSet<IProjectFeatureExtension>();
\r
226 for (IProjectFeatureExtension ext : allExtensions) {
\r
227 if (injectionsProcessedFor.contains(ext))
\r
230 for (InjectedDependency injection : ext.injections()) {
\r
231 if (includedProjectFeatureIds.contains(injection.to.id)) {
\r
232 injectionsProcessedFor.add(ext);
\r
233 IProjectFeatureExtension injectionTargetExt = byId.get(injection.to.id);
\r
234 if (injectionTargetExt != null) {
\r
236 includedProjectFeatureIds.add(ext.getId());
\r
238 required.add(injectionTargetExt, ext);
\r
248 private void walkRequired(Collection<IProjectFeatureExtension> result, Set<IProjectFeatureExtension> visited, IProjectFeatureExtension extension) throws ProjectException {
\r
249 if (visited.add(extension)) {
\r
250 result.add(extension);
\r
251 for (ProjectFeatureReference ref : extension.requires()) {
\r
252 IProjectFeatureExtension reqExt = getExtension(ref);
\r
253 if (reqExt != null) {
\r
254 required.add(extension, reqExt);
\r
255 walkRequired(result, visited, reqExt);
\r
256 } else if (!ref.optional) {
\r
257 // This requirement was not optional, must report error!
\r
258 throw new ProjectException("Missing org.simantics.project.feature extension with id '" + ref.id + "' required by feature '" + extension.getId() + "'");
\r
265 private static Set<GroupReference> loadFeatureReferences(ReadGraph graph, Resource project) throws DatabaseException {
\r
266 ProjectResource PROJ = ProjectResource.getInstance(graph);
\r
267 Set<GroupReference> features = new HashSet<GroupReference>();
\r
268 for (Resource fs : graph.getObjects(project, PROJ.HasFeature)) {
\r
269 String refStr = graph.getRelatedValue(fs, PROJ.HasGroupId, Bindings.STRING);
\r
271 GroupReference ref = parseFeatureReference(refStr);
\r
273 } catch (IllegalArgumentException e) {
\r
275 e.printStackTrace();
\r
279 // See http://dev.simantics.org/index.php/Project_Development#Omnipresent_Project_Features
\r
280 features.add(GroupReference.OMNIPRESENT);
\r
285 private static Map<String, IProjectFeatureExtension> createIdMap(IProjectFeatureExtension[] extensions) {
\r
286 Map<String, IProjectFeatureExtension> byId = new HashMap<String, IProjectFeatureExtension>();
\r
287 for (IProjectFeatureExtension ext : extensions) {
\r
288 IProjectFeatureExtension e = byId.put(ext.getId(), ext);
\r
290 Activator.getDefault().getLog().log(
\r
291 new Status(IStatus.WARNING, Activator.PLUGIN_ID,
\r
292 "Multiple org.simantics.project.feature extensions defined with id '" + ext.getId()
\r
293 + "'.\nprevious: " + e + "\nnew: " + ext)
\r
300 static String ID_PATTERN_STRING = "[a-zA-Z_0-9]+(?:\\.[a-zA-Z_0-9]+)*";
\r
301 static String VERSION_PATTERN_STRING = ".*";
\r
302 static Pattern POSSIBLY_VERSIONED_ID_PATTERN = Pattern.compile("(" + ID_PATTERN_STRING + ")(?:/(" + VERSION_PATTERN_STRING + "))?");
\r
307 * @throws IllegalArgumentException if the reference string cannot be parsed
\r
309 private static GroupReference parseFeatureReference(String reference) {
\r
310 Matcher m = POSSIBLY_VERSIONED_ID_PATTERN.matcher(reference);
\r
312 return new GroupReference(m.group(1), m.group(2));
\r
314 throw new IllegalArgumentException("could not parse feature reference " + reference);
\r