Code Search for Developers
 
 
  

FormulaInfo.java from Texai at Krugle


Show FormulaInfo.java syntax highlighted

/*
 * FormulaInfo.java
 *
 * Created on October 24, 2006, 1:45 PM
 *
 * Description: Contains the specialized fields for Formula.
 *
 * Copyright (C) 2006 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.kb.ejb.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.Transient;
import org.texai.kb.Constants;

/**
 * Entity class FormulaInfo
 *
 * @author reed
 */
@Entity
public class FormulaInfo implements Serializable {       // NOPMD

  /**
   * Determines if a de-serialized file is compatible with this class.
   *
   * Maintainers must change this value if and only if the new version
   * of this class is not compatible with old versions. See Sun docs
   * for <a href=http://java.sun.com/products/jdk/1.1/docs/guide
   * /serialization/spec/version.doc.html> details. </a>
   *
   * Not necessary to include in first version of the class, but
   * included here as a reminder of its importance.
   */
  @Transient
  private static final long serialVersionUID = 1L;

  /** the id */
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long formulaInfoId;             // NOPMD

  /** the formula */
  @OneToOne
  private Formula formula;                // NOPMD

  /** the argument terms in this formula */
  @OneToMany(mappedBy = "formulaInfo", cascade={CascadeType.PERSIST})
  @OrderBy("argIndex ASC")
  private List<FormulaArgument> formulaArguments;    // NOPMD

  /**
   * the formula string
   * @serial
   */
  @Column(length=Constants.FORMULA_STRING_SIZE)
  @Basic
  private String formulaString;    // NOPMD

  /** the creator of this formula */
  @ManyToOne(optional=false, cascade=CascadeType.PERSIST, fetch=FetchType.LAZY)
  private AbstractReifiedTerm creator;
  
  /** the creation purpose of this formula */
  @ManyToOne(optional=false, cascade=CascadeType.PERSIST, fetch=FetchType.LAZY)
  private AbstractReifiedTerm creationPurpose;
  
  /** the creation time point of this formula */
  @ManyToOne(optional=false, cascade=CascadeType.PERSIST, fetch=FetchType.LAZY)
  private TimePoint creationTimePoint;
  
  /** Creates a new instance of FormulaInfo */
  public FormulaInfo() {
    super();
  }

  /** Creates a new instance of FormulaInfo.
   *
   * @param formula the persistent formula
   * @param termList the list of terms that constitute this formula
   * @param creator the creator 
   * @param creationPurpose the creation purpose
   * @param creationDate the creation time point
   */
  public FormulaInfo(final Formula formula, 
          final List<AbstractTerm> termList,
          final AbstractReifiedTerm creator,
          final AbstractReifiedTerm creationPurpose,
          final TimePoint creationTimePoint) {
    super();
    //Preconditions
    assert formula != null : "formula must not be null";
    assert termList != null : "termList must not be null";
    assert !termList.isEmpty() : "termList must not be empty";
    assert creator != null : "creator must not be null";
    assert creationPurpose != null : "creationPurpose must not be null";
    assert creationTimePoint != null : "creationTimePoint must not be null";

    final int termListSize = termList.size();
    formulaArguments = new ArrayList<FormulaArgument>(termListSize);
    for (int index = 0; index < termListSize; index++) {
      assert termList.get(index) != null : "formula term must not be null in " + termList;
      getFormulaArguments().add(new FormulaArgument(this, index, termList.get(index)));       // NOPMD
    }
    this.formula = formula;
    formulaString = composeFormulaStringFromTermList(termList);
    this.creator = creator;
    this.creationPurpose = creationPurpose;
    this.creationTimePoint = creationTimePoint;
  }

  /**
   * Gets the id of this FormulaInfo.
   *
   * @return the id
   */
  public Long getFormulaInfoId() {
    return this.formulaInfoId;
  }

  /** Returns a propositional formula string composed from the given term list.
   *
   * @param termList the list of terms
   * @return a string composed from the given term list
   */
  public static String composeFormulaStringFromTermList(final List<AbstractTerm> termList) {  // NOPMD
    //Preconditions
    assert termList != null : "termList must not be null";
    assert !termList.isEmpty() : "termList must not be empty";

    final StringBuilder stringBuilder = new StringBuilder(Constants.STRING_BUILDER_SIZE);
    final int termListSize = termList.size();
    final AbstractTerm firstTerm = termList.get(0);
    if (firstTerm instanceof AtomicTerm && "implies".equals(firstTerm.toString())) {
      assert termListSize == Constants.IMPLIES_SIZE : "IMPLIES must be length 3 " + termList.toString();
      stringBuilder.append(termList.get(1).toString());
      stringBuilder.append(" -> ");
      stringBuilder.append(termList.get(2).toString());
    } else if (firstTerm instanceof AtomicTerm && "thereExists".equals(firstTerm.toString())) {
      assert termListSize == Constants.THERE_EXISTS_SIZE : "THERE EXISTS must be length 3 " + termList.toString();
      stringBuilder.append(firstTerm.toString());
      stringBuilder.append(" ");
      stringBuilder.append(termList.get(1).toString());
      final String qualifiedFormula = termList.get(2).toString();
      if (qualifiedFormula.charAt(0) == '(') {
        stringBuilder.append(" ");
        stringBuilder.append(qualifiedFormula);
      } else {
        stringBuilder.append(" (");
        stringBuilder.append(qualifiedFormula);
        stringBuilder.append(")");
      }
    } else if (firstTerm instanceof AtomicTerm && "not".equals(firstTerm.toString())) {
      assert termListSize == 2 : "NOT must be length 2 " + termList.toString();
      stringBuilder.append("!");
      stringBuilder.append(termList.get(1).toString());
    } else if (firstTerm instanceof AtomicTerm && "and".equals(firstTerm.toString())) {
      assert termListSize > 2 : "AND must be at least length 3 " + termList.toString();
      final int termListSizeLessOne = termListSize - 1;
      stringBuilder.append("(");
      for (int i = 1; i < termListSize; i++) {
        stringBuilder.append(termList.get(i));
        if (i < termListSizeLessOne) {
          stringBuilder.append(" && ");
        }
      }
      stringBuilder.append(")");
    } else if (firstTerm instanceof AtomicTerm && "or".equals(firstTerm.toString())) {
      assert termListSize > 2 : "OR must be at least length 3 " + termList.toString();
      final int termListSizeLessOne = termListSize - 1;
      stringBuilder.append("(");
      for (int i = 1; i < termListSize; i++) {
        stringBuilder.append(termList.get(i));
        if (i < termListSizeLessOne) {
          stringBuilder.append(" || ");
        }
      }
      stringBuilder.append(")");
    } else {
      stringBuilder.append(firstTerm.toString());
      if (termListSize > 1) {
        stringBuilder.append("(");
        for (int i = 1; i < termListSize; i++) {
          stringBuilder.append(termList.get(i).toString());
          if (i < termListSize - 1) {
            stringBuilder.append(", ");
          }
        }
        stringBuilder.append(")");
      }
    }
    return stringBuilder.toString();
  }

