Code Search for Developers
 
 
  

ConstructionInitializer.java from Texai at Krugle


Show ConstructionInitializer.java syntax highlighted

/*
 * ConstructionInitializer.java
 *
 * Created on January 12, 2007, 9:30 AM
 *
 * Description: Initializes the construction grammar domain entities.
 *
 * 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.grammar;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import net.sf.ehcache.CacheManager;
import org.apache.log4j.Logger;
import org.texai.grammar.domainEntity.WordFormConstruction;
import org.texai.grammar.domainEntity.WordSenseConstruction;
import org.texai.kb.CacheInitializer;
import org.texai.kb.Constants;
import org.texai.kb.persistence.RDFEntityManager;
import org.texai.lexicon.domainEntity.TexaiEnglishWordForm;
import org.texai.lexicon.domainEntity.TexaiEnglishWordSense;
import org.texai.util.TexaiException;

/**
 *
 * @author reed
 */
public final class ConstructionInitializer {
  
  /** the limit during development testing */
//  private static final int TEST_LIMIT = 15;
  private static final int TEST_LIMIT = 10000000;
  
  /** the limit that when exceeded causes the entity manager to be cleared of stale managed objects */
  private static final int CLEAR_ENTITY_MANAGER_LIMIT = 20;
  
  /** the entity manager factory */
  private EntityManagerFactory entityManagerFactory;
  
  /** the entity manager */
  private EntityManager entityManager;
  
  /** the RDF entity manager */
  private RDFEntityManager rdfEntityManager;
  
  /** the entity transaction */
  private EntityTransaction entityTransaction;
  
  /** the log4j logger */
  private static final Logger LOGGER = Logger.getLogger(ConstructionInitializer.class.getName());
  
  /** the number of items processed */
  private int nbrItemsProcessed = 0;
  
  /** Creates a new instance of ConstructionInitializer. */
  public ConstructionInitializer() {
    super();
  }
  
  /** Initializes the application and injects the dependencies for out-of-the-container execution of J2EE session beans. */
  private void initialize() {
//    entityManagerFactory = Persistence.createEntityManagerFactory(Constants.TEST_PERSISTENCE_UNIT_NAME);
//    entityManager = entityManagerFactory.createEntityManager();
//    CacheInitializer.initializeCaches();
//    domainEntityManager = new DomainEntityManagerBean();
//    domainEntityManager.setEntityManager(entityManager);
//    domainEntityManager.injectSharedBeanDependencies();
//    domainEntityManager.setCreator(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_SOME_CYCLIST));
//    domainEntityManager.setCreationPurpose(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_OPEN_CYC_PROJECT));
//    entityTransaction = entityManager.getTransaction();
  }
  
