11package ai .timefold .solver .core .impl ;
22
3- import java .util .Collection ;
43import java .util .List ;
54import java .util .Objects ;
65
@@ -24,8 +23,8 @@ protected AbstractFromConfigFactory(Config_ config) {
2423
2524 public static <Solution_ > EntitySelectorConfig getDefaultEntitySelectorConfigForEntity (
2625 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 ()
2928 .withId (entityClass .getName ())
3029 .withEntityClass (entityClass );
3130 return deduceEntitySortManner (configPolicy , entityDescriptor , entitySelectorConfig );
@@ -44,15 +43,15 @@ public static <Solution_> EntitySelectorConfig deduceEntitySortManner(HeuristicC
4443
4544 protected EntityDescriptor <Solution_ > deduceEntityDescriptor (HeuristicConfigPolicy <Solution_ > configPolicy ,
4645 Class <?> entityClass ) {
47- SolutionDescriptor < Solution_ > solutionDescriptor = configPolicy .getSolutionDescriptor ();
46+ var solutionDescriptor = configPolicy .getSolutionDescriptor ();
4847 return entityClass == null
4948 ? getTheOnlyEntityDescriptor (solutionDescriptor )
5049 : getEntityDescriptorForClass (solutionDescriptor , entityClass );
5150 }
5251
5352 private EntityDescriptor <Solution_ > getEntityDescriptorForClass (SolutionDescriptor <Solution_ > solutionDescriptor ,
5453 Class <?> entityClass ) {
55- EntityDescriptor < Solution_ > entityDescriptor = solutionDescriptor .getEntityDescriptorStrict (entityClass );
54+ var entityDescriptor = solutionDescriptor .getEntityDescriptorStrict (entityClass );
5655 if (entityDescriptor == null ) {
5756 throw new IllegalArgumentException (
5857 """
@@ -65,7 +64,7 @@ Check your solver configuration. If that class (%s) is not in the entityClassSet
6564 }
6665
6766 protected EntityDescriptor <Solution_ > getTheOnlyEntityDescriptor (SolutionDescriptor <Solution_ > solutionDescriptor ) {
68- Collection < EntityDescriptor < Solution_ >> entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ();
67+ var entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ();
6968 if (entityDescriptors .size () != 1 ) {
7069 throw new IllegalArgumentException (
7170 "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
7675
7776 protected EntityDescriptor <Solution_ >
7877 getTheOnlyEntityDescriptorWithBasicVariables (SolutionDescriptor <Solution_ > solutionDescriptor ) {
79- Collection < EntityDescriptor < Solution_ >> entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ()
78+ var entityDescriptors = solutionDescriptor .getGenuineEntityDescriptors ()
8079 .stream ()
8180 .filter (EntityDescriptor ::hasAnyGenuineBasicVariables )
8281 .toList ();
@@ -88,6 +87,20 @@ protected EntityDescriptor<Solution_> getTheOnlyEntityDescriptor(SolutionDescrip
8887 return entityDescriptors .iterator ().next ();
8988 }
9089
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+
91104 protected GenuineVariableDescriptor <Solution_ > deduceGenuineVariableDescriptor (EntityDescriptor <Solution_ > entityDescriptor ,
92105 String variableName ) {
93106 return variableName == null
@@ -97,7 +110,7 @@ protected GenuineVariableDescriptor<Solution_> deduceGenuineVariableDescriptor(E
97110
98111 protected GenuineVariableDescriptor <Solution_ > getVariableDescriptorForName (EntityDescriptor <Solution_ > entityDescriptor ,
99112 String variableName ) {
100- GenuineVariableDescriptor < Solution_ > variableDescriptor = entityDescriptor .getGenuineVariableDescriptor (variableName );
113+ var variableDescriptor = entityDescriptor .getGenuineVariableDescriptor (variableName );
101114 if (variableDescriptor == null ) {
102115 throw new IllegalArgumentException (
103116 """
@@ -109,8 +122,7 @@ The config (%s) has a variableName (%s) which is not a valid planning variable o
109122 }
110123
111124 protected GenuineVariableDescriptor <Solution_ > getTheOnlyVariableDescriptor (EntityDescriptor <Solution_ > entityDescriptor ) {
112- List <GenuineVariableDescriptor <Solution_ >> variableDescriptorList =
113- entityDescriptor .getGenuineVariableDescriptorList ();
125+ var variableDescriptorList = entityDescriptor .getGenuineVariableDescriptorList ();
114126 if (variableDescriptorList .size () != 1 ) {
115127 throw new IllegalArgumentException (
116128 "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
123135 protected List <GenuineVariableDescriptor <Solution_ >> deduceVariableDescriptorList (
124136 EntityDescriptor <Solution_ > entityDescriptor , List <String > variableNameIncludeList ) {
125137 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 ();
128158 if (variableNameIncludeList == null ) {
129159 return variableDescriptorList ;
130160 }
0 commit comments