  /** Gets the formula string.
   *
   * @return the formula string
   */
  public String getFormulaString() {
    return formulaString;
  }

  /** Gets the creator.
   *
   * @return the creator
   */
  public AbstractReifiedTerm getCreator() {
    return creator;
  }

  /** Gets the creation purpose.
   *
   * @return the creation purpose
   */
  public AbstractReifiedTerm getCreationPurpose() {
    return creationPurpose;
  }

  /** Gets the creation time point.
   *
   * @return the creation point
   */
  public TimePoint getCreationTimePoint() {
    return creationTimePoint;
  }

  /**
   * Returns a hash code value for the object.  This implementation computes
   * a hash code value based on the unchanging formulaString.
   * @return a hash code value for this object
   */
  @Override
  public int hashCode() {
    return getFormulaString().hashCode();
  }

  /**
   * Determines whether another object is equal to this object.
   *
   * @param object the reference object with which to compare
   * @return <code>true</code> if this object is the same as the argument;
   * <code>false</code> otherwise.
   */
  @Override
  public boolean equals(final Object object) {
    if (!(object instanceof FormulaInfo)) {
      return false;
    }
    final FormulaInfo that = (FormulaInfo) object;
    return this.getTermList().equals(that.getTermList());
  }

  /** Returns a string representation of this object.
   *
   * @return a string representation of this object
   */
  @Override
  public String toString() {
    return getFormulaString();
  }

  /** Returns this formula as a list of terms.
   *
   * @return this formula as a list of terms
   */
  public List<AbstractTerm> getTermList() {
    final int argsSize = getFormulaArguments().size();
    final List<AbstractTerm> termList = new ArrayList<AbstractTerm>(argsSize);
    for (int index = 0; index < argsSize; index++) {
      termList.add(getFormulaArguments().get(index).getArg());
    }
    return termList;
  }

  /** Returns the list of indexed formula arguments
   *
   * @return the list of indexed formula arguments
   */
  public List<FormulaArgument> getFormulaArguments() {
    return formulaArguments;
  }

  /** Returns a CycL representation of this object.
   *
   * @return a CycL representation of this object
   */
  public String toCycLString() {
    final StringBuilder stringBuilder = new StringBuilder(Constants.STRING_BUILDER_SIZE);
    final List<AbstractTerm> termList = getTermList();
    stringBuilder.append("(");
    final int termListSize = termList.size();
    for (int i = 0; i < termListSize; i++) {
      stringBuilder.append(termList.get(i).toCycLString());
      if (i < termListSize - 1) {
        stringBuilder.append(" ");
      }
    }
    stringBuilder.append(")");
    return stringBuilder.toString();
  }

  /** Returns a propositional formula string composed from the given term list.
   *
   * @param termList the list of terms
   * @return a string composed from the given term list
   */
  public String composeFormulaString(final List<AbstractTerm> termList) {  // NOPMD
    //Preconditions
    assert termList != null : "termList must not be null";
    assert !termList.isEmpty() : "termList must not be empty";

    return composeFormulaStringFromTermList(termList);
  }

  /** gets the formula
   *
   * @return the formula
   */
  public Formula getFormula() {
    return formula;
  }

}




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

  AbstractGAF.java
  AbstractGAFInfo.java
  AbstractReifiedTerm.java
  AbstractTerm.java
  AtomicTerm.java
  AtomicTermInfo.java
  BinaryGAF.java
  BinaryGAFInfo.java
  Formula.java
  FormulaArgument.java
  FormulaInfo.java
  NonAtomicTerm.java
  NonAtomicTermInfo.java
  PBlob.java
  PBlobInfo.java
  PCharacter.java
  PCharacterInfo.java
  PClob.java
  PClobInfo.java
  PDate.java
  PDateInfo.java
  PDouble.java
  PDoubleInfo.java
  PLong.java
  PLongInfo.java
  PString.java
  PStringInfo.java
  PVariable.java
  PVariableInfo.java
  QuaternaryGAF.java
  QuaternaryGAFInfo.java
  QuintaryGAF.java
  QuintaryGAFInfo.java
  Rule.java
  RuleInfo.java
  Symbol.java
  SymbolInfo.java
  TernaryGAF.java
  TernaryGAFInfo.java
  TimePoint.java
  TimePointInfo.java
  UnaryGAF.java
  UnaryGAFInfo.java
  package.html