MtasCQLParserBasicSentenceCondition.java.html 16.3 KB
<?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> &gt; <a href="index.source.html" class="el_package">mtas.parser.cql.util</a> &gt; <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&lt;MtasCQLParserBasicSentencePartCondition&gt; 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&lt;MtasCQLParserBasicSentencePartCondition&gt;();</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 : &quot;condition word should be positive in sentence definition&quot;;</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(&quot;already simplified&quot;);</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(&quot;already simplified&quot;);</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&lt;MtasCQLParserBasicSentencePartCondition&gt; 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(&quot;already simplified&quot;);</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 &lt; 0) || (min &gt; max) || (max &lt; 1)) {</span>
<span class="nc" id="L130">        throw new ParseException(&quot;Illegal number {&quot; + min + &quot;,&quot; + max + &quot;}&quot;);</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(&quot;already simplified&quot;);</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&lt;MtasCQLParserBasicSentencePartCondition&gt; newPartList;
<span class="fc" id="L172">      MtasCQLParserBasicSentencePartCondition lastPart = null;</span>
<span class="fc" id="L173">      newPartList = new ArrayList&lt;MtasCQLParserBasicSentencePartCondition&gt;();</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() &amp;&amp; !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() &amp;&amp; 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() &amp;&amp; !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.">                &amp;&amp; (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&lt;MtasCQLParserBasicSentencePartCondition&gt; 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&lt;MtasSpanSequenceItem&gt; 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&lt;MtasSpanSequenceItem&gt;();</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() &gt; 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&lt;MtasSpanSequenceItem&gt;();</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(&quot;BASIC SENTENCE&quot;);</span>
<span class="nc bnc" id="L301" title="All 2 branches missed.">    if (optional) {</span>
<span class="nc" id="L302">      text.append(&quot; OPTIONAL&quot;);</span>
    }
<span class="nc" id="L304">    text.append(&quot;\n&quot;);</span>
<span class="nc bnc" id="L305" title="All 2 branches missed.">    if (simplified) {</span>
      try {
<span class="nc" id="L307">        text.append(&quot;- Query: &quot; + getQuery().toString(getQuery().getField()));</span>
<span class="nc" id="L308">      } catch (ParseException e) {</span>
<span class="nc" id="L309">        text.append(&quot;- Query: &quot; + 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(&quot;  - &quot;, &quot;   &quot;));</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>