2
* @(#)RoleFiller.java 1.6 06/10/30
4
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7
* This code is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License version 2 only, as
9
* published by the Free Software Foundation. Sun designates this
10
* particular file as subject to the "Classpath" exception as provided
11
* by Sun in the LICENSE file that accompanied this code.
13
* This code is distributed in the hope that it will be useful, but WITHOUT
14
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
* version 2 for more details (a copy is included in the LICENSE file that
17
* accompanied this code).
19
* You should have received a copy of the GNU General Public License version
20
* 2 along with this work; if not, write to the Free Software Foundation,
21
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24
* CA 95054 USA or visit www.sun.com if you need additional information or
30
* @author Jacek R. Ambroziak
31
* @group Sun Microsystems Laboratories
34
package com.sun.java.help.search;
36
import java.io.PrintStream;
40
static int Threshold = 300;
42
private ConceptData _conceptData;
43
private byte _fixedRole;
44
private short _filled;
48
private RoleFiller _next;
49
private RoleFiller[] _fillers;
51
public static final RoleFiller STOP = new RoleFiller();
53
private RoleFiller() {}
55
public RoleFiller(int nColumns, ConceptData first, int role,
59
_fixedRole = (byte)role; // primary/constitutive concept/role
60
_filled = (short)(1 << _fixedRole);
61
_begin = pos; // offset in file
62
_end = _begin + first.getConceptLength();
65
_fillers = new RoleFiller[nColumns];
66
_fillers[role] = this;
69
public void print(PrintStream out) {
70
out.println(_begin + ", " + _end);
73
void makeQueryHit(Query q, int nColumns, int doc, double penalty)
75
if (q.goodEnough(penalty))
78
q.getConceptArrayOfNewHit(penalty,
79
new Location(doc, _begin, _end));
80
for (int i = 0; i < nColumns; i++)
81
array[i] = (_filled & 1 << i) != 0 ? _fillers[i].getConcept() : 0;
86
return _filled > (1 << _fixedRole);
90
return _conceptData.getScore();
94
return _conceptData.getConcept();
101
void use(RoleFiller[] place, int index)
103
if (place[index] != null)
105
RoleFiller rf = place[index];
108
while (rf._limit >= _begin)
110
// check if we can grow/improve a hit
111
// we don't ever replace filler's fixed role
112
if (_fixedRole != rf._fixedRole)
114
if ((rf._filled & (1 << _fixedRole)) == 0) // not filled yet
116
rf._filled |= 1 << _fixedRole;
117
rf._fillers[_fixedRole] = this;
121
rf.considerReplacementWith(this);
124
if (rf._next != null)
134
private void considerReplacementWith(RoleFiller replacement)
136
// !!! simplistic for now
137
// needs gap and out of order
138
int role = replacement._fixedRole;
139
if (replacement.getScore() > _fillers[role].getScore())
140
_fillers[role] = replacement;
143
private double penalty(Query query, int nColumns)
145
int length = _end - _begin + 1;
146
double penalty = query.lookupPenalty(_filled);
147
// !!! here is a chance to check against query if hit worth scoring further
148
// might not be if query already has lots of good hits
149
for (int i = 0; i < nColumns; i++)
150
if ((_filled & (1 << i)) != 0)
152
penalty += _fillers[i]._conceptData.getPenalty();
153
length -= _fillers[i]._conceptData.getConceptLength() + 1;
154
if ((_filled >> (i + 1)) != 0)
155
for (int j = i + 1; j < nColumns; j++)
156
if ((_filled & 1 << j) != 0 && _fillers[j]._begin < _begin)
157
penalty += query.getOutOufOrderPenalty();
159
return penalty + length*query.getGapPenalty();
162
public void scoreList(Query query, int document)
164
int nColumns = query.getNColumns();
165
RoleFiller candidateHit = this; // function called for the head of list
166
RoleFiller next; // lookahead: if overlap, if so, is it better
168
// 'candidateHit' always points at the current candidate to be converted to a QueryHit
169
// 'penalty' is its penalty
170
// 'next' is used to explore earlier overlapping fillers
171
// the decision to emit a QueryHit is made when either there's no next
172
// or next doesn't overlap the current candidate
173
// the loop's logic makes sure that at emit time there's no better/earlier filler
174
// to overlap with the candidate
176
double penalty = candidateHit.penalty(query, nColumns);
178
for (next = candidateHit._next; next != null; next = next._next)
179
if (next._end < candidateHit._begin) // no overlap
181
candidateHit.makeQueryHit(query, nColumns, document, penalty);
183
penalty = candidateHit.penalty(query, nColumns);
187
// !!! can be computed in two steps
188
double penalty2 = next.penalty(query, nColumns);
189
if (penalty2 <= penalty) // prefer next, disregard candidateHit
195
candidateHit.makeQueryHit(query, nColumns, document, penalty);