-
Notifications
You must be signed in to change notification settings - Fork 3
/
IntersectionService.java
120 lines (108 loc) · 4.23 KB
/
IntersectionService.java
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
/*******************************************************************************
* Copyright (c) 2021, 2023 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.services.collection;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
import org.eclipse.acceleo.query.validation.type.ICollectionType;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.acceleo.query.validation.type.NothingType;
/**
* Intersection {@link IService}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class IntersectionService extends AbstractCollectionService {
/**
* Constructor.
*
* @param serviceMethod
* the method that realizes the service
* @param serviceInstance
* the instance on which the service must be called
* @param forWorkspace
* tells if the {@link IService} will be used in a workspace
*/
public IntersectionService(Method serviceMethod, Object serviceInstance, boolean forWorkspace) {
super(serviceMethod, serviceInstance, forWorkspace);
}
@Override
public Set<IType> getType(Call call, ValidationServices services, IValidationResult validationResult,
IReadOnlyQueryEnvironment queryEnvironment, List<IType> argTypes) {
final Set<IType> result = new LinkedHashSet<IType>();
final IType arg1Type;
if (argTypes.get(0) instanceof ICollectionType) {
arg1Type = ((ICollectionType)argTypes.get(0)).getCollectionType();
} else if (argTypes.get(0) instanceof NothingType) {
arg1Type = argTypes.get(0);
} else {
arg1Type = services.nothing(
"%s can only be called on collections, but %s was used as its receiver.", getName(),
argTypes.get(0));
}
final IType arg2Type;
if (argTypes.get(1) instanceof ICollectionType) {
arg2Type = ((ICollectionType)argTypes.get(1)).getCollectionType();
} else if (argTypes.get(1) instanceof NothingType) {
arg2Type = argTypes.get(1);
} else {
arg2Type = services.nothing(
"%s can only be called on collections, but %s was used as its argument.", getName(),
argTypes.get(1));
}
final Set<IType> resultRawTypes = services.intersection(arg1Type, arg2Type);
if (resultRawTypes.isEmpty()) {
if (arg1Type instanceof NothingType) {
resultRawTypes.add(arg1Type);
}
if (arg2Type instanceof NothingType) {
resultRawTypes.add(arg2Type);
}
if (resultRawTypes.isEmpty()) {
resultRawTypes.add(services.nothing("Nothing left after intersection of %s and %s", argTypes
.get(0), argTypes.get(1)));
}
}
for (IType resultRawType : resultRawTypes) {
result.add(createReturnCollectionWithType(queryEnvironment, resultRawType));
}
return result;
}
@Override
public Set<IType> validateAllType(ValidationServices services, IReadOnlyQueryEnvironment queryEnvironment,
Map<List<IType>, Set<IType>> allTypes) {
final Set<IType> result = new LinkedHashSet<IType>();
final StringBuilder builder = new StringBuilder();
for (Entry<List<IType>, Set<IType>> entry : allTypes.entrySet()) {
for (IType type : entry.getValue()) {
if (((ICollectionType)type).getCollectionType() instanceof NothingType) {
builder.append(MESSAGE_SEPARATOR);
builder.append(((NothingType)((ICollectionType)type).getCollectionType()).getMessage());
} else {
result.add(type);
}
}
}
if (result.isEmpty()) {
IType nothing = services.nothing("Nothing left after intersection:" + builder.toString());
result.add(createReturnCollectionWithType(queryEnvironment, nothing));
}
return result;
}
}