Code Search for Developers
 
 
  

RegularExpression.java from Texai at Krugle


Show RegularExpression.java syntax highlighted

/*
 * RegularExpression.java
 *
 * Created on January 15, 2007, 2:34 PM
 *
 * Description: Instances of this class are the construction constituents for regular expressions applied to text.
 *
 * Copyright (C) 2007 Stephen L. Reed.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package org.texai.grammar.domainEntity;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.FetchType;
import javax.persistence.OrderBy;
import net.jcip.annotations.NotThreadSafe;
import org.openrdf.model.URI;
import org.texai.grammar.understanding.ActiveConstituent;
import org.texai.grammar.understanding.ActiveRegularExpression;
import org.texai.kb.Constants;
import org.texai.kb.persistence.RDFEntity;
import org.texai.kb.persistence.RDFNamespace;
import org.texai.kb.persistence.RDFProperty;

/** Instances of this class are the construction constituents for regular expressions applied to text.
 *
 * @author reed
 */
@RDFEntity(
namespaces={
  @RDFNamespace(prefix="texai", namespaceURI=Constants.TEXAI_NAMESPACE),
  @RDFNamespace(prefix="cyc", namespaceURI=Constants.CYC_NAMESPACE)},
subject="texai:org.texai.grammar.domainEntity.RegularExpression", type="cyc:LinguisticObjectType", subClassOf="texai:CxgWordFormConstruction", context="texai:EnglishConstructionGrammarDomainContext")
@NotThreadSafe  // but effectively immutable
public class RegularExpression extends AbstractConstruction {
  
  /** the regular expression to match */
  @RDFProperty(predicate="texai:cxgConstituentRegularExpression")
  private String regularExpression;
  
  /** the substitution expression to generate */
  @RDFProperty(predicate="texai:cxgConstituentSubstitutionExpression")
  private String substitutionExpression;
  
  /** the regular expression group variables */
  @OrderBy("groupVariablePosition")
  @RDFProperty(predicate="texai:cxgConstituentRegularExpressionGroupVariableInfo", range="texai:org.texai.grammar.domainEntity.RegularExpression_GroupVariableInfo")
  private List<RegularExpression_GroupVariableInfo> groupVariableInfos;
  
  /**
   * Creates a new instance of RegularExpression.
   */
  public RegularExpression() {
    super();
    setLoggerUsingClass(this.getClass());
  }
  
  /** Creates a new instance of RegularExpression.
   *
   * @param name the construction name that identifies this instance, which in case of word constructions is also the represented word(s)
   * @param naturalLanguage the natural language to which this construction applies
   * @param regularExpression the regular expression to match
   * @param substitutionExpression the substitution expression to generate
   * @param groupVariables the regular expression group variables
   */
  public RegularExpression(
          final String name, 
          final URI naturalLanguage,
          final String regularExpression,
          final String substitutionExpression,
          final List<String> groupVariables) {
    super(name, naturalLanguage);
    //Preconditions
    assert regularExpression != null : "regularExpression must not be null";
    assert substitutionExpression != null : "substitutionExpression must not be null";
    
    this.regularExpression = regularExpression;
    this.substitutionExpression = substitutionExpression;
    setGroupVariables(groupVariables);
    setLoggerUsingClass(this.getClass());
  }

 /** Decorates this construction with an active construction that contains the 
   * behavior and transient state for parsing text.
   *
   * @return an ActiveRegularExpression that decorates this object
   */
  public ActiveConstituent decorate() {
    return new ActiveRegularExpression(this);
  }
  
  /** Sets the regular expression to match.
   *
   * @param regularExpression the regular expression to match
   */
  public void setRegularExpression(final String regularExpression) {
    //Preconditions
    assert regularExpression != null : "regularExpression must not be null";
    
    this.regularExpression = regularExpression;
  }
  
  /** Gets the regular expression to match.
   *
   * @return the regular expression to match
   */
  public String getRegularExpression() {
    return regularExpression;
  }
  
  /** Sets the substitution expression to generate.
   *
   * @param substitutionExpression the substitution expression to generate
   */
  public void setSubstitutionExpression(final String substitutionExpression) {
    //Preconditions
    assert substitutionExpression != null : "substitutionExpression must not be null";
    
    this.substitutionExpression = substitutionExpression;
  }
  
  /** Gets the substitution expression to generate.
   *
   * @return the substitution expression to generate
   */
  public String getSubstitutionExpression() {
    return substitutionExpression;
  }
  
  /** Sets the regular expression group variables.
   *
   * @param groupVariables the regular expression group variables
   */
  public void setGroupVariables(final List<String> groupVariables) {
    //Preconditions
    assert groupVariables != null : "groupVariables must not be null";
    
    if (groupVariableInfos == null) {
      groupVariableInfos = new ArrayList<RegularExpression_GroupVariableInfo>(groupVariables.size());
    }
    int index = 0;
    for (final String groupVariable : groupVariables) {
      assert groupVariable.length() > 0 : "groupVariable must not be an empty string";
      RegularExpression_GroupVariableInfo groupVariableInfo = null;
      if (groupVariableInfos.size() > index) {
        groupVariableInfo = groupVariableInfos.get(index);
        groupVariableInfo.setGroupVariable(groupVariable);
        groupVariableInfo.setGroupVariablePosition(index);
      } else {
        groupVariableInfo = new RegularExpression_GroupVariableInfo(groupVariable, index);
        groupVariableInfos.add(groupVariableInfo);
      }
      index++;
    }
  }
  
  /** Gets the regular expression group variables.
   *
   * @return the regular expression group variables
   */
  public List<String> getGroupVariables() {
    //Preconditions
    assert groupVariableInfos != null : "groupVariableInfos must not be null";
    
    final List<String> groupVariables = new ArrayList<String>(groupVariableInfos.size());
    for (final RegularExpression_GroupVariableInfo groupVariableInfo : groupVariableInfos) {
      groupVariables.add(groupVariableInfo.getGroupVariable());
    }
    return groupVariables;
  }
  
  /** Returns a hash code for this object.
   *
   * @return a hash code for this object
   */
  @Override
  public int hashCode() {
    if (getId() == null) {
      return super.hashCode();
    } else {
      return getId().hashCode();
    }
  }
  
  /** Returns whether the given object is equal to this object.
   *
   * @param obj the given object
   * @return whether the given object is equal to this object
   */
  @Override
  public boolean equals(final Object obj) {
    if (obj instanceof RegularExpression) {
      final RegularExpression that = (RegularExpression) obj;
      return this.getId().equals(that.getId());
    } else {
      return false;
    } 
  }
  
  /** Returns a string representation of this object. 
   *
   * @return a string representation of this object
   */
  public String toString() {
    return "[RegularExpression " + regularExpression + "]";
  }
  
}




See more files for this project here

Texai

Texai is an chatbot that intelligently seeks to acquire knowledge and friendly behaviors.

Project homepage: http://sourceforge.net/projects/texai
Programming language(s): Java,Shell Script,XML
License: other

  AbstractComposedConstruction.java
  AbstractConstruction.java
  AlternativeConstruction.java
  CategoryConstruction.java
  ConstituentAdapter.java
  OptionalConstruction.java
  RegularExpression.java
  RegularExpression_GroupVariableInfo.java
  RepetitiveConstruction.java
  SequenceConstruction.java
  SimpleConstruction.java
  ValidateGrammarEntities.java
  WordFormConstruction.java
  WordSenseConstruction.java
  XMLTagWord.java