-
Notifications
You must be signed in to change notification settings - Fork 37
/
InverseRelationShadowVariableDescriptor.java
182 lines (167 loc) · 9.38 KB
/
InverseRelationShadowVariableDescriptor.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
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
package org.optaplanner.core.impl.domain.variable.inverserelation;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.variable.AbstractVariableListener;
import org.optaplanner.core.api.domain.variable.InverseRelationShadowVariable;
import org.optaplanner.core.api.domain.variable.PlanningListVariable;
import org.optaplanner.core.api.domain.variable.PlanningVariable;
import org.optaplanner.core.api.domain.variable.PlanningVariableGraphType;
import org.optaplanner.core.config.util.ConfigUtils;
import org.optaplanner.core.impl.domain.common.accessor.MemberAccessor;
import org.optaplanner.core.impl.domain.entity.descriptor.EntityDescriptor;
import org.optaplanner.core.impl.domain.policy.DescriptorPolicy;
import org.optaplanner.core.impl.domain.variable.descriptor.GenuineVariableDescriptor;
import org.optaplanner.core.impl.domain.variable.descriptor.ListVariableDescriptor;
import org.optaplanner.core.impl.domain.variable.descriptor.ShadowVariableDescriptor;
import org.optaplanner.core.impl.domain.variable.descriptor.VariableDescriptor;
import org.optaplanner.core.impl.domain.variable.listener.VariableListenerWithSources;
import org.optaplanner.core.impl.domain.variable.supply.Demand;
import org.optaplanner.core.impl.domain.variable.supply.SupplyManager;
/**
* @param <Solution_> the solution type, the class with the {@link PlanningSolution} annotation
*/
public class InverseRelationShadowVariableDescriptor<Solution_> extends ShadowVariableDescriptor<Solution_> {
protected VariableDescriptor<Solution_> sourceVariableDescriptor;
protected boolean singleton;
protected boolean chained;
public InverseRelationShadowVariableDescriptor(EntityDescriptor<Solution_> entityDescriptor,
MemberAccessor variableMemberAccessor) {
super(entityDescriptor, variableMemberAccessor);
}
@Override
public void processAnnotations(DescriptorPolicy descriptorPolicy) {
// Do nothing
}
@Override
public void linkVariableDescriptors(DescriptorPolicy descriptorPolicy) {
linkShadowSources(descriptorPolicy);
}
/**
* Sourced on a basic genuine planning variable, the shadow type is a Collection (such as List or Set).
* Sourced on a list or chained planning variable, the shadow variable type is a single instance.
*
* @param descriptorPolicy descriptor policy
*/
private void linkShadowSources(DescriptorPolicy descriptorPolicy) {
InverseRelationShadowVariable shadowVariableAnnotation = variableMemberAccessor
.getAnnotation(InverseRelationShadowVariable.class);
Class<?> variablePropertyType = getVariablePropertyType();
Class<?> sourceClass;
if (Collection.class.isAssignableFrom(variablePropertyType)) {
Type genericType = variableMemberAccessor.getGenericType();
sourceClass = ConfigUtils.extractCollectionGenericTypeParameterLeniently(
"entityClass", entityDescriptor.getEntityClass(),
variablePropertyType, genericType,
InverseRelationShadowVariable.class, variableMemberAccessor.getName()).orElse(Object.class);
singleton = false;
} else {
sourceClass = variablePropertyType;
singleton = true;
}
EntityDescriptor<Solution_> sourceEntityDescriptor = getEntityDescriptor().getSolutionDescriptor()
.findEntityDescriptor(sourceClass);
if (sourceEntityDescriptor == null) {
throw new IllegalArgumentException("The entityClass (" + entityDescriptor.getEntityClass()
+ ") has an @" + InverseRelationShadowVariable.class.getSimpleName()
+ " annotated property (" + variableMemberAccessor.getName()
+ ") with a sourceClass (" + sourceClass
+ ") which is not a valid planning entity.");
}
String sourceVariableName = shadowVariableAnnotation.sourceVariableName();
// TODO can we getGenuineVariableDescriptor()?
sourceVariableDescriptor = sourceEntityDescriptor.getVariableDescriptor(sourceVariableName);
if (sourceVariableDescriptor == null) {
throw new IllegalArgumentException("The entityClass (" + entityDescriptor.getEntityClass()
+ ") has an @" + InverseRelationShadowVariable.class.getSimpleName()
+ " annotated property (" + variableMemberAccessor.getName()
+ ") with sourceVariableName (" + sourceVariableName
+ ") which is not a valid planning variable on entityClass ("
+ sourceEntityDescriptor.getEntityClass() + ").\n"
+ sourceEntityDescriptor.buildInvalidVariableNameExceptionMessage(sourceVariableName));
}
chained = (sourceVariableDescriptor instanceof GenuineVariableDescriptor) &&
((GenuineVariableDescriptor<Solution_>) sourceVariableDescriptor).isChained();
boolean list = (sourceVariableDescriptor instanceof GenuineVariableDescriptor) &&
((GenuineVariableDescriptor<Solution_>) sourceVariableDescriptor).isListVariable();
if (singleton) {
if (!chained && !list) {
throw new IllegalArgumentException("The entityClass (" + entityDescriptor.getEntityClass()
+ ") has an @" + InverseRelationShadowVariable.class.getSimpleName()
+ " annotated property (" + variableMemberAccessor.getName()
+ ") which does not return a " + Collection.class.getSimpleName()
+ " with sourceVariableName (" + sourceVariableName
+ ") which is neither a list variable @" + PlanningListVariable.class.getSimpleName()
+ " nor a chained variable @" + PlanningVariable.class.getSimpleName()
+ "(graphType=" + PlanningVariableGraphType.CHAINED + ")."
+ " Only list and chained variables support a singleton inverse.");
}
} else {
if (chained || list) {
throw new IllegalArgumentException("The entityClass (" + entityDescriptor.getEntityClass()
+ ") has an @" + InverseRelationShadowVariable.class.getSimpleName()
+ " annotated property (" + variableMemberAccessor.getName()
+ ") which returns a " + Collection.class.getSimpleName()
+ " (" + variablePropertyType
+ ") with sourceVariableName (" + sourceVariableName
+ ") which is a" + (chained
? " chained variable @" + PlanningVariable.class.getSimpleName()
+ "(graphType=" + PlanningVariableGraphType.CHAINED
+ "). A chained variable supports only a singleton inverse."
: " list variable @" + PlanningListVariable.class.getSimpleName()
+ ". A list variable supports only a singleton inverse."));
}
}
sourceVariableDescriptor.registerSinkVariableDescriptor(this);
}
@Override
public List<VariableDescriptor<Solution_>> getSourceVariableDescriptorList() {
return Collections.singletonList(sourceVariableDescriptor);
}
@Override
public Collection<Class<? extends AbstractVariableListener>> getVariableListenerClasses() {
if (singleton) {
if (chained) {
return Collections.singleton(SingletonInverseVariableListener.class);
} else {
return Collections.singleton(SingletonListInverseVariableListener.class);
}
} else {
return Collections.singleton(CollectionInverseVariableListener.class);
}
}
// ************************************************************************
// Worker methods
// ************************************************************************
@Override
public Demand<?> getProvidedDemand() {
if (singleton) {
if (chained) {
return new SingletonInverseVariableDemand<>(sourceVariableDescriptor);
} else {
return new SingletonListInverseVariableDemand<>(
(ListVariableDescriptor<Solution_>) sourceVariableDescriptor);
}
} else {
return new CollectionInverseVariableDemand<>(sourceVariableDescriptor);
}
}
@Override
public Iterable<VariableListenerWithSources<Solution_>> buildVariableListeners(SupplyManager supplyManager) {
return new VariableListenerWithSources<>(buildVariableListener(), sourceVariableDescriptor).toCollection();
}
private AbstractVariableListener<Solution_, Object> buildVariableListener() {
if (singleton) {
if (chained) {
return new SingletonInverseVariableListener<>(this, sourceVariableDescriptor);
} else {
return new SingletonListInverseVariableListener<>(
this, (ListVariableDescriptor<Solution_>) sourceVariableDescriptor);
}
} else {
return new CollectionInverseVariableListener<>(this, sourceVariableDescriptor);
}
}
}