Skip to main content
summaryrefslogtreecommitdiffstats
blob: ce22f67f61035e76d6fe016cfe9e0754bee1308d (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
/*******************************************************************************
 * Copyright (c) 2016 Willink Transformations and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     E.D.Willink - initial API and implementation
 *******************************************************************************/
package org.eclipse.qvtd.compiler.internal.qvtr2qvtc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.ocl.pivot.OCLExpression;
import org.eclipse.ocl.pivot.Property;
import org.eclipse.ocl.pivot.ShadowPart;
import org.eclipse.ocl.pivot.utilities.ClassUtil;
import org.eclipse.ocl.pivot.utilities.NameUtil;
import org.eclipse.qvtd.compiler.CompilerChainException;
import org.eclipse.qvtd.pivot.qvtbase.Function;
import org.eclipse.qvtd.pivot.qvtbase.FunctionParameter;
import org.eclipse.qvtd.pivot.qvtcore.utilities.QVTcoreHelper;
import org.eclipse.qvtd.pivot.qvtrelation.Key;

/**
 * KeyToMappingForIdentification synthesizes a Core constructor function/querythat enforces the uniqueness of a realized variable
 * with respect to its key parts.
 */
public class KeyToFunctionForIdentification
{
	protected final @NonNull QVTrToQVTc qvtr2qvtc;
	protected final @NonNull Key rKey;
	
	public KeyToFunctionForIdentification(@NonNull QVTrToQVTc qvtr2qvtc, @NonNull Key rKey) {
		this.qvtr2qvtc = qvtr2qvtc;
		this.rKey = rKey;
	}

	public @NonNull Function transform() throws CompilerChainException {
		QVTcoreHelper helper = qvtr2qvtc.getHelper();
		String functionName = qvtr2qvtc.createKeyFunctionName(rKey);
		List<@NonNull FunctionParameter> asParameters = new ArrayList<@NonNull FunctionParameter>();
		List<@NonNull ShadowPart> asShadowParts = new ArrayList<@NonNull ShadowPart>();
		for (@NonNull Property keyProperty : ClassUtil.nullFree(rKey.getPart())) {
			FunctionParameter cParameter = helper.createFunctionParameter(keyProperty);
			asParameters.add(cParameter);
			ShadowPart asShadowPart = helper.createShadowPart(keyProperty, helper.createVariableExp(cParameter));
			asShadowParts.add(asShadowPart);
		}
		for (@NonNull Property keyOppositeProperty : ClassUtil.nullFree(rKey.getOppositePart())) {
			Property keyProperty = ClassUtil.nonNullState(keyOppositeProperty.getOpposite());
			FunctionParameter cParameter = helper.createFunctionParameter(keyProperty);
			asParameters.add(cParameter);
			ShadowPart asShadowPart = helper.createShadowPart(keyProperty, helper.createVariableExp(cParameter));
			asShadowParts.add(asShadowPart);
		}
		Collections.sort(asParameters, NameUtil.NAMEABLE_COMPARATOR);
		org.eclipse.ocl.pivot.@NonNull Class identifiedClass = ClassUtil.nonNullState(rKey.getIdentifies());
		Function cFunction = helper.createFunction(functionName, identifiedClass, true, asParameters);
		OCLExpression asShadowExp = helper.createShadowExp(identifiedClass, asShadowParts);
		cFunction.setQueryExpression(asShadowExp);
		return cFunction;
	}
}

Back to the top