1
1
package ai .timefold .solver .core .impl ;
2
2
3
- import java .util .Collection ;
4
3
import java .util .List ;
5
4
import java .util .Objects ;
6
5
@@ -24,8 +23,8 @@ protected AbstractFromConfigFactory(Config_ config) {
24
23
25
24
public static <Solution_ > EntitySelectorConfig getDefaultEntitySelectorConfigForEntity (
26
25
HeuristicConfigPolicy <Solution_ > configPolicy , EntityDescriptor <Solution_ > entityDescriptor ) {
27
- Class <?> entityClass = entityDescriptor .getEntityClass ();
28
- EntitySelectorConfig entitySelectorConfig = new EntitySelectorConfig ()
26
+ var entityClass = entityDescriptor .getEntityClass ();
27
+ var entitySelectorConfig = new EntitySelectorConfig ()
29
28
.withId (entityClass .getName ())
30
29
.withEntityClass (entityClass );
31
30
return deduceEntitySortManner (configPolicy , entityDescriptor , entitySelectorConfig );
@@ -44,15 +43,15 @@ public static <Solution_> EntitySelectorConfig deduceEntitySortManner(HeuristicC
44
43
45
44
protected EntityDescriptor <Solution_ > deduceEntityDescriptor (HeuristicConfigPolicy <Solution_ > configPolicy ,
46
45
Class <?> entityClass ) {
47
- SolutionDescriptor < Solution_ > solutionDescriptor = configPolicy .getSolutionDescriptor ();
46
+ var solutionDescriptor = configPolicy .getSolutionDescriptor ();
48
47
return entityClass == null
49
48
? getTheOnlyEntityDescriptor (solutionDescriptor )
50
49
: getEntityDescriptorForClass (solutionDescriptor , entityClass );
51
50
}
52
51
53
52
private EntityDescriptor <Solution_ > getEntityDescriptorForClass (SolutionDescriptor <Solution_ > solutionDescriptor ,
54
53
Class <?> entityClass ) {
55
- EntityDescriptor < Solution_ > entityDescriptor = solutionDescriptor .getEntityDescriptorStrict (entityClass );
54
+ var entityDescriptor = solutionDescriptor .getEntityDescriptorStrict (entityClass );
56
55
if (entityDescriptor == null ) {
57
56
throw new IllegalArgumentException (
58
57
"""
@@ -65,7 +64,7 @@ Check your solver configuration. If that class (%s) is not in the entityClassSet
65
64
}
66
65
67
66
protected EntityDescriptor <Solution_ > getTheOnlyEntityDescriptor (SolutionDescriptor <Solution_ > solutionDescriptor ) {
68
- Collection < EntityDescriptor < Solution_ >> entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ();
67
+ var entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ();
69
68
if (entityDescriptors .size () != 1 ) {
70
69
throw new IllegalArgumentException (
71
70
"The config (%s) has no entityClass configured and because there are multiple in the entityClassSet (%s), it cannot be deduced automatically."
@@ -76,7 +75,7 @@ protected EntityDescriptor<Solution_> getTheOnlyEntityDescriptor(SolutionDescrip
76
75
77
76
protected EntityDescriptor <Solution_ >
78
77
getTheOnlyEntityDescriptorWithBasicVariables (SolutionDescriptor <Solution_ > solutionDescriptor ) {
79
- Collection < EntityDescriptor < Solution_ >> entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ()
78
+ var entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ()
80
79
.stream ()
81
80
.filter (EntityDescriptor ::hasAnyGenuineBasicVariables )
82
81
.toList ();
@@ -88,6 +87,20 @@ protected EntityDescriptor<Solution_> getTheOnlyEntityDescriptor(SolutionDescrip
88
87
return entityDescriptors .iterator ().next ();
89
88
}
90
89
90
+ protected EntityDescriptor <Solution_ >
91
+ getTheOnlyEntityDescriptorWithListVariable (SolutionDescriptor <Solution_ > solutionDescriptor ) {
92
+ var entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ()
93
+ .stream ()
94
+ .filter (EntityDescriptor ::hasAnyGenuineListVariables )
95
+ .toList ();
96
+ if (entityDescriptors .size () != 1 ) {
97
+ throw new IllegalArgumentException (
98
+ "Impossible state: the config (%s) has no entityClass configured and because there are multiple in the entityClassSet (%s), it cannot be deduced automatically."
99
+ .formatted (config , solutionDescriptor .getEntityClassSet ()));
100
+ }
101
+ return entityDescriptors .iterator ().next ();
102
+ }
103
+
91
104
protected GenuineVariableDescriptor <Solution_ > deduceGenuineVariableDescriptor (EntityDescriptor <Solution_ > entityDescriptor ,
92
105
String variableName ) {
93
106
return variableName == null
@@ -97,7 +110,7 @@ protected GenuineVariableDescriptor<Solution_> deduceGenuineVariableDescriptor(E
97
110
98
111
protected GenuineVariableDescriptor <Solution_ > getVariableDescriptorForName (EntityDescriptor <Solution_ > entityDescriptor ,
99
112
String variableName ) {
100
- GenuineVariableDescriptor < Solution_ > variableDescriptor = entityDescriptor .getGenuineVariableDescriptor (variableName );
113
+ var variableDescriptor = entityDescriptor .getGenuineVariableDescriptor (variableName );
101
114
if (variableDescriptor == null ) {
102
115
throw new IllegalArgumentException (
103
116
"""
@@ -109,8 +122,7 @@ The config (%s) has a variableName (%s) which is not a valid planning variable o
109
122
}
110
123
111
124
protected GenuineVariableDescriptor <Solution_ > getTheOnlyVariableDescriptor (EntityDescriptor <Solution_ > entityDescriptor ) {
112
- List <GenuineVariableDescriptor <Solution_ >> variableDescriptorList =
113
- entityDescriptor .getGenuineVariableDescriptorList ();
125
+ var variableDescriptorList = entityDescriptor .getGenuineVariableDescriptorList ();
114
126
if (variableDescriptorList .size () != 1 ) {
115
127
throw new IllegalArgumentException (
116
128
"The config (%s) has no configured variableName for entityClass (%s) and because there are multiple variableNames (%s), it cannot be deduced automatically."
@@ -123,8 +135,26 @@ protected GenuineVariableDescriptor<Solution_> getTheOnlyVariableDescriptor(Enti
123
135
protected List <GenuineVariableDescriptor <Solution_ >> deduceVariableDescriptorList (
124
136
EntityDescriptor <Solution_ > entityDescriptor , List <String > variableNameIncludeList ) {
125
137
Objects .requireNonNull (entityDescriptor );
126
- List <GenuineVariableDescriptor <Solution_ >> variableDescriptorList =
127
- entityDescriptor .getGenuineVariableDescriptorList ();
138
+ var variableDescriptorList = entityDescriptor .getGenuineVariableDescriptorList ();
139
+ if (variableNameIncludeList == null ) {
140
+ return variableDescriptorList ;
141
+ }
142
+
143
+ return variableNameIncludeList .stream ()
144
+ .map (variableNameInclude -> variableDescriptorList .stream ()
145
+ .filter (variableDescriptor -> variableDescriptor .getVariableName ().equals (variableNameInclude ))
146
+ .findFirst ()
147
+ .orElseThrow (() -> new IllegalArgumentException (
148
+ "The config (%s) has a variableNameInclude (%s) which does not exist in the entity (%s)'s variableDescriptorList (%s)."
149
+ .formatted (config , variableNameInclude , entityDescriptor .getEntityClass (),
150
+ variableDescriptorList ))))
151
+ .toList ();
152
+ }
153
+
154
+ protected List <GenuineVariableDescriptor <Solution_ >> deduceBasicVariableDescriptorList (
155
+ EntityDescriptor <Solution_ > entityDescriptor , List <String > variableNameIncludeList ) {
156
+ Objects .requireNonNull (entityDescriptor );
157
+ var variableDescriptorList = entityDescriptor .getGenuineBasicVariableDescriptorList ();
128
158
if (variableNameIncludeList == null ) {
129
159
return variableDescriptorList ;
130
160
}
0 commit comments