diff options
author | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-07-08 00:20:56 (GMT) |
---|---|---|
committer | Stefan Radomski <radomski@tk.informatik.tu-darmstadt.de> | 2014-07-08 00:20:56 (GMT) |
commit | dbd110e2b7eb08c65218a5f9d09ef12fdc62c04a (patch) | |
tree | 93444e4c106eeca34d928521bedbda8080410795 /src | |
parent | d5e1f6397c52513018cd59972cf5ca8740de18eb (diff) | |
download | uscxml-dbd110e2b7eb08c65218a5f9d09ef12fdc62c04a.zip uscxml-dbd110e2b7eb08c65218a5f9d09ef12fdc62c04a.tar.gz uscxml-dbd110e2b7eb08c65218a5f9d09ef12fdc62c04a.tar.bz2 |
First signs of Lua Datamodel
Diffstat (limited to 'src')
-rw-r--r-- | src/uscxml/Factory.cpp | 11 | ||||
-rw-r--r-- | src/uscxml/Interpreter.cpp | 4 | ||||
-rw-r--r-- | src/uscxml/interpreter/InterpreterDraft6.cpp | 6 | ||||
-rw-r--r-- | src/uscxml/plugins/datamodel/CMakeLists.txt | 24 | ||||
-rw-r--r-- | src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp | 199 | ||||
-rw-r--r-- | src/uscxml/plugins/datamodel/lua/LuaDataModel.h | 111 |
6 files changed, 350 insertions, 5 deletions
diff --git a/src/uscxml/Factory.cpp b/src/uscxml/Factory.cpp index 0f3e8c9..62756fc 100644 --- a/src/uscxml/Factory.cpp +++ b/src/uscxml/Factory.cpp @@ -104,6 +104,10 @@ # include "uscxml/plugins/datamodel/prolog/swi/SWIDataModel.h" # endif +# ifdef LUA_FOUND +# include "uscxml/plugins/datamodel/lua/LuaDataModel.h" +# endif + #include "uscxml/plugins/datamodel/xpath/XPathDataModel.h" #include "uscxml/plugins/datamodel/promela/PromelaDataModel.h" @@ -299,6 +303,13 @@ void Factory::registerPlugins() { } #endif +#if (defined LUA_FOUND && defined BUILD_DM_LUA) + { + LuaDataModel* dataModel = new LuaDataModel(); + registerDataModel(dataModel); + } +#endif + #if (defined BUILD_DM_PROMELA) { PromelaDataModel* dataModel = new PromelaDataModel(); diff --git a/src/uscxml/Interpreter.cpp b/src/uscxml/Interpreter.cpp index e6fc8fa..d010a8a 100644 --- a/src/uscxml/Interpreter.cpp +++ b/src/uscxml/Interpreter.cpp @@ -2250,8 +2250,8 @@ NodeSet<std::string> InterpreterImpl::getProperAncestors(const Arabica::DOM::Nod if (iequals(TAGNAME(nodeElem), _nsInfo.xmlNSPrefix + "scxml")) // do not return scxml root itself - this is somewhat ill-defined break; if (!iequals(TAGNAME(nodeElem), _nsInfo.xmlNSPrefix + "parallel") && - !iequals(TAGNAME(nodeElem), _nsInfo.xmlNSPrefix + "state") && - !iequals(TAGNAME(nodeElem), _nsInfo.xmlNSPrefix + "scxml")) + !iequals(TAGNAME(nodeElem), _nsInfo.xmlNSPrefix + "state") && + !iequals(TAGNAME(nodeElem), _nsInfo.xmlNSPrefix + "scxml")) break; if (node == s2) break; diff --git a/src/uscxml/interpreter/InterpreterDraft6.cpp b/src/uscxml/interpreter/InterpreterDraft6.cpp index 20f89d2..e2ba95e 100644 --- a/src/uscxml/interpreter/InterpreterDraft6.cpp +++ b/src/uscxml/interpreter/InterpreterDraft6.cpp @@ -948,10 +948,10 @@ void InterpreterDraft6::enterStates(const Arabica::XPath::NodeSet<std::string>& if (isFinal(stateElem)) { internalDoneSend(stateElem); Node<std::string> parent = stateElem.getParentNode(); - + if (parent.getNodeType() == Node_base::ELEMENT_NODE && - parent.getParentNode().getNodeType() == Node_base::ELEMENT_NODE && - isParallel(Element<std::string>(parent.getParentNode()))) { + parent.getParentNode().getNodeType() == Node_base::ELEMENT_NODE && + isParallel(Element<std::string>(parent.getParentNode()))) { Element<std::string> grandParent = (Element<std::string>)parent.getParentNode(); Arabica::XPath::NodeSet<std::string> childs = getChildStates(grandParent); diff --git a/src/uscxml/plugins/datamodel/CMakeLists.txt b/src/uscxml/plugins/datamodel/CMakeLists.txt index 1b2a047..acf7344 100644 --- a/src/uscxml/plugins/datamodel/CMakeLists.txt +++ b/src/uscxml/plugins/datamodel/CMakeLists.txt @@ -79,6 +79,30 @@ file(GLOB NULL_DATAMODEL list (APPEND USCXML_FILES ${NULL_DATAMODEL}) +# Lua datamodel + +if (BUILD_DM_LUA AND LUA_FOUND) + set(USCXML_DATAMODELS "lua ${USCXML_DATAMODELS}") + file(GLOB LUA_DATAMODEL + lua/*.cpp + lua/*.h + ) + if (BUILD_AS_PLUGINS) + source_group("" FILES ${LUA_DATAMODEL}) + add_library(datamodel_lua SHARED ${LUA_DATAMODEL} "../Plugins.cpp") + target_link_libraries(datamodel_lua + uscxml + ${LUA_LIBRARIES} + ) + set_target_properties(datamodel_lua PROPERTIES FOLDER "Plugin DataModel") + set_target_properties(datamodel_lua PROPERTIES COMPILE_FLAGS "-DPLUMA_EXPORTS") + set_target_properties(datamodel_lua PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIR}/lib") + else() + list (APPEND USCXML_FILES ${LUA_DATAMODEL}) + endif() +endif() + + # XPath datamodel if (BUILD_DM_XPATH) diff --git a/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp new file mode 100644 index 0000000..6fb0369 --- /dev/null +++ b/src/uscxml/plugins/datamodel/lua/LuaDataModel.cpp @@ -0,0 +1,199 @@ +/** + * @file + * @author 2012-2014 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 "LuaDataModel.h" +#include "LuaBridge.h" +#include "uscxml/DOMUtils.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 pluginConnect(pluma::Host& host) { + host.add( new LuaDataModelProvider() ); + return true; +} +#endif + +LuaDataModel::LuaDataModel() { + _luaState = NULL; +} + +static int luaEventData(lua_State * l); +static int luaEventOrigin(lua_State * l); +static int luaEventOriginType(lua_State * l); +static int luaEventRaw(lua_State * l); +static int luaEventXML(lua_State * l); +static int luaEventName(lua_State * l); +static int luaEventContent(lua_State * l); +static int luaEventSendId(lua_State * l); +static int luaEventInvokeId(lua_State * l); +static int luaEventDestructor(lua_State * l); + +static int luaInFunction(lua_State * l) { + luabridge::LuaRef ref = luabridge::getGlobal(l, "__interpreter"); + InterpreterImpl* interpreter = ref.cast<InterpreterImpl*>(); + + int stackSize = lua_gettop(l); + for (int i = 0; i < stackSize; i++) { + if (!lua_isstring(l, -1 - i)) + continue; + std::string stateName = lua_tostring(l, -1 - i); + if (interpreter->isInState(stateName)) + continue; + lua_pushboolean(l, 0); + return 1; + } + lua_pushboolean(l, 1); + return 1; +} + + +boost::shared_ptr<DataModelImpl> LuaDataModel::create(InterpreterImpl* interpreter) { + boost::shared_ptr<LuaDataModel> dm = boost::shared_ptr<LuaDataModel>(new LuaDataModel()); + dm->_interpreter = interpreter; + dm->_luaState = luaL_newstate(); + luaL_openlibs(dm->_luaState); + + luabridge::getGlobalNamespace(dm->_luaState).beginClass<InterpreterImpl>("__interpreter").endClass(); + luabridge::getGlobalNamespace(dm->_luaState).addCFunction("In", luaInFunction); + + luabridge::setGlobal(dm->_luaState, dm->_interpreter, "__interpreter"); + + return dm; +} + +LuaDataModel::~LuaDataModel() { + if (_luaState != NULL) + lua_close(_luaState); +} + +void LuaDataModel::pushContext() { +} + +void LuaDataModel::popContext() { +} + +void LuaDataModel::initialize() { +} + +void LuaDataModel::setEvent(const Event& event) { +} + +Data LuaDataModel::getStringAsData(const std::string& content) { + Data data = Data::fromJSON(content); + if (data.empty()) { + data = Data(content, Data::VERBATIM); + } + return data; +} + +bool LuaDataModel::validate(const std::string& location, const std::string& schema) { + return true; +} + +bool LuaDataModel::isLocation(const std::string& expr) { + return true; +} + +uint32_t LuaDataModel::getLength(const std::string& expr) { + return 0; +} + +void LuaDataModel::setForeach(const std::string& item, + const std::string& array, + const std::string& index, + uint32_t iteration) { +} + +void LuaDataModel::eval(const Arabica::DOM::Element<std::string>& scriptElem, + const std::string& expr) { + +} + +bool LuaDataModel::isDeclared(const std::string& expr) { + return true; +} + +void LuaDataModel::assign(const Arabica::DOM::Element<std::string>& assignElem, + const Arabica::DOM::Node<std::string>& node, + const std::string& content) { + +} + +void LuaDataModel::assign(const std::string& location, const Data& data) { + +} + +void LuaDataModel::init(const Arabica::DOM::Element<std::string>& dataElem, + const Arabica::DOM::Node<std::string>& node, + const std::string& content) { + +} + +void LuaDataModel::init(const std::string& location, const Data& data) { + +} + +/** + * The boolean expression language consists of the In predicate only. It has the + * form 'In(id)', where id is the id of a state in the enclosing state machine. + * The predicate must return 'true' if and only if that state is in the current + * state configuration. + */ +bool LuaDataModel::evalAsBool(const Arabica::DOM::Node<std::string>& node, const std::string& expr) { + // we need the result of the expression on the lua stack -> has to "return"! + std::string trimmedExpr = boost::trim_copy(expr); + if (!boost::starts_with(trimmedExpr, "return")) { + trimmedExpr = "return(" + trimmedExpr + ")"; + } + int error = luaL_loadstring(_luaState, trimmedExpr.c_str()) || lua_pcall(_luaState, 0, LUA_MULTRET, 0); + if (error) { + std::string errMsg = lua_tostring(_luaState, -1); + lua_pop(_luaState, 1); /* pop error message from the stack */ + ERROR_EXECUTION_THROW(errMsg); + } + int stackSize = lua_gettop(_luaState); + if (stackSize != 1) + return false; + if (lua_isboolean(_luaState, -1)) + return lua_toboolean(_luaState, -1); + return false; +} + + +std::string LuaDataModel::evalAsString(const std::string& expr) { + return expr; +} + +double LuaDataModel::evalAsNumber(const std::string& expr) { + return 0; +} + +}
\ No newline at end of file diff --git a/src/uscxml/plugins/datamodel/lua/LuaDataModel.h b/src/uscxml/plugins/datamodel/lua/LuaDataModel.h new file mode 100644 index 0000000..9677307 --- /dev/null +++ b/src/uscxml/plugins/datamodel/lua/LuaDataModel.h @@ -0,0 +1,111 @@ +/** + * @file + * @author 2012-2014 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 + */ + +#ifndef LUADATAMODEL_H_113E014C +#define LUADATAMODEL_H_113E014C + +#include "uscxml/Interpreter.h" +#include <list> + +#ifdef BUILD_AS_PLUGINS +#include "uscxml/plugins/Plugins.h" +#endif + +extern "C" { +#include "lua.h" +#include "lualib.h" +#include "lauxlib.h" +} + +namespace uscxml { +class Event; +class Data; +} + +namespace uscxml { + +class LuaDataModel : public DataModelImpl { +public: + LuaDataModel(); + virtual ~LuaDataModel(); + virtual boost::shared_ptr<DataModelImpl> create(InterpreterImpl* interpreter); + + virtual std::list<std::string> getNames() { + std::list<std::string> names; + names.push_back("lua"); + return names; + } + + virtual void initialize(); + virtual void setEvent(const Event& event); + + virtual bool validate(const std::string& location, const std::string& schema); + virtual bool isLocation(const std::string& expr); + + virtual uint32_t getLength(const std::string& expr); + virtual void setForeach(const std::string& item, + const std::string& array, + const std::string& index, + uint32_t iteration); + + virtual void pushContext(); + virtual void popContext(); + + virtual void assign(const Arabica::DOM::Element<std::string>& assignElem, + const Arabica::DOM::Node<std::string>& node, + const std::string& content); + virtual void assign(const std::string& location, const Data& data); + + virtual void init(const Arabica::DOM::Element<std::string>& dataElem, + const Arabica::DOM::Node<std::string>& node, + const std::string& content); + virtual void init(const std::string& location, const Data& data); + + virtual Data getStringAsData(const std::string& content); + virtual bool isDeclared(const std::string& expr); + + virtual void eval(const Arabica::DOM::Element<std::string>& scriptElem, + const std::string& expr); + virtual std::string evalAsString(const std::string& expr); + virtual bool evalAsBool(const Arabica::DOM::Node<std::string>& node, const std::string& expr); + virtual double evalAsNumber(const std::string& expr); + +protected: + + static int luaEventData(lua_State * l); + static int luaEventOrigin(lua_State * l); + static int luaEventOriginType(lua_State * l); + static int luaEventRaw(lua_State * l); + static int luaEventXML(lua_State * l); + static int luaEventName(lua_State * l); + static int luaEventContent(lua_State * l); + static int luaEventSendId(lua_State * l); + static int luaEventInvokeId(lua_State * l); + static int luaEventDestructor(lua_State * l); + + lua_State* _luaState; +}; + +#ifdef BUILD_AS_PLUGINS +PLUMA_INHERIT_PROVIDER(LuaDataModel, DataModelImpl); +#endif + +} + +#endif /* end of include guard: LUADATAMODEL_H_113E014C */ |