summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2013-05-01 23:25:28 (GMT)
committerStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2013-05-01 23:25:28 (GMT)
commit0202a8780179c710b2596e25567d4cf480dca277 (patch)
tree4c33ead20947aad00bda5055648248e84842a4e7 /src
parent1dc899c70362de2eed1f6dc074dc5238df0c7f3c (diff)
downloaduscxml-0202a8780179c710b2596e25567d4cf480dca277.zip
uscxml-0202a8780179c710b2596e25567d4cf480dca277.tar.gz
uscxml-0202a8780179c710b2596e25567d4cf480dca277.tar.bz2
More work on Prolog datamodel
Diffstat (limited to 'src')
-rw-r--r--src/uscxml/Message.cpp12
-rw-r--r--src/uscxml/Message.h3
-rw-r--r--src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp71
3 files changed, 67 insertions, 19 deletions
diff --git a/src/uscxml/Message.cpp b/src/uscxml/Message.cpp
index 67198ea..8a65ed2 100644
--- a/src/uscxml/Message.cpp
+++ b/src/uscxml/Message.cpp
@@ -111,6 +111,14 @@ Arabica::DOM::Document<std::string> Data::toDocument() {
}
Arabica::DOM::Node<std::string> Event::getFirstDOMElement() const {
+ return getFirstDOMElement(dom);
+}
+
+Arabica::DOM::Document<std::string> Event::getStrippedDOM() const {
+ return getStrippedDOM(dom);
+}
+
+Arabica::DOM::Node<std::string> Event::getFirstDOMElement(const Arabica::DOM::Document<std::string> dom) {
Arabica::DOM::Node<std::string> data = dom.getDocumentElement().getFirstChild();
while (data) {
if (data.getNodeType() == Arabica::DOM::Node_base::TEXT_NODE) {
@@ -125,11 +133,11 @@ Arabica::DOM::Node<std::string> Event::getFirstDOMElement() const {
return data;
}
-Arabica::DOM::Document<std::string> Event::getStrippedDOM() const {
+Arabica::DOM::Document<std::string> Event::getStrippedDOM(const Arabica::DOM::Document<std::string> dom) {
Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
Arabica::DOM::Document<std::string> document = domFactory.createDocument("", "", 0);
if (dom) {
- document.getDocumentElement().appendChild(document.importNode(getFirstDOMElement(), true));
+ document.getDocumentElement().appendChild(document.importNode(getFirstDOMElement(dom), true));
}
return document;
}
diff --git a/src/uscxml/Message.h b/src/uscxml/Message.h
index 3bb30fc..1d8cb53 100644
--- a/src/uscxml/Message.h
+++ b/src/uscxml/Message.h
@@ -142,6 +142,9 @@ public:
Arabica::DOM::Node<std::string> getFirstDOMElement() const;
Arabica::DOM::Document<std::string> getStrippedDOM() const;
+ static Arabica::DOM::Node<std::string> getFirstDOMElement(const Arabica::DOM::Document<std::string> dom);
+ static Arabica::DOM::Document<std::string> getStrippedDOM(const Arabica::DOM::Document<std::string> dom);
+
std::string getRaw() {
return raw;
}
diff --git a/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp b/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp
index 7e63174..b031c00 100644
--- a/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp
+++ b/src/uscxml/plugins/datamodel/prolog/swi/SWIDataModel.cpp
@@ -50,6 +50,13 @@ boost::shared_ptr<DataModelImpl> SWIDataModel::create(InterpreterImpl* interpret
// load SWI XML parser
PlCall("use_module", PlCompound("library", PlTerm("sgml")));
+
+ // load json parser
+ PlCall("use_module", PlCompound("library", PlTerm("http/json")));
+ PlCall("use_module", PlCompound("library", PlTerm("http/json_convert")));
+
+ // 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()))));
@@ -336,21 +343,41 @@ void SWIDataModel::assign(const Arabica::DOM::Element<std::string>& assignElem,
predicate = ATTR(assignElem, "location");
if (predicate.size() > 0) {
- size_t aritySep = predicate.find_first_of("/");
- if (aritySep != std::string::npos) {
- std::string functor = predicate.substr(0, aritySep);
- std::string arity = predicate.substr(aritySep + 1);
- std::string callAssert = "assert";
- if (HAS_ATTR(assignElem, "type")) {
- std::string type = ATTR(assignElem, "type");
- if (boost::iequals(type, "retract")) {
- PlCall("retractall", PlCompound(functor.c_str(), strTo<long>(arity)));
- } else if(boost::iequals(type, "append")) {
- callAssert = "assertz";
- } else if(boost::iequals(type, "prepend")) {
- callAssert = "asserta";
- }
+ std::string callAssert = "assert";
+ std::string type;
+ if (HAS_ATTR(assignElem, "type")) {
+ type = ATTR(assignElem, "type");
+ if(boost::iequals(type, "append")) {
+ callAssert = "assertz";
+ } else if(boost::iequals(type, "prepend")) {
+ callAssert = "asserta";
+ }
+ }
+
+ URL domUrl;
+ Data json;
+ if (!doc)
+ json = Data::fromJSON(expr);
+ if (doc) {
+ std::stringstream dataInitStr;
+ std::stringstream xmlDoc;
+ Arabica::DOM::Node<std::string> node = Event::getFirstDOMElement(doc);
+ while(node) {
+ xmlDoc << node;
+ node = node.getNextSibling();
}
+ domUrl = URL::toLocalFile(xmlDoc.str(), ".pl");
+ if (boost::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 (boost::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;
@@ -358,12 +385,22 @@ void SWIDataModel::assign(const Arabica::DOM::Element<std::string>& assignElem,
std::string fact = boost::trim_copy(item);
if (fact.length() == 0)
continue;
- PlCall((callAssert + "(" + functor + "(" + fact + "))").c_str());
+ PlCall((callAssert + "(" + predicate + "(" + fact + "))").c_str());
}
-
}
} else if (expr.length() > 0) {
- eval(expr);
+ if (boost::equals(TAGNAME(assignElem), "data")) {
+ eval(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());
+ }
+ }
}
}