Code Search for Developers
 
 
  

Formula.java from Texai at Krugle


Show Formula.java syntax highlighted

/*
 * Formula.java
 *
 * Created on March 30, 2007, 4:07 PM
 *
 * Description:
 *
 * 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.kb.entity;

import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.texai.kb.Constants;
import org.texai.kb.ejb.session.shared.KBPartitionFacadeLocal;
import org.texai.util.TexaiException;

/**
 *
 * @author reed
 */
@Entity
public class Formula extends AbstractBookkeepingTerm {
  
  /** the types for terms in this formula */
  private byte[] formulaTermTypes;                         // NOPMD

  /** the ids for terms in this formula */
  private int[] formulaTermIds;                         // NOPMD

  /** the formula string hash code */
  @SecondaryKey(relate=Relationship.MANY_TO_ONE)
  private int formulaStringHashCode;
  
  /** the formula string */
  private String formulaString;                            // NOPMD

  /** Creates a new instance of Formula. */
  public Formula() {
    super();
  }
  
  /** Creates a new instance of Formula.
   *
   * @param termId the database term ID
   * @param formulaTerms the terms in this formula
   * @param creator the creator 
   * @param creationPurpose the creation purpose
   * @param creationDate the creation date
   */
  public Formula(
          final int termId,
          final List<AbstractTerm> formulaTerms,
          final AbstractReifiedTerm creator,
          final AbstractReifiedTerm creationPurpose,
          final Date creationDate) {
    this(
            termId,
            getTermTypes(formulaTerms),
            getTermIds(formulaTerms),
            composeFormulaStringFromTermList(formulaTerms),
            creator == null ? (byte) 0 : creator.getTermType(),
            creator == null ? 0 : creator.getTermId(),
            creationPurpose == null ? (byte) 0 : creationPurpose.getTermType(),
            creationPurpose == null ? 0 : creationPurpose.getTermId(),
            creationDate);
  }

  /** Creates a new instance of Formula that omits the term list which will be subsequently added.
   *
   * @param termId the database term ID
   * @param formulaTerms the terms in this formula
   * @param creator the creator 
   * @param creationPurpose the creation purpose
   * @param creationDate the creation date
   */
  public Formula(
          final int termId,
          final String formulaString,
          final AbstractReifiedTerm creator,
          final AbstractReifiedTerm creationPurpose,
          final Date creationDate) {
    super(
          termId,
          creator,
          creationPurpose,
          creationDate);
    //Preconditions
    assert formulaString != null : "formulaString must not be null";
    
    this.formulaString = formulaString;
    formulaStringHashCode = formulaString.hashCode();
  }

  /** Creates a new instance of Formula.
   *
   * @param termId the database term ID
   * @param formulaTermTypes the types for terms in this formula
   * @param formulaTermIds the ids for terms in this formula
   * @param formulaString the formula string
   * @param creatorTermType the creator term type
   * @param creatorTermId the creator term id
   * @param creationPurposeTermType the creation purpose term type
   * @param creationPurposeTermId the creation purpose term id
   * @param creationDate the creation date
   */
  public Formula(
          final int termId,
          final byte[] formulaTermTypes,
          final int[] formulaTermIds,
          final String formulaString,
          final byte creatorTermType,
          final int creatorTermId,
          final byte creationPurposeTermType,
          final int creationPurposeTermId,
          final Date creationDate) {
    super(
            termId,
            creatorTermType,
            creatorTermId,
            creationPurposeTermType,
            creationPurposeTermId,
            creationDate);
    //Preconditions
    assert formulaTermIds.length > 0 : "formulaTermIds must not be empty";
    assert formulaTermIds.length == formulaTermTypes.length : 
      "formulaTermIds (" + formulaTermIds.length + ") must be same size as formulaTermTypes (" + formulaTermTypes.length + ")";
    
    this.formulaTermTypes = formulaTermTypes;
    this.formulaTermIds = formulaTermIds;
    this.formulaString = formulaString;
    formulaStringHashCode = formulaString.hashCode();
  }

