Code Search for Developers
 
 
  

DomainEntityManagerBean.java from Texai at Krugle


Show DomainEntityManagerBean.java syntax highlighted

/*
 * DomainEntityManagerBean.java
 *
 * Created on March 6, 2007, 11:37 AM
 *
 * Description: Provides a facade for the commonly used domain entity persistence methods.  Each
 * application thread must use its own instance of DomainEntityManagerBean.
 *
 * 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.ejb.session;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.EnvironmentMutableConfig;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.TransactionConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;
import org.texai.kb.Constants;
import org.texai.kb.ejb.session.shared.AssociationEditorBean;
import org.texai.kb.ejb.session.shared.AssociationEditorLocal;
import org.texai.kb.ejb.session.shared.AssociationFinderBean;
import org.texai.kb.ejb.session.shared.AssociationFinderLocal;
import org.texai.kb.ejb.session.shared.TermDefinitionAccessorBean;
import org.texai.kb.ejb.session.shared.TermDefinitionAccessorLocal;
import org.texai.kb.ejb.session.shared.TermDeleterFacadeBean;
import org.texai.kb.ejb.session.shared.TermDeleterFacadeLocal;
import org.texai.kb.ejb.session.shared.TermFinderFacadeBean;
import org.texai.kb.ejb.session.shared.TermFinderFacadeLocal;
import org.texai.kb.entity.AtomicTerm;
import org.texai.kb.entity.BinaryGAF;
import org.texai.kb.entity.Formula;
import org.texai.kb.entity.AbstractReifiedTerm;
import org.texai.kb.entity.Symbol;
import org.texai.kb.entity.AbstractTerm;
import org.texai.kb.entity.helper.TermLoaderBean;
import org.texai.kb.entity.helper.TermLoaderLocal;
import org.texai.util.TexaiException;

/**
 *
 * @author reed
 */
@Stateless
public class DomainEntityManagerBean implements DomainEntityManagerLocal {
  
  /** the Oracle Berkeley DB environment, shared among all instances */
  private static Environment environment;
  
  /** the entity store */
  private static EntityStore entityStore;
  
  /** the term finder, which is injected by the container */
  @EJB
  private TermFinderFacadeLocal termFinderFacade;          // NOPMD
  
  /** the term deleter, which is injected by the container */
  @EJB
  private TermDeleterFacadeLocal termDeleterFacade;        // NOPMD
  
  /** the association finder, which is injected by the container */
  @EJB
  private AssociationFinderLocal associationFinder;        // NOPMD
  
  /** the association editor, which is injected by the container */
  @EJB
  private AssociationEditorLocal associationEditor;        // NOPMD
  
  /** the term definition accessor, which is injected by the container */
  @EJB
  private TermDefinitionAccessorLocal termDefinitionAccessor;        // NOPMD
  
  /** the domain entity loader, which is injected by the container */
  @EJB
  private DomainEntityLoaderLocal domainEntityLoader;        // NOPMD
  
  /** the domain entity persister, which is injected by the container */
  @EJB
  private DomainEntityPersisterLocal domainEntityPersister;        // NOPMD
  
  /** the domain entity deleter, which is injected by the container */
  @EJB
  private DomainEntityDeleterLocal domainEntityDeleter;        // NOPMD
  
  /** the term loader */
  @EJB
  private TermLoaderLocal termLoader;
  
  /** the home directory of the Oracle Berkeley DB, which is injected by the container */
  private String environmentPathName = Constants.ORACLE_BERKELEY_DB_HOME;
  
  /** the creator */
  private AbstractReifiedTerm creator;
  
  /** the creation purpose */
  private AbstractReifiedTerm creationPurpose;
  
  /** the logger */
  private transient Logger logger;                         // NOPMD
  
  /** the transaction configuration */
  private transient TransactionConfig transactionConfig;
  
  /** the transaction */
  private transient Transaction transaction;
  
  /** Creates a new instance of DomainEntityManagerBean. */
  public DomainEntityManagerBean() {
    super();
    logger = Logger.getLogger(DomainEntityManagerBean.class);
  }
  
  /** Closes the environment and entity store. */
  public static void close() {
    try {
      entityStore.close();
      environment.close();
    } catch (final DatabaseException ex) {
      throw new TexaiException(ex);
    }
  }
  
  /** Sets the home directory of the Oracle Berkeley DB.
   *
   * @param environmentPathName the home directory of the Oracle Berkeley DB
   */
  public void setEnvironmentPathName(final String environmentPathName) {
    //Preconditions
    assert environmentPathName != null : "environmentPathName must not be null";
    assert !environmentPathName.isEmpty() : "environmentPathName must not be an empty string";
    
    this.environmentPathName = environmentPathName;
  }
  
