Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d7faac20d6f5beedc055ef320004ee5cdb36a796 (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
/*****************************************************************************
 * Copyright (c) 2016, 2017 CEA LIST and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   CEA LIST - Initial API and implementation
 *   Alain Le Guennec (Esterel Technologies) - bug 519809
 *****************************************************************************/
package org.eclipse.papyrus.infra.types.core.utils;

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.Stack;

import org.eclipse.gmf.runtime.emf.type.core.IAdviceBindingDescriptor;
import org.eclipse.papyrus.infra.types.AdviceConfiguration;
import org.eclipse.papyrus.infra.types.ElementTypeConfiguration;
import org.eclipse.papyrus.infra.types.ExternallyRegisteredAdvice;
import org.eclipse.papyrus.infra.types.SpecializationTypeConfiguration;
import org.eclipse.papyrus.infra.types.core.registries.AdviceConfigurationTypeRegistry;

//Implements Tarjan's strongly connected components algorithm
public class TypesConfigurationsCycleUtil {


	protected static Collection<Collection<Object>> computeStronglyConnectedComponents(Set<String> vertices, Map<String, Set<String>> edges) {
		int index = 0;
		Map<String, Integer> lowIndex = new HashMap<String, Integer>();
		Collection<String> visitedVertices = new HashSet<String>();
		Stack<String> stack = new Stack<String>();
		Collection<Collection<Object>> stronglyConnnectedComponents = new ArrayList<Collection<Object>>();

		for (String vertex : vertices) {
			if (!visitedVertices.contains(vertex))
				dfs(vertex, vertices, edges, stronglyConnnectedComponents, stack, lowIndex, visitedVertices, index);
		}

		return stronglyConnnectedComponents;
	}


	protected static void dfs(String vertex, Set<String> vertices, Map<String, Set<String>> map, Collection<Collection<Object>> stronglyConnnectedComponents, Stack<String> stack, Map<String, Integer> lowIndex, Collection<String> visitedVertices,
			int index) {
		lowIndex.put(vertex, index++);
		visitedVertices.add(vertex);
		stack.push(vertex);
		int minIndex = lowIndex.get(vertex);
		for (String targetVertex : map.get(vertex)) {
			if (!visitedVertices.contains(targetVertex))
				dfs(targetVertex, vertices, map, stronglyConnnectedComponents, stack, lowIndex, visitedVertices, index);
			if (lowIndex.get(targetVertex) < minIndex)
				minIndex = lowIndex.get(targetVertex);
		}
		if (minIndex < lowIndex.get(vertex)) {
			lowIndex.put(vertex, minIndex);
			return;
		}
		List<Object> component = new ArrayList<Object>();
		String memberVertex;
		do {
			memberVertex = stack.pop();
			component.add(memberVertex);
			lowIndex.put(memberVertex, vertices.size());
		} while (!memberVertex.equals(vertex));

		if (component.size() > 1) {
			stronglyConnnectedComponents.add(component);
		}
	}

	/**
	 * @since 3.0
	 */
	static public Map<String, OrientedGraph<String>> getDependenciesAmongAdvices(Collection<AdviceConfiguration> adviceConfigurations) {
		Map<String, OrientedGraph<String>> adviceDependencies = new HashMap<String, OrientedGraph<String>>();
		for (AdviceConfiguration adviceConfiguration : adviceConfigurations) {
			IAdviceBindingDescriptor descriptor = AdviceConfigurationTypeRegistry.getInstance().getEditHelperAdviceDecriptor(adviceConfiguration);

			String type = descriptor.getTypeId();

			String currentAdviceConfigurationClassName = (adviceConfiguration instanceof ExternallyRegisteredAdvice)
				? ((ExternallyRegisteredAdvice) adviceConfiguration).getEditHelperAdviceClassName()
				: descriptor.getEditHelperAdvice().getClass().getName();
			// Add current to the vertices
			if (!adviceDependencies.containsKey(type)) {
				adviceDependencies.put(type, new OrientedGraph<String>());
			}
			OrientedGraph<String> deps = adviceDependencies.get(type);
			deps.addVertex(currentAdviceConfigurationClassName);

			// Add dependencies vertices
			for (AdviceConfiguration after : adviceConfiguration.getAfter()) {
				IAdviceBindingDescriptor descriptorAfter = AdviceConfigurationTypeRegistry.getInstance().getEditHelperAdviceDecriptor(after);
				deps.addEdge(currentAdviceConfigurationClassName, descriptorAfter.getEditHelperAdvice().getClass().getName());
			}

			for (AdviceConfiguration before : adviceConfiguration.getBefore()) {
				IAdviceBindingDescriptor descriptorBefore = AdviceConfigurationTypeRegistry.getInstance().getEditHelperAdviceDecriptor(before);
				deps.addEdge(descriptorBefore.getEditHelperAdvice().getClass().getName(), currentAdviceConfigurationClassName);
			}
		}

		return adviceDependencies;
	}

	static public Collection<Collection<Object>> getCyclesInAdvices(Set<String> vertices, Map<String, Set<String>> edges) {

		return computeStronglyConnectedComponents(vertices, edges);
	}

	static public OrientedGraph<String> getDependenciesAmongElementTypes(Collection<ElementTypeConfiguration> elementTypesConfigurations) {
		OrientedGraph<String> elementTypeDependencies = new OrientedGraph<String>();

		for (ElementTypeConfiguration elementTypeConfiguration : elementTypesConfigurations) {
			String currentElementTypeID = elementTypeConfiguration.getIdentifier();

			// Add current to the vertices
			elementTypeDependencies.addVertex(currentElementTypeID);

			// Add dependencies vertices
			if (elementTypeConfiguration instanceof SpecializationTypeConfiguration) {
				for (ElementTypeConfiguration specializedType : ((SpecializationTypeConfiguration) elementTypeConfiguration).getSpecializedTypes()) {
					elementTypeDependencies.addEdge(currentElementTypeID, specializedType.getIdentifier());
				}
			}
		}

		return elementTypeDependencies;
	}

	static public Collection<Collection<Object>> getCyclesAmongElementTypes(Set<String> vertices, Map<String, Set<String>> edges) {
		return computeStronglyConnectedComponents(vertices, edges);
	}
}

Back to the top