001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.ArrayList;
021:
022: /** Expert: Describes the score computation for document and query. */
023: public class Explanation implements java.io.Serializable {
024: private float value; // the value of this node
025: private String description; // what it represents
026: private ArrayList details; // sub-explanations
027:
028: public Explanation() {
029: }
030:
031: public Explanation(float value, String description) {
032: this .value = value;
033: this .description = description;
034: }
035:
036: /**
037: * Indicates whether or not this Explanation models a good match.
038: *
039: * <p>
040: * By default, an Explanation represents a "match" if the value is positive.
041: * </p>
042: * @see #getValue
043: */
044: public boolean isMatch() {
045: return (0.0f < getValue());
046: }
047:
048: /** The value assigned to this explanation node. */
049: public float getValue() {
050: return value;
051: }
052:
053: /** Sets the value assigned to this explanation node. */
054: public void setValue(float value) {
055: this .value = value;
056: }
057:
058: /** A description of this explanation node. */
059: public String getDescription() {
060: return description;
061: }
062:
063: /** Sets the description of this explanation node. */
064: public void setDescription(String description) {
065: this .description = description;
066: }
067:
068: /**
069: * A short one line summary which should contain all high level
070: * information about this Explanation, without the "Details"
071: */
072: protected String getSummary() {
073: return getValue() + " = " + getDescription();
074: }
075:
076: /** The sub-nodes of this explanation node. */
077: public Explanation[] getDetails() {
078: if (details == null)
079: return null;
080: return (Explanation[]) details.toArray(new Explanation[0]);
081: }
082:
083: /** Adds a sub-node to this explanation node. */
084: public void addDetail(Explanation detail) {
085: if (details == null)
086: details = new ArrayList();
087: details.add(detail);
088: }
089:
090: /** Render an explanation as text. */
091: public String toString() {
092: return toString(0);
093: }
094:
095: protected String toString(int depth) {
096: StringBuffer buffer = new StringBuffer();
097: for (int i = 0; i < depth; i++) {
098: buffer.append(" ");
099: }
100: buffer.append(getSummary());
101: buffer.append("\n");
102:
103: Explanation[] details = getDetails();
104: if (details != null) {
105: for (int i = 0; i < details.length; i++) {
106: buffer.append(details[i].toString(depth + 1));
107: }
108: }
109:
110: return buffer.toString();
111: }
112:
113: /** Render an explanation as HTML. */
114: public String toHtml() {
115: StringBuffer buffer = new StringBuffer();
116: buffer.append("<ul>\n");
117:
118: buffer.append("<li>");
119: buffer.append(getSummary());
120: buffer.append("<br />\n");
121:
122: Explanation[] details = getDetails();
123: if (details != null) {
124: for (int i = 0; i < details.length; i++) {
125: buffer.append(details[i].toHtml());
126: }
127: }
128:
129: buffer.append("</li>\n");
130: buffer.append("</ul>\n");
131:
132: return buffer.toString();
133: }
134: }
|