Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c55862b264a9ea05ef0afd063ed33b73837da93b (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
/******************************************************************************
 * Copyright (c) 2007, 2020 Borland Software Corporation, CEA LIST, Artal
 * 
 * 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: 
 *     bblajer - initial API and implementation
 *     Artem Tikhomirov (Borland) - Migration to OCL expressions
 *     Aurélien Didier (ARTAL) - aurelien.didier51@gmail.com - Bug 569174
 *******************************************************************************/
package org.eclipse.papyrus.gmf.internal.xpand.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.papyrus.gmf.internal.xpand.ResourceManager;
import org.eclipse.papyrus.gmf.internal.xpand.ast.Advice;
import org.eclipse.papyrus.gmf.internal.xpand.model.AnalysationIssue;
import org.eclipse.papyrus.gmf.internal.xpand.model.ExecutionContext;
import org.eclipse.papyrus.gmf.internal.xpand.model.ExecutionContextImpl;
import org.eclipse.papyrus.gmf.internal.xpand.model.Scope;
import org.eclipse.papyrus.gmf.internal.xpand.model.StatefulResource;
import org.eclipse.papyrus.gmf.internal.xpand.model.XpandAdvice;
import org.eclipse.papyrus.gmf.internal.xpand.model.XpandDefinition;
import org.eclipse.papyrus.gmf.internal.xpand.model.XpandResource;

/**
 * Composes several Xpand ast trees into a single resource. Definitions are merged: 
 * if definitions with duplicate signatures are found, the one that comes first (i.e., from a more recent source) wins. 
 * Advice declarations are aggregated: if several advice declarations have the same signature, all are returned in the order
 * in which they are declared.
 */
class CompositeXpandResource implements XpandResource, StatefulResource {
	private final XpandResource[] myDefinitions;
	private final XpandResource[] myAdvices;
	private XpandAdvice[] myCachedAdvices;
	private XpandDefinition[] myCachedDefinitions;
	private String[] myImportedNamespaces;
	private String[] myImportedExtensions;

	/**
	 * Creates a composite resource from a non-empty array of definition resources and optional advice resources.
	 * @param manager Resource manager used to create this resource. It will not be remembered by the resource.
	 * @param definitions an array of definition resources. Must not be empty.
	 * @param advices an array of advice resources or <code>null</code> if no advice resources are available.
	 */
	public CompositeXpandResource(ResourceManager manager, XpandResource[] definitions, XpandResource[] advices) {
		myDefinitions = definitions;
		myAdvices = advices == null ? NO_RESOURCES : advices;
	}
	
	public void initialize(Scope scope) {
		ArrayList<XpandDefinition> allDefinitions = new ArrayList<XpandDefinition>();
		HashSet<DefinitionSignature> signatures = new HashSet<DefinitionSignature>();
		ExecutionContext context = new ExecutionContextImpl(scope);
		// Definitions are merged in the following order: first, all advice
		// resources from newest to oldest, then all
		// non-advice resources, from newest to oldest.
		mergeDefinitions(context, myAdvices, allDefinitions, signatures);
		mergeDefinitions(context, myDefinitions, allDefinitions, signatures);
		myCachedDefinitions = allDefinitions.toArray(new XpandDefinition[allDefinitions.size()]);
		// Advice declarations are collected (without merging) in the order from
		// oldest to newest.
		// Only advice resources are taken into consideration.
		if (myAdvices.length > 0) {
			ArrayList<XpandAdvice> allAdvices = new ArrayList<XpandAdvice>();
			collectAdvices(myAdvices, allAdvices);
			myCachedAdvices = allAdvices.toArray(new XpandAdvice[allAdvices.size()]);
		} else {
			myCachedAdvices = NO_ADVICE;
		}
	}
	
	public boolean isInitialized() {
		return myCachedDefinitions != null && myCachedAdvices != null;
	}

	private void mergeDefinitions(ExecutionContext context, XpandResource[] resources, List<XpandDefinition> collector, Set<DefinitionSignature> usedSignatures) {
		for (int i = 0; i < resources.length; i++) {
			XpandResource nextResource = resources[i];
			context = context.cloneWithResource(nextResource);
			XpandDefinition[] definitions = nextResource.getDefinitions();
			for (XpandDefinition nextDefinition : definitions) {
				DefinitionSignature signature = DefinitionSignature.create(context, nextDefinition);
				if (signature == null || usedSignatures.contains(signature)) {
					continue;
				}
				usedSignatures.add(signature);
				collector.add(nextDefinition);
			}
		}
	}

	private void collectAdvices(XpandResource[] resources, List<XpandAdvice> collector) {
		for (int i = resources.length - 1; i >= 0; i--) {
			XpandResource nextResource = resources[i];
			XpandAdvice[] advices = nextResource.getAdvices();
			for (XpandAdvice nextAdvice : advices) {
				collector.add(nextAdvice);
			}
		}
	}

	public XpandAdvice[] getAdvices() {
		if (!isInitialized()) {
			throw new IllegalStateException("Stateful resource " + getFullyQualifiedName() + " was not initialized");
		}
		return myCachedAdvices;
	}

	public XpandDefinition[] getDefinitions() {
		if (!isInitialized()) {
			throw new IllegalStateException("Stateful resource " + getFullyQualifiedName() + " was not initialized");
		}
		return myCachedDefinitions;
	}

	public String getFullyQualifiedName() {
		return myDefinitions[0].getFullyQualifiedName();
	}

	public String[] getImportedExtensions() {
		if (myImportedExtensions == null) {
			LinkedHashSet<String> result = new LinkedHashSet<String>();
			for (XpandResource nextResource : myDefinitions) {
				for (String nextImport : nextResource.getImportedExtensions()) {
					result.add(nextImport);
				}
			}
			for (XpandResource nextResource : myAdvices) {
				for (String nextImport : nextResource.getImportedExtensions()) {
					result.add(nextImport);
				}
			}
			myImportedExtensions = result.toArray(new String[result.size()]);
		}
		return myImportedExtensions;
	}

	public String[] getImportedNamespaces() {
		if (myImportedNamespaces == null) {
			LinkedHashSet<String> result = new LinkedHashSet<String>();
			for (XpandResource nextResource : myDefinitions) {
				for (String nextImport : nextResource.getImportedNamespaces()) {
					result.add(nextImport);
				}
			}
			for (XpandResource nextResource : myAdvices) {
				for (String nextImport : nextResource.getImportedNamespaces()) {
					result.add(nextImport);
				}
			}
			myImportedNamespaces = result.toArray(new String[result.size()]);
		}
		return myImportedNamespaces;
	}

	public void analyze(ExecutionContext ctx, Set<AnalysationIssue> issues) {
		for (XpandResource next : myDefinitions) {
			next.analyze(ctx, issues);
		}
		for (XpandResource next : myAdvices) {
			next.analyze(ctx, issues);
		}
	}

	private static final XpandResource[] NO_RESOURCES = new XpandResource[0];
	private static final Advice[] NO_ADVICE = new Advice[0];
}

Back to the top