  /** Returns the array of term types corresponding to the given list of terms.
   *
   * @param terms the given list of terms
   * @return the list of term types corresponding to the given list of terms
   */
  private static byte[] getTermTypes(final List<AbstractTerm> terms) {
    //Preconditions
    assert terms != null : "terms must not be null";
    
    final int terms_size = terms.size();
    final byte[] termTypes = new byte[terms_size];
    for (int i = 0; i < terms_size; i++) {
      termTypes[i] = terms.get(i).getTermType();
    }
    return termTypes;
  }
  
  /** Returns the array of term ids corresponding to the given list of terms.
   *
   * @param terms the given list of terms
   * @return the list of term ids corresponding to the given list of terms
   */
  private static int[] getTermIds(final List<AbstractTerm> terms) {
    //Preconditions
    assert terms != null : "terms must not be null";
    
    final int terms_size = terms.size();
    final int[] termIds = new int[terms_size];
    for (int i = 0; i < terms_size; i++) {
      termIds[i] = terms.get(i).getTermId();
    }
    return termIds;
  }
    
  /** Gets the term type.
   *
   * @return the term type
   */
  public byte getTermType() {
    return Constants.FORMULA;
  }

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

  /**
   * Returns a hash code value for the object. 
   * @return a hash code value for this object
   */
  @Override
  public int hashCode() {
    return getTermId();
  }

  /**
   * 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 Formula)) {
      return false;
    }
    final Formula that = (Formula) object;
    return this.getTermId() == that.getTermId();
  }

  /** Returns a string representation of this object.
   *
   * @param kbPartitionFacade the KB partition facade
   * @return a string representation of this object
   */
  public String toString(final KBPartitionFacadeLocal kbPartitionFacade) {
    return getFormulaString();
  }

  /** 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();
  }

  /** Returns the list formula terms.
   *
   * @param kbPartitionFacade the KB partition facade
   * @return the list formula terms
   */
  public List<AbstractTerm> getFormulaTerms(final KBPartitionFacadeLocal kbPartitionFacade) {
    if (kbPartitionFacade == null) {
      throw new TexaiException("kbPartitionFacade must not be null");
    }
    final List<AbstractTerm> formulaTerms = new ArrayList<AbstractTerm>(formulaTermIds.length);
    for (int i = 0; i < formulaTermIds.length; i++) {
      formulaTerms.add(kbPartitionFacade.findTermByTermTypeAndId(formulaTermTypes[i], formulaTermIds[i]));
    }
    return formulaTerms;
  }

  /** Sets the list formula terms.
   *
   * @param termList the list formula terms
   */
  public void setFormulaTerms(final List<AbstractTerm> termList) {
    //Preconditions
    assert termList != null : "termList must not be null";
    assert !termList.isEmpty() : "termList must not be empty";
    
    final int termList_size = termList.size();
    formulaTermTypes = new byte[termList_size];
    formulaTermIds = new int[termList_size];
    for (int i = 0; i < termList_size; i++) {
      final AbstractTerm term = termList.get(i);
      formulaTermTypes[i] = term.getTermType();
      formulaTermIds[i] = term.getTermId();
    }
  }

  /** Returns a CycL representation of this object.
   *
   * @param kbPartitionFacade the KB partition facade
   * @return a CycL representation of this object
   */
  public String toCycLString(final KBPartitionFacadeLocal kbPartitionFacade) {
    final StringBuilder stringBuilder = new StringBuilder(Constants.STRING_BUILDER_SIZE);
    final List<AbstractTerm> termList = getFormulaTerms(kbPartitionFacade);
    stringBuilder.append("(");
    final int termListSize = termList.size();
    for (int i = 0; i < termListSize; i++) {
      stringBuilder.append(termList.get(i).toCycLString(kbPartitionFacade));
      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);
  }
  
}




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

  AbstractBookkeepingTerm.java
  AbstractGAF.java
  AbstractReifiedTerm.java
  AbstractTerm.java
  AtomicTerm.java
  BinaryGAF.java
  Formula.java
  LocalTermIdReference.java
  NonAtomicTerm.java
  PByteArray.java
  PDate.java
  PDouble.java
  PLong.java
  PString.java
  PVariable.java
  QuaternaryGAF.java
  QuintaryGAF.java
  RemoteTermIdReference.java
  Rule.java
  StubReifiedTerm.java
  StubTerm.java
  Symbol.java
  TermIdSequence.java
  TernaryGAF.java
  UnaryGAF.java