AssociationFinderBean.java from Texai at Krugle
Show AssociationFinderBean.java syntax highlighted
/*
* AssociationFinderBean.java
*
* Created on November 1, 2006, 3:37 PM
*
* Description: Finds the associated terms given partial constitutents of propositions.
*
* 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.session.shared;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityIndex;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.SecondaryIndex;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.Stack;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.log4j.Logger;
import org.texai.kb.Constants;
import org.texai.kb.entity.AtomicTerm;
import org.texai.kb.entity.BinaryGAF;
import org.texai.kb.entity.AbstractReifiedTerm;
import org.texai.kb.entity.AbstractTerm;
import org.texai.kb.entity.StubTerm;
import org.texai.util.ByteUtils;
import org.texai.util.TexaiException;
/**
*
* @author reed
*/
@Stateless
public class AssociationFinderBean implements AssociationFinderLocal {
/** the logger */
private Logger logger; // NOPMD
/** the entity store */
private EntityStore entityStore;
/** the primary index for binary GAFS and their subclasses */
private PrimaryIndex<Integer, BinaryGAF> binaryGAFByTermId;
/** the predicate secondary index for binary GAFs */
private SecondaryIndex<Integer, Integer, BinaryGAF> binaryGAFByPredicateTermId;
/** the arg1 secondary index for GAFs */
private SecondaryIndex<Integer, Integer, BinaryGAF> binaryGAFByArg1TermId;
/** the arg2 secondary index for GAFs */
private SecondaryIndex<Integer, Integer, BinaryGAF> binaryGAFByArg2TermId;
/** Creates a new instance of AssociationFinderBean */
public AssociationFinderBean() {
super();
logger = Logger.getLogger(AssociationFinderBean.class.getName());
}
/** Gathers the stub terms and stub contexts from the arg1 position of matching binary ground atomic formulas in every context.
*
* @param predicate the predicate of matching binary gafs
* @param arg2 the first argument term of matching binary gafs
* @return the set of pairs of stub terms from the arg1 position of matching binary ground atomic formulas and
* the stub context in which they were found
*/
public Set<List<AbstractTerm>> gatherArg1TermsAndContextsFromBinaryGAFs(
final AtomicTerm predicate,
final AbstractTerm arg2) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert arg2 != null : "arg2 must not be null";
final Set<List<AbstractTerm>> pairs = new HashSet<List<AbstractTerm>>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg2TermId.subIndex(arg2.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()) {
final List<AbstractTerm> pair = new ArrayList<AbstractTerm>(2); // NOPMD
pair.add(new StubTerm(binaryGAF.getArg1TermType(), binaryGAF.getArg1TermId()));
pair.add(new StubTerm(binaryGAF.getContextTermType(), binaryGAF.getContextTermId()));
pairs.add(pair);
}
}
logger.debug("gatherArg1TermsAndContextsFromBinaryGAFs(" + predicate + ", " + arg2 + "): " + pairs);
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return pairs;
}
/** Gathers the stub terms and stub contexts from the arg2 position of matching binary ground atomic formulas in every context.
*
* @param predicate the predicate of matching binary gafs
* @param arg1 the first argument term of matching binary gafs
* @return the set of pairs of stub terms from the arg2 position of matching binary ground atomic formulas and
* the stub context in which they were found
*/
public Set<List<AbstractTerm>> gatherArg2TermsAndContextsFromBinaryGAFs(
final AtomicTerm predicate,
final AbstractTerm arg1) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert arg1 != null : "arg1 must not be null";
final Set<List<AbstractTerm>> pairs = new HashSet<List<AbstractTerm>>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg1TermId.subIndex(arg1.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()) {
final List<AbstractTerm> pair = new ArrayList<AbstractTerm>(2); // NOPMD
pair.add(new StubTerm(binaryGAF.getArg1TermType(), binaryGAF.getArg1TermId()));
pair.add(new StubTerm(binaryGAF.getContextTermType(), binaryGAF.getContextTermId()));
pairs.add(pair);
}
}
logger.debug("gatherArg2TermsAndContextsFromBinaryGAFs(" + predicate + ", " + arg1 + "): " + pairs);
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return pairs;
}
/** Gathers ordered list of the stub terms, strengths and stub contexts from the arg1 position of matching
* binary ground atomic formulas in every context.
*
* @param predicate the predicate of matching binary gafs
* @param arg2 the second argument term of matching binary gafs
* @return the ordered list of triples of stub terms from the arg1 position of matching binary ground atomic formulas, the strength and
* the stub context in which they were found
*/
public List<List<Object>> gatherArg1TermsStrengthsAndContextsFromBinaryGAFs(
final AtomicTerm predicate,
final AbstractTerm arg2) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert arg2 != null : "arg2 must not be null";
final List<List<Object>> triples = new ArrayList<List<Object>>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg2TermId.subIndex(arg2.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()) {
final List<Object> triple = new ArrayList<Object>(3); // NOPMD
triple.add(new StubTerm(binaryGAF.getArg1TermType(), binaryGAF.getArg1TermId()));
triple.add(binaryGAF.getStrength());
triple.add(new StubTerm(binaryGAF.getContextTermType(), binaryGAF.getContextTermId()));
triples.add(triple);
}
}
logger.debug("gatherArg1TermsStrengthsAndContextsFromBinaryGAFs(" + predicate + ", " + arg2 + "): " + triples);
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return triples;
}
/** Gathers ordered list of the stub terms, strengths and stub contexts from the arg2 position of matching
* binary ground atomic formulas in every context.
*
* @param predicate the predicate of matching binary gafs
* @param arg1 the first argument term of matching binary gafs
* @return the ordered list of triples of stub terms from the arg2 position of matching binary ground atomic formulas, the strength and
* the stub context in which they were found
*/
public List<List<Object>> gatherArg2TermsStrengthsAndContextsFromBinaryGAFs(
final AtomicTerm predicate,
final AbstractTerm arg1) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert arg1 != null : "arg1 must not be null";
final List<List<Object>> triples = new ArrayList<List<Object>>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg1TermId.subIndex(arg1.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()) {
final List<Object> triple = new ArrayList<Object>(3); // NOPMD
triple.add(new StubTerm(binaryGAF.getArg1TermType(), binaryGAF.getArg1TermId()));
triple.add(binaryGAF.getStrength());
triple.add(new StubTerm(binaryGAF.getContextTermType(), binaryGAF.getContextTermId()));
triples.add(triple);
}
}
logger.debug("gatherArg2TermsStrengthsAndContextsFromBinaryGAFs(" + predicate + ", " + arg1 + "): " + triples);
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return triples;
}
/** Gathers the stub terms from the arg2 position of matching binary ground atomic formulas that
* occur only in the given context.
*
* @param predicate the predicate of matching binary gafs
* @param arg1 the first argument term of matching binary gafs
* @param context the most specific context of those from which matching gafs are gathered
* @return the stub terms from the arg2 position of match binary ground atomic formulas
*/
public Set<AbstractTerm> gatherArg2TermsFromBinaryGAFsWithinContext(
final AtomicTerm predicate,
final AbstractTerm arg1,
final AbstractReifiedTerm context) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert arg1 != null : "arg1 must not be null";
assert context != null : "context must not be null";
final Set<AbstractTerm> arg2s = new HashSet<AbstractTerm>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg1TermId.subIndex(arg1.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()) {
if (binaryGAF.getContextTermId() == context.getTermId()) {
arg2s.add(new StubTerm(binaryGAF.getArg2TermType(), binaryGAF.getArg2TermId()));
}
}
}
logger.debug("gatherArg2TermsFromBinaryGAFsWithinContext(" + predicate + ", " + arg1 + ", " + context + "): " + arg2s);
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return arg2s;
}
/** Gathers the term ids from the arg1 position of matching binary ground atomic formulas that
* occur only in the given context.
*
* @param predicate the predicate of matching binary gafs
* @param arg2 the first argument term of matching binary gafs
* @param context the most specific context of those from which matching gafs are gathered
* @return the term ids from the arg1 position of match binary ground atomic formulas
*/
public Set<Integer> gatherArg1TermIdsFromBinaryGAFsWithinContext(
final AtomicTerm predicate,
final AbstractTerm arg2,
final AbstractReifiedTerm context) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert arg2 != null : "arg2 must not be null";
assert context != null : "context must not be null";
final Set<Integer> arg1TermIds = new HashSet<Integer>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg2TermId.subIndex(arg2.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()
&& binaryGAF.getContextTermId() == context.getTermId()) {
arg1TermIds.add(binaryGAF.getArg1TermId());
}
}
logger.debug("gatherArg1TermsIdsFromBinaryGAFsWithinContext(" + predicate + ", " + arg2 + ", " + context + "): " + arg1TermIds);
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return arg1TermIds;
}
/** Gathers the binary ground atomic formulas having the given arg1 or arg2 from all contexts.
*
* @param arg the first or second argument term of matching binary gafs
* @return the binary ground atomic formulas having the given arg
*/
public Set<BinaryGAF> gatherBinaryGAFsByArgTerm(final AbstractTerm arg) {
//Preconditions
assert arg != null : "arg must not be null";
final Set<BinaryGAF> binaryGAFs = new HashSet<BinaryGAF>();
try {
EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg1TermId.subIndex(arg.getTermId());
EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
binaryGAFs.add(binaryGAF);
}
entityIndex = binaryGAFByArg2TermId.subIndex(arg.getTermId());
entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
binaryGAFs.add(binaryGAF);
}
logger.debug("gatherBinaryGAFsByArgTerm(" + arg + "): " + binaryGAFs); // NOPMD
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return binaryGAFs;
}
/** Gathers the binary ground atomic formulas having the given arg1 and context.
*
* @param arg1 the first argument term of matching binary gafs
* @param context the context of those from which matching gafs are gathered
* @return the binary ground atomic formulas having the given arg1 and context
*/
public Set<BinaryGAF> gatherBinaryGAFsByArg1TermWithinContext(
final AbstractTerm arg1,
final AbstractReifiedTerm context) {
final Set<BinaryGAF> binaryGAFs = new HashSet<BinaryGAF>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex = binaryGAFByArg1TermId.subIndex(arg1.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getContextTermId() == context.getTermId()) {
binaryGAFs.add(binaryGAF);
}
}
logger.debug("gatherBinaryGAFsByArg1TermWithinContext(" + arg1 + ", " + context + "): " + binaryGAFs); // NOPMD
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return binaryGAFs;
}
/** Gathers the list of term ids for binary gafs having the given predicate and context.
*
* @param predicate the predicate of matching binary gafs
* @param context the most specific context of those from which matching gafs are gathered
* @return the list of term ids for binary gafs having the given predicate and context
*/
public List<Integer> gatherBinaryGAFTermIdsByPredicate(final AtomicTerm predicate, final AbstractReifiedTerm context) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert context != null : "context must not be null";
final List<Integer> termIds = new ArrayList<Integer>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex =
binaryGAFByPredicateTermId.subIndex(predicate.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()) {
termIds.add(binaryGAF.getTermId());
}
}
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return termIds;
}
/** Gathers the list of info for binary gafs having the given predicate and context. Each element is a
* triple consisting of [term id, arg1 term id, context term id].
*
* @param predicate the predicate of matching binary gafs
* @param context the most specific context of those from which matching gafs are gathered
* @return the list of info for binary gafs having the given predicate and context
*/
public List<Integer[]> gatherBinaryGAFInfoByPredicate(final AtomicTerm predicate, final AbstractReifiedTerm context) {
//Preconditions
assert predicate != null : "predicate must not be null";
assert context != null : "context must not be null";
final List<Integer[]> triples = new ArrayList<Integer[]>();
try {
final EntityIndex<Integer, BinaryGAF> entityIndex =
binaryGAFByPredicateTermId.subIndex(predicate.getTermId());
final EntityCursor<BinaryGAF> entityCursor = entityIndex.entities();
for (final BinaryGAF binaryGAF : entityCursor) {
if (binaryGAF.getPredicateTermId() == predicate.getTermId()) {
final Integer[] triple = new Integer[3]; // NOPMD
triple[0] = binaryGAF.getTermId();
triple[1] = binaryGAF.getArg1TermId();
triple[2] = binaryGAF.getContextTermId();
triples.add(triple);
}
}
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
return triples;
}
/** Sets the entity store.
*
* @param entityStore
*/
public void setEntityStore(final EntityStore entityStore) {
//Preconditions
assert entityStore != null : "entityStore must not be null";
this.entityStore = entityStore;
}
/** Initializes the primary and secondary indicies. */
public void initializeIndices() {
//Preconditions
assert entityStore != null : "entityStore must not be null";
try {
// primary indicies
binaryGAFByTermId = entityStore.getPrimaryIndex(Integer.class, BinaryGAF.class);
// secondary indicies
binaryGAFByPredicateTermId = entityStore.getSecondaryIndex(binaryGAFByTermId, Integer.class, Constants.FIELD_PREDICATE_TERM_ID);
binaryGAFByArg1TermId = entityStore.getSecondaryIndex(binaryGAFByTermId, Integer.class, Constants.FIELD_ARG1_TERM_ID);
binaryGAFByArg2TermId = entityStore.getSecondaryIndex(binaryGAFByTermId, Integer.class, Constants.FIELD_ARG2_TERM_ID);
} catch (final DatabaseException ex) {
throw new TexaiException(ex);
}
//Postconditions
assert binaryGAFByTermId != null : "binaryGAFByTermId must not be null";
assert binaryGAFByPredicateTermId != null : "binaryGAFByPredicateTermId must not be null";
assert binaryGAFByArg1TermId != null : "binaryGAFByArg1TermId must not be null";
assert binaryGAFByArg2TermId != null : "binaryGAFByArg2TermId must not be null";
}
}
See more files for this project here