  /** Injects the shared session bean dependencies when executed out of the container, and lazily initializes
    the database environment and entity store that are shared among all threads. */
  public void injectSharedBeanDependencies() {
    if (environment == null) {
      logger.info("  initializing Oracle Berkeley DB Java Edition");
      final StoreConfig storeConfig = new StoreConfig();
      final boolean readOnly = false;
      storeConfig.setReadOnly(readOnly);
      storeConfig.setTransactional(!readOnly);
      storeConfig.setAllowCreate(!readOnly);
      transactionConfig = new TransactionConfig();
      transactionConfig.setReadUncommitted(true);
      final EnvironmentConfig environmentConfig = new EnvironmentConfig();
      environmentConfig.setReadOnly(readOnly);
      environmentConfig.setAllowCreate(!readOnly);
      environmentConfig.setTransactional(!readOnly);
      final File envHome = new File(environmentPathName);
      try {
        environment = new Environment(envHome, environmentConfig);
        final EnvironmentMutableConfig environmentMutableConfig = new EnvironmentMutableConfig();
        environmentMutableConfig.setTxnNoSync(true);
        environment.setMutableConfig(environmentMutableConfig);
        entityStore = new EntityStore(environment, "EntityStore", storeConfig);
      } catch (final DatabaseException ex) {
        throw new TexaiException(ex);
      }
    }
    // construct the shared session beans
    termFinderFacade = new TermFinderFacadeBean();
    termFinderFacade.setEntityStore(entityStore);
    termFinderFacade.initializeIndices();
    
    termDeleterFacade = new TermDeleterFacadeBean();
    termDeleterFacade.setEntityStore(entityStore);
    termDeleterFacade.initializeIndices();
    
    associationFinder = new AssociationFinderBean();
    associationFinder.setEntityStore(entityStore);
    associationFinder.initializeIndices();
    
    associationEditor = new AssociationEditorBean();
    associationEditor.setEntityStore(entityStore);
    associationEditor.initializeIndices();
    
    termDefinitionAccessor = new TermDefinitionAccessorBean();
    
    termLoader = new TermLoaderBean();
    termLoader.setEntityStore(entityStore);
    termLoader.initializeIndices();
    
    // inject their shared dependencies
    termFinderFacade.setTermDefinitionAccessor(termDefinitionAccessor);
    termFinderFacade.setTermLoader(termLoader);
    associationFinder.setTermFinderFacade(termFinderFacade);
    associationFinder.setTermLoader(termLoader);
    associationEditor.setTermFinderFacade(termFinderFacade);
    termDefinitionAccessor.setTermFinderFacade(termFinderFacade);
    termDefinitionAccessor.setAssociationFinder(associationFinder);
    termDeleterFacade.setAssociationFinder(associationFinder);
    termDeleterFacade.setTermFinderFacade(termFinderFacade);
    
    // construct the domain entity access session beans
    domainEntityLoader = new DomainEntityLoaderBean();
    domainEntityLoader.setAssociationFinder(associationFinder);
    domainEntityLoader.setTermDefinitionAccessor(termDefinitionAccessor);
    domainEntityLoader.setTermFinderFacade(termFinderFacade);
    
    domainEntityPersister = new DomainEntityPersisterBean();
    domainEntityPersister.setAssociationFinder(associationFinder);
    domainEntityPersister.setAssociationEditor(associationEditor);
    domainEntityPersister.setTermDefinitionAccessor(termDefinitionAccessor);
    domainEntityPersister.setTermDeleterFacade(termDeleterFacade);
    domainEntityPersister.setTermFinderFacade(termFinderFacade);
    
    domainEntityDeleter = new DomainEntityDeleterBean();
    domainEntityDeleter.setAssociationFinder(associationFinder);
    domainEntityDeleter.setTermDefinitionAccessor(termDefinitionAccessor);
    domainEntityDeleter.setTermDeleterFacade(termDeleterFacade);
    domainEntityDeleter.setTermFinderFacade(termFinderFacade);
  }
  
  /** Sets the creator.
   *
   * @param creator the creator
   */
  public void setCreator(final AbstractReifiedTerm creator) {
    //Preconditions
    assert creator != null : "creator must not be null";
    
    this.creator = creator;
  }
  
  /** Gets the creator.
   *
   * @return the creator
   */
  public AbstractReifiedTerm getCreator() {
    return creator;
  }
  
  /** Sets the creation purpose.
   *
   * @param creationPurpose the creation purpose
   */
  public void setCreationPurpose(final AbstractReifiedTerm creationPurpose) {
    //Preconditions
    assert creationPurpose != null : "creationPurpose must not be null";
    
    this.creationPurpose = creationPurpose;
  }
  
