source: CQIClient/src/main/java/eu/clarin/cqi/client/CqiResult.java @ 2761

Last change on this file since 2761 was 2761, checked in by akislev, 11 years ago

one-off bug is fixed

File size: 7.8 KB
Line 
1/**
2 * This software is copyright (c) 2012 by - Seminar fuer Sprachwissenschaft
3 * (http://www.sfs.uni-tuebingen.de/) This is free software. You can
4 * redistribute it and/or modify it under the terms described in the GNU General
5 * Public License v3 of which you should have received a copy. Otherwise you can
6 * download it from
7 *
8 * http://www.gnu.org/licenses/gpl-3.0.txt
9 *
10 * @copyright Seminar fuer Sprachwissenschaft (http://www.sfs.uni-tuebingen.de/)
11 *
12 * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License
13 * v3
14 */
15package eu.clarin.cqi.client;
16
17import java.nio.charset.Charset;
18
19/**
20 *
21 * @author akislev
22 */
23public class CqiResult {
24
25    private static final int BUFFER_SIZE = 10;
26    private static final int MATCH_START = 0;
27    private static final int MATCH_END = 1;
28    private static final int TARGET = 2;
29    private static final int CONTEXT_START = 3;
30    private static final int CONTEXT_END = 4;
31    private final int[] buffer = new int[BUFFER_SIZE];
32    private final int[][] results = new int[5][BUFFER_SIZE];
33    private final CqiClient client;
34    private final String corpusName;
35    private final String subCorpusName;
36    private final Charset charset;
37    private final int resultSize;
38    private final String contextStructuralAttributeName;
39    private final String[] metadataStructuralAttributeNames;
40    private final String[][] metadataStructuralAttributeValues;
41    private int bufferedSize;
42    private int bufferedStart;
43    private int bufferIndex = -1;
44
45    CqiResult(CqiClient client, String corpusName, String subCorpusName, Charset charset, int resultSize, String contextStructuralAttribute, String[] metadataStructuralAttributes) {
46        this.client = client;
47        this.corpusName = corpusName;
48        this.subCorpusName = subCorpusName;
49        this.resultSize = resultSize;
50        this.charset = charset;
51        this.contextStructuralAttributeName = String.format("%s.%s", corpusName, contextStructuralAttribute);
52        if (metadataStructuralAttributes != null) {
53            this.metadataStructuralAttributeNames = new String[metadataStructuralAttributes.length];
54            for (int i = 0; i < metadataStructuralAttributes.length; i++) {
55                this.metadataStructuralAttributeNames[i] = String.format("%s.%s", corpusName, metadataStructuralAttributes[i]);
56            }
57            this.metadataStructuralAttributeValues = new String[metadataStructuralAttributes.length][BUFFER_SIZE];
58        } else {
59            this.metadataStructuralAttributeNames = null;
60            this.metadataStructuralAttributeValues = null;
61        }
62    }
63
64    /**
65     * Retrieves the current match index.
66     *
67     * @return the current match number; {@code -1} if there is no current match
68     */
69    public int getIndex() {
70        return bufferedStart + bufferIndex;
71    }
72
73    /**
74     * Moves the cursor to the given match number in this {@code MatchSet}
75     * object.
76     *
77     * @return {@code true} if the cursor is moved to a position in this
78     * {@code MatchSet} object; {@code false} if the cursor is before the first
79     * match or after the last match
80     * @exception CqiClientException
81     */
82    public boolean absolute(int match) throws CqiClientException {
83        if (match < 0 || match >= resultSize) {
84            return false;
85        }
86        bufferedStart = match;
87        return bufferResults();
88    }
89
90    /**
91     * Moves the cursor froward one row from its current position. A
92     * {@code CqiResult} cursor is initially positioned before the first match;
93     * the first call to the method {@code next} makes the first match the
94     * current match.
95     *
96     * @return {@code true} if the new current row is valid; {@code false} if
97     * there are no more matches
98     * @exception CqiClientException
99     */
100    public boolean next() throws CqiClientException {
101        if (++bufferIndex >= bufferedSize) {
102            return bufferResults();
103        } else {
104            return true;
105        }
106    }
107
108    private boolean bufferResults() throws CqiClientException {
109        bufferedStart += bufferedSize;
110        if (bufferedStart < resultSize) {
111            final int bufferedEnd = (bufferedStart + BUFFER_SIZE > resultSize) ? resultSize - 1 : bufferedStart + BUFFER_SIZE - 1;
112            bufferedSize = bufferedEnd - bufferedStart + 1;
113            bufferIndex = -1;
114            client.dumpSubCorpus(subCorpusName, CqiClient.CQI_CONST_FIELD_MATCH, bufferedStart, bufferedEnd, results[MATCH_START]);
115            client.dumpSubCorpus(subCorpusName, CqiClient.CQI_CONST_FIELD_MATCHEND, bufferedStart, bufferedEnd, results[MATCH_END]);
116            client.dumpSubCorpus(subCorpusName, CqiClient.CQI_CONST_FIELD_TARGET, bufferedStart, bufferedEnd, results[TARGET]);
117            client.cpos2LBound(contextStructuralAttributeName, results[MATCH_START], results[CONTEXT_START], bufferedSize);
118            client.cpos2RBound(contextStructuralAttributeName, results[MATCH_END], results[CONTEXT_END], bufferedSize);
119            if (metadataStructuralAttributeNames != null) {
120                for (int i = 0; i < metadataStructuralAttributeNames.length; i++) {
121                    String attrName = metadataStructuralAttributeNames[i];
122                    client.cpos2Struc(attrName, results[MATCH_START], buffer, bufferedSize);
123                    metadataStructuralAttributeValues[i] = client.struc2Str(attrName, buffer, charset);
124                }
125            }
126            return true;
127        } else {
128            return false;
129        }
130    }
131
132    /**
133     * Retrieves the starting position of the current match
134     *
135     * @return the starting position
136     */
137    public int getMatchStart() {
138        return results[MATCH_START][bufferIndex];
139    }
140
141    /**
142     * Retrieves the end position of the current match
143     *
144     * @return the end position
145     */
146    public int getMatchEnd() {
147        return results[MATCH_END][bufferIndex];
148    }
149
150    /**
151     * Retrieves the target position for the current match
152     *
153     * @return the target positions
154     */
155    public int getTarget() {
156        return results[TARGET][bufferIndex];
157    }
158
159    /**
160     * Retrieves the starting position of the context structural attribute
161     * surrounding the current match
162     *
163     * @return the starting positions
164     */
165    public int getContextStart() {
166        return results[CONTEXT_START][bufferIndex];
167    }
168
169    /**
170     * Retrieves the end position of the context structural attribute
171     * surrounding the current match
172     *
173     * @return the end positions
174     */
175    public int getContextEnd() {
176        return results[CONTEXT_END][bufferIndex];
177    }
178
179    /**
180     * Retrieves values of a given positional attribute between given positions
181     *
182     * @param positionalAttribute
183     * @param fromPosition
184     * @param toPosition
185     * @return positional attribute values
186     */
187    public String[] getValues(String positionalAttribute, int fromPosition, int toPosition) throws CqiClientException {
188        return client.cpos2Str(String.format("%s.%s", corpusName, positionalAttribute), fromPosition, toPosition, charset);
189    }
190
191    /**
192     * Retrieves a value of a structural attribute with an index given
193     * surrounding the current match
194     *
195     * @return the value of a structural attribute
196     */
197    public String getStructuralAttributeValue(int attributeIndex) {
198        if (metadataStructuralAttributeValues != null) {
199            return metadataStructuralAttributeValues[attributeIndex][bufferIndex];
200        } else {
201            throw new UnsupportedOperationException("no structural attributes were provided");
202        }
203    }
204
205    /**
206     * Clears this result from the memory
207     *
208     * @throws CqiClientException
209     */
210    public void clear() throws CqiClientException {
211        client.dropSubCorpus(subCorpusName);
212    }
213
214    public int size() {
215        return resultSize;
216    }
217}
Note: See TracBrowser for help on using the repository browser.