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.core.solution.director;
 
 import java.util.List;
 import java.util.Map;
 
Default implementation for SolutionDirector.

 
 public class DefaultSolutionDirector implements SolutionDirector {
 
     public static final String GLOBAL_SCORE_CALCULATOR_KEY = "scoreCalculator";
 
     protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
 
     protected RuleBase ruleBase;
     protected ScoreDefinition scoreDefinition;
 
     protected Solution workingSolution;
     protected StatefulSession workingMemory;
 
     protected long calculateCount;
 
         return ;
     }
 
     public void setSolutionDescriptor(SolutionDescriptor solutionDescriptor) {
         this. = solutionDescriptor;
     }
 
     public RuleBase getRuleBase() {
         return ;
     }
 
     public void setRuleBase(RuleBase ruleBase) {
         this. = ruleBase;
     }
 
     public ScoreDefinition getScoreDefinition() {
         return ;
     }
 
     public void setScoreDefinition(ScoreDefinition scoreDefinition) {
         this. = scoreDefinition;
          = scoreDefinition.buildScoreCalculator();
     }
 
     public Solution getWorkingSolution() {
         return ;
     }
 
     public void setWorkingSolution(Solution workingSolution) {
         this. = workingSolution;
         resetWorkingMemory();
     }
 
     public WorkingMemory getWorkingMemory() {
         return ;
     }
 
    public long getCalculateCount() {
        return ;
    }
    // ************************************************************************
    // Calculated methods
    // ************************************************************************
    public void resetCalculateCount() {
         = 0L;
    }
    private void resetWorkingMemory() {
        if ( != null) {
            .dispose();
        }
        for (Object fact : getWorkingFacts()) {
            .insert(fact);
        }
    }
    public Collection<ObjectgetWorkingFacts() {
    }
    }
    public boolean isWorkingSolutionInitialized() {
    }
        .fireAllRules();
        Score score = .calculateScore();
        .setScore(score);
        ++;
        return score;
    }

    

Parameters:
presumedScore never null
    public void assertWorkingScore(Score presumedScore) {
        StatefulSession tmpWorkingMemory = .newStatefulSession();
        ScoreCalculator tmpScoreCalculator = .clone();
        tmpWorkingMemory.setGlobal(tmpScoreCalculator);
        for (Object fact : getWorkingFacts()) {
            tmpWorkingMemory.insert(fact);
        }
        tmpWorkingMemory.fireAllRules();
        Score realScore = tmpScoreCalculator.calculateScore();
        tmpWorkingMemory.dispose();
        if (!presumedScore.equals(realScore)) {
            throw new IllegalStateException(
                    "The presumedScore (" + presumedScore + ") is corrupted because it is not the realScore  ("
                            + realScore + ").\n"
                            + "Presumed workingMemory:\n" + buildConstraintOccurrenceSummary()
                            + "Real workingMemory:\n" + buildConstraintOccurrenceSummary(tmpWorkingMemory));
        }
    }

    
    }

    
TODO Refactor this with the ConstraintOccurrenceTotal class: https://jira.jboss.org/jira/browse/JBRULES-2510

Parameters:
summaryWorkingMemory sometimes null
Returns:
never null
    public String buildConstraintOccurrenceSummary(WorkingMemory summaryWorkingMemory) {
        .trace("Building ConstraintOccurrence summary");
        if (summaryWorkingMemory == null) {
            return "  The workingMemory is null.";
        }
        Map<StringSummaryLinesummaryLineMap = new TreeMap<StringSummaryLine>();
        Iterator<ConstraintOccurrenceit = (Iterator<ConstraintOccurrence>) summaryWorkingMemory.iterateObjects(
                new ClassObjectFilter(ConstraintOccurrence.class));
        while (it.hasNext()) {
            ConstraintOccurrence occurrence = it.next();
            .trace("    Adding ConstraintOccurrence ({})"occurrence);
            SummaryLine summaryLine = summaryLineMap.get(occurrence.getRuleId());
            if (summaryLine == null) {
                summaryLine = new SummaryLine();
                summaryLineMap.put(occurrence.getRuleId(), summaryLine);
            }
            summaryLine.increment();
            if (occurrence instanceof IntConstraintOccurrence) {
                summaryLine.addWeight(((IntConstraintOccurrenceoccurrence).getWeight());
            } else if (occurrence instanceof DoubleConstraintOccurrence) {
                summaryLine.addWeight(((DoubleConstraintOccurrenceoccurrence).getWeight());
            } else if (occurrence instanceof UnweightedConstraintOccurrence) {
                summaryLine.addWeight(1);
            } else {
                throw new IllegalStateException("Cannot determine occurrenceScore of ConstraintOccurrence class: "
                        + occurrence.getClass());
            }
        }
        StringBuilder summary = new StringBuilder();
        for (Map.Entry<StringSummaryLinesummaryLineEntry : summaryLineMap.entrySet()) {
            SummaryLine summaryLine = summaryLineEntry.getValue();
            summary.append("  Score rule (").append(summaryLineEntry.getKey()).append(") has count (")
                    .append(summaryLine.getCount()).append(") and weight total (")
                    .append(summaryLine.getWeightTotal()).append(").\n");
        }
        return summary.toString();
    }
    private static class SummaryLine {
        private int count = 0;
        private Number weightTotal = null;
        public int getCount() {
            return ;
        }
        public Number getWeightTotal() {
            return ;
        }
        public void increment() {
            ++;
        }
        public void addWeight(Integer weight) {
            if ( == null) {
                 = 0;
            }
             = ((Integer) + weight;
        }
        public void addWeight(Double weight) {
            if ( == null) {
                 = 0.0;
            }
             = ((Double) + weight;
        }
    }
New to GrepCode? Check out our FAQ X