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/uscxml/plugins | |
parent | 1088a76a715ef8a2728ad3fa8cde02bde4eec760 (diff) | |
download | uscxml-57b7b1cb196ea9f51aebaba1a838b9a86a9f83fe.zip uscxml-57b7b1cb196ea9f51aebaba1a838b9a86a9f83fe.tar.gz uscxml-57b7b1cb196ea9f51aebaba1a838b9a86a9f83fe.tar.bz2 |
More work on Prolog datamodel
Diffstat (limited to 'src/uscxml/plugins')
-rw-r--r-- | src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp | 59 |
1 files changed, 50 insertions, 9 deletions
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 |