<?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 */ public MtasCQLParserBasicSentenceCondition(MtasSpanQuery ignore, <span class="fc" id="L45"> Integer maximumIgnoreLength) {</span> <span class="fc" id="L46"> partList = new ArrayList<MtasCQLParserBasicSentencePartCondition>();</span> <span class="fc" id="L47"> minimumOccurence = 1;</span> <span class="fc" id="L48"> maximumOccurence = 1;</span> <span class="fc" id="L49"> optional = false;</span> <span class="fc" id="L50"> simplified = false;</span> <span class="fc" id="L51"> this.ignoreClause = ignore;</span> <span class="fc" id="L52"> this.maximumIgnoreLength = maximumIgnoreLength;</span> <span class="fc" id="L53"> }</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="L62" title="1 of 2 branches missed."> assert w.getCondition()</span> <span class="pc bpc" id="L63" title="1 of 2 branches missed."> .not() == false : "condition word should be positive in sentence definition";</span> <span class="pc bpc" id="L64" title="1 of 2 branches missed."> if (!simplified) {</span> <span class="fc" id="L65"> partList.add(w);</span> } else { <span class="nc" id="L67"> throw new ParseException("already simplified");</span> } <span class="fc" id="L69"> }</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="L79" title="1 of 2 branches missed."> if (!simplified) {</span> <span class="fc" id="L80"> partList.add(g);</span> } else { <span class="nc" id="L82"> throw new ParseException("already simplified");</span> } <span class="fc" id="L84"> }</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="L94" title="1 of 2 branches missed."> if (!simplified) {</span> <span class="fc" id="L95"> List<MtasCQLParserBasicSentencePartCondition> newWordList = s</span> <span class="fc" id="L96"> .getPartList();</span> <span class="fc" id="L97"> partList.addAll(newWordList);</span> <span class="fc" id="L98"> } else {</span> <span class="nc" id="L99"> throw new ParseException("already simplified");</span> } <span class="fc" id="L101"> }</span> /** * Gets the minimum occurence. * * @return the minimum occurence */ public int getMinimumOccurence() { <span class="nc" id="L109"> return minimumOccurence;</span> } /** * Gets the maximum occurence. * * @return the maximum occurence */ public int getMaximumOccurence() { <span class="nc" id="L118"> 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="L129" title="1 of 2 branches missed."> if (!simplified) {</span> <span class="pc bpc" id="L130" title="3 of 6 branches missed."> if ((min < 0) || (min > max) || (max < 1)) {</span> <span class="nc" id="L131"> throw new ParseException("Illegal number {" + min + "," + max + "}");</span> } <span class="pc bpc" id="L133" title="1 of 2 branches missed."> if (min == 0) {</span> <span class="nc" id="L134"> optional = true;</span> } <span class="fc" id="L136"> minimumOccurence = Math.max(1, min);</span> <span class="fc" id="L137"> maximumOccurence = max;</span> } else { <span class="nc" id="L139"> throw new ParseException("already simplified");</span> } <span class="fc" id="L141"> }</span> /** * Checks if is optional. * * @return true, if is optional */ public boolean isOptional() { <span class="fc" id="L149"> 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(),</span> ignoreClause, maximumIgnoreLength); <span class="fc" id="L271"> currentQuery = new MtasSpanSequenceItem(q, part.isOptional());</span> <span class="fc" id="L272"> } else {</span> <span class="fc" id="L273"> currentQuery = new MtasSpanSequenceItem(part.getQuery(),</span> <span class="fc" id="L274"> part.isOptional());</span> } // add to list, if it exists <span class="fc bfc" id="L277" title="All 2 branches covered."> if (currentQueryList != null) {</span> <span class="fc" id="L278"> currentQueryList.add(currentQuery);</span> <span class="fc" id="L279"> currentQuery = null;</span> } <span class="fc" id="L281"> }</span> <span class="fc bfc" id="L282" title="All 2 branches covered."> if (currentQueryList != null) {</span> <span class="fc" id="L283"> return new MtasSpanSequenceQuery(currentQueryList, ignoreClause,</span> maximumIgnoreLength); <span class="fc bfc" id="L285" title="All 2 branches covered."> } else if (currentQuery.isOptional()) {</span> <span class="fc" id="L286"> currentQueryList = new ArrayList<MtasSpanSequenceItem>();</span> <span class="fc" id="L287"> currentQueryList.add(currentQuery);</span> <span class="fc" id="L288"> currentQuery = null;</span> <span class="fc" id="L289"> return new MtasSpanSequenceQuery(currentQueryList, ignoreClause,</span> maximumIgnoreLength); } else { <span class="fc" id="L292"> return currentQuery.getQuery();</span> } } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { <span class="nc" id="L303"> StringBuilder text = new StringBuilder("BASIC SENTENCE");</span> <span class="nc bnc" id="L304" title="All 2 branches missed."> if (optional) {</span> <span class="nc" id="L305"> text.append(" OPTIONAL");</span> } <span class="nc" id="L307"> text.append("\n");</span> <span class="nc bnc" id="L308" title="All 2 branches missed."> if (simplified) {</span> try { <span class="nc" id="L310"> text.append("- Query: " + getQuery().toString(getQuery().getField()));</span> <span class="nc" id="L311"> } catch (ParseException e) {</span> <span class="nc" id="L312"> text.append("- Query: " + e.getMessage());</span> <span class="nc" id="L313"> }</span> } else { <span class="nc bnc" id="L315" title="All 2 branches missed."> for (MtasCQLParserBasicSentencePartCondition word : partList) {</span> <span class="nc" id="L316"> text.append(word.toString(" - ", " "));</span> <span class="nc" id="L317"> }</span> } <span class="nc" id="L319"> 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>