001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.planning.plugin.completion;
028:
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.List;
032:
033: import org.cougaar.planning.ldm.plan.AllocationResult;
034: import org.cougaar.planning.ldm.plan.PlanElement;
035: import org.cougaar.planning.ldm.plan.Task;
036: import org.cougaar.util.UnaryPredicate;
037:
038: /**
039: */
040: public class CompletionCalculator {
041: protected static final double CONFIDENCE_THRESHHOLD = 0.89999;
042:
043: protected static final UnaryPredicate TASK_PRED = new UnaryPredicate() {
044: public boolean execute(Object o) {
045: return (o instanceof Task);
046: }
047: };
048:
049: protected UnaryPredicate pred;
050:
051: public UnaryPredicate getPredicate() {
052: if (pred == null) {
053: pred = createPredicate();
054: }
055: return pred;
056: }
057:
058: public double calculate(Collection c) {
059: int n = (c != null ? c.size() : 0);
060: if (n <= 0) {
061: return 1.0;
062: }
063: double sum = 0.0;
064: if (c instanceof List) {
065: List l = (List) c;
066: for (int i = 0; i < n; i++) {
067: Object o = l.get(i);
068: sum += getConfidence(o);
069: }
070: } else {
071: Iterator x = c.iterator();
072: for (int i = 0; i < n; i++) {
073: Object o = x.next();
074: sum += getConfidence(o);
075: }
076: }
077: return (sum / n);
078: }
079:
080: protected UnaryPredicate createPredicate() {
081: // need to count all tasks, even though we're only
082: // interested in the tasks with alloc results.
083: //
084: // If this is changed then the completion servlet
085: // must also be fixed! The servlet assumes that
086: // the basic "CompletionCalculator" predicate
087: // matches all tasks.
088: return TASK_PRED;
089: }
090:
091: protected double adjustConfRating(double confRating) {
092: return Math.min(confRating / CONFIDENCE_THRESHHOLD, 1.0);
093: }
094:
095: protected double getConfidence(Object o) {
096: if (o instanceof Task) {
097: Task task = (Task) o;
098: PlanElement pe = task.getPlanElement();
099: if (pe != null) {
100: AllocationResult ar = pe.getEstimatedResult();
101: if (ar != null) {
102: return adjustConfRating(ar.getConfidenceRating());
103: }
104: }
105: }
106: return 0.0;
107: }
108:
109: public boolean isConfident(double confRating) {
110: return adjustConfRating(confRating) >= 1.0;
111: }
112:
113: public String getConfidenceThreshholdString(boolean positive) {
114: if (positive) {
115: return "conf > " + CONFIDENCE_THRESHHOLD;
116: } else {
117: return "conf <= " + CONFIDENCE_THRESHHOLD;
118: }
119: }
120: }
|