  /** Gets the creation purpose.
   *
   * @return the creation purpose
   */
  public AbstractReifiedTerm getCreationPurpose() {
    return creationPurpose;
  }
  
  /** Begins a new transaction. */
  public void beginTransaction() {
    //Preconditions
    assert environment != null : "environment must not be null";
    assert transactionConfig != null : "transactionConfig must not be null";
    
    try {
      transaction = environment.beginTransaction(null, transactionConfig);
    } catch (final DatabaseException ex) {
      throw new TexaiException(ex);
    }
  }
  
  /** Commits the current transaction. */
  public void commit() {
    //Preconditions
    assert transaction != null : "transaction must not be null";
    
    try {
      transaction.commit();
    } catch (DatabaseException ex) {
      throw new TexaiException(ex);
    }
  }

  /** Aborts the current transaction. */
  public void abort() {
    //Preconditions
    assert transaction != null : "transaction must not be null";
    
    try {
      transaction.abort();
    } catch (DatabaseException ex) {
      throw new TexaiException(ex);
    }
  }

  // facade methods from the term finder
  
  /** Finds the AtomicTerm having the given termName or null if not found.
   *
   * @param termName the given term name
   * @return the AtomicTerm having the given termName or null if not found
   */
  public AtomicTerm findAtomicTermByTermName(final String termName) {
    return termFinderFacade.findAtomicTermByTermName(termName);
  }
  
  /** Creates the Symbol having the given nameValue.
   *
   * @param nameValue the given name value
   * @return the Symbol having the given nameValue
   */
  public Symbol createSymbolByNameValue(final String nameValue) {
    return termFinderFacade.createSymbolByNameValue(nameValue);
  }
  
  /** Creates the Formula having the given term list.
   *
   * @param termList the list of terms that constitute this formula
   * @param kbPartition the KB partition
   * @return the persisted Formula having the given term list
   */
  public Formula createFormulaByTermList(final List<AbstractTerm> termList, final AtomicTerm kbPartition) {
    return termFinderFacade.createFormulaByTermList(termList, creator, creationPurpose, kbPartition);
  }
  
  /**
   * Creates the defined atomic term.
   *
   * @param termName the term name
   * @param prettyName the pretty name
   * @param commentString the comment string
   * @param isaTerms the list of isa (type) terms
   * @param kbPartition the KB partition
   * @return the specified defined term
   */
  public AtomicTerm createDefinedTerm(
          final String termName,
          final String prettyName,
          final String commentString,
          final List<AbstractReifiedTerm> isaTerms,
          final AtomicTerm kbPartition) {
    //Preconditions
    assert creator != null : "creator must not be null";
    assert creationPurpose != null : "creationPurpose must not be null";
    assert kbPartition != null : "kbPartition must not be null";
    
    return termFinderFacade.createDefinedTerm(
            termName,
            prettyName,
            commentString,
            isaTerms,
            creator,
            creationPurpose,
            kbPartition);
  }
  
  /**
   * Creates the specified binary predicate term with default list of isa (type) terms [BinaryPredicate, PredicateFocalInArg1],
   * and list of genlPreds (super property) terms [conceptuallyRelated].
   *
   * @param termName the predicate name
   * @param prettyName the pretty name
   * @param commentString the comment string
   * @param argConstraintTerms the list of argument constraint terms
   * @param kbPartition the KB partition
   * @return the specified binary predicate term
   */
  public AtomicTerm createBinaryPredicateTerm(
          final String termName,
          final String prettyName,
          final String commentString,
          final List<AbstractReifiedTerm> argConstraintTerms,
          final AtomicTerm kbPartition) {
    //Preconditions
    assert creator != null : "creator must not be null";
    assert creationPurpose != null : "creationPurpose must not be null";
    assert kbPartition != null : "kbPartition must not be null";
    
    return termFinderFacade.createBinaryPredicateTerm(
            termName,
            prettyName,
            commentString,
            argConstraintTerms,
            creator,
            creationPurpose,
            kbPartition);
  }
  
