[Mobies-commit] [commit] r4281 - in UDM/trunk: bin include src src/UdmUtil
endre at redhat3.isis.vanderbilt.edu
endre at redhat3.isis.vanderbilt.edu
Sat Mar 29 16:19:41 CDT 2014
Author: endre
Date: Sat Mar 29 16:19:41 2014
New Revision: 4281
Log:
serialization to a JSON string of any UDM subtree. bin/UdmJson utility that creates a json output file of any data network,
Modified:
UDM/trunk/bin/Makefile.OSX
UDM/trunk/include/UdmUtil.h
UDM/trunk/include/UdmXmi.h
UDM/trunk/src/Makefile.OSX
UDM/trunk/src/UdmUtil/UdmUtil.cpp
Modified: UDM/trunk/bin/Makefile.OSX
==============================================================================
--- UDM/trunk/bin/Makefile.OSX Mon Mar 24 01:24:44 2014 (r4280)
+++ UDM/trunk/bin/Makefile.OSX Sat Mar 29 16:19:41 2014 (r4281)
@@ -1,6 +1,6 @@
include ../Makefile.OSX.incl
-all: Udm UdmCopy UdmViz UdmBackendDump UdmOclPat UdmToXmi XmiToUdm
+all: Udm UdmCopy UdmViz UdmBackendDump UdmOclPat UdmToXmi XmiToUdm UdmJson
Udm:
$(CC) $(BCFLAGS) ../src/Udm/Udm.o -o Udm -lUdm -L../lib
@@ -18,6 +18,10 @@
XmiToUdm:
$(CC) $(BCFLAGS) ../src/XmiToUdm/XmiToUdm.o ../src/UdmXmi/XSLT.o ../src/UdmXmi/Transform.o -o XmiToUdm -lUdm -L../lib -lxalan-c -L $(XALANCROOT)/lib -lxerces-c -L $(XERCESCROOT)/lib
+UdmJson:
+ $(CC) $(BCFLAGS) ../src/UdmJson/UdmJson.o -o UdmJson -lUdm -L../lib -lxalan-c -L $(XALANCROOT)/lib -lxerces-c -L $(XERCESCROOT)/lib
+
+
clean:
- rm -f Udm UdmCopy UdmViz UdmBackendDump UdmOclPat UdmToXmi XmiToUdm
+ rm -f Udm UdmCopy UdmViz UdmBackendDump UdmOclPat UdmToXmi XmiToUdm UdmJson
Modified: UDM/trunk/include/UdmUtil.h
==============================================================================
--- UDM/trunk/include/UdmUtil.h Mon Mar 24 01:24:44 2014 (r4280)
+++ UDM/trunk/include/UdmUtil.h Sat Mar 29 16:19:41 2014 (r4281)
@@ -41,6 +41,7 @@
bool is_lib_part;
};
extern UDM_DLL const CopyOpts DefCopyOpts;
+
//utility which copies a subtree from a data network to another
//consistent (same) meta is assumed.
@@ -66,6 +67,12 @@
std::transform(c.begin(), c.end(), back_inserter(ret), std::ptr_fun(SimpleExtractName));
return ret;
}
+
+ //get any UDM attribute as string
+ // if convert_any_type is true, any kind of attribute, even arrays, will be converted to string
+
+ UDM_DLL string GetAttributeAsString( const ::Udm::ObjectImpl *impl, ::Uml::Attribute& attr, bool convert_any_type = false);
+
//note: escape_char is not escaped unless it is contained in to_escape_chars
UDM_DLL string escape_chars(const string &src, const char escape_char, const string &to_escape_chars);
@@ -81,7 +88,12 @@
UDM_DLL string doubleToString(double val, int minimum_precision = 2);
// returns false if parsing failed
UDM_DLL bool stringToDouble(const char* val, double& out);
- UDM_DLL string stacktrace();
+ UDM_DLL string stacktrace();
+
+ //json stuff
+ UDM_DLL void write_json(const ::Udm::Object& obj, const string & FileName, bool child_attr_subtree = false);
+
+
};
#endif //MOBIES_UDMUTIL_H
Modified: UDM/trunk/include/UdmXmi.h
==============================================================================
--- UDM/trunk/include/UdmXmi.h Mon Mar 24 01:24:44 2014 (r4280)
+++ UDM/trunk/include/UdmXmi.h Sat Mar 29 16:19:41 2014 (r4281)
@@ -1,6 +1,6 @@
#ifndef MOBIES_UDM_XMI_H
-#define MOBIEX_UDM_XMI_H
+#define MOBIES_UDM_XMI_H
#include "ErrHand.h"
Modified: UDM/trunk/src/Makefile.OSX
==============================================================================
--- UDM/trunk/src/Makefile.OSX Mon Mar 24 01:24:44 2014 (r4280)
+++ UDM/trunk/src/Makefile.OSX Sat Mar 29 16:19:41 2014 (r4281)
@@ -1,4 +1,4 @@
-SUBDIRS = MPCGen StrResGen Udm UdmBackendDump UdmBase UdmCopy UdmDom UdmOcl UdmOclPat UdmPOI UdmPython UdmSwig UdmToXmi UdmUtil UdmViz UdmXmi Uml XmiToUdm
+SUBDIRS = MPCGen StrResGen Udm UdmBackendDump UdmBase UdmCopy UdmDom UdmOcl UdmOclPat UdmPOI UdmPython UdmSwig UdmToXmi UdmUtil UdmViz UdmXmi Uml XmiToUdm UdmJson
all:
@for dir in $(SUBDIRS);\
Modified: UDM/trunk/src/UdmUtil/UdmUtil.cpp
==============================================================================
--- UDM/trunk/src/UdmUtil/UdmUtil.cpp Mon Mar 24 01:24:44 2014 (r4280)
+++ UDM/trunk/src/UdmUtil/UdmUtil.cpp Sat Mar 29 16:19:41 2014 (r4281)
@@ -24,6 +24,12 @@
#include <limits>
#include <string.h>
+//boost is needed for handling json serialization
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/json_parser.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/fusion/include/for_each.hpp>
+
using namespace Uml;
using namespace Udm;
@@ -89,36 +95,129 @@
false, // is_dn_copy
false // is_lib_part
};
+
+ template <typename Variant>
+ struct to_string
+ {
+ private:
+ Variant data;
+
+ public:
+ to_string(Variant &d) : data(d) {}
+ operator std::string() const
+ {
+ try
+ {
+ return boost::lexical_cast<std::string>(data);
+ }
+ catch (const boost::bad_lexical_cast &)
+ {
+ return std::string();
+ }
+ }
+
+ };
+
+ template <class Sequence>
+ vector<std::string> stringize(vector<Sequence>& seq)
+ {
+ vector<std::string> result;
+ typename vector< Sequence>::iterator i = seq.begin();
+ for (; i!= seq.end(); i++)
+ {
+ Sequence item = *i;
+ to_string<Sequence> data = to_string<Sequence>(item);
+ result.push_back((string)data);
+ }
+
+ return result;
+ }
int reqCopyObjectHierarchy(ObjectImpl* p_srcRoot, ObjectImpl* p_dstRoot, DataNetwork* p_dstBackend, bool & finished, copy_assoc_map& cam );
-
+
+ UDM_DLL string GetAttributeAsString( const ::Udm::ObjectImpl *impl, ::Uml::Attribute& attr, bool convert_any_type)
+ {
+ string ret;
+ if (!attr) throw udm_exception("GetAttributeAsString: Null Attribute object was provided");
+
+ if ( (impl == NULL) || (impl == &_null) ) throw udm_exception("GetAttributeAsString: Object handler is null");
+
+ string result;
+
+ if (convert_any_type && ( (attr.max()>1) || (attr.max() == -1) ) )
+
+ {
+ vector<string> attr_val_as_str;
+
+ if (attr.type() == "Integer")
+ {
+ vector<__int64> attr_val = impl->getIntegerAttrArr(attr);
+ attr_val_as_str = stringize(attr_val);
+ }
+ else if (attr.type() == "Boolean")
+ {
+ vector<bool> attr_val = impl->getBooleanAttrArr(attr);
+ attr_val_as_str = stringize(attr_val);
+
+ } else if (attr.type() == "Real")
+ {
+ vector<double> attr_val = impl->getRealAttrArr(attr);
+ attr_val_as_str = stringize(attr_val);
+ } else if (attr.type() == "String")
+ {
+ attr_val_as_str = impl->getStringAttrArr(attr);
+ }
+
+ result = vector_to_string(attr_val_as_str,';');
+
+ } else if (convert_any_type && !(attr.type() == "String") )
+ {
+
+ if (attr.type() == "Integer")
+ {
+ __int64 attr_val = impl->getIntegerAttr(attr);
+ result = to_string<__int64>(attr_val);
+
+ }
+ else if (attr.type() == "Boolean")
+ {
+ bool attr_val = impl->getBooleanAttr(attr);
+ result = to_string<bool>(attr_val);
+
+ } else if (attr.type() == "Real")
+ {
+ double attr_val = impl->getRealAttr(attr);
+ result = to_string<double>(attr_val);
+ }
+
+ } else if (attr.type() == "String") result = impl->getStringAttr(attr);
+
+ return result;
+ }
+
+
UDM_DLL string ExtractName(const Udm::ObjectImpl *impl, const string &att_name )
{
if (impl->type().__impl() == NULL)
return string("<not discoverable>");
-
+
::Uml::Class cls = impl->type();
- set< ::Uml::Attribute> attrs=cls.attributes();
- // Adding parent attributes
- set< ::Uml::Attribute> aattrs=::Uml::AncestorAttributes(cls);
- attrs.insert(aattrs.begin(),aattrs.end());
-
+ set< ::Uml::Attribute> attrs=::Uml::AncestorAttributes(cls);
+
for(set< ::Uml::Attribute>::iterator ai = attrs.begin();ai != attrs.end(); ai++)
{
- if(string(ai->type())=="String")
- {
- string str=ai->name();
- if(str==att_name)
- {
-
- string value=impl->getStringAttr(*ai);
- if(value.empty())value="<empty string>";
- return value;
- }
- }
+ string str=ai->name();
+ if(str==att_name)
+ {
+ Uml::Attribute att = *ai;
+ string value = GetAttributeAsString(impl, att, false);
+
+ if(value.empty())value="<empty string>";
+ return value;
+ }
}
return string("<no name specified>");
@@ -1139,4 +1238,68 @@
return trace;
};
+
+
+ UDM_DLL boost::property_tree::ptree DiagramToPtree( const ::Udm::Object& obj, bool child_attr_subtree = false);
+ UDM_DLL boost::property_tree::ptree DiagramToPtree( const ::Udm::Object& obj, bool child_attr_subtree)
+ {
+ boost::property_tree::ptree pt, pt_attributes, pt_children;
+ const Udm::ObjectImpl * obj_impl = obj.__impl();
+
+
+ ::Udm::ObjectImpl::uniqueId_type id = obj_impl->uniqueId();
+ to_string< ::Udm::ObjectImpl::uniqueId_type> key = to_string< ::Udm::ObjectImpl::uniqueId_type>(id);
+
+ pt.push_back(boost::property_tree::ptree::value_type("_id", boost::property_tree::ptree(key)));
+
+
+ set < ::Uml::Attribute> attributes = AncestorAttributes(obj.type());
+
+ if (attributes.size()>0)
+ {
+ set < ::Uml::Attribute>::iterator attr_i = attributes.begin();
+
+ for(; attr_i != attributes.end(); attr_i ++)
+ {
+ ::Uml::Attribute attr = *attr_i;
+
+ if(child_attr_subtree)
+ pt_attributes.push_back(boost::property_tree::ptree::value_type(attr.name(), boost::property_tree::ptree(GetAttributeAsString(obj_impl, attr, true))));
+ else
+ pt.push_back(boost::property_tree::ptree::value_type(attr.name(), boost::property_tree::ptree(GetAttributeAsString(obj_impl, attr, true))));
+
+ }
+
+ if (child_attr_subtree) pt.push_back(boost::property_tree::ptree::value_type("_attributes", pt_attributes));
+ }
+
+ set<Object> children = obj.GetChildObjects();
+ if (children.size() > 0)
+ {
+ for (set<Object>::iterator child_i = children.begin(); child_i != children.end(); child_i++)
+ {
+ ::Udm::ObjectImpl::uniqueId_type child_id = child_i->__impl()->uniqueId();
+ to_string< ::Udm::ObjectImpl::uniqueId_type> child_key = to_string< ::Udm::ObjectImpl::uniqueId_type>(child_id);
+
+ if (child_attr_subtree)
+ pt_children.push_back(boost::property_tree::ptree::value_type(child_key, DiagramToPtree(*child_i)));
+ else
+ pt.push_back(boost::property_tree::ptree::value_type(child_key, DiagramToPtree(*child_i)));
+ }
+ if (child_attr_subtree)
+ pt.push_back(boost::property_tree::ptree::value_type("_children", pt_children));
+
+ }
+
+
+ return pt;
+
+ };
+
+ UDM_DLL void write_json(const ::Udm::Object& obj, const string & FileName, bool child_attr_subtree)
+ {
+ boost::property_tree::json_parser::write_json(FileName, DiagramToPtree(obj,child_attr_subtree));
+ }
+
};
+
More information about the Mobies-commit
mailing list