org.drools.planner.core.domain.entity.PlanningEntityDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of drools-planner-core Show documentation
Show all versions of drools-planner-core Show documentation
Drools Planner optimizes automated planning by combining metaheuristic search algorithms with rule
engine powered score calculation. This is the drools-planner-core module which contains metaheuristic
algorithms.
/*
* Copyright 2011 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.planner.core.domain.entity;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.drools.planner.api.domain.entity.PlanningEntity;
import org.drools.planner.api.domain.entity.PlanningEntityDifficultyWeightFactory;
import org.drools.planner.api.domain.variable.DependentPlanningVariable;
import org.drools.planner.api.domain.variable.PlanningVariable;
import org.drools.planner.core.domain.solution.SolutionDescriptor;
import org.drools.planner.core.domain.variable.DependentPlanningVariableDescriptor;
import org.drools.planner.core.domain.variable.PlanningVariableDescriptor;
import org.drools.planner.core.solution.Solution;
public class PlanningEntityDescriptor {
private final SolutionDescriptor solutionDescriptor;
private final Class planningEntityClass;
private final BeanInfo planningEntityBeanInfo;
private PlanningEntitySorter planningEntitySorter;
private Map planningVariableDescriptorMap;
private Map dependentPlanningVariableDescriptorMap;
public PlanningEntityDescriptor(SolutionDescriptor solutionDescriptor, Class planningEntityClass) {
this.solutionDescriptor = solutionDescriptor;
this.planningEntityClass = planningEntityClass;
try {
planningEntityBeanInfo = Introspector.getBeanInfo(planningEntityClass);
} catch (IntrospectionException e) {
throw new IllegalStateException("The planningEntityClass (" + planningEntityClass
+ ") is not a valid java bean.", e);
}
}
public void processAnnotations() {
processEntityAnnotations();
processPropertyAnnotations();
}
private void processEntityAnnotations() {
PlanningEntity planningEntityAnnotation = planningEntityClass.getAnnotation(PlanningEntity.class);
if (planningEntityAnnotation == null) {
throw new IllegalStateException("The planningEntityClass (" + planningEntityClass
+ ") has been specified as a planning entity in the configuration," +
" but does not have a PlanningEntity annotation.");
}
planningEntitySorter = new PlanningEntitySorter();
processDifficulty(planningEntityAnnotation);
}
private void processDifficulty(PlanningEntity planningEntityAnnotation) {
Class difficultyComparatorClass = planningEntityAnnotation.difficultyComparatorClass();
if (difficultyComparatorClass == PlanningEntity.NullDifficultyComparator.class) {
difficultyComparatorClass = null;
}
Class difficultyWeightFactoryClass
= planningEntityAnnotation.difficultyWeightFactoryClass();
if (difficultyWeightFactoryClass == PlanningEntity.NullDifficultyWeightFactory.class) {
difficultyWeightFactoryClass = null;
}
if (difficultyComparatorClass != null && difficultyWeightFactoryClass != null) {
throw new IllegalStateException("The planningEntityClass (" + planningEntityClass
+ ") cannot have a difficultyComparatorClass (" + difficultyComparatorClass.getName()
+ ") and a difficultyWeightFactoryClass (" + difficultyWeightFactoryClass.getName()
+ ") at the same time.");
}
if (difficultyComparatorClass != null) {
Comparator