Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / Explanation.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 namespace Mono.Lucene.Net.Search
21 {
22         
23         /// <summary>Expert: Describes the score computation for document and query. </summary>
24         [Serializable]
25         public class Explanation
26         {
27                 private float value_Renamed; // the value of this node
28                 private System.String description; // what it represents
29                 private System.Collections.ArrayList details; // sub-explanations
30                 
31                 public Explanation()
32                 {
33                 }
34                 
35                 public Explanation(float value_Renamed, System.String description)
36                 {
37                         this.value_Renamed = value_Renamed;
38                         this.description = description;
39                 }
40                 
41                 /// <summary> Indicates whether or not this Explanation models a good match.
42                 /// 
43                 /// <p/>
44                 /// By default, an Explanation represents a "match" if the value is positive.
45                 /// <p/>
46                 /// </summary>
47                 /// <seealso cref="getValue">
48                 /// </seealso>
49                 public virtual bool IsMatch()
50                 {
51                         return (0.0f < GetValue());
52                 }
53                 
54                 
55                 
56                 /// <summary>The value assigned to this explanation node. </summary>
57                 public virtual float GetValue()
58                 {
59                         return value_Renamed;
60                 }
61                 /// <summary>Sets the value assigned to this explanation node. </summary>
62                 public virtual void  SetValue(float value_Renamed)
63                 {
64                         this.value_Renamed = value_Renamed;
65                 }
66                 
67                 /// <summary>A description of this explanation node. </summary>
68                 public virtual System.String GetDescription()
69                 {
70                         return description;
71                 }
72                 /// <summary>Sets the description of this explanation node. </summary>
73                 public virtual void  SetDescription(System.String description)
74                 {
75                         this.description = description;
76                 }
77                 
78                 /// <summary> A short one line summary which should contain all high level
79                 /// information about this Explanation, without the "Details"
80                 /// </summary>
81                 protected internal virtual System.String GetSummary()
82                 {
83                         return GetValue() + " = " + GetDescription();
84                 }
85                 
86                 /// <summary>The sub-nodes of this explanation node. </summary>
87                 public virtual Explanation[] GetDetails()
88                 {
89                         if (details == null)
90                                 return null;
91                         return (Explanation[]) details.ToArray(typeof(Explanation));
92                 }
93                 
94                 /// <summary>Adds a sub-node to this explanation node. </summary>
95                 public virtual void  AddDetail(Explanation detail)
96                 {
97                         if (details == null)
98                                 details = new System.Collections.ArrayList();
99                         details.Add(detail);
100                 }
101                 
102                 /// <summary>Render an explanation as text. </summary>
103                 public override System.String ToString()
104                 {
105                         return ToString(0);
106                 }
107                 public /*protected internal*/ virtual System.String ToString(int depth)
108                 {
109                         System.Text.StringBuilder buffer = new System.Text.StringBuilder();
110                         for (int i = 0; i < depth; i++)
111                         {
112                                 buffer.Append("  ");
113                         }
114                         buffer.Append(GetSummary());
115                         buffer.Append("\n");
116                         
117                         Explanation[] details = GetDetails();
118                         if (details != null)
119                         {
120                                 for (int i = 0; i < details.Length; i++)
121                                 {
122                                         buffer.Append(details[i].ToString(depth + 1));
123                                 }
124                         }
125                         
126                         return buffer.ToString();
127                 }
128                 
129                 
130                 /// <summary>Render an explanation as HTML. </summary>
131                 public virtual System.String ToHtml()
132                 {
133                         System.Text.StringBuilder buffer = new System.Text.StringBuilder();
134                         buffer.Append("<ul>\n");
135                         
136                         buffer.Append("<li>");
137                         buffer.Append(GetSummary());
138                         buffer.Append("<br />\n");
139                         
140                         Explanation[] details = GetDetails();
141                         if (details != null)
142                         {
143                                 for (int i = 0; i < details.Length; i++)
144                                 {
145                                         buffer.Append(details[i].ToHtml());
146                                 }
147                         }
148                         
149                         buffer.Append("</li>\n");
150                         buffer.Append("</ul>\n");
151                         
152                         return buffer.ToString();
153                 }
154                 
155                 /// <summary> Small Util class used to pass both an idf factor as well as an
156                 /// explanation for that factor.
157                 /// 
158                 /// This class will likely be held on a {@link Weight}, so be aware 
159                 /// before storing any large or un-serializable fields.
160                 /// 
161                 /// </summary>
162                 [Serializable]
163                 public abstract class IDFExplanation
164                 {
165                         /// <returns> the idf factor
166                         /// </returns>
167                         public abstract float GetIdf();
168                         /// <summary> This should be calculated lazily if possible.
169                         /// 
170                         /// </summary>
171                         /// <returns> the explanation for the idf factor.
172                         /// </returns>
173                         public abstract System.String Explain();
174                 }
175         }
176 }