Skip to main content
summaryrefslogtreecommitdiffstats
blob: 364cc14eb9b0626929d72db5d8297a83e903a95c (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
/*******************************************************************************
 * Copyright (c) 2014, 2015 The University of York and Willink Transformations.
 * 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:
 *     Horacio Hoyos - initial API and implementation
 ******************************************************************************/
package org.eclipse.qvtd.compiler.internal.qvtr2qvtc;

import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.ocl.pivot.CollectionType;
import org.eclipse.ocl.pivot.OCLExpression;
import org.eclipse.ocl.pivot.PivotFactory;
import org.eclipse.ocl.pivot.Type;
import org.eclipse.ocl.pivot.Variable;
import org.eclipse.ocl.pivot.utilities.ClassUtil;
import org.eclipse.qvtd.compiler.internal.utilities.CompilerUtil;
import org.eclipse.qvtd.pivot.qvtbase.Domain;
import org.eclipse.qvtd.pivot.qvtbase.Rule;
import org.eclipse.qvtd.pivot.qvtrelation.DomainPattern;
import org.eclipse.qvtd.pivot.qvtrelation.Relation;
import org.eclipse.qvtd.pivot.qvtrelation.RelationDomain;
import org.eclipse.qvtd.pivot.qvtrelation.RelationalTransformation;
import org.eclipse.qvtd.pivot.qvttemplate.CollectionTemplateExp;
import org.eclipse.qvtd.pivot.qvttemplate.ObjectTemplateExp;
import org.eclipse.qvtd.pivot.qvttemplate.PropertyTemplateItem;
import org.eclipse.qvtd.pivot.qvttemplate.TemplateExp;

/*public*/ class RelationalTransformationToTracePackage
{
	protected final @NonNull QVTrToQVTc qvtr2qvtc;

	public RelationalTransformationToTracePackage(@NonNull QVTrToQVTc qvtr2qvtc) {
		this.qvtr2qvtc = qvtr2qvtc;
	}

	public org.eclipse.ocl.pivot.@NonNull Package doRelationalTransformationToTracePackage(@NonNull RelationalTransformation rt) {
		org.eclipse.ocl.pivot.Package p = PivotFactory.eINSTANCE.createPackage();
		p.setName("P" + rt.getName());
		p.setNsPrefix("P" + rt.getName());
//		p.setURI(p.getName());
		qvtr2qvtc.putTracePackage(rt, p);
		List<org.eclipse.ocl.pivot.@NonNull Class> ownedClasses = ClassUtil.nullFree(p.getOwnedClasses());
		for (@NonNull Rule r : ClassUtil.nullFree(rt.getRule())) {
			if (r instanceof Relation) {
				ownedClasses.add(doRelationToTraceClass((Relation)r));
			}
		}
		CompilerUtil.normalizeNameables(ownedClasses);
		return p;
	}

	private org.eclipse.ocl.pivot.@NonNull Class doRelationToTraceClass(@NonNull Relation rRelation) {	
		@SuppressWarnings("null")org.eclipse.ocl.pivot.@NonNull Class traceClass = PivotFactory.eINSTANCE.createClass();
		qvtr2qvtc.putRelationTrace(rRelation, traceClass);
		traceClass.setName("T" + rRelation.getName());
		for (@NonNull Variable rVariable : qvtr2qvtc.getMiddleDomainVariables(rRelation))  {
			createTraceProperty(traceClass, rVariable);
		}
		for (@NonNull Domain rDomain : ClassUtil.nullFree(rRelation.getDomain())) {
			for (@NonNull DomainPattern rDomainPattern : ClassUtil.nullFree(((RelationDomain)rDomain).getPattern())) {
				TemplateExp rTemplateExp = ClassUtil.nonNullState(rDomainPattern.getTemplateExpression());
				doSubTemplateToTraceClassProps(rTemplateExp, traceClass);
			}
		}
		CompilerUtil.normalizeNameables(ClassUtil.nullFree(traceClass.getOwnedProperties()));
		return traceClass;
	}

	private void doSubTemplateToTraceClassProps(@NonNull TemplateExp te, org.eclipse.ocl.pivot.@NonNull Class rc) {
		Variable tv = ClassUtil.nonNullState(te.getBindsTo());
		if (te instanceof CollectionTemplateExp) {
			createTraceProperty(rc, tv);			// ?? not required for CollectionTemplateExp's
			CollectionTemplateExp cte = (CollectionTemplateExp) te;
			Variable collectionVariable = cte.getBindsTo();
			CollectionType collectionType = (CollectionType)collectionVariable.getType();
			Type elementType = ClassUtil.nonNullState(collectionType.getElementType());
			int argIndex = 0;
			for (@NonNull OCLExpression m : ClassUtil.nullFree(cte.getMember())) {
				if (m instanceof TemplateExp) {
					doSubTemplateToTraceClassProps((TemplateExp)m, rc);
				}
				else {
					createTraceProperty(rc, collectionVariable.getName() + "_" + argIndex, elementType);
				}
				argIndex++;
			}
			Variable rv = cte.getRest();
			if (rv != null) {
				createTraceProperty(rc, rv);
			}
		}
		else if (te instanceof ObjectTemplateExp) {
			createTraceProperty(rc, tv);			// ?? not required for CollectionTemplateExp's
			ObjectTemplateExp ote = (ObjectTemplateExp) te;
			for (@NonNull PropertyTemplateItem pt : ClassUtil.nullFree(ote.getPart())) {
				OCLExpression value = ClassUtil.nonNullState(pt.getValue());
				if (value instanceof TemplateExp) {
					doSubTemplateToTraceClassProps((TemplateExp)value, rc);
				}
			}
		}
	}

	private void createTraceProperty(org.eclipse.ocl.pivot.@NonNull Class rc, @NonNull Variable tv) {
		String vn = ClassUtil.nonNullState(tv.getName());
		Type c = ClassUtil.nonNullState(tv.getType());
		qvtr2qvtc.whenTraceProperty(rc, vn, c);
	}

	private void createTraceProperty(org.eclipse.ocl.pivot.@NonNull Class rc, @NonNull String name, @NonNull Type type) {
		qvtr2qvtc.whenTraceProperty(rc, name, type);
	}
}

Back to the top