Skip to main content
summaryrefslogtreecommitdiffstats
blob: 432d687faf853d1f28dbea2ee69e43835e94e7b6 (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
package org.eclipse.qvtd.cs2as.compiler.internal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.pivot.Class;
import org.eclipse.ocl.pivot.Element;
import org.eclipse.ocl.pivot.ExpressionInOCL;
import org.eclipse.ocl.pivot.Model;
import org.eclipse.ocl.pivot.NamedElement;
import org.eclipse.ocl.pivot.OCLExpression;
import org.eclipse.ocl.pivot.Package;
import org.eclipse.ocl.pivot.Property;
import org.eclipse.ocl.pivot.ShadowExp;
import org.eclipse.ocl.pivot.ShadowPart;
import org.eclipse.ocl.pivot.utilities.ClassUtil;

public class OCL2QVTpUtil {
	
	public static Function<EObject, @NonNull Stream<@NonNull EObject>> getAllContents() {
		return eObject -> {
			Iterable<EObject> it = () -> eObject.eAllContents();
			return StreamSupport.stream(it.spliterator(),false);
		};
	}
	
	public static Function<EObject, @NonNull Stream<@NonNull EObject>> getAllContentsIncludingSelf() {
		return eObject -> {
			List<EObject> allContents = new ArrayList<EObject>();
			for (Iterator<EObject> it = eObject.eAllContents(); it.hasNext(); ) {
				allContents.add(it.next());
			}
			allContents.add(eObject);
			return allContents.stream();
		};
	}
	
	public static Function<EObject, @NonNull Stream<@NonNull EObject>> getAllContainers() {
		return eObject -> {
			List<EObject> containers = new ArrayList<EObject>();
			for (EObject container = eObject.eContainer(); container != null; container = container.eContainer()) {
				containers.add(container);
			}
			return containers.stream();
		};
	}
	
	public static Function<ShadowExp, @NonNull Class> getExpressionContextType() {
		return shadowExp -> {
			return (Class) getContainingExpressionInOCL().apply(shadowExp)
					.getOwnedContext().getType();
		};
	}
	
	public static Function<OCLExpression, @NonNull ExpressionInOCL> getContainingExpressionInOCL() {
		return oclExp -> {
			EObject container = oclExp.eContainer();
			while (! (container instanceof ExpressionInOCL)) {
				container = container.eContainer();
			}
			assert(container != null); // Assuming all expression are contained in an ExpressionInOCL
			return (ExpressionInOCL)container;
		};
	}
	
	public static Function<Element, @Nullable Model> getModel() {
		return element -> {
			EObject eObject = element;
			while (eObject != null) {
				if (eObject instanceof Model) {
					return (Model)eObject;
				}
				eObject = eObject.eContainer();
			}
			return null;
		};
	}
	

	public static Function<@NonNull String, @NonNull String> firstToLowerCase() {
		return input -> {
			if (input.isEmpty()) {
				return input;
			}
			char c[] = input.toCharArray();
			c[0] = Character.toLowerCase(c[0]);
			return new String(c);
		};
	}
	
	public static Function<@NonNull String, @NonNull String> firstToUpperCase() {
		return input -> {
			if (input.isEmpty()) {
				return input;
			}
			char c[] = input.toCharArray();
			c[0] = Character.toUpperCase(c[0]);
			return new String(c);
		};
	}
	
	public static Function<@NonNull ShadowExp, @NonNull String> getCreationMappingName() { 
		return shadowExp -> {
			return "c" + getExpressionContextType().apply(shadowExp).getName() + "_2_" + getShadowTypeName().apply(shadowExp);
		};
	}
	
	public static Function<@NonNull ShadowPart, @NonNull String> getUpdateMappingName() {
		return shadowPart -> {
			ShadowExp shadowExp = (ShadowExp)shadowPart.eContainer();
		 	Property refProp = shadowPart.getReferredProperty();
			return 'u' + getExpressionContextType().apply(shadowExp).getName() + "_2_" 
		 			+ getShadowTypeName().apply(shadowExp) + '_' + refProp.getName(); 
		};
	}
	
	private static Function<@NonNull ShadowExp, @NonNull String> getShadowTypeName() {
		return shadowExp -> { 
			ExpressionInOCL expInOCL = getContainingExpressionInOCL().apply(shadowExp);
			List<ShadowExp> sameTypeShadowExps = getAllContents().apply(expInOCL)
				.filter(ShadowExp.class::isInstance)
				.map(ShadowExp.class::cast)
				.filter(x -> x.getType() == shadowExp.getType())
				.collect(Collectors.toList());
			if (sameTypeShadowExps.size() > 1) { // If there are more shadow exps returning the same type:
				return shadowExp.getType().getName() + "_" + sameTypeShadowExps.indexOf(shadowExp);
			} else {
				return shadowExp.getType().getName();
			}
		};
	}
	
	
	public static Function<@NonNull Class, @NonNull Package>  getOwningPackage() {
		return aClass -> {
			return aClass.getOwningPackage();
		};
	}
	
	public static Function<@NonNull Class, @NonNull String>  getOwningPackageName() {
		return aClass -> {
			return getName().apply(getOwningPackage().apply(aClass));
		};
	}
	
	private static Function<@NonNull NamedElement, @NonNull String> getName() {
		return namedElement -> { return namedElement == null ? "" : namedElement.getName(); };
	}
	
	public static Function<@NonNull Class, @NonNull Set<@NonNull Class>> getSuperClasses() {
		// FIXME ClassRelantionghip has cashes
		return aClass -> {
			Set<Class> result = new LinkedHashSet<Class>();
			for (Class superClass : ClassUtil.nullFree(aClass.getSuperClasses())) {
				result.add(superClass);
				result.addAll(getSuperClasses().apply(superClass));
			}
			return result;
		};
	}
}

Back to the top