MtasCQLParserBasicSentenceCondition.java.html
16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasCQLParserBasicSentenceCondition.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.parser.cql.util</a> > <span class="el_source">MtasCQLParserBasicSentenceCondition.java</span></div><h1>MtasCQLParserBasicSentenceCondition.java</h1><pre class="source lang-java linenums">package mtas.parser.cql.util;
import java.util.ArrayList;
import java.util.List;
import mtas.parser.cql.ParseException;
import mtas.search.spans.MtasSpanRecurrenceQuery;
import mtas.search.spans.MtasSpanSequenceItem;
import mtas.search.spans.MtasSpanSequenceQuery;
import mtas.search.spans.util.MtasSpanQuery;
/**
* The Class MtasCQLParserBasicSentenceCondition.
*/
<span class="pc bpc" id="L15" title="1 of 2 branches missed.">public class MtasCQLParserBasicSentenceCondition {</span>
/** The part list. */
private List<MtasCQLParserBasicSentencePartCondition> partList;
/** The minimum occurence. */
private int minimumOccurence;
/** The maximum occurence. */
private int maximumOccurence;
/** The simplified. */
private boolean simplified;
/** The optional. */
private boolean optional;
/** The ignore clause. */
private MtasSpanQuery ignoreClause;
/** The maximum ignore length. */
private Integer maximumIgnoreLength;
/**
* Instantiates a new mtas CQL parser basic sentence condition.
*
* @param ignore the ignore
* @param maximumIgnoreLength the maximum ignore length
*/
<span class="fc" id="L44"> public MtasCQLParserBasicSentenceCondition(MtasSpanQuery ignore, Integer maximumIgnoreLength) {</span>
<span class="fc" id="L45"> partList = new ArrayList<MtasCQLParserBasicSentencePartCondition>();</span>
<span class="fc" id="L46"> minimumOccurence = 1;</span>
<span class="fc" id="L47"> maximumOccurence = 1;</span>
<span class="fc" id="L48"> optional = false;</span>
<span class="fc" id="L49"> simplified = false;</span>
<span class="fc" id="L50"> this.ignoreClause = ignore;</span>
<span class="fc" id="L51"> this.maximumIgnoreLength = maximumIgnoreLength;</span>
<span class="fc" id="L52"> }</span>
/**
* Adds the word.
*
* @param w the w
* @throws ParseException the parse exception
*/
public void addWord(MtasCQLParserWordFullCondition w) throws ParseException {
<span class="pc bpc" id="L61" title="1 of 2 branches missed."> assert w.getCondition()</span>
<span class="pc bpc" id="L62" title="1 of 2 branches missed."> .not() == false : "condition word should be positive in sentence definition";</span>
<span class="pc bpc" id="L63" title="1 of 2 branches missed."> if (!simplified) {</span>
<span class="fc" id="L64"> partList.add(w);</span>
} else {
<span class="nc" id="L66"> throw new ParseException("already simplified");</span>
}
<span class="fc" id="L68"> }</span>
/**
* Adds the group.
*
* @param g the g
* @throws ParseException the parse exception
*/
public void addGroup(MtasCQLParserGroupFullCondition g)
throws ParseException {
<span class="pc bpc" id="L78" title="1 of 2 branches missed."> if (!simplified) {</span>
<span class="fc" id="L79"> partList.add(g);</span>
} else {
<span class="nc" id="L81"> throw new ParseException("already simplified");</span>
}
<span class="fc" id="L83"> }</span>
/**
* Adds the basic sentence.
*
* @param s the s
* @throws ParseException the parse exception
*/
public void addBasicSentence(MtasCQLParserBasicSentenceCondition s)
throws ParseException {
<span class="pc bpc" id="L93" title="1 of 2 branches missed."> if (!simplified) {</span>
<span class="fc" id="L94"> List<MtasCQLParserBasicSentencePartCondition> newWordList = s</span>
<span class="fc" id="L95"> .getPartList();</span>
<span class="fc" id="L96"> partList.addAll(newWordList);</span>
<span class="fc" id="L97"> } else {</span>
<span class="nc" id="L98"> throw new ParseException("already simplified");</span>
}
<span class="fc" id="L100"> }</span>
/**
* Gets the minimum occurence.
*
* @return the minimum occurence
*/
public int getMinimumOccurence() {
<span class="nc" id="L108"> return minimumOccurence;</span>
}
/**
* Gets the maximum occurence.
*
* @return the maximum occurence
*/
public int getMaximumOccurence() {
<span class="nc" id="L117"> return maximumOccurence;</span>
}
/**
* Sets the occurence.
*
* @param min the min
* @param max the max
* @throws ParseException the parse exception
*/
public void setOccurence(int min, int max) throws ParseException {
<span class="pc bpc" id="L128" title="1 of 2 branches missed."> if (!simplified) {</span>
<span class="pc bpc" id="L129" title="3 of 6 branches missed."> if ((min < 0) || (min > max) || (max < 1)) {</span>
<span class="nc" id="L130"> throw new ParseException("Illegal number {" + min + "," + max + "}");</span>
}
<span class="pc bpc" id="L132" title="1 of 2 branches missed."> if (min == 0) {</span>
<span class="nc" id="L133"> optional = true;</span>
}
<span class="fc" id="L135"> minimumOccurence = Math.max(1, min);</span>
<span class="fc" id="L136"> maximumOccurence = max;</span>
} else {
<span class="nc" id="L138"> throw new ParseException("already simplified");</span>
}
<span class="fc" id="L140"> }</span>
/**
* Checks if is optional.
*
* @return true, if is optional
*/
public boolean isOptional() {
<span class="fc" id="L148"> return optional;</span>
}
/**
* Sets the optional.
*
* @param status the new optional
* @throws ParseException the parse exception
*/
public void setOptional(boolean status) throws ParseException {
<span class="fc" id="L159"> optional = status;</span>
<span class="fc" id="L160"> }</span>
/**
* Simplify.
*
* @throws ParseException the parse exception
*/
public void simplify() throws ParseException {
<span class="pc bpc" id="L168" title="1 of 2 branches missed."> if (!simplified) {</span>
<span class="fc" id="L169"> simplified = true;</span>
<span class="fc" id="L170"> boolean optionalParts = true;</span>
List<MtasCQLParserBasicSentencePartCondition> newPartList;
<span class="fc" id="L172"> MtasCQLParserBasicSentencePartCondition lastPart = null;</span>
<span class="fc" id="L173"> newPartList = new ArrayList<MtasCQLParserBasicSentencePartCondition>();</span>
// try and merge equal basicSentencePart (word/group) conditions
<span class="fc bfc" id="L175" title="All 2 branches covered."> for (MtasCQLParserBasicSentencePartCondition part : partList) {</span>
<span class="fc bfc" id="L176" title="All 4 branches covered."> if ((lastPart == null) || !lastPart.equals(part)) {</span>
<span class="fc" id="L177"> lastPart = part;</span>
<span class="fc" id="L178"> newPartList.add(part);</span>
<span class="fc bfc" id="L179" title="All 2 branches covered."> if (!part.isOptional()) {</span>
<span class="fc" id="L180"> optionalParts = false;</span>
}
} else {
int newMinimumOccurence;
int newMaximumOccurence;
<span class="fc bfc" id="L185" title="All 4 branches covered."> if (!lastPart.isOptional() && !part.isOptional()) {</span>
<span class="fc" id="L186"> newMinimumOccurence = lastPart.getMinimumOccurence()</span>
<span class="fc" id="L187"> + part.getMinimumOccurence();</span>
<span class="fc" id="L188"> newMaximumOccurence = lastPart.getMaximumOccurence()</span>
<span class="fc" id="L189"> + part.getMaximumOccurence();</span>
<span class="fc" id="L190"> lastPart.setOccurence(newMinimumOccurence, newMaximumOccurence);</span>
<span class="pc bpc" id="L191" title="1 of 4 branches missed."> } else if (!lastPart.isOptional() && part.isOptional()) {</span>
<span class="pc bpc" id="L192" title="1 of 2 branches missed."> if (part.getMinimumOccurence() == 1) {</span>
<span class="fc" id="L193"> newMinimumOccurence = lastPart.getMinimumOccurence()</span>
<span class="fc" id="L194"> + part.getMinimumOccurence() - 1;</span>
<span class="fc" id="L195"> newMaximumOccurence = lastPart.getMaximumOccurence()</span>
<span class="fc" id="L196"> + part.getMaximumOccurence();</span>
<span class="fc" id="L197"> lastPart.setOccurence(newMinimumOccurence, newMaximumOccurence);</span>
} else {
<span class="nc" id="L199"> lastPart = part;</span>
<span class="nc" id="L200"> newPartList.add(part);</span>
<span class="nc bnc" id="L201" title="All 2 branches missed."> if (!part.isOptional()) {</span>
<span class="nc" id="L202"> optionalParts = false;</span>
}
}
<span class="pc bpc" id="L205" title="2 of 4 branches missed."> } else if (lastPart.isOptional() && !part.isOptional()) {</span>
<span class="pc bpc" id="L206" title="1 of 2 branches missed."> if (lastPart.getMinimumOccurence() == 1) {</span>
<span class="fc" id="L207"> newMinimumOccurence = lastPart.getMinimumOccurence()</span>
<span class="fc" id="L208"> + part.getMinimumOccurence() - 1;</span>
<span class="fc" id="L209"> newMaximumOccurence = lastPart.getMaximumOccurence()</span>
<span class="fc" id="L210"> + part.getMaximumOccurence();</span>
<span class="fc" id="L211"> lastPart.setOccurence(newMinimumOccurence, newMaximumOccurence);</span>
<span class="fc" id="L212"> lastPart.setOptional(false);</span>
<span class="fc" id="L213"> optionalParts = false;</span>
} else {
<span class="nc" id="L215"> lastPart = part;</span>
<span class="nc" id="L216"> newPartList.add(part);</span>
<span class="nc" id="L217"> optionalParts = false;</span>
}
} else {
<span class="nc bnc" id="L220" title="All 2 branches missed."> if ((lastPart.getMinimumOccurence() == 1)</span>
<span class="nc bnc" id="L221" title="All 2 branches missed."> && (part.getMinimumOccurence() == 1)) {</span>
<span class="nc" id="L222"> newMinimumOccurence = 1;</span>
<span class="nc" id="L223"> newMaximumOccurence = lastPart.getMaximumOccurence()</span>
<span class="nc" id="L224"> + part.getMaximumOccurence();</span>
<span class="nc" id="L225"> lastPart.setOccurence(newMinimumOccurence, newMaximumOccurence);</span>
<span class="nc" id="L226"> lastPart.setOptional(true);</span>
} else {
<span class="nc" id="L228"> lastPart = part;</span>
<span class="nc" id="L229"> newPartList.add(part);</span>
}
}
}
<span class="fc" id="L233"> }</span>
<span class="fc" id="L234"> partList = newPartList;</span>
<span class="fc bfc" id="L235" title="All 2 branches covered."> if (optionalParts) {</span>
<span class="fc" id="L236"> optional = true;</span>
}
}
<span class="fc" id="L239"> }</span>
/**
* Gets the part list.
*
* @return the part list
*/
private List<MtasCQLParserBasicSentencePartCondition> getPartList() {
<span class="fc" id="L247"> return partList;</span>
}
/**
* Gets the query.
*
* @return the query
* @throws ParseException the parse exception
*/
public MtasSpanQuery getQuery() throws ParseException {
<span class="fc" id="L257"> simplify();</span>
<span class="fc" id="L258"> MtasSpanSequenceItem currentQuery = null;</span>
<span class="fc" id="L259"> List<MtasSpanSequenceItem> currentQueryList = null;</span>
<span class="fc bfc" id="L260" title="All 2 branches covered."> for (MtasCQLParserBasicSentencePartCondition part : partList) {</span>
// start list
<span class="fc bfc" id="L262" title="All 2 branches covered."> if (currentQuery != null) {</span>
<span class="fc" id="L263"> currentQueryList = new ArrayList<MtasSpanSequenceItem>();</span>
<span class="fc" id="L264"> currentQueryList.add(currentQuery);</span>
<span class="fc" id="L265"> currentQuery = null;</span>
}
<span class="fc bfc" id="L267" title="All 2 branches covered."> if (part.getMaximumOccurence() > 1) {</span>
<span class="fc" id="L268"> MtasSpanQuery q = new MtasSpanRecurrenceQuery(part.getQuery(),</span>
<span class="fc" id="L269"> part.getMinimumOccurence(), part.getMaximumOccurence(), ignoreClause, maximumIgnoreLength);</span>
<span class="fc" id="L270"> currentQuery = new MtasSpanSequenceItem(q, part.isOptional());</span>
<span class="fc" id="L271"> } else {</span>
<span class="fc" id="L272"> currentQuery = new MtasSpanSequenceItem(part.getQuery(),</span>
<span class="fc" id="L273"> part.isOptional());</span>
}
// add to list, if it exists
<span class="fc bfc" id="L276" title="All 2 branches covered."> if (currentQueryList != null) {</span>
<span class="fc" id="L277"> currentQueryList.add(currentQuery);</span>
<span class="fc" id="L278"> currentQuery = null;</span>
}
<span class="fc" id="L280"> }</span>
<span class="fc bfc" id="L281" title="All 2 branches covered."> if (currentQueryList != null) {</span>
<span class="fc" id="L282"> return new MtasSpanSequenceQuery(currentQueryList, ignoreClause, maximumIgnoreLength);</span>
<span class="fc bfc" id="L283" title="All 2 branches covered."> } else if (currentQuery.isOptional()) {</span>
<span class="fc" id="L284"> currentQueryList = new ArrayList<MtasSpanSequenceItem>();</span>
<span class="fc" id="L285"> currentQueryList.add(currentQuery);</span>
<span class="fc" id="L286"> currentQuery = null;</span>
<span class="fc" id="L287"> return new MtasSpanSequenceQuery(currentQueryList, ignoreClause, maximumIgnoreLength);</span>
} else {
<span class="fc" id="L289"> return currentQuery.getQuery();</span>
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
<span class="nc" id="L300"> StringBuilder text = new StringBuilder("BASIC SENTENCE");</span>
<span class="nc bnc" id="L301" title="All 2 branches missed."> if (optional) {</span>
<span class="nc" id="L302"> text.append(" OPTIONAL");</span>
}
<span class="nc" id="L304"> text.append("\n");</span>
<span class="nc bnc" id="L305" title="All 2 branches missed."> if (simplified) {</span>
try {
<span class="nc" id="L307"> text.append("- Query: " + getQuery().toString(getQuery().getField()));</span>
<span class="nc" id="L308"> } catch (ParseException e) {</span>
<span class="nc" id="L309"> text.append("- Query: " + e.getMessage());</span>
<span class="nc" id="L310"> }</span>
} else {
<span class="nc bnc" id="L312" title="All 2 branches missed."> for (MtasCQLParserBasicSentencePartCondition word : partList) {</span>
<span class="nc" id="L313"> text.append(word.toString(" - ", " "));</span>
<span class="nc" id="L314"> }</span>
}
<span class="nc" id="L316"> return text.toString();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>