/** @file AimlQBot.cpp * @author Alessandro Polo * @version $Id: AimlQBot.cpp 14 2009-03-02 20:29:02Z alex $ * @brief * File containing methods for the wosh::services::AimlQBot class. * The header for this class can be found in AimlQBot.h, check that file * for class description. ****************************************************************************/ /* Copyright (c) 2009, WOSH - Wide Open Smart Home * by Alessandro Polo - OpenSmartHome.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the OpenSmartHome.com WOSH nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ****************************************************************************/ #include "AimlQBot.h" #include #include #include #include using namespace std; namespace wosh { namespace services { ////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS AimlQBot::AimlQBot() : Thread("wosh.Service.AimlQBot.WorkerThread") { this->connector = NULL; this->log = NULL; } AimlQBot::~AimlQBot() { if ( isThreadRunning() ) { quitThread(); Thread::waitThread( 10000 ); } } //////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS ////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////// RUN THREAD void AimlQBot::runThread() { this->running = true; /* ASSERT( this->log != NULL ) log->log( Logger::VERBOSE, " worker:runThread(): Starting loop.." ); bool displayTree = false, runRegression = false; string aimlSet = "Charlixv0.3"; string basedir = "/shared/WOSH3/src/bundles/services/AimlQBot/"; //QString basedir = "./"; parser.setLog(&logStream); //AIMLParser* parser = new AIMLParser(&logStream); loadVars( basedir ); loadSubstitutions( basedir ); if (runRegression) { parser.runRegression(); return; } loadFolderSet( basedir + "aiml_set/" + aimlSet ); if (displayTree) parser.displayTree(); log->log( Logger::VERBOSE, " worker:runThread(): Starting loop.." ); QString result = parser.getResponse("hello"); std::cout << "Bot: " << result.toAscii().data() << endl; char buf[256]; string buffer; while( this->running ) { Thread::sleepForMSec(1000); std::cout << "You: "; std::cin.getline(buf, 256); if ( strlen(buf) > 2 ) buf[strlen(buf)-1] = '\0'; buf[strlen(buf)-1] = '\0'; buffer.assign(buf); if ( buffer == "exit" ) break; QString result = parser.getResponse( QString(buffer.c_str()).trimmed()); std::cout << "Bot: " << result.toAscii().data() << endl; } log->log( Logger::VERBOSE, " worker:runThread(): Exiting loop.." ); saveVars( basedir ); */ } string AimlQBot::evaluate( const string& message ) { log->log( Logger::VERBOSE, " worker:evaluate(%s)", message.c_str() ); QString result = parser.getResponse( QString::fromStdString(message) ); return result.toStdString(); } ////////////////////////////////////////////////////////////////////////////////////////////////// RUN THREAD ////////////////////////////////////////////////////////////////////////////////////////////////////////////// WRESULT AimlQBot::loadFolderSet( const string& folder_path ) { QString dirname = QString::fromStdString(folder_path); QDir dir(dirname); QStringList files = dir.entryList( "*.aiml" ); log->log( Logger::VERBOSE, " worker:loadFolderSet(%s) : %i files found", folder_path.c_str(), files.count() ); bool loaded = false; uint i = 0; for ( QStringList::Iterator it = files.begin(); it != files.end(); ++it ) { loaded = this->parser.loadAiml(dirname + *it); if ( loaded ) log->log( Logger::INFO, " worker:runThread(): Loaded %s.", (*it).toAscii().data() ); else log->log( Logger::WARNING, " worker:runThread(): FAILED Loading %s.", (*it).toAscii().data() ); i++; } parser.displayTree(); return WRET_OK; } WRESULT AimlQBot::loadVars( const string& folder_path ) { log->log( Logger::VERBOSE, " worker:loadVars(%s)", folder_path.c_str() ); bool loaded = false; QString basedir = QString::fromStdString(folder_path); loaded = this->parser.loadVars( basedir + "utils/vars.xml", false ); loaded = this->parser.loadVars( basedir + "utils/bot.xml", true ); if ( loaded ) return WRET_OK; log->log( Logger::CRITICAL, " worker:loadVars(%s) : Failed Loading '%s'", folder_path.c_str(), "utils/vars.xml" ); return WRET_ERR_INTERNAL; return WRET_OK; } WRESULT AimlQBot::saveVars( const string& folder_path ) { log->log( Logger::VERBOSE, " worker:saveVars(%s)", folder_path.c_str() ); QString basedir = QString::fromStdString(folder_path); bool loaded = this->parser.saveVars( basedir + "utils/vars.xml" ); if ( loaded ) return WRET_OK; log->log( Logger::CRITICAL, " worker:saveVars(%s) : Failed Saving '%s'", folder_path.c_str(), "utils/vars.xml" ); return WRET_ERR_INTERNAL; } WRESULT AimlQBot::loadSubstitutions( const string& folder_path ) { log->log( Logger::VERBOSE, " worker:loadSubstitutions(%s)", folder_path.c_str() ); QString basedir = QString::fromStdString(folder_path); bool loaded = this->parser.loadSubstitutions( basedir + "utils/substitutions.xml" ); if ( loaded ) return WRET_OK; log->log( Logger::CRITICAL, " worker:loadSubstitutions(%s) : Failed Loading '%s'", folder_path.c_str(), "utils/vars.xml" ); return WRET_ERR_INTERNAL; } ////////////////////////////////////////////////////////////////////////////////////////////////////////////// }; // namespace services }; // namespace wosh