summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2013-08-13 17:30:58 (GMT)
committerStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2013-08-13 17:30:58 (GMT)
commitcf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9 (patch)
treed0561246efce6d170050b64d48f8b49316f149af
parent422dedee98e956a7f4cffa69a4ba0a34716dec7f (diff)
downloaduscxml-cf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9.zip
uscxml-cf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9.tar.gz
uscxml-cf45e4bf71dd2a8b27c50d247c3f09a1e22e1fa9.tar.bz2
More work on java datamodel interface
-rw-r--r--contrib/java/.classpath1
-rw-r--r--contrib/java/.project5
-rw-r--r--contrib/java/src/org/uscxml/tests/TestData.java23
-rw-r--r--contrib/java/src/org/uscxml/tests/TestDataModel.java11
-rw-r--r--contrib/java/src/org/uscxml/tests/TestInvoker.java9
-rw-r--r--src/bindings/swig/java/org/uscxml/Data.java88
-rw-r--r--src/bindings/swig/java/uscxml.i89
-rw-r--r--src/uscxml/Factory.h2
8 files changed, 214 insertions, 14 deletions
diff --git a/contrib/java/.classpath b/contrib/java/.classpath
index 053c69a..96001cb 100644
--- a/contrib/java/.classpath
+++ b/contrib/java/.classpath
@@ -2,6 +2,7 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="uscxml"/>
+ <classpathentry kind="src" path="bindings"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
diff --git a/contrib/java/.project b/contrib/java/.project
index 4c68397..fda3442 100644
--- a/contrib/java/.project
+++ b/contrib/java/.project
@@ -16,6 +16,11 @@
</natures>
<linkedResources>
<link>
+ <name>bindings</name>
+ <type>2</type>
+ <location>/Users/sradomski/Documents/TK/Code/uscxml/src/bindings/swig/java</location>
+ </link>
+ <link>
<name>uscxml</name>
<type>2</type>
<location>/Users/sradomski/Documents/TK/Code/uscxml/build/cli/src/bindings/swig/java</location>
diff --git a/contrib/java/src/org/uscxml/tests/TestData.java b/contrib/java/src/org/uscxml/tests/TestData.java
new file mode 100644
index 0000000..cb2fc0b
--- /dev/null
+++ b/contrib/java/src/org/uscxml/tests/TestData.java
@@ -0,0 +1,23 @@
+package org.uscxml.tests;
+
+import org.uscxml.Data;
+import org.uscxml.DataNative;
+
+public class TestData {
+
+ public static void main(String[] args) {
+ System.load("/Users/sradomski/Documents/TK/Code/uscxml/build/cli/lib/libuscxmlNativeJava64_d.jnilib");
+ {
+ Data data = Data.fromJSON("[1,2,3,4,5]");
+ DataNative nData2 = Data.toNative(data);
+ Data data2 = new Data(nData2);
+ System.out.println(data2);
+ }
+ {
+ Data data = Data.fromJSON("{ \"foo\": \"bar\", \"faz\": 12 }");
+ DataNative nData2 = Data.toNative(data);
+ Data data2 = new Data(nData2);
+ System.out.println(data2);
+ }
+ }
+}
diff --git a/contrib/java/src/org/uscxml/tests/TestDataModel.java b/contrib/java/src/org/uscxml/tests/TestDataModel.java
index 0721501..06a56d7 100644
--- a/contrib/java/src/org/uscxml/tests/TestDataModel.java
+++ b/contrib/java/src/org/uscxml/tests/TestDataModel.java
@@ -1,14 +1,11 @@
package org.uscxml.tests;
import org.uscxml.Data;
+import org.uscxml.DataNative;
import org.uscxml.Event;
import org.uscxml.Factory;
import org.uscxml.Interpreter;
import org.uscxml.JavaDataModel;
-import org.uscxml.SWIGTYPE_p_Arabica__DOM__DocumentT_std__string_t;
-import org.uscxml.SWIGTYPE_p_Arabica__DOM__ElementT_std__string_t;
-import org.uscxml.SWIGTYPE_p_boost__shared_ptrT_uscxml__DataModelImpl_t;
-import org.uscxml.SWIGTYPE_p_uscxml__InterpreterImpl;
import org.uscxml.StringSet;
@@ -55,14 +52,14 @@ public class TestDataModel extends JavaDataModel {
}
@Override
- public Data getStringAsData(String content) {
+ public DataNative getStringAsData(String content) {
/**
* Evaluate the string as a value expression and
* transform it into a JSON-like Data structure
*/
System.out.println("getStringAsData " + content);
Data data = new Data();
- return data;
+ return Data.toNative(data);
}
@Override
@@ -79,7 +76,7 @@ public class TestDataModel extends JavaDataModel {
public void setForeach(String item, String array, String index, long iteration) {
/**
* Prepare an iteration of the foreach element, by setting the variable in index
- * to the curent iteration and setting the variable in item to the current item
+ * to the current iteration and setting the variable in item to the current item
* from array.
*/
System.out.println("setForeach " + item + " " + index + " " + iteration);
diff --git a/contrib/java/src/org/uscxml/tests/TestInvoker.java b/contrib/java/src/org/uscxml/tests/TestInvoker.java
index b41bd43..8f68c6b 100644
--- a/contrib/java/src/org/uscxml/tests/TestInvoker.java
+++ b/contrib/java/src/org/uscxml/tests/TestInvoker.java
@@ -1,6 +1,7 @@
package org.uscxml.tests;
import org.uscxml.Data;
+import org.uscxml.DataNative;
import org.uscxml.Event;
import org.uscxml.Factory;
import org.uscxml.Interpreter;
@@ -20,10 +21,10 @@ public class TestInvoker extends JavaInvoker {
}
@Override
- public Data getDataModelVariables() {
+ public DataNative getDataModelVariables() {
Data data = new Data();
- data.getArray().add(new Data("foo", Data.Type.VERBATIM));
- return data;
+ data.array.add(new Data("foo", Data.Type.VERBATIM));
+ return Data.toNative(data);
}
@Override
@@ -35,7 +36,7 @@ public class TestInvoker extends JavaInvoker {
public void invoke(InvokeRequest req) {
System.out.println("invoke");
- System.out.println(Data.toJSON(req.getData()));
+ System.out.println(req.getData());
System.out.println(req.getXML());
Event ev = new Event();
diff --git a/src/bindings/swig/java/org/uscxml/Data.java b/src/bindings/swig/java/org/uscxml/Data.java
new file mode 100644
index 0000000..ce295af
--- /dev/null
+++ b/src/bindings/swig/java/org/uscxml/Data.java
@@ -0,0 +1,88 @@
+package org.uscxml;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+public class Data {
+ public Map<String, Data> compound = new HashMap<String, Data>();
+ public List<Data> array = new LinkedList<Data>();
+ public String atom;
+ public Type type = Type.INTERPRETED;
+
+ public enum Type {
+ VERBATIM, INTERPRETED
+ }
+
+ public static Data fromJSON(String jsonString) {
+ return new Data(DataNative.fromJSON(jsonString));
+ }
+
+ public Data() {
+ }
+
+ public Data(String atom, Data.Type type) {
+ this.atom = atom;
+ this.type = type;
+ }
+
+ public Data(DataNative nativeData) {
+ if (!nativeData.getCompound().empty()) {
+ // data is a key value compound
+ for(String key : nativeData.getCompound()) {
+ this.compound.put(key, new Data(nativeData.getCompound().get(key)));
+ }
+ } else if (!nativeData.getArray().isEmpty()) {
+ // data is an array
+ for (int i = 0; i < nativeData.getArray().size(); i++) {
+ this.array.add(new Data(nativeData.getArray().get(i)));
+ }
+ } else {
+ // data is a single atom
+ this.atom = nativeData.getAtom();
+ if (nativeData.getType() == DataNative.Type.INTERPRETED) {
+ this.type = Type.INTERPRETED;
+ } else {
+ this.type = Type.VERBATIM;
+ }
+ }
+ }
+
+ @Override
+ public String toString() {
+ return toJSON();
+ }
+
+ public String toJSON() {
+ DataNative nativeData = toNative(this);
+ return DataNative.toJSON(nativeData);
+ }
+
+ public static DataNative toNative(Data data) {
+ DataNative nativeData = new DataNative();
+ //nativeData.swigCMemOwn = false;
+ if (data.compound != null && !data.compound.isEmpty()) {
+ DataMap nativeDataMap = new DataMap();
+ for (String key : data.compound.keySet()) {
+ nativeDataMap.set(key, toNative(data.compound.get(key)));
+ }
+ nativeData.setCompound(nativeDataMap);
+ } else if (data.array != null && !data.array.isEmpty()) {
+ DataList nativeDataList = new DataList();
+ for (Data item : data.array) {
+ nativeDataList.add(toNative(item));
+ }
+ nativeData.setArray(nativeDataList);
+ } else {
+ nativeData.setAtom(data.atom);
+ if (data.type == Type.INTERPRETED) {
+ nativeData.setType(DataNative.Type.INTERPRETED);
+ } else {
+ nativeData.setType(DataNative.Type.VERBATIM);
+ }
+ }
+ return nativeData;
+ }
+
+}
diff --git a/src/bindings/swig/java/uscxml.i b/src/bindings/swig/java/uscxml.i
index 7f496cd..d469498 100644
--- a/src/bindings/swig/java/uscxml.i
+++ b/src/bindings/swig/java/uscxml.i
@@ -9,10 +9,12 @@
%include <stl.i>
%include <std_map.i>
+%include "std_string.i"
%include <inttypes.i>
%include "stl_set.i"
%include "stl_list.i"
+
%include <boost_shared_ptr.i>
typedef uscxml::Data Data;
@@ -50,10 +52,18 @@ typedef uscxml::SendRequest SendRequest;
#include "../../../uscxml/Message.h"
#include "../../../uscxml/Factory.h"
#include "../../../uscxml/Interpreter.h"
+
+//#include <DOM/Document.hpp>
+//#include <DOM/Node.hpp>
+//#include <DOM/Element.hpp>
+//#include <DOM/Attr.hpp>
+//#include <DOM/Text.hpp>
+
#include "JavaInvoker.h"
#include "JavaDataModel.h"
using namespace uscxml;
+using namespace Arabica::DOM;
#include "JavaInvoker.cpp"
#include "JavaDataModel.cpp"
@@ -64,25 +74,88 @@ using namespace uscxml;
%ignore uscxml::SCXMLParser;
%ignore uscxml::InterpreterImpl;
+%ignore create();
+
%ignore uscxml::Interpreter::getDelayQueue();
%ignore uscxml::JavaInvoker::create(InterpreterImpl*);
-%ignore uscxml::JavaDataModel::create(InterpreterImpl*);
+%ignore uscxml::JavaDataModel::create(InterpreterImpl*);
%ignore uscxml::JavaDataModel::init(const Arabica::DOM::Element<std::string>&, const Arabica::DOM::Document<std::string>&, const std::string&);
%ignore uscxml::JavaDataModel::init(const std::string&, const Data&);
%ignore uscxml::JavaDataModel::assign(const Arabica::DOM::Element<std::string>&, const Arabica::DOM::Document<std::string>&, const std::string&);
%ignore uscxml::JavaDataModel::assign(const std::string&, const Data&);
%ignore uscxml::JavaDataModel::eval(const Arabica::DOM::Element<std::string>&, const std::string&);
-%template(DataMap) std::map<std::string, uscxml::Data>;
+%ignore uscxml::Event::Event(const Arabica::DOM::Node<std::string>&);
+%ignore uscxml::Event::getStrippedDOM;
+%ignore uscxml::Event::getFirstDOMElement;
+%ignore uscxml::Event::getDOM();
+%ignore uscxml::Event::setDOM(const Arabica::DOM::Document<std::string>&);
+%ignore uscxml::Event::toDocument();
+
%template(DataList) std::list<uscxml::Data>;
%template(StringSet) std::set<std::string>;
+%rename Data DataNative;
+# %typemap(jstype) uscxml::Data "Data"
+# %typemap(javaout) uscxml::Data {
+# return new Data(new DataNative($jnicall, $owner));
+# }
+
+# %typemap(javadirectorin) uscxml::Data "new Data($jniinput)"
+# %typemap(javadirectorout) uscxml::Data "new Data($jniinput)"
%feature("director") uscxml::JavaInvoker;
%feature("director") uscxml::JavaDataModel;
+// Provide an iterable interface for maps
+// http://stackoverflow.com/questions/9465856/no-iterator-for-java-when-using-swig-with-cs-stdmap
+
+%typemap(javainterfaces) MapKeyIterator "java.util.Iterator<String>"
+%typemap(javacode) MapKeyIterator %{
+ public void remove() throws UnsupportedOperationException {
+ throw new UnsupportedOperationException();
+ }
+
+ public String next() throws java.util.NoSuchElementException {
+ if (!hasNext()) {
+ throw new java.util.NoSuchElementException();
+ }
+ return nextImpl();
+ }
+%}
+
+%javamethodmodifiers MapKeyIterator::nextImpl "private";
+%inline %{
+ struct MapKeyIterator {
+ typedef std::map<std::string, Data> map_t;
+ MapKeyIterator(const map_t& m) : it(m.begin()), map(m) {}
+ bool hasNext() const {
+ return it != map.end();
+ }
+
+ const std::string nextImpl() {
+ const std::pair<std::string, Data>& ret = *it++;
+ return ret.first;
+ }
+ private:
+ map_t::const_iterator it;
+ const map_t& map;
+ };
+%}
+%typemap(javainterfaces) std::map<std::string, Data> "Iterable<String>"
+
+%newobject std::map<std::string, uscxml::Data>::iterator() const;
+%extend std::map<std::string, uscxml::Data> {
+ MapKeyIterator *iterator() const {
+ return new MapKeyIterator(*$self);
+ }
+}
+
+%template(DataMap) std::map<std::string, uscxml::Data>;
+
+
//***********************************************
// Parse the header file to generate wrappers
//***********************************************
@@ -90,6 +163,18 @@ using namespace uscxml;
%include "../../../uscxml/Factory.h"
%include "../../../uscxml/Message.h"
%include "../../../uscxml/Interpreter.h"
+
+# %include <DOM/Document.hpp>
+# %include <DOM/Node.hpp>
+# %include <DOM/Element.hpp>
+# %include <DOM/Attr.hpp>
+# %include <DOM/Text.hpp>
+
%include "JavaInvoker.h"
%include "JavaDataModel.h"
+# %template(XMLDocument) Arabica::DOM::Document<std::string>;
+# %template(XMLNode) Arabica::DOM::Node<std::string>;
+# %template(XMLElement) Arabica::DOM::Element<std::string>;
+# %template(XMLAttr) Arabica::DOM::Attr<std::string>;
+# %template(XMLText) Arabica::DOM::Text<std::string>;
diff --git a/src/uscxml/Factory.h b/src/uscxml/Factory.h
index 74cb2a4..0432f34 100644
--- a/src/uscxml/Factory.h
+++ b/src/uscxml/Factory.h
@@ -299,7 +299,7 @@ public:
class DataModel {
public:
DataModel() : _impl() {}
- DataModel(boost::shared_ptr<DataModelImpl> const impl) : _impl(impl) { }
+ DataModel(const boost::shared_ptr<DataModelImpl> impl) : _impl(impl) { }
DataModel(const DataModel& other) : _impl(other._impl) { }
virtual ~DataModel() {};