  /**
   * Creates the specified binary predicate term.
   *
   *
   * @param termName the predicate name
   * @param prettyName the pretty name
   * @param commentString the comment string
   * @param isaTerms the list of isa (type) terms, defaults to [BinaryPredicate, PredicateFocalInArg1]
   * @param genlPredTerms the list of genlPreds (super property) terms, defaults to [conceptuallyRelated]
   * @param argConstraintTerms the list of argument constraint terms
   * @param kbPartition the KB partition
   * @return the specified binary predicate term
   */
  public AtomicTerm createBinaryPredicateTerm(
          final String termName,
          final String prettyName,
          final String commentString,
          final List<AbstractReifiedTerm> isaTerms,
          final List<AbstractReifiedTerm> genlPredTerms,
          final List<AbstractReifiedTerm> argConstraintTerms,
          final AtomicTerm kbPartition) {
    //Preconditions
    assert creator != null : "creator must not be null";
    assert creationPurpose != null : "creationPurpose must not be null";
    assert kbPartition != null : "kbPartition must not be null";
    
    return termFinderFacade.createBinaryPredicateTerm(
            termName,
            prettyName,
            commentString,
            isaTerms,
            genlPredTerms,
            argConstraintTerms,
            creator,
            creationPurpose,
            kbPartition);
  }
  
  /**
   * Creates the specified collection term.
   *
   * @param termName the term name
   * @param prettyName the pretty name
   * @param commentString the comment string
   * @param isaTerms the list of isa (type) terms
   * @param genlsTerms the list of genls (superclass) terms
   * @param kbPartition the KB partition
   * @return the specified collection term
   */
  public AtomicTerm createCollectionTerm(
          final String termName,
          final String prettyName,
          final String commentString,
          final List<AbstractReifiedTerm> isaTerms,
          final List<AbstractReifiedTerm> genlsTerms,
          final AtomicTerm kbPartition) {
    //Preconditions
    assert creator != null : "creator must not be null";
    assert creationPurpose != null : "creationPurpose must not be null";
    assert kbPartition != null : "kbPartition must not be null";
    
    return termFinderFacade.createCollectionTerm(
            termName,
            prettyName,
            commentString,
            isaTerms,
            genlsTerms,
            creator,
            creationPurpose,
            kbPartition);
  }
  
  /**
   * Creates the specified context term.
   *
   * @param termName the term name
   * @param prettyName the pretty name
   * @param commentString the comment string
   * @param isaTerms the list of isa (type) terms which defaults to [Microtheory] when empty
   * @param genlMtTerms the list of genlMt (super context) terms which defaults to [BaseKB] when empty
   * @param kbPartition the KB partition
   * @return the specified collection term
   */
  public AtomicTerm createContextTerm(
          final String termName,
          final String prettyName,
          final String commentString,
          final List<AbstractReifiedTerm> isaTerms,
          final List<AbstractReifiedTerm> genlMtTerms,
          final AtomicTerm kbPartition) {
    //Preconditions
    assert creator != null : "creator must not be null";
    assert creationPurpose != null : "creationPurpose must not be null";
    assert kbPartition != null : "kbPartition must not be null";
    
    return termFinderFacade.createContextTerm(
            termName,
            prettyName,
            commentString,
            isaTerms,
            genlMtTerms,
            creator,
            creationPurpose,
            kbPartition);
  }
  
  // facade methods for the domain entity loader
  
  /** Loads the domain entity from propositions in the knowledge base given its term id.
   *
   * @param instanceTerm the atomic term that represents the domain entity
   * @return the domain entity
   */
  public Object loadDomainEntity(final AtomicTerm instanceTerm) {
    return domainEntityLoader.loadDomainEntity(instanceTerm);
  }

  /** Loads the domain entity from propositions in the knowledge base given its term id.
   *
   * @param termId the id of the atomic term that represents the domain entity
   * @return the domain entity
   */
  public Object loadDomainEntity(final UUID termId) {
    return domainEntityLoader.loadDomainEntity(termId);
  }
  
  /** Loads domain entities having the given property and value.
   *
   * @param property the given property
   * @param value the value of the property
   * @param domainEntityClass the class of the desired domain entity
   * @return the domain entities having the given property and value
   */
  public Set<Object> loadDomainEntitiesByPropertyValue(
          final AtomicTerm property,
          final Object value,
          final Class domainEntityClass) {
    return domainEntityLoader.loadDomainEntitiesByPropertyValue(property, value, domainEntityClass);
  }
  
  /** Loads the domain entity from propositions in the knowledge base given an identifying property and value.
   *
   * @param property the identifying property
   * @param value the value of the property which identifies the desired domain entity
   * @param domainEntityClass the class of the desired domain entity
   * @return the domain entity having the given property and value, or null if not found
   */
  public Object loadDomainEntityByIndentifyingPropertyValue(
          final AtomicTerm property,
          final Object value,
          final Class domainEntityClass) {
    return domainEntityLoader.loadDomainEntityByIndentifyingPropertyValue(property, value, domainEntityClass);
  }
  
