summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp
blob: fe881dcfaf3230bce0566b3cd26c63ceeaaf9eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "uscxml/Common.h"
#include "uscxml/config.h"
#include "SWIDataModel.h"
#include "uscxml/Message.h"
#include <glog/logging.h>

#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif

namespace uscxml {

#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool connect(pluma::Host& host) {
	host.add( new SWIDataModelProvider() );
	return true;
}
#endif

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* 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;
}

void SWIDataModel::registerIOProcessor(const std::string& name, const IOProcessor& ioprocessor) {
	std::cout << "SWIDataModel::registerIOProcessor" << std::endl;
}

void SWIDataModel::setSessionId(const std::string& sessionId) {
	std::cout << "SWIDataModel::setSessionId" << std::endl;
	_sessionId = sessionId;
}

void SWIDataModel::setName(const std::string& name) {
	std::cout << "SWIDataModel::setName" << std::endl;
	_name = name;
}

SWIDataModel::~SWIDataModel() {
}

void SWIDataModel::pushContext() {
	std::cout << "SWIDataModel::pushContext" << std::endl;
}

void SWIDataModel::popContext() {
	std::cout << "SWIDataModel::popContext" << std::endl;
}

void SWIDataModel::initialize() {
	std::cout << "SWIDataModel::initialize" << std::endl;
}

void SWIDataModel::setEvent(const Event& event) {
	std::cout << "SWIDataModel::setEvent" << std::endl;
	_event = event;
}

Data SWIDataModel::getStringAsData(const std::string& content) {
	std::cout << "SWIDataModel::getStringAsData" << std::endl;
	Data data;
	return data;
}

bool SWIDataModel::validate(const std::string& location, const std::string& schema) {
	std::cout << "SWIDataModel::validate" << std::endl;
	return true;
}

uint32_t SWIDataModel::getLength(const std::string& expr) {
	std::cout << "SWIDataModel::getLength" << std::endl;
	return 0;
}

void SWIDataModel::eval(const std::string& expr) {
	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) {
	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) {
	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) {
	eval(data.atom);
}

void SWIDataModel::assign(const std::string& location, const std::string& expr) {
	eval(expr);
}

}