Initial revision
[beagle.git] / Lucene.Net / Search / Explanation.cs
blob2ea47c900573b58fada30720a8335856ec98de18
1 using System;
2 using System.Text;
3 using System.Collections;
4 using Lucene.Net.Util;
6 namespace Lucene.Net.Search
8 /* ====================================================================
9 * The Apache Software License, Version 1.1
11 * Copyright (c) 2003 The Apache Software Foundation. All rights
12 * reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in
23 * the documentation and/or other materials provided with the
24 * distribution.
26 * 3. The end-user documentation included with the redistribution,
27 * if any, must include the following acknowledgment:
28 * "This product includes software developed by the
29 * Apache Software Foundation (http://www.apache.org/)."
30 * Alternately, this acknowledgment may appear in the software itself,
31 * if and wherever such third-party acknowledgments normally appear.
33 * 4. The names "Apache" and "Apache Software Foundation" and
34 * "Apache Lucene" must not be used to endorse or promote products
35 * derived from this software without prior written permission. For
36 * written permission, please contact apache@apache.org.
38 * 5. Products derived from this software may not be called "Apache",
39 * "Apache Lucene", nor may "Apache" appear in their name, without
40 * prior written permission of the Apache Software Foundation.
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
43 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
45 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
46 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
49 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
50 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
51 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
52 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 * ====================================================================
56 * This software consists of voluntary contributions made by many
57 * individuals on behalf of the Apache Software Foundation. For more
58 * information on the Apache Software Foundation, please see
59 * <http://www.apache.org/>.
62 /// <summary>
63 /// Expert: Describes the score computation for document and query.
64 /// </summary>
65 [Serializable]
66 public class Explanation
68 private float value; // the value of this node
69 private String description; // what it represents
70 private ArrayList details; // sub-explanations
72 public Explanation() {}
74 public Explanation(float value, String description)
76 this.value = value;
77 this.description = description;
80 /// <summary>
81 /// The value assigned to this explanation node.
82 /// </summary>
83 /// <returns></returns>
84 public float GetValue() { return value; }
86 /// <summary>
87 /// Sets the value assigned to this explanation node.
88 /// </summary>
89 /// <param name="value"></param>
90 public void SetValue(float value) { this.value = value; }
92 /// <summary>
93 /// A description of this explanation node.
94 /// </summary>
95 /// <returns></returns>
96 public String GetDescription() { return description; }
98 /// <summary>
99 /// Sets the description of this explanation node.
100 /// </summary>
101 /// <param name="description"></param>
102 public void SetDescription(String description)
104 this.description = description;
107 /// <summary>
108 /// The sub-nodes of this explanation node.
109 /// </summary>
110 /// <returns></returns>
111 public Explanation[] GetDetails()
113 if (details == null)
114 return null;
115 return (Explanation[])details.ToArray(typeof(Explanation));
118 /// <summary>
119 /// The sub-nodes of this explanation node.
120 /// </summary>
121 /// <param name="detail"></param>
122 public void AddDetail(Explanation detail)
124 if (details == null)
125 details = new ArrayList();
126 details.Add(detail);
129 /// <summary>
130 /// Render an explanation as HTML.
131 /// </summary>
132 /// <returns></returns>
133 public override String ToString()
135 return ToString(0);
138 private String ToString(int depth)
140 StringBuilder buffer = new StringBuilder();
141 for (int i = 0; i < depth; i++)
143 buffer.Append(" ");
145 buffer.Append(Number.ToString(GetValue()));
146 buffer.Append(" = ");
147 buffer.Append(GetDescription());
148 buffer.Append("\n");
150 Explanation[] details = GetDetails();
151 if (details != null)
153 for (int i = 0 ; i < details.Length; i++)
155 buffer.Append(details[i].ToString(depth+1));
159 return buffer.ToString();
162 /// <summary>
163 /// Render an explanation as HTML.
164 /// </summary>
165 /// <returns></returns>
166 public String ToHtml()
168 StringBuilder buffer = new StringBuilder();
169 buffer.Append("<ul>\n");
171 buffer.Append("<li>");
172 buffer.Append(Number.ToString(GetValue()));
173 buffer.Append(" = ");
174 buffer.Append(GetDescription());
175 buffer.Append("</li>\n");
177 Explanation[] details = GetDetails();
178 if (details != null)
180 for (int i = 0 ; i < details.Length; i++)
182 buffer.Append(details[i].ToHtml());
186 buffer.Append("</ul>\n");
188 return buffer.ToString();