NonAtomicTerm.java from Texai at Krugle
Show NonAtomicTerm.java syntax highlighted
/*
* NonAtomicTerm.java
*
* Created on March 31, 2007, 4:12 AM
*
* Description: NonAtomicTerm is a persistent non-atomic term in the Texai logical representation language.
*
* 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 java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.Date;
import org.texai.kb.Constants;
import org.texai.kb.ejb.session.shared.KBPartitionFacadeLocal;
import org.texai.util.TexaiException;
/**
*
* @author reed
*/
@Entity
public class NonAtomicTerm extends AbstractReifiedTerm {
/** the formula term id */
private int formulaTermId;
/** Creates a new instance of NonAtomicTerm. */
public NonAtomicTerm() {
super();
}
/** Creates a new instance of NonAtomicTerm.
*
* @param termId the database term ID
* @param termName the term name, which if "%" indicates that the term name and pretty name are generated from the term id
* @param prettyName the pretty name
* @param formula the formula
* @param creator the creator
* @param creationPurpose the creation purpose
* @param creationDate the creation date
*/
public NonAtomicTerm(
final int termId,
final String termName,
final String prettyName,
final Formula formula,
final AbstractReifiedTerm creator,
final AbstractReifiedTerm creationPurpose,
final Date creationDate) {
super(
termId,
termName,
prettyName,
creator,
creationPurpose,
creationDate);
//Preconditions
assert formula != null : "formula must not be null";
this.formulaTermId = formula.getTermId();
}
/** Creates a new instance of NonAtomicTerm.
*
* @param termId the database term ID
* @param termName the term name, which if "%" indicates that the term name and pretty name are generated from the term id
* @param prettyName the pretty name
* @param formulaTermId the formula term id
* @param creatorTermType the term type of creator term of this atomic term
* @param creatorTermId the term id of creator term of this atomic term
* @param creationPurposeTermType the term type of the creation purpose of this atomic term
* @param creationPurposeTermId the term id of the creation purpose of this atomic term
* @param creationDate the creation date
* @param kbPartitionId the KB partition term id
*/
public NonAtomicTerm(
final int termId,
final String termName,
final String prettyName,
final int formulaTermId,
final byte creatorTermType,
final int creatorTermId,
final byte creationPurposeTermType,
final int creationPurposeTermId,
final Date creationDate) {
super(
termId,
termName,
prettyName,
creatorTermType,
creatorTermId,
creationPurposeTermType,
creationPurposeTermId,
creationDate);
//Preconditions
assert formulaTermId != 0 : "formulaTermId must not be zero";
this.formulaTermId = formulaTermId;
}
/** Gets the term type.
*
* @return the term type
*/
public byte getTermType() {
return Constants.NON_ATOMIC;
}
/** Gets the formula term id.
*
* @return the formula term id
*/
public int getFormulaTermId() {
return formulaTermId;
}
/** Sets the formula term id.
*
* @param the formula term id
*/
public void setFormulaTermId(final int formulaTermId) {
//Preconditions
assert formulaTermId != 0 : "formulaTermId must not be zero";
this.formulaTermId = formulaTermId;
}
/** Gets the formula.
*
* @param kbPartitionFacade the KB partition facade
* @return the formula
*/
public Formula getFormula(final KBPartitionFacadeLocal kbPartitionFacade) {
if (kbPartitionFacade == null) {
throw new TexaiException("termLoader must not be null");
}
return (Formula) kbPartitionFacade.findTermByTermTypeAndId(Constants.FORMULA, formulaTermId);
}
/** Sets the formula.
*
* @param formula the formula
*/
public void setFormula(final Formula formula) {
//Preconditions
assert formula != null : "formula must not be null";
formulaTermId = formula.getTermId();
}
/**
* 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 NonAtomicTerm)) {
return false;
}
final NonAtomicTerm that = (NonAtomicTerm) 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 getFormula(kbPartitionFacade).toString(kbPartitionFacade);
}
/** 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) {
return getFormula(kbPartitionFacade).toCycLString(kbPartitionFacade);
}
/** Returns the native non-atomic formula string corresponding to the given CycL formatted string.
* (CityNamedFn "Shlomi" Israel) --> CityNamedFn("Shlomi", Israel)
*
* @param cycLString the given CycL formatted string
* @return the native non-atomic formula string
*/
public static String cycLStringToString(final String cycLString) {
final StringBuilder stringBuilder = new StringBuilder(Constants.STRING_BUILDER_SIZE);
try {
final StreamTokenizer streamTokenizer = new StreamTokenizer(new StringReader(cycLString));
streamTokenizer.resetSyntax();
streamTokenizer.quoteChar('"');
streamTokenizer.wordChars('0', '9');
streamTokenizer.wordChars('a', 'z');
streamTokenizer.wordChars('A', 'Z');
streamTokenizer.wordChars('-', '-');
boolean isLeftParenParsed = false;
int token = streamTokenizer.nextToken();
while (token != StreamTokenizer.TT_EOF) {
if (token == '(') {
isLeftParenParsed = true;
} else if (token == ' ') {
if (isLeftParenParsed) {
isLeftParenParsed = false;
stringBuilder.append("(");
} else {
stringBuilder.append(", ");
}
} else if (streamTokenizer.sval == null) {
stringBuilder.append((char) token);
} else if (token == '"') {
stringBuilder.append('"');
stringBuilder.append(streamTokenizer.sval);
stringBuilder.append('"');
} else {
stringBuilder.append(streamTokenizer.sval);
}
token = streamTokenizer.nextToken();
}
} catch (final IOException ex) {
throw new TexaiException(ex);
}
return stringBuilder.toString();
}
}
See more files for this project here