diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-01-16 00:31:12 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2013-01-16 00:31:12 (GMT) |
commit | 57b7b1cb196ea9f51aebaba1a838b9a86a9f83fe (patch) | |
tree | d2ae2a83f1357b66c9712b741a2ad048444caa06 /src | |
parent | 1088a76a715ef8a2728ad3fa8cde02bde4eec760 (diff) | |
download | uscxml-57b7b1cb196ea9f51aebaba1a838b9a86a9f83fe.zip uscxml-57b7b1cb196ea9f51aebaba1a838b9a86a9f83fe.tar.gz uscxml-57b7b1cb196ea9f51aebaba1a838b9a86a9f83fe.tar.bz2 |
More work on Prolog datamodel
Diffstat (limited to 'src')
-rw-r--r-- | src/uscxml/Factory.h | 1 | ||||
-rw-r--r-- | src/uscxml/Interpreter.cpp | 11 | ||||
-rw-r--r-- | src/uscxml/Interpreter.h | 1 | ||||
-rw-r--r-- | src/uscxml/URL.cpp | 68 | ||||
-rw-r--r-- | src/uscxml/URL.h | 9 | ||||
-rw-r--r-- | src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp | 59 |
6 files changed, 117 insertions, 32 deletions
diff --git a/src/uscxml/Factory.h b/src/uscxml/Factory.h index 29f7f9a..3cc8018 100644 --- a/src/uscxml/Factory.h +++ b/src/uscxml/Factory.h @@ -159,6 +159,7 @@ public: virtual void eval(const std::string& expr) { return _impl->eval(expr); } virtual std::string evalAsString(const std::string& expr) { return _impl->evalAsString(expr); } virtual bool evalAsBool(const std::string& expr) { return _impl->evalAsBool(expr); } + virtual void assign(const std::string& location, const std::string& expr) { return _impl->assign(location, expr); } virtual void assign(const std::string& location, const Data& data) { return _impl->assign(location, data); } diff --git a/src/uscxml/Interpreter.cpp b/src/uscxml/Interpreter.cpp index 5449272..9063cef 100644 --- a/src/uscxml/Interpreter.cpp +++ b/src/uscxml/Interpreter.cpp @@ -33,6 +33,7 @@ Interpreter::Interpreter() : Arabica::SAX2DOM::Parser<std::string>() { _thread = NULL; _sendQueue = NULL; _running = false; + _done = false; #ifdef _WIN32 WSADATA wsaData; @@ -172,6 +173,7 @@ Interpreter::~Interpreter() { } void Interpreter::start() { + _done = false; _thread = new tthread::thread(Interpreter::run, this); } @@ -180,10 +182,12 @@ void Interpreter::run(void* instance) { } bool Interpreter::runOnMainThread(int fps, bool blocking) { + if (_done) + return false; + if (fps > 0) { uint64_t nextRun = _lastRunOnMainThread + (1000 / fps); if (blocking) { - tthread::lock_guard<tthread::mutex> lock(_mutex); while(nextRun > tthread::timeStamp()) { tthread::this_thread::sleep_for(tthread::chrono::milliseconds(nextRun - tthread::timeStamp())); } @@ -194,6 +198,7 @@ bool Interpreter::runOnMainThread(int fps, bool blocking) { _lastRunOnMainThread = tthread::timeStamp(); + tthread::lock_guard<tthread::mutex> lock(_mutex); std::map<std::string, IOProcessor>::iterator ioProcessorIter = _ioProcessors.begin(); while(ioProcessorIter != _ioProcessors.end()) { ioProcessorIter->second.runOnMainThread(); @@ -1411,8 +1416,10 @@ void Interpreter::enterStates(const Arabica::XPath::NodeSet<std::string>& enable } for (int i = 0; i < _configuration.size(); i++) { Arabica::DOM::Element<std::string> stateElem = (Arabica::DOM::Element<std::string>)_configuration[i]; - if (isFinal(stateElem) && parentIsScxmlState(stateElem)) + if (isFinal(stateElem) && parentIsScxmlState(stateElem)) { _running = false; + _done = true; + } } } diff --git a/src/uscxml/Interpreter.h b/src/uscxml/Interpreter.h index ab8cf6d..d5c719b 100644 --- a/src/uscxml/Interpreter.h +++ b/src/uscxml/Interpreter.h @@ -171,6 +171,7 @@ protected: std::string _nsPrefix; bool _running; + bool _done; Binding _binding; Arabica::XPath::NodeSet<std::string> _configuration; Arabica::XPath::NodeSet<std::string> _statesToInvoke; diff --git a/src/uscxml/URL.cpp b/src/uscxml/URL.cpp index ca3e8f7..db128ef 100644 --- a/src/uscxml/URL.cpp +++ b/src/uscxml/URL.cpp @@ -52,26 +52,13 @@ const bool URLImpl::toAbsoluteCwd() { return toAbsolute(std::string("file://" + std::string(currPath) + "/")); } -const bool URLImpl::toAbsolute(const std::string& baseUrl) { - if (_uri.is_absolute()) - return true; - _uri = Arabica::io::URI(baseUrl, _uri.as_string()); - if (!_uri.is_absolute()) - return false; - return true; -} - -const std::string URLImpl::asLocalFile(const std::string& suffix, bool reload) { - // this is already a local file - if (_uri.scheme().compare("file") == 0) - return _uri.path(); - - if (_localFile.length() > 0 && !reload) - return _localFile; - +std::string URLImpl::getLocalFilename(const std::string& suffix) { if (_localFile.length() > 0) - remove(_localFile.c_str()); + return _localFile; + if (_uri.scheme().compare("file") == 0) + return _uri.path(); + // try hard to find a temporary directory const char* tmpDir = NULL; if (tmpDir == NULL) @@ -106,10 +93,49 @@ const std::string URLImpl::asLocalFile(const std::string& suffix, bool reload) { _close(fd); #else close(fd); -#endif - _localFile = std::string(tmpl); +#endif + return std::string(tmpl); +} - std::ofstream file(tmpl, std::ios_base::out); +boost::shared_ptr<URLImpl> URLImpl::toLocalFile(const std::string& content, const std::string& suffix) { + boost::shared_ptr<URLImpl> urlImpl = boost::shared_ptr<URLImpl>(new URLImpl()); + urlImpl->_localFile = urlImpl->getLocalFilename(suffix); + urlImpl->_uri = std::string("file://") + urlImpl->_localFile; + + std::ofstream file(urlImpl->_localFile.c_str(), std::ios_base::out); + if(file.is_open()) { + file << content; + file.close(); + } else { + return boost::shared_ptr<URLImpl>(); + } + + return urlImpl; +} + +const bool URLImpl::toAbsolute(const std::string& baseUrl) { + if (_uri.is_absolute()) + return true; + _uri = Arabica::io::URI(baseUrl, _uri.as_string()); + if (!_uri.is_absolute()) + return false; + return true; +} + +const std::string URLImpl::asLocalFile(const std::string& suffix, bool reload) { + // this is already a local file + if (_uri.scheme().compare("file") == 0) + return _uri.path(); + + if (_localFile.length() > 0 && !reload) + return _localFile; + + if (_localFile.length() > 0) + remove(_localFile.c_str()); + + _localFile = getLocalFilename(suffix); + + std::ofstream file(_localFile.c_str(), std::ios_base::out); if(file.is_open()) { file << URL(this->shared_from_this()); file.close(); diff --git a/src/uscxml/URL.h b/src/uscxml/URL.h index 07921f3..5bcbd27 100644 --- a/src/uscxml/URL.h +++ b/src/uscxml/URL.h @@ -22,6 +22,8 @@ public: const bool toAbsolute(const std::string& baseUrl); const std::string asLocalFile(const std::string& suffix, bool reload = false); + static boost::shared_ptr<URLImpl> toLocalFile(const std::string& content, const std::string& suffix); + const bool isAbsolute() const { return _uri.is_absolute(); } const std::string scheme() const { return _uri.scheme(); } const std::string host() const { return _uri.host(); } @@ -30,6 +32,8 @@ public: const std::string asString() const { return _uri.as_string(); } private: + std::string getLocalFilename(const std::string& suffix); + Arabica::io::URI _uri; std::string _localFile; }; @@ -41,6 +45,11 @@ public: URL(boost::shared_ptr<URLImpl> const impl) : _impl(impl) { } URL(const URL& other) : _impl(other._impl) { } virtual ~URL() {}; + + static URL toLocalFile(const std::string& content, const std::string& suffix) { + boost::shared_ptr<URLImpl> impl = URLImpl::toLocalFile(content, suffix); + return URL(impl); + } operator bool() const { return _impl;} bool operator< (const URL& other) const { return _impl < other._impl; } diff --git a/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp b/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp index e753a32..ffb48f9 100644 --- a/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp +++ b/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp @@ -1,4 +1,5 @@ #include "uscxml/Common.h" +#include "uscxml/config.h" #include "SWIDataModel.h" #include "uscxml/Message.h" #include <glog/logging.h> @@ -23,8 +24,24 @@ SWIDataModel::SWIDataModel() { boost::shared_ptr<DataModelImpl> SWIDataModel::create(Interpreter* interpreter) { boost::shared_ptr<SWIDataModel> dm = boost::shared_ptr<SWIDataModel>(new SWIDataModel()); dm->_interpreter = interpreter; - const char* swiPath = "/foo"; - dm->_plEngine = new PlEngine((char*)swiPath); + + const char* swibin = getenv("SWI_BINARY"); + if (swibin == NULL) + swibin = SWI_BINARY; + const char* quiet = "--quiet"; + + static char * av[] = { + (char*)swibin, + (char*)quiet, + // "-s", + // "/Users/sradomski/Documents/TK/Code/pl-devel/demo/likes.pl", + NULL}; + if(!PL_initialise(2,av)) { + LOG(ERROR) << "Error intializing prolog engine"; + PL_halt(1); + return boost::shared_ptr<DataModelImpl>(); + } + return dm; } @@ -79,25 +96,49 @@ uint32_t SWIDataModel::getLength(const std::string& expr) { } void SWIDataModel::eval(const std::string& expr) { - std::cout << "SWIDataModel::eval" << std::endl; + URL localPLFile = URL::toLocalFile(expr, ".pl"); + PlCall("user", "load_files", PlTermv(localPLFile.asLocalFile(".pl").c_str())) || LOG(ERROR) << "Could not execute prolog from file"; } bool SWIDataModel::evalAsBool(const std::string& expr) { - std::cout << "SWIDataModel::evalAsBool" << std::endl; - return true; + PlCompound compound(expr.c_str()); + PlTermv termv(compound.arity()); + for (int i = 0; i < compound.arity(); i++) { + termv[i] = compound[i + 1]; + } + PlQuery query(compound.name(), termv); + return query.next_solution() > 0; } std::string SWIDataModel::evalAsString(const std::string& expr) { - std::cout << "SWIDataModel::evalAsString" << std::endl; - return std::string(""); + PlCompound compound(expr.c_str()); + if (strlen(compound.name())) { + PlTermv termv(compound.arity()); + for (int i = 0; i < compound.arity(); i++) { + termv[i] = compound[i + 1]; + } + PlQuery query(compound.name(), termv); + + std::stringstream ss; + while (query.next_solution()) { + for (int i = 0; i < compound.arity(); i++) { + const char* separator = ""; + ss << separator << (char *)termv[i]; + separator = ", "; + } + ss << std::endl; + } + return ss.str(); + } + return std::string(compound); } void SWIDataModel::assign(const std::string& location, const Data& data) { - std::cout << "SWIDataModel::assign" << std::endl; + eval(data.atom); } void SWIDataModel::assign(const std::string& location, const std::string& expr) { - std::cout << "SWIDataModel::assign" << std::endl; + eval(expr); } }
\ No newline at end of file |