BinaryGAF.java from Texai at Krugle
Show BinaryGAF.java syntax highlighted
/*
* BinaryGAF.java
*
* Created on March 30, 2007, 4:07 PM
*
* Description: BinaryGAF is a persistent binary ground atomic formula (unary propostion) 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 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 BinaryGAF extends AbstractGAF {
/** the first argument term type */
private byte arg1TermType;
/** the first argument term id */
@SecondaryKey(relate=Relationship.MANY_TO_ONE)
private int arg1TermId;
/** the second argument term type */
private byte arg2TermType;
/** the second argument term id */
@SecondaryKey(relate=Relationship.MANY_TO_ONE)
private int arg2TermId;
/** Creates a new instance of BinaryGAF. */
public BinaryGAF() {
super();
}
/** Creates a new instance of BinaryGAF.
*
* @param termId the database term ID
* @param predicate the predicate
* @param args the argument terms
* @param context the assertion context (i.e. Cyc microtheory)
* @param strength the assertion strength
* @param generatedPhrase the generated phrase for this ground atomic formula
* @param creator the creator
* @param creationPurpose the creation purpose
* @param creationDate the creation date
*/
public BinaryGAF(
final int termId,
final AbstractReifiedTerm predicate,
final List<AbstractTerm> args,
final AbstractReifiedTerm context,
final double strength,
final String generatedPhrase,
final AbstractReifiedTerm creator,
final AbstractReifiedTerm creationPurpose,
final Date creationDate) {
this(
termId,
predicate.getTermType(),
predicate.getTermId(),
args.get(Constants.ARG1_INDEX).getTermType(),
args.get(Constants.ARG1_INDEX).getTermId(),
args.get(Constants.ARG2_INDEX).getTermType(),
args.get(Constants.ARG2_INDEX).getTermId(),
context.getTermType(),
context.getTermId(),
strength,
generatedPhrase,
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 BinaryGAF.
*
* @param termId the database term ID
* @param termId the database term ID
* @param predicateTermType the predicate term type
* @param predicateTermId the predicate term id
* @param arg1TermType the first argument term type
* @param arg1TermId the first argument term id
* @param arg2TermType the second argument term type
* @param arg2TermId the second argument term id
* @param contextTermType the assertion context (i.e. Cyc microtheory) term type
* @param contextTermId the assertion context (i.e. Cyc microtheory) term id
* @param strength the assertion strength
* @param generatedPhrase the generated phrase for this ground atomic formula
* @param creatorTermType the creator term type of this ground atomic formula
* @param creatorTermId the creator term id of this ground atomic formula
* @param creationPurposeTermType the creation purpose term type of this ground atomic formula
* @param creationPurposeTermId the creation purpose term id of this ground atomic formula
* @param creationDate the creation date
*/
public BinaryGAF(
final int termId,
final byte predicateTermType,
final int predicateTermId,
final byte arg1TermType,
final int arg1TermId,
final byte arg2TermType,
final int arg2TermId,
final byte contextTermType,
final int contextTermId,
final double strength,
final String generatedPhrase,
final byte creatorTermType,
final int creatorTermId,
final byte creationPurposeTermType,
final int creationPurposeTermId,
final Date creationDate) {
super(
termId,
predicateTermType,
predicateTermId,
contextTermType,
contextTermId,
strength,
generatedPhrase,
creatorTermType,
creatorTermId,
creationPurposeTermType,
creationPurposeTermId,
creationDate);
//Preconditions
assert arg1TermType != 0 : "arg1TermType must not be zero";
assert arg1TermId != 0 : "arg1TermId must not be zero";
assert arg2TermType != 0 : "arg2TermType must not be zero";
assert arg2TermId != 0 : "arg2TermId must not be zero";
this.arg1TermType = arg1TermType;
this.arg1TermId = arg1TermId;
this.arg2TermType = arg2TermType;
this.arg2TermId = arg2TermId;
}
/** Gets the term type.
*
* @return the term type
*/
public byte getTermType() {
return Constants.BINARY_GAF;
}
/** Returns the arguments.
*
* @param kbPartitionFacade the KB partition facade
* @return the arguments
*/
public List<AbstractTerm> getArgs(final KBPartitionFacadeLocal kbPartitionFacade) {
//Preconditions
assert kbPartitionFacade != null : "kbPartitionFacade must not be null";
final List<AbstractTerm> args = new ArrayList<AbstractTerm>(Constants.BINARY_ARGS_SIZE);
final AbstractTerm arg1 = getArg1(kbPartitionFacade);
assert arg1 != null : "arg1 must not be null, type " + arg1TermType + ", term id " + arg1TermId;
args.add(arg1);
final AbstractTerm arg2 = getArg2(kbPartitionFacade);
assert arg2 != null : "arg2 must not be null, type " + arg2TermType + ", term id " + arg2TermId;
args.add(arg2);
return args;
}
/** Sets the arguments.
*
* @param args the list of two argument terms
*/
public void setArgs(final List<AbstractTerm> args) {
//Preconditions
assert args != null : "args must not be null";
assert args.size() == Constants.BINARY_ARGS_SIZE : "args must be size 2";
setArg1(args.get(0));
setArg2(args.get(1));
}
/** Gets the arg1 term type.
*
* @return the arg1 term type
*/
public byte getArg1TermType() {
return arg1TermType;
}
/** Sets the arg1 term type.
*
* @param arg1TermType the arg1 term type
*/
public void setArg1TermType(final byte arg1TermType) {
this.arg1TermType = arg1TermType;
}
/** Gets the arg1 term id.
*
* @return the arg1 term id
*/
public int getArg1TermId() {
return arg1TermId;
}
/** Sets the arg1 term id.
*
* @param arg1TermId the arg1 term id
*/
public void setArg1TermId(final int arg1TermId) {
//Preconditions
assert arg1TermId != 0 : "arg1TermId must not be zero";
this.arg1TermId = arg1TermId;
}
/** Gets the first argument
*
* @param kbPartitionFacade the KB partition facade
* @return the first argument
*/
public AbstractTerm getArg1(final KBPartitionFacadeLocal kbPartitionFacade) {
//Preconditions
if (kbPartitionFacade == null) {
throw new TexaiException("termLoader must not be null");
}
return kbPartitionFacade.findTermByTermTypeAndId(arg1TermType, arg1TermId);
}
/** Sets the first argument.
*
* @param arg1 the first argument
*/
public void setArg1(final AbstractTerm arg1) {
//Preconditions
assert arg1 != null : "arg1 must not be null";
arg1TermType = arg1.getTermType();
arg1TermId = arg1.getTermId();
}
/** Gets the arg2 term type
*
* @return the arg2 term type
*/
public byte getArg2TermType() {
return arg2TermType;
}
/** Gets the arg2 term type.
*
* @return the arg2 term type
*/
public void setArg2TermType(final byte arg2TermType) {
this.arg2TermType = arg2TermType;
}
/** Gets the arg2 term id.
*
* @return the arg2 term id
*/
public int getArg2TermId() {
return arg2TermId;
}
/** Sets the arg2 term id.
*
* @param arg2TermId the arg2 term id
*/
public void setArg2TermId(final int arg2TermId) {
//Preconditions
assert arg2TermId != 0 : "arg2TermId must not be zero";
this.arg2TermId = arg2TermId;
}
/** Gets the second argument
*
* @param kbPartitionFacade the KB partition facade
* @return the second argument
*/
public AbstractTerm getArg2(final KBPartitionFacadeLocal kbPartitionFacade) {
//Preconditions
if (kbPartitionFacade == null) {
throw new TexaiException("termLoader must not be null");
}
return kbPartitionFacade.findTermByTermTypeAndId(arg2TermType, arg2TermId);
}
/** Sets the second argument.
*
* @param arg2 the second argument
*/
public void setArg2(final AbstractTerm arg2) {
//Preconditions
assert arg2 != null : "arg2 must not be null";
arg2TermType = arg2.getTermType();
arg2TermId = arg2.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 BinaryGAF)) {
return false;
}
final BinaryGAF that = (BinaryGAF) 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
*/
@Override
public String toString(final KBPartitionFacadeLocal kbPartitionFacade) {
//Preconditions
assert kbPartitionFacade != null : "kbPartitionFacade must not be null";
final StringBuilder stringBuilder = new StringBuilder(Constants.STRING_BUILDER_SIZE);
stringBuilder.append(getPredicate(kbPartitionFacade).toString(kbPartitionFacade));
stringBuilder.append("(");
final List<AbstractTerm> args = getArgs(kbPartitionFacade);
final int arity = args.size();
for (int i = 0; i < arity; i++) {
stringBuilder.append(args.get(i).toString());
if (i < arity - 1) {
stringBuilder.append(", ");
}
}
stringBuilder.append(")");
return stringBuilder.toString();
}
/** 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) {
//Preconditions
assert kbPartitionFacade != null : "kbPartitionFacade must not be null";
final StringBuilder stringBuilder = new StringBuilder(Constants.STRING_BUILDER_SIZE);
stringBuilder.append("(");
stringBuilder.append(getPredicate(kbPartitionFacade).toCycLString(kbPartitionFacade));
final List<AbstractTerm> args = getArgs(kbPartitionFacade);
final int arity = args.size();
for (int i = 0; i < arity; i++) {
stringBuilder.append(" ");
stringBuilder.append(args.get(i).toCycLString(kbPartitionFacade));
}
stringBuilder.append(")");
return stringBuilder.toString();
}
}
See more files for this project here