  /** Initializes the grammar constructions. */
  private void initializeGrammarConstructions() {
//    entityTransaction.begin();
//    final List<AbstractReifiedTerm> isaTerms = new ArrayList<AbstractReifiedTerm>();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_INDIVIDUAL));
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CYCLIST));
//    creator = domainEntityManager.findOrCreateDefinedTerm(
//            Constants.TERM_NAME_CONSTRUCTION_GRAMMAR_INITIALIZATION_PROCESS,
//            "the texai construction grammar initialization process",
//            "This is the process that initializes the texai grammar constructions.",
//            isaTerms);
//    isaTerms.clear();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_INDIVIDUAL));
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CYC_BASED_PROJECT));
//    LOGGER.info("creator: " + creator);
//    creationPurpose = domainEntityManager.findOrCreateDefinedTerm(
//            Constants.TERM_NAME_CONSTRUCTION_GRAMMAR_INITIALIZATION_PROJECT,
//            "the texai construction grammar initialization project",
//            "This is the project that initializes the texai grammar constructions.",
//            isaTerms);
//    LOGGER.info("creationPurpose: " + creationPurpose);
//    List<AbstractReifiedTerm> genlMtTerms = new ArrayList<AbstractReifiedTerm>();
//    genlMtTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_WORDNET21_DOMAIN_CONTEXT));
//    domainEntityManager.findOrCreateContextTerm(
//            Constants.TERM_NAME_ENGLISH_CONSTRUCTION_GRAMMAR_DOMAIN_CONTEXT,
//            Constants.TERM_NAME_ENGLISH_CONSTRUCTION_GRAMMAR_DOMAIN_CONTEXT,
//            Constants.TERM_NAME_ENGLISH_CONSTRUCTION_GRAMMAR_DOMAIN_CONTEXT
//            + " is the context that contains the WordNet v2.1 domain entities and their associations.",
//            new ArrayList<AbstractReifiedTerm>(),
//            genlMtTerms);
//    isaTerms.clear();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_LINGUISTIC_OBJECT_TYPE));
//    final List<AbstractReifiedTerm> genlsTerms = new ArrayList<AbstractReifiedTerm>();
//    genlsTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_LEXICAL_ITEM));
//    final AtomicTerm cxgConstituent = domainEntityManager.findOrCreateCollectionTerm(
//            Constants.TERM_NAME_CXG_CONSTRUCTION,
//            "construction grammar constituent",
//            "Instances of the collection CxgConstruction are constituents of the construction grammar.",
//            isaTerms,
//            genlsTerms);
//    isaTerms.clear();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_LINGUISTIC_OBJECT_TYPE));
//    genlsTerms.clear();
//    genlsTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CXG_CONSTRUCTION));
//    final AtomicTerm cxgWordConstruction = domainEntityManager.findOrCreateCollectionTerm(
//            Constants.TERM_NAME_CXG_WORD_FORM_CONSTRUCTION,
//            "construction grammar word construction",
//            "Instances of the collection CxgWordConstruction are word constructions of the construction grammar.",
//            isaTerms,
//            genlsTerms);
//    isaTerms.clear();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_LINGUISTIC_OBJECT_TYPE));
//    genlsTerms.clear();
//    genlsTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CXG_CONSTRUCTION));
//    final AtomicTerm cxgComposedConstruction = domainEntityManager.findOrCreateCollectionTerm(
//            Constants.TERM_NAME_CXG_COMPOSED_CONSTRUCTION,
//            "construction grammar composed construction",
//            "Instances of the collection CxgComposedConstruction are composed constructions of the construction grammar.",
//            isaTerms,
//            genlsTerms);
//    isaTerms.clear();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_LINGUISTIC_OBJECT_TYPE));
//    genlsTerms.clear();
//    genlsTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CXG_CONSTRUCTION));
//    final AtomicTerm cxgConstituentAdapter = domainEntityManager.findOrCreateCollectionTerm(
//            Constants.TERM_NAME_CXG_CONSTITUENT_ADAPTER,
//            "construction grammar constituent adapter",
//            "Instances of the collection CxgConstituent are constituent adapters in the construction grammar.",
//            isaTerms,
//            genlsTerms);
//    isaTerms.clear();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_OBJECT_TYPE));
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_TEMPORAL_STUFF_TYPE));
//    genlsTerms.clear();
//    genlsTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_INTELLIGENT_AGENT));
//    AtomicTerm dialogUser = domainEntityManager.findOrCreateCollectionTerm(
//            Constants.TERM_NAME_DIALOG_USER,
//            "dialog user",
//            "Instances of the collection DialogUser are users of the dialog system.",
//            isaTerms,
//            genlsTerms);
//    isaTerms.clear();
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_OBJECT_TYPE));
//    isaTerms.add(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CYC_AGENT));
//    AtomicTerm texai = domainEntityManager.findOrCreateDefinedTerm(
//            Constants.TERM_NAME_TEXAI,
//            "texai",
//            "Texai is the intelligent software agent manifested by the texai software system.",
//            isaTerms);
//    entityTransaction.commit();
    
    
    
    
//    entityTransaction.begin();
//    AtomicTerm property = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CXG_CONSTRUCTION_NAME);
//    final AtomicTerm englishLanguage = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_ENGLISH_LANGUAGE);
//    
//    // NewConverstationTag -------------------------------------------
//    XMLTagWord newConversationTag = null;
//    if (property != null) {
//      newConversationTag = (XMLTagWord) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//              property,
//              GrammarConstants.TAG_NEW_CONVERSATION,
//              XMLTagWord.class);
//    }
//    if (newConversationTag == null) {
//      newConversationTag = new XMLTagWord(GrammarConstants.TAG_NEW_CONVERSATION + "_XMLTagWord", englishLanguage);
//      domainEntityManager.persistDomainEntity(newConversationTag);
//      assert newConversationTag.getTermId() != null : "termId must not be null";
//      entityTransaction.commit();
//      entityTransaction.begin();
//      property = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CXG_CONSTRUCTION_NAME);
//    }
//    assert property != null : "term not found for " + Constants.TERM_NAME_CXG_CONSTRUCTION_NAME;
//    LOGGER.info("initialized: " + newConversationTag);
//    
//    // NewConversation -------------------------------------------
//    SimpleConstruction newConversation = null;
//    newConversation = (SimpleConstruction) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.CXN_NEW_CONVERSATION,
//            SimpleConstruction.class);
//    if (newConversation == null) {
//      newConversation = new SimpleConstruction(GrammarConstants.CXN_NEW_CONVERSATION, englishLanguage);
//    }
//    ConstituentAdapter newConversationTagAdapter = newConversation.getSimpleConstituentAdapter();
//    if (newConversationTagAdapter == null) {
//      newConversationTagAdapter = new ConstituentAdapter(newConversationTag);
//      newConversation.setSimpleConstituentAdapter(newConversationTagAdapter);
//    }
//    newConversationTag.getParentComposedConstructions().add(newConversation);
//    domainEntityManager.persistDomainEntity(newConversationTag);
//    LOGGER.info("initialized: " + newConversation);
//    
//    // OpenFromUserTag -------------------------------------------
//    XMLTagWord openFromUserTag = null;
//    openFromUserTag = (XMLTagWord) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.TAG_OPEN_FROM_USER,
//            XMLTagWord.class);
//    if (openFromUserTag == null) {
//      openFromUserTag = new XMLTagWord(GrammarConstants.TAG_OPEN_FROM_USER + "_XMLTagWord", englishLanguage);
//    }
//    LOGGER.info("initialized: " + openFromUserTag);
//    
//    // CloseFromUserTag -------------------------------------------
//    XMLTagWord closeFromUserTag = null;
//    closeFromUserTag = (XMLTagWord) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.TAG_CLOSE_FROM_USER,
//            XMLTagWord.class);
//    if (closeFromUserTag == null) {
//      closeFromUserTag = new XMLTagWord(GrammarConstants.TAG_CLOSE_FROM_USER + "_XMLTagWord", englishLanguage);
//    }
//    LOGGER.info("initialized: " + closeFromUserTag);
//    
//    // UserId -------------------------------------------
//    RegularExpression userId = null;
//    userId = (RegularExpression) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.CXN_REGEX_USER_ID,
//            RegularExpression.class);
//    final List<String> groupVariables = new ArrayList<String>(2);
//    groupVariables.add(GrammarConstants.SYMBOL_USER_NAME);
//    groupVariables.add(GrammarConstants.SYMBOL_JABBER_HOST);
//    final String fromUserRegularExpression = "([a-zA-Z_\\-0-9]+)@([a-zA-Z_\\-0-9\\.]+)";
//    final String fromUserSubstitutionExpression = "NOT-USED";
//    if (userId == null) {
//      userId = new RegularExpression(
//              GrammarConstants.CXN_REGEX_USER_ID,
//              englishLanguage,
//              fromUserRegularExpression,
//              fromUserSubstitutionExpression,
//              groupVariables);
//    } else {
//      userId.setRegularExpression(fromUserRegularExpression);
//      userId.setSubstitutionExpression(fromUserSubstitutionExpression);
//      userId.setGroupVariables(groupVariables);
//    }
//    domainEntityManager.persistDomainEntity(userId);
//    LOGGER.info("initialized: " + userId);
//    
//    // FromUser -------------------------------------------
//    SequenceConstruction fromUser = null;
//    fromUser = (SequenceConstruction) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.CXN_FROM_USER,
//            SequenceConstruction.class);
//    if (fromUser == null) {
//      fromUser = new SequenceConstruction(GrammarConstants.CXN_FROM_USER, englishLanguage);
//    }
//    List<ConstituentAdapter> fromUserOrderedConstituentAdapters = fromUser.getOrderedConstituentAdapters();
//    ConstituentAdapter openFromUserTagAdapter = null;
//    if (fromUserOrderedConstituentAdapters.size() > 0) {
//      openFromUserTagAdapter = fromUserOrderedConstituentAdapters.get(0);
//      openFromUserTagAdapter.setConstituent(openFromUserTag);
//      openFromUserTagAdapter.setConstituentPosition(0);
//    } else {
//      openFromUserTagAdapter = new ConstituentAdapter(openFromUserTag, 0);
//      fromUserOrderedConstituentAdapters.add(openFromUserTagAdapter);
//    }
//    openFromUserTag.getParentComposedConstructions().add(fromUser);
//    domainEntityManager.persistDomainEntity(openFromUserTag);
//    
//    ConstituentAdapter userIdAdapter = null;
//    if (fromUserOrderedConstituentAdapters.size() > 1) {
//      userIdAdapter = fromUserOrderedConstituentAdapters.get(1);
//      userIdAdapter.setConstituent(userId);
//      userIdAdapter.setConstituentPosition(1);
//    } else {
//      userIdAdapter = new ConstituentAdapter(userId, 1);
//      fromUserOrderedConstituentAdapters.add(userIdAdapter);
//    }
//    userId.getParentComposedConstructions().add(fromUser);
//    domainEntityManager.persistDomainEntity(userId);
//    
//    ConstituentAdapter closeFromUserTagAdapter = null;
//    if (fromUserOrderedConstituentAdapters.size() > 2) {
//      closeFromUserTagAdapter = fromUserOrderedConstituentAdapters.get(2);
//      closeFromUserTagAdapter.setConstituent(closeFromUserTag);
//      closeFromUserTagAdapter.setConstituentPosition(1);
//    } else {
//      closeFromUserTagAdapter = new ConstituentAdapter(closeFromUserTag, 2);
//      fromUserOrderedConstituentAdapters.add(closeFromUserTagAdapter);
//    }
//    closeFromUserTag.getParentComposedConstructions().add(fromUser);
//    domainEntityManager.persistDomainEntity(closeFromUserTag);
//    
//    if (fromUserOrderedConstituentAdapters.size() > 3) {
//      fromUser.setOrderedConstituentAdapters(fromUserOrderedConstituentAdapters.subList(0, 2));
//    }
//    
//    LOGGER.info("  fromUser constituents: " + fromUser.getConstituents());
//    LOGGER.info("initialized: " + fromUser);
//    
//    //TODO
//    // GreetingNoun: hello -------------------------------------------
//    GreetingNoun hello = null;
//    
//    hello = (GreetingNoun) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.WORD_HELLO,
//            GreetingNoun.class);
//    if (hello == null) {
//      final TexaiEnglishWordForm helloWordForm = (TexaiEnglishWordForm) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//              domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_TEXAI_WORD_FORM),
//              "hello",
//              TexaiEnglishWordForm.class);
//      assert helloWordForm != null : "helloWordForm must not be null";
//      hello = new GreetingNoun(GrammarConstants.WORD_HELLO + "_GreetingNoun", englishLanguage, helloWordForm);
//    }
//    LOGGER.info("initialized: " + hello);
//    
//    // FormalGreetingToMe -------------------------------------------
//    SimpleConstruction formalGreetingToMe = null;
//    formalGreetingToMe = (SimpleConstruction) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.CXN_FORMAL_GREETING_TO_ME,
//            SimpleConstruction.class);
//    if (formalGreetingToMe == null) {
//      formalGreetingToMe = new SimpleConstruction(GrammarConstants.CXN_FORMAL_GREETING_TO_ME, englishLanguage);
//    }
//    ConstituentAdapter helloAdapter = formalGreetingToMe.getSimpleConstituentAdapter();
//    if (helloAdapter == null) {
//      helloAdapter = new ConstituentAdapter(hello);
//      formalGreetingToMe.setSimpleConstituentAdapter(helloAdapter);
//    }
//    formalGreetingToMe.setSimpleConstituentAdapter(helloAdapter);
//    hello.getParentComposedConstructions().add(formalGreetingToMe);
//    domainEntityManager.persistDomainEntity(hello);
//    LOGGER.info("initialized: " + formalGreetingToMe);
//    
//    // OpenBodyTag -------------------------------------------
//    XMLTagWord openBodyTag = null;
//    openBodyTag = (XMLTagWord) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.TAG_OPEN_BODY,
//            XMLTagWord.class);
//    if (openBodyTag == null) {
//      openBodyTag = new XMLTagWord(GrammarConstants.TAG_OPEN_BODY + "_XMLTagWord", englishLanguage);
//    }
//    LOGGER.info("initialized: " + openBodyTag);
//    
//    // CloseBodyTag -------------------------------------------
//    XMLTagWord closeBodyTag = null;
//    closeBodyTag = (XMLTagWord) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.TAG_CLOSE_BODY,
//            XMLTagWord.class);
//    if (closeBodyTag == null) {
//      closeBodyTag = new XMLTagWord(GrammarConstants.TAG_CLOSE_BODY + "_XMLTagWord", englishLanguage);
//    }
//    LOGGER.info("initialized: " + closeBodyTag);
//    
//    // UtteranceToMe -------------------------------------------
//    SequenceConstruction utteranceToMe = null;
//    utteranceToMe = (SequenceConstruction) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.CXN_UTTERANCE_TO_ME,
//            SequenceConstruction.class);
//    if (utteranceToMe == null) {
//      utteranceToMe = new SequenceConstruction(GrammarConstants.CXN_UTTERANCE_TO_ME, englishLanguage);
//    }
//    List<ConstituentAdapter> utteranceToMerOrderedConstituentAdapters = utteranceToMe.getOrderedConstituentAdapters();
//    ConstituentAdapter fromUserAdapter = null;
//    if (utteranceToMerOrderedConstituentAdapters.size() > 0) {
//      fromUserAdapter = utteranceToMerOrderedConstituentAdapters.get(0);
//      fromUserAdapter.setConstituent(fromUser);
//      fromUserAdapter.setConstituentPosition(0);
//    } else {
//      fromUserAdapter = new ConstituentAdapter(fromUser, 0);
//      utteranceToMerOrderedConstituentAdapters.add(fromUserAdapter);
//    }
//    // isa(:DIALOG-USER, DialogUser)
//    final AtomicTerm isa = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_ISA);
//    final Symbol dialogUserSymbol = domainEntityManager.findOrCreateSymbolByNameValue(GrammarConstants.SYMBOL_DIALOG_USER);
//    dialogUser = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_DIALOG_USER);
//    List<AbstractTerm> termList = new ArrayList<AbstractTerm>();
//    termList.add(isa);
//    termList.add(dialogUserSymbol);
//    termList.add(dialogUser);
//    Formula formula = domainEntityManager.findOrCreateFormulaByTermList(termList);
//    fromUserAdapter.addProposition(formula);
//    // properNameStrings(:DIALOG-USER, :USER-NAME)
//    final AtomicTerm properNameStrings = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_PROPER_NAME_STRINGS);
//    final Symbol conversationSymbol = domainEntityManager.findOrCreateSymbolByNameValue(GrammarConstants.SYMBOL_CONVERSATION);
//    final Symbol userNameSymbol = domainEntityManager.findOrCreateSymbolByNameValue(GrammarConstants.SYMBOL_USER_NAME);
//    termList = new ArrayList<AbstractTerm>();
//    termList.add(properNameStrings);
//    termList.add(conversationSymbol);
//    termList.add(userNameSymbol);
//    formula = domainEntityManager.findOrCreateFormulaByTermList(termList);
//    fromUserAdapter.addProposition(formula);
//    // deliberateActors(:CONVERSATION, :DIALOG-USER)
//    final AtomicTerm deliberateActors = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_DELIBERATE_ACTORS);
//    texai = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_TEXAI);
//    termList = new ArrayList<AbstractTerm>();
//    termList.add(deliberateActors);
//    termList.add(conversationSymbol);
//    termList.add(dialogUserSymbol);
//    formula = domainEntityManager.findOrCreateFormulaByTermList(termList);
//    fromUserAdapter.addProposition(formula);
//    // initiator(:CONVERSATION, :DIALOG-USER)
//    final AtomicTerm initiator = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_INITIATOR);
//    termList = new ArrayList<AbstractTerm>();
//    termList.add(initiator);
//    termList.add(conversationSymbol);
//    termList.add(dialogUserSymbol);
//    formula = domainEntityManager.findOrCreateFormulaByTermList(termList);
//    fromUserAdapter.addProposition(formula);
//    LOGGER.info("propositions: " + fromUserAdapter.getPropositions());
//    fromUser.getParentComposedConstructions().add(utteranceToMe);
//    domainEntityManager.persistDomainEntity(fromUser);
//    ConstituentAdapter openBodyTagAdapter = null;
//    if (utteranceToMerOrderedConstituentAdapters.size() > 1) {
//      openBodyTagAdapter = utteranceToMerOrderedConstituentAdapters.get(1);
//      openBodyTagAdapter.setConstituent(openBodyTag);
//      openBodyTagAdapter.setConstituentPosition(1);
//    } else {
//      openBodyTagAdapter = new ConstituentAdapter(openBodyTag, 1);
//      utteranceToMerOrderedConstituentAdapters.add(openBodyTagAdapter);
//    }
//    openBodyTag.getParentComposedConstructions().add(utteranceToMe);
//    domainEntityManager.persistDomainEntity(openBodyTag);
//    
//    ConstituentAdapter formalGreetingToMeAdapter = null;
//    if (utteranceToMerOrderedConstituentAdapters.size() > 2) {
//      formalGreetingToMeAdapter = utteranceToMerOrderedConstituentAdapters.get(2);
//      formalGreetingToMeAdapter.setConstituent(formalGreetingToMe);
//      formalGreetingToMeAdapter.setConstituentPosition(2);
//    } else {
//      formalGreetingToMeAdapter = new ConstituentAdapter(formalGreetingToMe, 2);
//      utteranceToMerOrderedConstituentAdapters.add(formalGreetingToMeAdapter);
//    }
//    formalGreetingToMe.getParentComposedConstructions().add(utteranceToMe);
//    domainEntityManager.persistDomainEntity(formalGreetingToMe);
//    
//    ConstituentAdapter closeBodyTagAdapter = null;
//    if (utteranceToMerOrderedConstituentAdapters.size() > 3) {
//      closeBodyTagAdapter = utteranceToMerOrderedConstituentAdapters.get(3);
//      closeBodyTagAdapter.setConstituent(closeBodyTag);
//      closeBodyTagAdapter.setConstituentPosition(3);
//    } else {
//      closeBodyTagAdapter = new ConstituentAdapter(closeBodyTag, 3);
//      utteranceToMerOrderedConstituentAdapters.add(closeBodyTagAdapter);
//    }
//    closeBodyTag.getParentComposedConstructions().add(utteranceToMe);
//    domainEntityManager.persistDomainEntity(closeBodyTag);
//    if (utteranceToMerOrderedConstituentAdapters.size() > 4) {
//      utteranceToMe.setOrderedConstituentAdapters(utteranceToMerOrderedConstituentAdapters.subList(0, 3));
//    }
//    LOGGER.info("  utteranceToMe constituents: " + utteranceToMe.getConstituents());
//    domainEntityManager.persistDomainEntity(utteranceToMe);
//    LOGGER.info("initialized: " + utteranceToMe);
//    
//    // SomeoneStartingAConversationWithMe -------------------------------------------
//    SequenceConstruction someoneStartingAConversationWithMe = null;
//    someoneStartingAConversationWithMe = (SequenceConstruction) domainEntityManager.loadDomainEntityByIndentifyingPropertyValue(
//            property,
//            GrammarConstants.CXN_SOMEONE_STARTING_A_CONVERSATION_WITH_ME,
//            SequenceConstruction.class);
//    if (someoneStartingAConversationWithMe == null) {
//      someoneStartingAConversationWithMe =
//              new SequenceConstruction(GrammarConstants.CXN_SOMEONE_STARTING_A_CONVERSATION_WITH_ME, englishLanguage);
//    }
//    List<ConstituentAdapter> someoneStartingAConversationWithMeAdapters = someoneStartingAConversationWithMe.getOrderedConstituentAdapters();
//    ConstituentAdapter newConversationAdapter = null;
//    if (someoneStartingAConversationWithMeAdapters.size() > 0) {
//      newConversationAdapter = utteranceToMerOrderedConstituentAdapters.get(0);
//      newConversationAdapter.setConstituent(newConversation);
//      newConversationAdapter.setConstituentPosition(0);
//    } else {
//      newConversationAdapter = new ConstituentAdapter(newConversation, 0);
//      someoneStartingAConversationWithMeAdapters.add(newConversationAdapter);
//    }
//    // isa(:CONVERSATION, Conversation)
//    final AtomicTerm conversation = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CONVERSATION);
//    termList = new ArrayList<AbstractTerm>();
//    termList.add(isa);
//    termList.add(conversationSymbol);
//    termList.add(conversation);
//    formula = domainEntityManager.findOrCreateFormulaByTermList(termList);
//    newConversationAdapter.addProposition(formula);
//    // startingDate(:CONVERSATION, :NOW)
//    final AtomicTerm startingDate = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_STARTING_DATE);
//    final Symbol nowSymbol = domainEntityManager.findOrCreateSymbolByNameValue(GrammarConstants.SYMBOL_NOW);
//    termList = new ArrayList<AbstractTerm>();
//    termList.add(startingDate);
//    termList.add(conversationSymbol);
//    termList.add(nowSymbol);
//    formula = domainEntityManager.findOrCreateFormulaByTermList(termList);
//    newConversationAdapter.addProposition(formula);
//    // deliberateActors(:CONVERSATION, Texai)
//    texai = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_TEXAI);
//    termList = new ArrayList<AbstractTerm>();
//    termList.add(deliberateActors);
//    termList.add(conversationSymbol);
//    termList.add(texai);
//    formula = domainEntityManager.findOrCreateFormulaByTermList(termList);
//    newConversationAdapter.addProposition(formula);
//    LOGGER.info("newConversationAdapter: " + fromUserAdapter.getPropositions());
//    newConversation.getParentComposedConstructions().add(someoneStartingAConversationWithMe);
//    domainEntityManager.persistDomainEntity(newConversation);
//    
//    ConstituentAdapter utteranceToMeAdapter = null;
//    if (someoneStartingAConversationWithMeAdapters.size() > 1) {
//      utteranceToMeAdapter = utteranceToMerOrderedConstituentAdapters.get(1);
//      utteranceToMeAdapter.setConstituent(utteranceToMe);
//      utteranceToMeAdapter.setConstituentPosition(1);
//    } else {
//      utteranceToMeAdapter = new ConstituentAdapter(utteranceToMe, 1);
//      someoneStartingAConversationWithMeAdapters.add(utteranceToMeAdapter);
//    }
//    utteranceToMe.getParentComposedConstructions().add(someoneStartingAConversationWithMe);
//    domainEntityManager.persistDomainEntity(utteranceToMe);
//    if (someoneStartingAConversationWithMeAdapters.size() > 2) {
//      someoneStartingAConversationWithMe.setOrderedConstituentAdapters(someoneStartingAConversationWithMeAdapters.subList(0, 1));
//    }
//    LOGGER.info("  someoneStartingAConversationWithMe constituents: " + someoneStartingAConversationWithMe.getConstituents());
//    domainEntityManager.persistDomainEntity(someoneStartingAConversationWithMe);
//    LOGGER.info("initialized: " + someoneStartingAConversationWithMe);
//    
//    // activated constructions from words -------------------------------------------
//    Set<ComposedConstruction> activatedConstructions = newConversationTag.getActivatedComposedConstructions();
//    activatedConstructions.add(newConversation);
//    domainEntityManager.persistDomainEntity(newConversationTag);
//    
//    //TODO add back in when there are alternatives in the body
////    activatedConstructions = hello.getActivatedComposedConstructions();
////    activatedConstructions.add(formalGreetingToMe);
////    domainEntityManager.persistDomainEntity(hello);
//    
//    // activated constructions from completed composed constructions
//    activatedConstructions = newConversation.getActivatedComposedConstructions();
//    activatedConstructions.add(someoneStartingAConversationWithMe);
//    domainEntityManager.persistDomainEntity(newConversation);
//    
//    
//    entityTransaction.commit();
  }
    
  /** Creates word constructions. */
  private void createWordFormConstructions() {
//    LOGGER.info("gathering lexicon word forms");
//    final Iterator texaiEnglishWords_iter = domainEntityManager.domainEntityIterator(TexaiEnglishWordForm.class);
//    domainEntityManager.setValidateWellFormedFormula(true);
//    nbrItemsProcessed = 0;
//    entityTransaction.begin();
//    creator = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CONSTRUCTION_GRAMMAR_INITIALIZATION_PROCESS);
//    creationPurpose = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CONSTRUCTION_GRAMMAR_INITIALIZATION_PROJECT);
//    domainEntityManager.setCreator(creator);
//    domainEntityManager.setCreationPurpose(creationPurpose);
//    AtomicTerm englishLanguage = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_ENGLISH_LANGUAGE);
//    while (texaiEnglishWords_iter.hasNext()) {
//      final TexaiEnglishWordForm texaiEnglishWordForm = (TexaiEnglishWordForm) texaiEnglishWords_iter.next();
//      LOGGER.info("processing " + texaiEnglishWordForm);
//      final WordFormConstruction wordFormConstruction = new WordFormConstruction(
//              englishLanguage, 
//              texaiEnglishWordForm);
//      domainEntityManager.persistDomainEntity(wordFormConstruction);
//      nbrItemsProcessed++;
//      if (nbrItemsProcessed % 20 == 0) {
//        commit(nbrItemsProcessed);
//        englishLanguage = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_ENGLISH_LANGUAGE);
//      }
//    }
//    entityTransaction.commit();
//    entityManager.clear();
  }
    
  /** Commits a group of persisted domain entities to the knowledge base.
   *
   * @param count the count to log
   */
  private void commit(final int count) {
//    LOGGER.info("");
//    LOGGER.info("*** committing ***     " + count);
//    LOGGER.info("");
//    entityTransaction.commit();
//    entityManager.clear();
//    if (count >= 200) {
//      domainEntityManager.setValidateWellFormedFormula(false);
//    }
//    entityTransaction = entityManager.getTransaction();
//    entityTransaction.begin();
//    domainEntityManager.setCreator(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_SOME_CYCLIST));
//    domainEntityManager.setCreationPurpose(domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_OPEN_CYC_PROJECT));
  }
  
  /** Creates word sense constructions. */
  private void createWordSenseConstructions() {
//    LOGGER.info("gathering lexicon word senses");
//    final Iterator texaiEnglishSenses_iter = domainEntityManager.domainEntityIterator(TexaiEnglishWordSense.class);
//    domainEntityManager.setValidateWellFormedFormula(true);
//    nbrItemsProcessed = 0;
//    entityTransaction.begin();
//    creator = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CONSTRUCTION_GRAMMAR_INITIALIZATION_PROCESS);
//    creationPurpose = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_CONSTRUCTION_GRAMMAR_INITIALIZATION_PROJECT);
//    domainEntityManager.setCreator(creator);
//    domainEntityManager.setCreationPurpose(creationPurpose);
//    AtomicTerm englishLanguage = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_ENGLISH_LANGUAGE);
//    while (texaiEnglishSenses_iter.hasNext()) {
//      final TexaiEnglishWordSense texaiEnglishWordSense = (TexaiEnglishWordSense) texaiEnglishSenses_iter.next();
//      LOGGER.info("processing " + texaiEnglishWordSense);
//      for (final TexaiEnglishWordForm texaiEnglishWordForm : texaiEnglishWordSense.getTexaiEnglishWord().getTexaiEnglishWordForms()) {
//        LOGGER.info(" " + texaiEnglishWordForm);
//        final WordSenseConstruction wordSenseConstruction = new WordSenseConstruction(
//                texaiEnglishWordForm.getWordForm(),
//                englishLanguage, 
//                texaiEnglishWordSense);
//        domainEntityManager.persistDomainEntity(wordSenseConstruction);
//        nbrItemsProcessed++;
//        if (nbrItemsProcessed % 20 == 0) {
//          commit(nbrItemsProcessed);
//          englishLanguage = domainEntityManager.findAtomicTermByTermName(Constants.TERM_NAME_ENGLISH_LANGUAGE);
//        }
//      }
//    }
//    entityTransaction.commit();
//    entityManager.clear();
  }
  
  /** Finalizes this application. */
  private void finalization() {
    CacheManager.getInstance().shutdown();
    entityManager.close();
    entityManagerFactory.close();
    LOGGER.info("ConstructionInitializer completed");
  }
  
  /** Executes this application.
   *
   * @param args the command line arguments (unused)
   */
  public static void main(final String[] args) {
    final ConstructionInitializer constructionInitializer = new ConstructionInitializer();
    constructionInitializer.initialize();
    try {
      constructionInitializer.initializeGrammarConstructions();
//      constructionInitializer.createWordFormConstructions();
      constructionInitializer.createWordSenseConstructions();
    } catch (final TexaiException ex) {
      LOGGER.error(ex);
      ex.printStackTrace(System.err);
    } catch (final NullPointerException ex) {
      LOGGER.error(ex);
      ex.printStackTrace(System.err);
    }
    constructionInitializer.finalization();
  }
}




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

  domainEntity/
    AbstractComposedConstruction.java
    AbstractConstruction.java
    AlternativeConstruction.java
    CategoryConstruction.java
    ConstituentAdapter.java
    OptionalConstruction.java
    RegularExpression.java
    RegularExpression_GroupVariableInfo.java
    RepetitiveConstruction.java
    SequenceConstruction.java
    SimpleConstruction.java
    ValidateGrammarEntities.java
    WordFormConstruction.java
    WordSenseConstruction.java
    XMLTagWord.java
  integration/
    IntegrationGraph.java
    Link.java
    Node.java
  understanding/
    AbstractActiveComposedConstruction.java
    AbstractActiveConstruction.java
    ActiveAbstractComposedConstruction.java
    ActiveAlternativeConstruction.java
    ActiveCategoryConstruction.java
    ActiveComposedConstruction.java
    ActiveComposedConstructionSet.java
    ActiveConstituent.java
    ActiveOptionalConstruction.java
    ActiveRegularExpression.java
    ActiveRepetitiveConstruction.java
    ActiveSequenceConstruction.java
    ActiveSimpleConstruction.java
    ActiveWordFormConstruction.java
    ActiveXMLTagWord.java
  ComposedConstruction.java
  Constituent.java
  ConstructionInitializer.java
  GrammarConstants.java
  Token.java
  Tokenizer.java