aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/language/src/main/java/tools/refinery/language/resource/state/ProblemDerivedStateComputer.java
blob: d905aa9afbb938676ce71a9654a41b6979145d88 (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * SPDX-FileCopyrightText: 2021-2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.language.resource.state;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.Constants;
import org.eclipse.xtext.resource.DerivedStateAwareResource;
import org.eclipse.xtext.resource.IDerivedStateComputer;
import org.eclipse.xtext.resource.XtextResource;
import tools.refinery.language.model.problem.*;
import tools.refinery.language.utils.ProblemUtil;

import java.util.*;
import java.util.function.Function;

@Singleton
public class ProblemDerivedStateComputer implements IDerivedStateComputer {
	public static final String NEW_NODE = "new";

	@Inject
	@Named(Constants.LANGUAGE_NAME)
	private String languageName;

	@Inject
	private Provider<NodeNameCollector> nodeNameCollectorProvider;

	@Inject
	private DerivedVariableComputer derivedVariableComputer;

	@Override
	public void installDerivedState(DerivedStateAwareResource resource, boolean preLinkingPhase) {
		var problem = getProblem(resource);
		if (problem != null) {
			var adapter = getOrInstallAdapter(resource);
			installDerivedProblemState(problem, adapter, preLinkingPhase);
		}
	}

	protected Problem getProblem(Resource resource) {
		List<EObject> contents = resource.getContents();
		if (contents.isEmpty()) {
			return null;
		}
		EObject object = contents.get(0);
		if (object instanceof Problem problem) {
			return problem;
		}
		return null;
	}

	protected void installDerivedProblemState(Problem problem, Adapter adapter, boolean preLinkingPhase) {
		installDerivedClassDeclarationState(problem, adapter);
		if (preLinkingPhase) {
			return;
		}
		Set<String> nodeNames = installDerivedNodes(problem);
		derivedVariableComputer.installDerivedVariables(problem, nodeNames);
	}

	protected void installDerivedClassDeclarationState(Problem problem, Adapter adapter) {
		for (var statement : problem.getStatements()) {
			if (statement instanceof ClassDeclaration classDeclaration) {
				installOrRemoveNewNode(adapter, classDeclaration);
				for (var featureDeclaration : classDeclaration.getFeatureDeclarations()) {
					if (featureDeclaration instanceof ReferenceDeclaration referenceDeclaration) {
						installOrRemoveInvalidMultiplicityPredicate(adapter, classDeclaration, referenceDeclaration);
					}
				}
			}
		}
	}

	protected void installOrRemoveNewNode(Adapter adapter, ClassDeclaration declaration) {
		if (declaration.isAbstract()) {
			var newNode = declaration.getNewNode();
			if (newNode != null) {
				declaration.setNewNode(null);
				adapter.removeNewNode(declaration);
			}
		} else {
			if (declaration.getNewNode() == null) {
				var newNode = adapter.createNewNodeIfAbsent(declaration, key -> createNode(NEW_NODE));
				declaration.setNewNode(newNode);
			}
		}
	}

	protected void installOrRemoveInvalidMultiplicityPredicate(
			Adapter adapter, ClassDeclaration containingClassDeclaration, ReferenceDeclaration declaration) {
		if (ProblemUtil.hasMultiplicityConstraint(declaration)) {
			if (declaration.getInvalidMultiplicity() == null) {
				var invalidMultiplicity = adapter.createInvalidMultiplicityPredicateIfAbsent(declaration, key -> {
					var predicate = ProblemFactory.eINSTANCE.createPredicateDefinition();
					predicate.setError(true);
					predicate.setName("invalidMultiplicity");
					var parameter = ProblemFactory.eINSTANCE.createParameter();
					parameter.setParameterType(containingClassDeclaration);
					parameter.setName("node");
					predicate.getParameters().add(parameter);
					return predicate;
				});
				declaration.setInvalidMultiplicity(invalidMultiplicity);
			}
		} else {
			var invalidMultiplicity = declaration.getInvalidMultiplicity();
			if (invalidMultiplicity != null) {
				declaration.setInvalidMultiplicity(null);
				adapter.removeInvalidMultiplicityPredicate(declaration);
			}
		}
	}

	protected Set<String> installDerivedNodes(Problem problem) {
		var collector = nodeNameCollectorProvider.get();
		collector.collectNodeNames(problem);
		Set<String> nodeNames = collector.getNodeNames();
		List<Node> graphNodes = problem.getNodes();
		for (String nodeName : nodeNames) {
			var graphNode = createNode(nodeName);
			graphNodes.add(graphNode);
		}
		return nodeNames;
	}

	protected Node createNode(String name) {
		var node = ProblemFactory.eINSTANCE.createNode();
		node.setName(name);
		return node;
	}

	@Override
	public void discardDerivedState(DerivedStateAwareResource resource) {
		var problem = getProblem(resource);
		if (problem != null) {
			var adapter = getOrInstallAdapter(resource);
			discardDerivedProblemState(problem, adapter);
		}
	}

	protected void discardDerivedProblemState(Problem problem, Adapter adapter) {
		var abstractClassDeclarations = new HashSet<ClassDeclaration>();
		var referenceDeclarationsWithMultiplicity = new HashSet<ReferenceDeclaration>();
		problem.getNodes().clear();
		for (var statement : problem.getStatements()) {
			if (statement instanceof ClassDeclaration classDeclaration) {
				classDeclaration.setNewNode(null);
				if (classDeclaration.isAbstract()) {
					abstractClassDeclarations.add(classDeclaration);
				}
				for (var featureDeclaration : classDeclaration.getFeatureDeclarations()) {
					if (featureDeclaration instanceof ReferenceDeclaration referenceDeclaration &&
							ProblemUtil.hasMultiplicityConstraint(referenceDeclaration)) {
						referenceDeclarationsWithMultiplicity.add(referenceDeclaration);
					}
				}
			}
		}
		adapter.retainAll(abstractClassDeclarations, referenceDeclarationsWithMultiplicity);
		derivedVariableComputer.discardDerivedVariables(problem);
	}

	protected Adapter getOrInstallAdapter(Resource resource) {
		if (!(resource instanceof XtextResource)) {
			return new Adapter();
		}
		String resourceLanguageName = ((XtextResource) resource).getLanguageName();
		if (!languageName.equals(resourceLanguageName)) {
			return new Adapter();
		}
		var adapter = (Adapter) EcoreUtil.getAdapter(resource.eAdapters(), Adapter.class);
		if (adapter == null) {
			adapter = new Adapter();
			resource.eAdapters().add(adapter);
		}
		return adapter;
	}

	protected static class Adapter extends AdapterImpl {
		private final Map<ClassDeclaration, Node> newNodes = new HashMap<>();
		private final Map<ReferenceDeclaration, PredicateDefinition> invalidMultiplicityPredicates = new HashMap<>();

		public Node createNewNodeIfAbsent(ClassDeclaration classDeclaration,
										  Function<ClassDeclaration, Node> createNode) {
			return newNodes.computeIfAbsent(classDeclaration, createNode);
		}

		public void removeNewNode(ClassDeclaration classDeclaration) {
			newNodes.remove(classDeclaration);
		}

		public PredicateDefinition createInvalidMultiplicityPredicateIfAbsent(
				ReferenceDeclaration referenceDeclaration,
				Function<ReferenceDeclaration, PredicateDefinition> createPredicate) {
			return invalidMultiplicityPredicates.computeIfAbsent(referenceDeclaration, createPredicate);
		}

		public void removeInvalidMultiplicityPredicate(ReferenceDeclaration referenceDeclaration) {
			invalidMultiplicityPredicates.remove(referenceDeclaration);
		}

		public void retainAll(Collection<ClassDeclaration> abstractClassDeclarations,
							  Collection<ReferenceDeclaration> referenceDeclarationsWithMultiplicity) {
			newNodes.keySet().retainAll(abstractClassDeclarations);
			invalidMultiplicityPredicates.keySet().retainAll(referenceDeclarationsWithMultiplicity);
		}

		@Override
		public boolean isAdapterForType(Object type) {
			return Adapter.class == type;
		}
	}
}