Explanation.java
001 package gate.creole.annic.apache.lucene.search;
002 
003 /**
004  * Copyright 2004 The Apache Software Foundation
005  *
006  * Licensed under the Apache License, Version 2.0 (the "License");
007  * you may not use this file except in compliance with the License.
008  * You may obtain a copy of the License at
009  *
010  *     http://www.apache.org/licenses/LICENSE-2.0
011  *
012  * Unless required by applicable law or agreed to in writing, software
013  * distributed under the License is distributed on an "AS IS" BASIS,
014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015  * See the License for the specific language governing permissions and
016  * limitations under the License.
017  */
018 
019 import java.util.ArrayList;
020 
021 /** Expert: Describes the score computation for document and query. */
022 public class Explanation implements java.io.Serializable {
023   private float value;                            // the value of this node
024   private String description;                     // what it represents
025   private ArrayList details;                      // sub-explanations
026 
027   public Explanation() {}
028 
029   public Explanation(float value, String description) {
030     this.value = value;
031     this.description = description;
032   }
033 
034   /** The value assigned to this explanation node. */
035   public float getValue() { return value; }
036   /** Sets the value assigned to this explanation node. */
037   public void setValue(float value) { this.value = value; }
038 
039   /** A description of this explanation node. */
040   public String getDescription() { return description; }
041   /** Sets the description of this explanation node. */
042   public void setDescription(String description) {
043     this.description = description;
044   }
045 
046   /** The sub-nodes of this explanation node. */
047   public Explanation[] getDetails() {
048     if (details == null)
049       return null;
050     return (Explanation[])details.toArray(new Explanation[0]);
051   }
052 
053   /** Adds a sub-node to this explanation node. */
054   public void addDetail(Explanation detail) {
055     if (details == null)
056       details = new ArrayList();
057     details.add(detail);
058   }
059 
060   /** Render an explanation as text. */
061   public String toString() {
062     return toString(0);
063   }
064   private String toString(int depth) {
065     StringBuffer buffer = new StringBuffer();
066     for (int i = 0; i < depth; i++) {
067       buffer.append("  ");
068     }
069     buffer.append(getValue());
070     buffer.append(" = ");
071     buffer.append(getDescription());
072     buffer.append("\n");
073 
074     Explanation[] details = getDetails();
075     if (details != null) {
076       for (int i = ; i < details.length; i++) {
077         buffer.append(details[i].toString(depth+1));
078       }
079     }
080 
081     return buffer.toString();
082   }
083 
084 
085   /** Render an explanation as HTML. */
086   public String toHtml() {
087     StringBuffer buffer = new StringBuffer();
088     buffer.append("<ul>\n");
089 
090     buffer.append("<li>");
091     buffer.append(getValue());
092     buffer.append(" = ");
093     buffer.append(getDescription());
094     buffer.append("</li>\n");
095 
096     Explanation[] details = getDetails();
097     if (details != null) {
098       for (int i = ; i < details.length; i++) {
099         buffer.append(details[i].toHtml());
100       }
101     }
102 
103     buffer.append("</ul>\n");
104 
105     return buffer.toString();
106   }
107 }