Show Rule.java syntax highlighted
/*
* Rule.java
*
* Created on March 31, 2007, 5:14 AM
*
* Description: Rule is a persistent rule 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 com.sleepycat.persist.model.Relationship;
import com.sleepycat.persist.model.SecondaryKey;
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 Rule extends AbstractBookkeepingTerm {
/** the formula term id */
@SecondaryKey(relate=Relationship.ONE_TO_ONE)
private int formulaTermId; // NOPMD
/** the rule context (i.e. Cyc microtheory) term type */
private byte contextTermType;
/** the rule context (i.e. Cyc microtheory) term id */
private int contextTermId;
/** the rule strength */
private double strength;
/** the KB partition term id */
private int kbPartitionTermId;
/** Creates a new instance of Rule. */
public Rule() {
super();
}
/** Creates a new instance of Rule.
*
* @param termId the database term ID
* @param formula the formula
* @param context the context
* @param strength the strength
* @param creator the creator
* @param creationPurpose the creation purpose
* @param creationDate the creation date
*/
public Rule(
final int termId,
final Formula formula,
final AbstractReifiedTerm context,
final Double strength,
final AbstractReifiedTerm creator,
final AbstractReifiedTerm creationPurpose,
final Date creationDate) {
this(
termId,
formula.getTermId(),
context.getTermType(),
context.getTermId(),
strength,
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 Rule.
*
* @param termId the database term ID
* @param formulaTermId the formula term id
* @param contextTermType the context term type
* @param contextTermId the context term id
* @param strength the strength
* @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 Rule(
final int termId,
final int formulaTermId,
final byte contextTermType,
final int contextTermId,
final Double strength,
final byte creatorTermType,
final int creatorTermId,
final byte creationPurposeTermType,
final int creationPurposeTermId,
final Date creationDate) {
super(
termId,
creatorTermType,
creatorTermId,
creationPurposeTermType,
creationPurposeTermId,
creationDate);
//Preconditions
assert formulaTermId != 0 : "formulaTermId must not be zero";
assert contextTermType != 0 : "contextTermType must not be zero";
assert contextTermId != 0 : "contextTermId must not be zero";
assert strength != null : "strength must not be null";
assert creationDate != null : "creationDate must not be null";
this.formulaTermId = formulaTermId;
this.contextTermType = contextTermType;
this.contextTermId = contextTermId;
this.strength = strength;
}
/** Gets the term type.
*
* @return the term type
*/
public byte getTermType() {
return Constants.RULE;
}
/** Gets the formula.
*
* @param kbPartitionFacade the KB partition facade
* @return the formula
*/
public Formula getFormula(final KBPartitionFacadeLocal kbPartitionFacade) {
if (kbPartitionFacade == null) {
throw new TexaiException("kbPartitionFacade 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();
}
/** Sets the formula term id.
*
* @param formulaTermId 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 term id.
*
* @return the formula term id
*/
public int getFormulaTermId() {
return formulaTermId;
}
/** Gets the context term type.
*
* @return the context term type
*/
public byte getContextTermType() {
return contextTermType;
}
/** Sets the context term type.
*
* @param contextTermType the context term type
*/
public void setContextTermType(final byte contextTermType) {
this.contextTermType = contextTermType;
}
/** Gets the context term id.
*
* @return the context term id
*/
public int getContextTermId() {
return contextTermId;
}
/** Sets the context term id.
*
* @param contextTermId the context term id
*/
public void setContextTermId(final int contextTermId) {
//Preconditions
assert contextTermId != 0 : "contextTermId must not be zero";
this.contextTermId = contextTermId;
}
/** Gets the context
*
* @param kbPartitionFacade the KB partition facade
* @return the context
*/
public AbstractReifiedTerm getContext(final KBPartitionFacadeLocal kbPartitionFacade) {
if (kbPartitionFacade == null) {
throw new TexaiException("kbPartitionFacade must not be null");
}
return (AbstractReifiedTerm) kbPartitionFacade.findTermByTermTypeAndId(contextTermType, contextTermId);
}
/** Sets the context.
*
* @param context the context
*/
public void setContext(final AbstractReifiedTerm context) {
//Preconditions
assert context != null : "context must not be null";
contextTermType = context.getTermType();
contextTermId = context.getTermId();
}
/** Gets the strength.
*
* @return the strength
*/
public Double getStrength() {
return strength;
}
/** Sets the strength.
*
* @param strength the strength
*/
public void setStrength(final Double strength) {
this.strength = strength;
}
/**
* 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 Rule)) {
return false;
}
final Rule that = (Rule) 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);
}
}
See more files for this project here