NonAtomicTerm.java from Texai at Krugle
Show NonAtomicTerm.java syntax highlighted
/*
* NonAtomicTerm.java
*
* Created on October 15, 2006, 11:01 PM
*
* Description:
*
* 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.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import org.texai.kb.Constants;
import org.texai.util.TexaiException;
/**
* Entity class NonAtomicTerm
*
* @author reed
*/
@Entity
@NamedQuery(name=Constants.QUERY_FIND_NON_ATOMIC_TERM_BY_TERM_NAME,
query="SELECT n FROM NonAtomicTerm n, NonAtomicTermInfo ni WHERE ni.termName = :termName AND ni.nonAtomicTerm = n")
public class NonAtomicTerm extends AbstractReifiedTerm {
/**
* 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 non-atomic term information */
@OneToOne(fetch=FetchType.LAZY, mappedBy = "nonAtomicTerm", cascade=CascadeType.ALL)
private NonAtomicTermInfo nonAtomicTermInfo; // NOPMD
/** Creates a new instance of NonAtomicTerm. */
public NonAtomicTerm() {
super();
}
/** Creates a new instance of NonAtomicTerm.
*
* @param termName the term name
* @param prettyName the pretty name
* @param uuid the uuid string
* @param formula the formula
* @param creator the creator
* @param creationPurpose the creation purpose
* @param creationTimePoint the creation time point
*/
public NonAtomicTerm(
final String termName,
final String prettyName,
final String uuid,
final Formula formula,
final AbstractReifiedTerm creator,
final AbstractReifiedTerm creationPurpose,
final TimePoint creationTimePoint) {
super();
nonAtomicTermInfo = new NonAtomicTermInfo(
this,
termName,
prettyName,
uuid,
formula,
creator,
creationPurpose,
creationTimePoint);
}
/** Creates a new instance of NonAtomicTerm.
*
* @param prettyName the pretty name
* @param uuid the uuid string
* @param formula the formula
* @param creator the creator
* @param creationPurpose the creation purpose
* @param creationTimePoint the creation time point
*/
public NonAtomicTerm(
final String prettyName,
final String uuid,
final Formula formula,
final AbstractReifiedTerm creator,
final AbstractReifiedTerm creationPurpose,
final TimePoint creationTimePoint) {
super();
nonAtomicTermInfo = new NonAtomicTermInfo(
this,
null,
prettyName,
uuid,
formula,
creator,
creationPurpose,
creationTimePoint);
}
/** Creates a new instance of NonAtomicTerm with a generated UUID string.
*
* @param prettyName the pretty name
* @param formula the formula
* @param creator the creator
* @param creationPurpose the creation purpose
* @param creationTimePoint the creation time point
*/
public NonAtomicTerm(
final String prettyName,
final Formula formula,
final AbstractReifiedTerm creator,
final AbstractReifiedTerm creationPurpose,
final TimePoint creationTimePoint) {
super();
nonAtomicTermInfo = new NonAtomicTermInfo(
this,
null,
prettyName,
null,
formula,
creator,
creationPurpose,
creationTimePoint);
}
/** Gets the non-atomic term information.
*
* @return the non-atomic term information
*/
public NonAtomicTermInfo getNonAtomicTermInfo() {
return nonAtomicTermInfo;
}
/** Returns the formula.
*
* @return the formula
*/
public Formula getFormula() {
return getNonAtomicTermInfo().getFormula();
}
/** Gets the creator.
*
* @return the creator
*/
public AbstractReifiedTerm getCreator() {
return getNonAtomicTermInfo().getCreator();
}
/** Gets the creation purpose.
*
* @return the creation purpose
*/
public AbstractReifiedTerm getCreationPurpose() {
return getNonAtomicTermInfo().getCreationPurpose();
}
/** Gets the creation time point.
*
* @return the creation time point
*/
public TimePoint getCreationTimePoint() {
return getNonAtomicTermInfo().getCreationTimePoint();
}
/**
* Returns a hash code value for the object. This implementation computes
* a hash code value based on the unchanging termName.
* @return a hash code value for this object
*/
@Override
public int hashCode() {
return getNonAtomicTermInfo().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 NonAtomicTerm)) {
return false;
}
final NonAtomicTerm that = (NonAtomicTerm) object;
return this.getNonAtomicTermInfo().getFormula().equals(that.getNonAtomicTermInfo().getFormula());
}
/** Returns a CycL representation of this object.
*
* @return a CycL representation of this object
*/
public String toCycLString() {
return getNonAtomicTermInfo().toCycLString();
}
/** Returns a string representation of this object.
*
* @return a string representation of this object
*/
@Override
public String toString() {
return getNonAtomicTermInfo().toString();
}
/** 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