Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.config.solver;
 
 import java.util.List;
 import java.util.Set;
 
 
 @XStreamAlias("solver")
 public class SolverConfig {
 
     protected static final long DEFAULT_RANDOM_SEED = 0L;
 
     // Warning: all fields are null (and not defaulted) because they can be inherited
     // and also because the input config file should match the output config file
 
     protected EnvironmentMode environmentMode = null;
     protected Long randomSeed = null;
 
     protected Class<? extends SolutionsolutionClass = null;
     @XStreamImplicit(itemFieldName = "planningEntityClass")
     protected Set<Class<?>> planningEntityClassSet = null;
 
     protected RuleBase ruleBase = null;
     @XStreamImplicit(itemFieldName = "scoreDrl")
     protected List<StringscoreDrlList = null;
     @XStreamAlias("scoreDefinition")
 
     @XStreamAlias("termination")
 
     @XStreamImplicit()
     protected List<SolverPhaseConfigsolverPhaseConfigList = null;
 
     public EnvironmentMode getEnvironmentMode() {
         return ;
     }
 
     public void setEnvironmentMode(EnvironmentMode environmentMode) {
         this. = environmentMode;
     }
 
     public Long getRandomSeed() {
         return ;
     }
 
     public void setRandomSeed(Long randomSeed) {
         this. = randomSeed;
     }
 
     public Class<? extends SolutiongetSolutionClass() {
         return ;
     }
 
    public void setSolutionClass(Class<? extends SolutionsolutionClass) {
        this. = solutionClass;
    }
    public Set<Class<?>> getPlanningEntityClassSet() {
        return ;
    }
    public void setPlanningEntityClassSet(Set<Class<?>> planningEntityClassSet) {
        this. = planningEntityClassSet;
    }
    public RuleBase getRuleBase() {
        return ;
    }
    public void setRuleBase(RuleBase ruleBase) {
        this. = ruleBase;
    }
    public List<StringgetScoreDrlList() {
        return ;
    }
    public void setScoreDrlList(List<StringscoreDrlList) {
        this. = scoreDrlList;
    }
        return ;
    }
    public void setScoreDefinitionConfig(ScoreDefinitionConfig scoreDefinitionConfig) {
        this. = scoreDefinitionConfig;
    }
        return ;
    }
    public void setTerminationConfig(TerminationConfig terminationConfig) {
        this. = terminationConfig;
    }
        return ;
    }
    public void setSolverPhaseConfigList(List<SolverPhaseConfigsolverPhaseConfigList) {
        this. = solverPhaseConfigList;
    }
    // ************************************************************************
    // Builder methods
    // ************************************************************************
    public Solver buildSolver() {
        DefaultSolver solver = new DefaultSolver();
        BasicPlumbingTermination basicPlumbingTermination = new BasicPlumbingTermination();
        solver.setBasicPlumbingTermination(basicPlumbingTermination);
        if ( != .) {
            if ( != null) {
                solver.setRandomSeed();
            } else {
                solver.setRandomSeed();
            }
        }
        SolutionDescriptor solutionDescriptor = buildSolutionDescriptor();
        DefaultSolutionDirector solutionDirector = new DefaultSolutionDirector();
        solutionDirector.setSolutionDescriptor(solutionDescriptor);
        solutionDirector.setRuleBase(buildRuleBase());
        ScoreDefinition scoreDefinition = .buildScoreDefinition();
        solutionDirector.setScoreDefinition(scoreDefinition);
        solver.setSolutionDirector(solutionDirector);
        Termination termination = .buildTermination(scoreDefinitionbasicPlumbingTermination);
        solver.setTermination(termination);
        BestSolutionRecaller bestSolutionRecaller = new BestSolutionRecaller();
        solver.setBestSolutionRecaller(bestSolutionRecaller);
        if ( == null || .isEmpty()) {
            throw new IllegalArgumentException(
                    "Configure at least 1 phase (for example <localSearch>) in the solver configuration.");
        }
        List<SolverPhasesolverPhaseList = new ArrayList<SolverPhase>(.size());
        for (SolverPhaseConfig solverPhaseConfig : ) {
            SolverPhase solverPhase = solverPhaseConfig.buildSolverPhase(,
                    solutionDescriptorscoreDefinitiontermination);
            ((AbstractSolverPhasesolverPhase).setBestSolutionRecaller(bestSolutionRecaller);
            solverPhaseList.add(solverPhase);
        }
        solver.setSolverPhaseList(solverPhaseList);
        return solver;
    }
        if ( == null) {
            throw new IllegalArgumentException("Configure a <solutionClass> in the solver configuration.");
        }
        SolutionDescriptor solutionDescriptor = new SolutionDescriptor();
        if ( == null || .isEmpty()) {
            throw new IllegalArgumentException(
                    "Configure at least 1 <planningEntityClass> in the solver configuration.");
        }
        for (Class<?> planningEntityClass : ) {
            PlanningEntityDescriptor planningEntityDescriptor = new PlanningEntityDescriptor(
                    solutionDescriptorplanningEntityClass);
            solutionDescriptor.addPlanningEntityDescriptor(planningEntityDescriptor);
        }
        return solutionDescriptor;
    }
    private RuleBase buildRuleBase() {
        if ( != null) {
            if ( != null && !.isEmpty()) {
                throw new IllegalArgumentException("If ruleBase is not null, the scoreDrlList (" + 
                        + ") must be empty.");
            }
            return ;
        } else {
            PackageBuilder packageBuilder = new PackageBuilder();
            for (String scoreDrl : ) {
                InputStream scoreDrlIn = getClass().getResourceAsStream(scoreDrl);
                if (scoreDrlIn == null) {
                    throw new IllegalArgumentException("scoreDrl (" + scoreDrl + ") does not exist as a classpath resource.");
                }
                try {
                    packageBuilder.addPackageFromDrl(new InputStreamReader(scoreDrlIn"utf-8"));
                } catch (DroolsParserException e) {
                    throw new IllegalArgumentException("scoreDrl (" + scoreDrl + ") could not be loaded."e);
                } catch (IOException e) {
                    throw new IllegalArgumentException("scoreDrl (" + scoreDrl + ") could not be loaded."e);
                } finally {
                    IOUtils.closeQuietly(scoreDrlIn);
                }
            }
            RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration();
            RuleBase ruleBase = RuleBaseFactory.newRuleBase(ruleBaseConfiguration);
            if (packageBuilder.hasErrors()) {
                throw new IllegalStateException("There are errors in the scoreDrl's:"
                        + packageBuilder.getErrors().toString());
            }
            ruleBase.addPackage(packageBuilder.getPackage());
            return ruleBase;
        }
    }
    public void inherit(SolverConfig inheritedConfig) {
        if ( == null) {
             = inheritedConfig.getEnvironmentMode();
        }
        if ( == null) {
             = inheritedConfig.getRandomSeed();
        }
        if ( == null) {
             = inheritedConfig.getSolutionClass();
        }
        if ( == null) {
             = inheritedConfig.getPlanningEntityClassSet();
        } else if (inheritedConfig.getPlanningEntityClassSet() != null) {
            .addAll(inheritedConfig.getPlanningEntityClassSet());
        }
        if ( == null) {
             = inheritedConfig.getRuleBase();
        }
        if ( == null) {
             = inheritedConfig.getScoreDrlList();
        } else {
            List<StringinheritedScoreDrlList = inheritedConfig.getScoreDrlList();
            if (inheritedScoreDrlList != null) {
                for (String inheritedScoreDrl : inheritedScoreDrlList) {
                    if (!.contains(inheritedScoreDrl)) {
                        .add(inheritedScoreDrl);
                    }
                }
            }
        }
        if ( == null) {
             = inheritedConfig.getScoreDefinitionConfig();
        } else if (inheritedConfig.getScoreDefinitionConfig() != null) {
            .inherit(inheritedConfig.getScoreDefinitionConfig());
        }
        if ( == null) {
             = inheritedConfig.getTerminationConfig();
        } else if (inheritedConfig.getTerminationConfig() != null) {
            .inherit(inheritedConfig.getTerminationConfig());
        }
        if ( == null) {
             = inheritedConfig.getSolverPhaseConfigList();
        } else if (inheritedConfig.getSolverPhaseConfigList() != null) {
            // The inherited solverPhaseConfigList should be before the non-inherited one
            List<SolverPhaseConfigmergedList = new ArrayList<SolverPhaseConfig>(
                    inheritedConfig.getSolverPhaseConfigList());
            mergedList.addAll();
             = mergedList;
        }
    }
New to GrepCode? Check out our FAQ X