  /** Returns an iterator over the set of domain entity terms that represent instances of the given domain entity class.
   *
   * @param domainEntityClass the given domain entity class
   */
  public Iterator<Object> domainEntityIterator(final Class domainEntityClass) {
    return domainEntityLoader.domainEntityIterator(domainEntityClass);
  }
  
  // facade methods for the domain entity persister
  
  /** Persists the given domain entity as propositions in the knowledge base.
   *
   * @param domainEntity the domain entity
   * @return the instance term that represents the domain entity
   */
  public AtomicTerm persistDomainEntity(
          final Object domainEntity) {
    //Preconditions
    assert creator != null : "creator must not be null";
    assert creationPurpose != null : "creationPurpose must not be null";
    
    return domainEntityPersister.persistDomainEntity(domainEntity, creator, creationPurpose);
  }
  
  /** Sets the indicator to validate persisted gafs as a well-formed formulas.
   *
   * @param validateWellFormedFormula the indicator to validate persisted gafs as a well-formed formulas
   */
  public void setValidateWellFormedFormula(boolean validateWellFormedFormula) {
    domainEntityPersister.setValidateWellFormedFormula(validateWellFormedFormula);
  }
  
  // facade methods for the domain entity persister
  
  /** Deletes the given domain entity from the knowledge base.
   *
   * @param domainEntity the domain entity
   */
  public void deleteDomainEntity(final Object domainEntity) {
    domainEntityDeleter.deleteDomainEntity(domainEntity);
  }
  
  // facade methods for the association finder
  
  /** Returns an iterator over the set of binary gafs with the given predicate and given relevant context.
   *
   * @param predicate the predicate of matching binary gafs
   * @param context the most specific context of those from which matching gafs are gathered
   * @return an iterator over the binary ground atomic formulas having the given predicate and context
   */
  public Iterator<BinaryGAF> binaryGAFByPredicateIterator(
          final AtomicTerm predicate,
          final AbstractReifiedTerm context) {
    return associationFinder.binaryGAFByPredicateIterator(predicate, context);
  }
  
  // accessors to the wrapped session beans
  
  /** Gets the term finder facade.
   *
   * @return the term finder facade
   */
  public TermFinderFacadeLocal getTermFinderFacade() {
    return termFinderFacade;
  }
  
  // Miscellaneous
  
  /** Edits the specified binary GAF.
   *
   * @param predicate the predicate
   * @param oldArg1 the old arg1 value
   * @param newArg1 the new arg1 value
   * @param oldArg2 the old arg2 value
   * @param newArg2 the new arg2 value
   * @param context the assertion context
   * @return the edited binary GAF
   */
  public BinaryGAF editBinaryGAF(
          final AtomicTerm predicate,
          final AbstractTerm oldArg1,
          final AbstractTerm newArg1,
          final AbstractTerm oldArg2,
          final AbstractTerm newArg2,
          final AbstractReifiedTerm context) {
    
    final List<AbstractTerm> args = new ArrayList<AbstractTerm>();
    args.add(oldArg1);
    args.add(oldArg2);
    BinaryGAF binaryGAF = termFinderFacade.findBinaryGAFByConstituents(
            predicate,
            args,
            context);
    if (binaryGAF == null) {
      throw new TexaiException("binary gaf was not found for editing " + predicate + "(" + oldArg1 + ", " + oldArg2 + ") in " + context);
    }
    args.clear();
    args.add(newArg1);
    args.add(newArg2);
    binaryGAF.setArgs(args);
    associationEditor.editBinaryGAF(binaryGAF);
    return binaryGAF;
  }
  
}




See more files for this project here

Texai

Texai is an chatbot that intelligently seeks to acquire knowledge and friendly behaviors.

Project homepage: http://sourceforge.net/projects/texai
Programming language(s): Java,Shell Script,XML
License: other

  shared/
    AssociationEditorBean.java
    AssociationEditorLocal.java
    AssociationFinderBean.java
    AssociationFinderLocal.java
    KBPartitionFacadeBean.java
    KBPartitionFacadeLocal.java
    KBShardFacadeBean.java
    TermDeleterFacadeBean.java
    TermDeleterFacadeLocal.java
    TermFinderFacadeBean.java
    TermFinderFacadeLocal.java
    TermManagerBean.java
    TermManagerLocal.java
  AbstractDomainEntityAccessor.java
  DomainEntityDeleterBean.java
  DomainEntityDeleterLocal.java
  DomainEntityLoaderBean.java
  DomainEntityLoaderLocal.java
  DomainEntityManagerBean.java
  DomainEntityManagerLocal.java
  DomainEntityPersisterBean.java
  DomainEntityPersisterLocal.java
  package.html