summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp')
-rw-r--r--src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp59
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