summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp
blob: 303d0741cde2b14292f3788eb6f0262d8ee4cd28 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/**
 *  @file
 *  @author     2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#include <boost/algorithm/string.hpp>

#include "uscxml/Common.h"
#include "uscxml/config.h"
#include "SWIDataModel.h"
#include "uscxml/DOMUtils.h"
#include "uscxml/Message.h"
#include <glog/logging.h>

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

// these are defined but not exported by swi-prolog 7
extern "C" {
	PL_EXPORT(int) PL_is_dict(term_t t);
	PL_EXPORT(int) PL_for_dict(term_t dict, int (*func)(term_t key, term_t value, int last, void *closure), void *closure, int flags);
}
#define RETHROW_PLEX_AS_EVENT \
catch (PlException plex) { \
	Event e; \
	e.name = "error.execution"; \
	e.data.compound["cause"] = (char*)plex; \
	throw e; \
} \

// this might evolve into multi-threaded prolog, but no need for now
#define SET_PL_CONTEXT \
_dmPtr = this;

#define PL_MODULE \
_interpreter.getSessionId().c_str() \
 
#define UNSET_PL_ENGINE(dm) \
PL_set_engine(NULL, NULL);

#define SET_PL_ENGINE(dm) \
assert(_swiEngines.find(dm) != _swiEngines.end()); \
int rc = PL_set_engine(_swiEngines[dm], NULL); \
assert(rc == PL_ENGINE_SET); \
_dmPtr = dm;

namespace uscxml {

using namespace Arabica::XPath;
using namespace Arabica::DOM;

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

// SWI prolog does not support passing user data
static SWIDataModel* _dmPtr;
static std::map<SWIDataModel*, PL_engine_t> _swiEngines;

PL_blob_t SWIDataModel::blobType =
{ PL_BLOB_MAGIC,
	PL_BLOB_NOCOPY,
	(char*)"blob",
	releaseBlob,
	compareBlob,
	writeBlob,
	acquireBlob
};

SWIDataModel::SWIDataModel() {
}

SWIDataModel::~SWIDataModel() {
	try {
		if (_swiEngines.find(this) != _swiEngines.end()) {
			PL_destroy_engine(_swiEngines[this]);
			_swiEngines.erase(this);
		}
	} RETHROW_PLEX_AS_EVENT;
}

boost::shared_ptr<DataModelImpl> SWIDataModel::create(InterpreterImpl* interpreter) {
	try {
		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";

		int argc = 2;
		static char * av[] = {
			(char*)swibin,
			(char*)quiet,
			NULL
		};

		PL_engine_t engine;

		if (!PL_is_initialised(NULL, NULL)) {
			if(!PL_initialise(argc,av)) {
				LOG(ERROR) << "Error intializing prolog engine";
				PL_halt(1);
				return boost::shared_ptr<DataModelImpl>();
			}

			PL_set_engine(PL_ENGINE_CURRENT, &engine);

			// load SWI XML parser
			try {
				PlCall("use_module", PlCompound("library", PlTerm("sgml")));
			} catch (PlException plex) {

				LOG(ERROR) << "Cannot load prolog sgml module - make sure you have it installed in your prolog runtime: " << (char*)plex;
				throw plex;
			}

			// load json parser
			try {
				PlCall("use_module", PlCompound("library", PlTerm("http/json")));
				PlCall("use_module", PlCompound("library", PlTerm("http/json_convert")));
			} catch (PlException plex) {
				LOG(ERROR) << "Cannot load prolog json module or json_convert - make sure you have it installed in your prolog runtime: " << (char*)plex;
				throw plex;
			}

		} else {
			LOG(WARNING) << "Instantiating more than one SWI prolog datamodel will lead to weird effects as I cannot seperate the environments";
			engine = PL_create_engine(NULL);
		}

		assert(engine);
		_swiEngines[dm.get()] = engine;
		_dmPtr = dm.get();

		int rc = PL_set_engine(engine, NULL);
		assert(rc == PL_ENGINE_SET);
		(void)rc;

		_plModule = boost::replace_all_copy(interpreter->getSessionId(), "-", "");
		boost::replace_all(_plModule, "0", "g");
		boost::replace_all(_plModule, "1", "h");
		boost::replace_all(_plModule, "2", "i");
		boost::replace_all(_plModule, "3", "j");
		boost::replace_all(_plModule, "4", "k");
		boost::replace_all(_plModule, "5", "l");
		boost::replace_all(_plModule, "6", "m");
		boost::replace_all(_plModule, "7", "n");
		boost::replace_all(_plModule, "8", "o");
		boost::replace_all(_plModule, "9", "p");

		// use atoms for double quoted
		PlCall("set_prolog_flag(double_quotes,atom).");

		// set system variables
		PlCall("assert", PlCompound("sessionid", PlTerm(PlString(dm->_interpreter->getSessionId().c_str()))));
		PlCall("assert", PlCompound("name", PlTerm(PlString(dm->_interpreter->getName().c_str()))));

		std::map<std::string, IOProcessor>::const_iterator ioProcIter = dm->_interpreter->getIOProcessors().begin();
		while(ioProcIter != dm->_interpreter->getIOProcessors().end()) {
			Data ioProcData = ioProcIter->second.getDataModelVariables();

			if (ioProcIter->first.find_first_of(":/'") == std::string::npos) {
				std::stringstream ioProcShortCall;
				ioProcShortCall << "assert(ioprocessors(" << ioProcIter->first << "(location('" << ioProcData.compound["location"].atom << "'))))";
				PlCall(ioProcShortCall.str().c_str());
			}
			std::stringstream ioProcCall;
			ioProcCall << "assert(ioprocessors(name('" << ioProcIter->first << "'), location('" << ioProcData.compound["location"].atom << "')))";
			PlCall(ioProcCall.str().c_str());

			ioProcIter++;
		}

		// the in predicate
		PlRegister("user", "in", 1, SWIDataModel::inPredicate);
		return dm;
	} RETHROW_PLEX_AS_EVENT;
}

foreign_t SWIDataModel::inPredicate(term_t a0, int arity, void* context) {
	try {
		char *s;
		if ( PL_get_atom_chars(a0, &s) ) {
			NodeSet<std::string> config = _dmPtr->_interpreter->getConfiguration();
			for (int i = 0; i < config.size(); i++) {
				if (HAS_ATTR(config[i], "id") && strcmp(ATTR(config[i], "id").c_str(), s) == 0) {
					return TRUE;
				}
			}
		}
		return FALSE;
	} RETHROW_PLEX_AS_EVENT;
}

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

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) {

	SET_PL_CONTEXT;
	// remove old event
	try {
		PlCall("retractall(event(_))");

		// simple values
		PlCall("assert", PlCompound("event", PlCompound("name", PlTerm(event.name.c_str()))));
		PlCall("assert", PlCompound("event", PlCompound("origin", PlString(event.origin.c_str()))));
		PlCall("assert", PlCompound("event", PlCompound("origintype", PlString(event.invokeid.c_str()))));
		PlCall("assert", PlCompound("event", PlCompound("invokeid", PlTerm(event.origintype.c_str()))));
		PlCall("assert", PlCompound("event", PlCompound("raw", PlString(event.raw.c_str()))));

		// event.type
		std::string type;
		switch (event.eventType) {
		case Event::PLATFORM:
			type = "platform";
			break;
		case Event::INTERNAL:
			type = "internal";
			break;
		case Event::EXTERNAL:
			type = "external";
			break;
		}
		PlCall("assert", PlCompound("event", PlCompound("type", PlTerm(type.c_str()))));

		// event.sendid
		if (!event.hideSendId)
			PlCall("assert", PlCompound("event", PlCompound("sendid", PlTerm(event.sendid.c_str()))));

		// event.data
		URL domUrl;
		if (event.dom) {
			std::stringstream dataInitStr;
			std::stringstream xmlDoc;
//			xmlDoc << event.getFirstDOMElement();
			xmlDoc << event.dom;
			domUrl = URL::toLocalFile(xmlDoc.str(), ".pl");
			dataInitStr << "load_xml_file('" << domUrl.asLocalFile(".pl") << "', XML), copy_term(XML,DATA), assert(event(data(DATA)))";
			PlCall(dataInitStr.str().c_str());
		} else if (event.content.size() > 0) {
			PlCall("assert", PlCompound("event", PlCompound("data", PlString(Interpreter::spaceNormalize(event.content).c_str()))));
		} else if (event.data) {
			assertFromData(event.data, "event(data(", 2);
		}

		// event.params
		size_t uniqueKeys = 0;
		Event::params_t::const_iterator paramIter = event.params.begin();
		while(paramIter != event.params.end()) {
			uniqueKeys++;
			paramIter = event.params.upper_bound(paramIter->first);
		}
		if (uniqueKeys > 0) {
			paramIter = event.params.begin();
			for(int i = 0; paramIter != event.params.end(); i++) {
				std::stringstream paramArray;
				Event::params_t::const_iterator	lastValueIter = event.params.upper_bound(paramIter->first);

				paramArray << paramIter->first << "([";
				std::string termSep = "";

				for (int j = 0; paramIter != lastValueIter; j++) {
					paramArray << termSep << "'" << paramIter->second << "'";
					termSep = ", ";
					paramIter++;
				}
				paramArray << "])";
				std::stringstream paramExpr;
				paramExpr << "assert(event(param(" << paramArray.str() << ")))";
				//std::cout << paramExpr.str() << std::endl;
				PlCall(paramExpr.str().c_str());

				paramIter = lastValueIter;
			}
		}
	} RETHROW_PLEX_AS_EVENT;
}

void SWIDataModel::assertFromData(const Data& data, const std::string& expr, size_t nesting) {
	if (data.atom.size() > 0) {
		std::stringstream ss;
		ss << expr << "(";
		nesting++;

		if (data.type == Data::VERBATIM) {
			ss << "\"" << data.atom << "\"";
		} else {
			ss << data.atom;
		}
		
		for (size_t i = 0; i < nesting; i++) {
			ss << ")";
		}
		PlCall("assert", PlCompound(ss.str().c_str()));
		return;
	}
	
	if (data.compound.size() > 0) {
		std::map<std::string, Data>::const_iterator compIter = data.compound.begin();
		while(compIter != data.compound.end()) {
			assertFromData(compIter->second, expr + "(" + compIter->first, nesting + 1);
			compIter++;
		}
	}
	
	if (data.array.size() > 0) {
		std::list<Data>::const_iterator arrIter = data.array.begin();
		while(arrIter != data.array.end()) {
			assertFromData(*arrIter, expr, nesting);
			arrIter++;
		}
	}
	
	if (data.node) {
		std::stringstream dataInitStr;
		std::stringstream xmlDoc;

		xmlDoc << data.node;
		URL domUrl = URL::toLocalFile(xmlDoc.str(), ".pl");
		dataInitStr << "load_xml_file('" << domUrl.asLocalFile(".pl") << "', XML), ";
		dataInitStr << "copy_term(XML,DATA), ";
		dataInitStr << "assert(";
		dataInitStr << expr << "(DATA)";
		
		for (size_t i = 0; i < nesting; i++) {
			dataInitStr << ")";
		}

		PlCall(dataInitStr.str().c_str());
		return;
	}
}

Data SWIDataModel::getStringAsData(const std::string& content) {
	SET_PL_CONTEXT
	try {
		PlTerm term(content.c_str());
		return(termAsData(term));
	} RETHROW_PLEX_AS_EVENT
}

Data SWIDataModel::termAsData(PlTerm term) {
	Data data;

//	std::cout << term.name() << (char*)term << std::endl;
	
	switch (term.type()) {
		case PL_TERM:
			for (int i = 1; i <= term.arity(); i++) { // arguments start at 1
				data.compound[term.name()].array.push_back(termAsData(term[i]));
			}
			break;
		case PL_INTEGER:
		case PL_FLOAT:
		case PL_SHORT:
		case PL_INT:
		case PL_LONG:
		case PL_DOUBLE:
			data.atom = std::string(term);
			data.type = Data::INTERPRETED;
			break;
		case PL_ATOM:
			data.atom = std::string(term);
			data.type = Data::VERBATIM;
			break;
#ifdef SWI_HAS_PL_NIL
		case PL_NIL:
			data.array.push_back(Data("", Data::VERBATIM));
			break;
#endif
#ifdef SWI_HAS_PL_LIST_PAIR
		case PL_LIST_PAIR: {
			PlTail tail(term);
			PlTerm item;
			while(tail.next(item)) {
				data.array.push_back(termAsData(item));
			}
			break;
		}
#endif
#ifdef SWI_HAS_DICT
		case PL_DICT: {
			std::string key(term);
			size_t curlyPos = key.find_first_of("{");
			if (curlyPos == std::string::npos || curlyPos == 0) {
				// no key given
				PL_for_dict(term, SWIDataModel::dictCallBack, &data, 0);
			} else {
				// with key given
				Data& tmp = data.compound[boost::trim_copy(key.substr(0, curlyPos))];
				PL_for_dict(term, SWIDataModel::dictCallBack, &tmp, 0);
			}
			break;
		}
#endif
		default:
			LOG(ERROR) << "Prolog type " << term.type() << " at '" << (char*)term << "' not supported";
			break;
	}
	return data;
}

int SWIDataModel::dictCallBack(term_t key, term_t value, int last, void *closure) {
	Data* data = (Data*)closure;
	PlTerm keyTerm(key);
	data->compound[(char*)keyTerm] = termAsData(value);
	return 0;
}

PlTerm SWIDataModel::dataAsTerm(Data data) {
	if (data.atom.length() > 0) {
		return PlTerm(data.atom.c_str());
	}
	if (data.array.size() > 0) {
		PlTerm head;
		PlTail list(head);
		
		std::list<Data>::const_iterator arrIter = data.array.begin();
		while(arrIter != data.array.end()) {
			list.append(dataAsTerm(*arrIter));
			arrIter++;
		}
		list.close();
		return PlTail(head);
	}
	if (data.compound.size() > 0) {
		if (data.compound.size() == 1 && data.compound.begin()->second.array.size() > 0) {
			// this used to be a prolog compound
			const Data& arr = data.compound.begin()->second;
			PlTermv termv(arr.array.size());
			int index = 0;

			std::list<Data>::const_iterator arrIter = arr.array.begin();
			while(arrIter != arr.array.end()) {
				termv[index] = dataAsTerm(*arrIter);
				index++;
				arrIter++;
			}
			return PlCompound(data.compound.begin()->first.c_str(), termv);
		} else if (data.compound.size() == 1 && data.compound.begin()->second.compound.size() > 0) {
			// this used to be a named dict - until we have dict support in C/C++ use PL_chars_to_term
			std::stringstream dictSS;
			std::string seperator;
			dictSS << data.compound.begin()->first << "{";
			
			std::map<std::string, Data>::const_iterator keyIter = data.compound.begin()->second.compound.begin();
			while(keyIter != data.compound.begin()->second.compound.end()) {
				dictSS << seperator << keyIter->first << ":" << (char*)dataAsTerm(keyIter->second);
				seperator = ",";
				keyIter++;
			}
			dictSS << "}";
			return PlCompound(dictSS.str().c_str());
			
		} else {
			// an array of dicts
			PlTermv termv(data.compound.size());
			int index = 0;
			
			std::map<std::string, Data>::const_iterator compIter = data.compound.begin();
			while(compIter != data.compound.end()) {
				termv[index] = PlCompound(compIter->first.c_str(), dataAsTerm(compIter->second));
				index++;
				compIter++;
			}
			return PlCompound(data.compound.begin()->first.c_str(), termv);

		}
	}
	if (data.binary) {
		LOG(ERROR) << "Binary data with prolog datamodel still very experimental";
//		term_t binTerm = PL_new_term_ref();
//		PL_put_blob(binTerm, data.binary->data, data.binary->size, &blobType);
//		return binTerm;
	}
	if (data.node) {
		LOG(ERROR) << "DOM in event with prolog datamodel still very experimental";
		std::stringstream dataInitStr;
		std::stringstream xmlDoc;
		//			xmlDoc << event.getFirstDOMElement();
		xmlDoc << data.node;
		URL domUrl = URL::toLocalFile(xmlDoc.str(), ".pl");
		dataInitStr << "load_xml_file('" << domUrl.asLocalFile(".pl") << "', XML), copy_term(XML,DATA), assert(event(data(DATA)))";
		PlCall(dataInitStr.str().c_str());
	}
	
	return PlTerm();
}

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

uint32_t SWIDataModel::getLength(const std::string& expr) {
	SET_PL_CONTEXT
	try {
		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);
		uint32_t length = 0;
		while(query.next_solution() > 0)
			length++;
		return length;
	} RETHROW_PLEX_AS_EVENT;
}

void SWIDataModel::setForeach(const std::string& item,
                              const std::string& array,
                              const std::string& index,
                              uint32_t iteration) {
	SET_PL_CONTEXT
	try {
		PlCompound compound(array.c_str());
		PlCompound orig(array.c_str());
		PlTermv termv(compound.arity());
		for (int i = 0; i < compound.arity(); i++) {
			termv[i] = compound[i + 1];
		}
		{
			int tmp = iteration + 1;
			PlQuery query(compound.name(), termv);
			while (tmp) {
				query.next_solution();
				tmp--;
			}
		}
		PlCall("retractall", PlCompound(index.c_str(), 1));
		PlCall("retractall", PlCompound(item.c_str(), 1));
		PlCall("assert", PlCompound(index.c_str(), PlTerm((long)iteration)));

		std::map<std::string, PlTerm> vars = resolveAtoms(compound, orig);
		std::map<std::string, PlTerm>::iterator varIter = vars.begin();
		while(varIter != vars.end()) {
			PlCall("assert", PlCompound(item.c_str(), varIter->second));
			varIter++;
		}
	} RETHROW_PLEX_AS_EVENT;
}

void SWIDataModel::eval(const Element<std::string>& scriptElem, const std::string& expr) {
	SET_PL_CONTEXT
	try {
		if (scriptElem && HAS_ATTR(scriptElem, "type") && iequals(ATTR(scriptElem, "type"), "query")) {
			evalAsBool(expr);
		} else {
			URL localPLFile = URL::toLocalFile(expr, ".pl");
			PlCall("user", "load_files", PlTermv(localPLFile.asLocalFile(".pl").c_str())) || LOG(ERROR) << "Could not execute prolog from file";
		}
	} RETHROW_PLEX_AS_EVENT;
}

bool SWIDataModel::evalAsBool(const std::string& expr) {
	return evalAsBool(Arabica::DOM::Node<std::string>(), expr);
}

bool SWIDataModel::evalAsBool(const Arabica::DOM::Node<std::string>& node, const std::string& expr) {
	SET_PL_CONTEXT
	try {
		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;
	} catch(...) {
		return false;
	}
}

std::string SWIDataModel::evalAsString(const std::string& expr) {
	SET_PL_CONTEXT
	try {
		
		PlCompound orig(expr.c_str()); // keep the original to find variables
		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;
			std::string solSeparator = "";
			while (query.next_solution()) {
				ss << solSeparator;
				std::map<std::string, PlTerm> vars = resolveAtoms(compound, orig);
				std::map<std::string, PlTerm>::const_iterator varIter = vars.begin();

				std::string varSeparator = "";
				while(varIter != vars.end()) {
					ss << varSeparator << (char *)varIter->second;
					varSeparator = ", ";
					varIter++;
				}
				solSeparator = "\n";
			}
			return ss.str();
		}
		return std::string(compound);

	} catch(PlException plex) {
		// we got an exception while trying to evaluate as compound
		PlTerm term(expr.c_str());
		if (term.type() == PL_ATOM || term.type() == PL_CHARS || term.type() == PL_STRING) {
			return std::string(term);
		} else {
			Event e;
			e.name = "error.execution";
			e.data.compound["cause"] = (char*)plex;
			throw e;
		}
	}
}

// this is similar to http://etalis.googlecode.com/svn/eEtalis/src/term.c
std::map<std::string, PlTerm> SWIDataModel::resolveAtoms(PlTerm& term, PlTerm& orig) {
	SET_PL_CONTEXT
	try {
		std::map<std::string, PlTerm> atoms;
		switch (orig.type()) {
		case PL_VARIABLE: {
			atoms[(char *)orig] = term;
			break;
		}
		case PL_ATOM:
			break;
		case PL_STRING:
			break;
		case PL_INTEGER:
			break;
		case PL_TERM:
			for (int i = 1; i <= orig.arity(); i++) {
				PlTerm newTerm = term[i];
				PlTerm newOrig = orig[i];
				std::map<std::string, PlTerm> result = resolveAtoms(newTerm, newOrig);
				atoms.insert(result.begin(), result.end());
			}
			break;
		default:
			LOG(ERROR) << "Resolving variable of unknown type in query solution";
		}
		return atoms;
	} RETHROW_PLEX_AS_EVENT
}

void SWIDataModel::assign(const Element<std::string>& assignElem,
                          const Node<std::string>& node,
                          const std::string& content) {
	SET_PL_CONTEXT
	try {
		std::string expr = content;
		std::string predicate;
		if (HAS_ATTR(assignElem, "expr")) {
			expr = ATTR(assignElem, "expr");
		}
		if (HAS_ATTR(assignElem, "id"))
			predicate = ATTR(assignElem, "id");
		if (HAS_ATTR(assignElem, "location"))
			predicate = ATTR(assignElem, "location");

		if (predicate.size() > 0) {
			std::string callAssert = "assert";
			std::string type;
			if (HAS_ATTR(assignElem, "type")) {
				type = ATTR(assignElem, "type");
				if(iequals(type, "append")) {
					callAssert = "assertz";
				} else if(iequals(type, "prepend")) {
					callAssert = "asserta";
				}
			}

			URL domUrl;
			Data json;
			if (!node)
				json = Data::fromJSON(expr);
			if (node) {
				std::stringstream dataInitStr;
				std::stringstream xmlDoc;
				Node<std::string> child = node;
				while(child) {
					xmlDoc << child;
					child = child.getNextSibling();
				}
				domUrl = URL::toLocalFile(xmlDoc.str(), ".pl");
				if (iequals(type, "retract"))
					PlCall("retractall", PlCompound(predicate.c_str(), 1));
				dataInitStr << "load_xml_file('" << domUrl.asLocalFile(".pl") << "', XML), copy_term(XML,DATA), " << callAssert << "(" << predicate << "(DATA))";
				PlCall(dataInitStr.str().c_str());
			} else if (json) {
				std::stringstream dataInitStr;
				if (iequals(type, "retract"))
					PlCall("retractall", PlCompound(predicate.c_str(), 1));
				dataInitStr << "json_to_prolog(" << expr << ", JSON), assert(" << predicate << "(JSON))";
				PlCall(dataInitStr.str().c_str());
			} else {
				// treat content as . seperated facts
				std::stringstream factStream(content);
				std::string item;
				while(std::getline(factStream, item, '.')) {
					std::string fact = boost::trim_copy(item);
					if (fact.length() == 0)
						continue;
					PlCall((callAssert + "(" + predicate + "(" + fact + "))").c_str());
				}
			}
		} else if (expr.length() > 0) {
			if (boost::equals(TAGNAME(assignElem), "data")) {
				eval(assignElem, expr);
			} else {
				std::stringstream exprStream(expr);
				std::string item;
				while(std::getline(exprStream, item, '.')) {
					std::string plExpr = boost::trim_copy(item);
					if (plExpr.length() == 0)
						continue;
					PlCall(plExpr.c_str());
				}
			}
		}
	} RETHROW_PLEX_AS_EVENT
}

void SWIDataModel::assign(const std::string& location, const Data& data) {
	eval(Element<std::string>(), data.atom);
}

void SWIDataModel::init(const Element<std::string>& dataElem,
                        const Node<std::string>& node,
                        const std::string& content) {
	assign(dataElem, node, content);
}
void SWIDataModel::init(const std::string& location, const Data& data) {
	assign(location, data);
}

bool SWIDataModel::isDeclared(const std::string& expr) {
	return true;
}

void SWIDataModel::acquireBlob(atom_t symbol) {
}
	
	
int SWIDataModel::releaseBlob(atom_t symbol) {
	return TRUE;
}

int SWIDataModel::compareBlob(atom_t a, atom_t b) {
	return 0;
}

int SWIDataModel::writeBlob(void *s, atom_t symbol, int flags) {
	return TRUE;
}

	
}