diff options
Diffstat (limited to 'src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp')
-rw-r--r-- | src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp | 70 |
1 files changed, 65 insertions, 5 deletions
diff --git a/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp b/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp index db91869..5d6455f 100644 --- a/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp +++ b/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp @@ -43,6 +43,12 @@ boost::shared_ptr<DataModelImpl> SWIDataModel::create(InterpreterImpl* interpret return boost::shared_ptr<DataModelImpl>(); } + // load SWI XML parser + PlCall("use_module", PlCompound("library", PlTerm("sgml"))); + PlCall("assert", PlCompound("sessionId", PlTerm(PlString(dm->_interpreter->getSessionId().c_str())))); + PlCall("assert", PlCompound("name", PlTerm(PlString(dm->_interpreter->getName().c_str())))); + + PlCall("assert(eventName(X) :- event(Y,_), arg(1, Y, X))."); return dm; } @@ -76,8 +82,55 @@ void SWIDataModel::initialize() { } void SWIDataModel::setEvent(const Event& event) { -// std::cout << "SWIDataModel::setEvent" << std::endl; - _event = event; + // remove old event + PlCall("system", "retractall", PlTermv("event")); + + PlTermv plEvent(7); + plEvent[0] = PlCompound("name", PlTerm(PlString(event.name.c_str()))); + plEvent[1] = PlCompound("raw", PlTerm(PlString(event.raw.c_str()))); + plEvent[2] = PlCompound("origin", PlTerm(PlString(event.origin.c_str()))); + plEvent[3] = PlCompound("origintype", PlTerm(PlString(event.origintype.c_str()))); + plEvent[4] = PlCompound("data", PlTerm(PlString(event.content.c_str()))); + + Event::params_t::const_iterator paramIter; + // count unique keys in params + paramIter = event.params.begin(); + size_t uniqueKeys = 0; + while(paramIter != event.params.end()) { + uniqueKeys++; + paramIter = event.params.upper_bound(paramIter->first); + } + + // create a compund for every unique key + PlTermv paramTerm(uniqueKeys); + paramIter = event.params.begin(); + for(int i = 0; paramIter != event.params.end(); i++) { + Event::params_t::const_iterator lastValueIter = event.params.upper_bound(paramIter->first); + size_t items = event.params.count(paramIter->first); + PlTermv keyTerm(items); + for (int j = 0; paramIter != lastValueIter; j++) { + keyTerm[j] = PlString(paramIter->second.c_str()); + paramIter++; + } + paramTerm[i] = PlCompound(paramIter->first.c_str(), keyTerm); + paramIter = lastValueIter; + } + plEvent[5] = PlCompound("params", paramTerm); + + PlTerm type; + switch (event.type) { + case Event::PLATFORM: + type = "platform"; + break; + case Event::INTERNAL: + type = "internal"; + break; + case Event::EXTERNAL: + type = "external"; + break; + } + plEvent[6] = PlCompound("type", type); + PlCall("assert", PlCompound("event", plEvent)); } Data SWIDataModel::getStringAsData(const std::string& content) { @@ -128,13 +181,12 @@ std::string SWIDataModel::evalAsString(const std::string& expr) { PlQuery query(compound.name(), termv); std::stringstream ss; + const char* separator = ""; 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(); } @@ -158,7 +210,15 @@ void SWIDataModel::assign(const std::string& location, const Data& data) { void SWIDataModel::init(const Arabica::DOM::Element<std::string>& dataElem, const Arabica::DOM::Document<std::string>& doc, - const std::string& content) {} + const std::string& content) { + std::string key; + if (HAS_ATTR(dataElem, "id")) { + key = ATTR(dataElem, "id"); + } else if (HAS_ATTR(dataElem, "location")) { + key = ATTR(dataElem, "location"); + } + assign(dataElem, doc, content); +} void SWIDataModel::init(const std::string& location, const Data& data) {} bool SWIDataModel::isDeclared(const std::string& expr) { |