aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/main/java/tools/refinery/language/scoping/imports/ImportCollector.java
blob: f3ab54ae92285476fb309569fd3b96a644349bf0 (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
/*
 * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.scoping.imports;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.linking.impl.LinkingHelper;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.util.IResourceScopeCache;
import tools.refinery.language.model.problem.ImportStatement;
import tools.refinery.language.model.problem.Problem;
import tools.refinery.language.model.problem.ProblemPackage;
import tools.refinery.language.naming.NamingUtil;
import tools.refinery.language.resource.LoadOnDemandResourceDescriptionProvider;
import tools.refinery.language.resource.ProblemResourceDescriptionStrategy;

import java.util.*;

@Singleton
public class ImportCollector {
	private static final String PREFIX = "tools.refinery.language.imports.";
	private static final String DIRECT_IMPORTS_KEY = PREFIX + "DIRECT_IMPORTS";
	private static final String ALL_IMPORTS_KEY = PREFIX + "ALL_IMPORTS";

	@Inject
	private IResourceScopeCache cache;

	@Inject
	private LinkingHelper linkingHelper;

	@Inject
	private IQualifiedNameConverter qualifiedNameConverter;

	@Inject
	private Provider<LoadOnDemandResourceDescriptionProvider> loadOnDemandProvider;

	@Inject
	private ImportAdapterProvider importAdapterProvider;

	public ImportCollection getDirectImports(Resource resource) {
		return cache.get(DIRECT_IMPORTS_KEY, resource, () -> this.computeDirectImports(resource));
	}

	protected ImportCollection computeDirectImports(Resource resource) {
		if (resource.getContents().isEmpty() || !(resource.getContents().getFirst() instanceof Problem problem)) {
			return ImportCollection.EMPTY;
		}
		var resourceSet = resource.getResourceSet();
		if (resourceSet == null) {
			return ImportCollection.EMPTY;
		}
		var adapter = importAdapterProvider.getOrInstall(resourceSet);
		var collection = new ImportCollection();
		collectAutomaticImports(collection, adapter);
		collectExplicitImports(problem, collection, adapter);
		collection.remove(resource.getURI());
		return collection;
	}

	private void collectAutomaticImports(ImportCollection importCollection, ImportAdapter adapter) {
		for (var library : adapter.getLibraries()) {
			for (var qualifiedName : library.getAutomaticImports()) {
				var uri = adapter.resolveQualifiedName(qualifiedName);
				if (uri != null) {
					importCollection.add(NamedImport.implicit(uri, qualifiedName));
				}
			}
		}
	}

	private void collectExplicitImports(Problem problem, ImportCollection collection, ImportAdapter adapter) {
		for (var statement : problem.getStatements()) {
			if (statement instanceof ImportStatement importStatement) {
				collectImportStatement(importStatement, collection, adapter);
			}
		}
	}

	private void collectImportStatement(ImportStatement importStatement, ImportCollection collection,
										ImportAdapter adapter) {
		var nodes = NodeModelUtils.findNodesForFeature(importStatement,
				ProblemPackage.Literals.IMPORT_STATEMENT__IMPORTED_MODULE);
		var aliasString = importStatement.getAlias();
		var alias = Strings.isNullOrEmpty(aliasString) ? QualifiedName.EMPTY :
				NamingUtil.stripRootPrefix(qualifiedNameConverter.toQualifiedName(aliasString));
		var referredProblem = (EObject) importStatement.eGet(ProblemPackage.Literals.IMPORT_STATEMENT__IMPORTED_MODULE,
				false);
		URI referencedUri = null;
		if (referredProblem != null && !referredProblem.eIsProxy()) {
			var resource = referredProblem.eResource();
			if (resource != null) {
				referencedUri = resource.getURI();
			}
		}
		for (var node : nodes) {
			var qualifiedNameString = linkingHelper.getCrossRefNodeAsString(node, true);
			if (Strings.isNullOrEmpty(qualifiedNameString)) {
				continue;
			}
			var qualifiedName = NamingUtil.stripRootPrefix(
					qualifiedNameConverter.toQualifiedName(qualifiedNameString));
			var uri = referencedUri == null ? adapter.resolveQualifiedName(qualifiedName) : referencedUri;
			if (uri != null) {
				collection.add(NamedImport.explicit(uri, qualifiedName, List.of(alias)));
			}
		}
	}

	public ImportCollection getAllImports(Resource resource) {
		return cache.get(ALL_IMPORTS_KEY, resource, () -> this.computeAllImports(resource));
	}

	protected ImportCollection computeAllImports(Resource resource) {
		var collection = new ImportCollection();
		collection.addAll(getDirectImports(resource).toList());
		var loadOnDemand = loadOnDemandProvider.get();
		loadOnDemand.setContext(resource);
		var seen = new HashSet<URI>();
		seen.add(resource.getURI());
		var queue = new ArrayDeque<>(collection.toUriSet());
		while (!queue.isEmpty()) {
			var uri = queue.removeFirst();
			seen.add(uri);
			collection.add(new TransitiveImport(uri));
			var resourceDescription = loadOnDemand.getResourceDescription(uri);
			if (resourceDescription == null) {
				continue;
			}
			var problemDescriptions = resourceDescription.getExportedObjectsByType(ProblemPackage.Literals.PROBLEM);
			for (var eObjectDescription : problemDescriptions) {
				for (var importedUri : getImports(eObjectDescription)) {
					if (!seen.contains(importedUri)) {
						queue.addLast(importedUri);
					}
				}
			}
		}
		collection.remove(resource.getURI());
		return collection;
	}

	protected List<URI> getImports(IEObjectDescription eObjectDescription) {
		var importString = eObjectDescription.getUserData(ProblemResourceDescriptionStrategy.IMPORTS);
		if (importString == null || importString.isEmpty()) {
			return List.of();
		}
		return Splitter.on(ProblemResourceDescriptionStrategy.IMPORTS_SEPARATOR).splitToStream(importString)
				.map(URI::createURI)
				.toList();
	}
}