[Mobies-commit] [commit] r4250 - in UDM/trunk: lib src/Udm/PythonAPIGen src/UdmPython tests tests/test_UdmPythonDS
endre at redhat3.isis.vanderbilt.edu
endre at redhat3.isis.vanderbilt.edu
Tue Jan 28 23:40:20 CST 2014
Author: endre
Date: Tue Jan 28 23:40:20 2014
New Revision: 4250
Log:
python domain-specific wrapper (continued)
Modified:
UDM/trunk/lib/UdmPython.py
UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp
UDM/trunk/src/UdmPython/UdmPython.cpp
UDM/trunk/tests/Makefile.OSX
UDM/trunk/tests/test_UdmPythonDS/test.py
Modified: UDM/trunk/lib/UdmPython.py
==============================================================================
--- UDM/trunk/lib/UdmPython.py Wed Jan 22 16:43:39 2014 (r4249)
+++ UDM/trunk/lib/UdmPython.py Tue Jan 28 23:40:20 2014 (r4250)
@@ -1,24 +1,172 @@
+def indent(indent_tabs=0):
+ indent = ""
+ for i in range(0,indent_tabs):
+ indent += "\t";
+ return indent
class UdmPython:
def __init__(self, impl):
self.__dict__['impl'] = impl
self.__dict__['id'] = impl.id
- #self.name = impl.name
+ self.__dict__['indent'] = 0
+
def is_lib_root(self):
return self.impl.is_lib_root
def is_lib_object(self):
return self.impl.is_lib_object
-
+
+ def _type(self):
+ import Uml
+ return Uml.Class.cast(self.impl.type)
+
+ def children(self,child_role=None, parent_role=None, child_type=None):
+ ret = []
+ for child in self.impl.children(child_role, parent_role, child_type):
+ ret.append(UdmPython(child))
+ return ret
+
+ def __impl(self):
+ return self.__dict__['impl']
def __eq__(self, other):
- return self.impl == other.impl
-# def __str__(self):
-# return self.impl.type.name+"(id="+str(self.id)+", name="+self.name+")"
+ return self.__dict__['impl'] == other.__dict__['impl']
+ def __ne__(self, other):
+ return self.__dict__['impl'] != other.__dict__['impl']
def __repr__(self):
return self.impl.__repr__()
+ def setIndent(self,i):
+ self.__dict__['indent'] = i
+
+ def __str__(self):
+ import Uml
+ line = str()
+ cl = Uml.Class(self.impl.type)
+ indent_tabs = self.__dict__['indent']
+ line = indent(indent_tabs) + "--------------------------------------------------------------------" + "\n"
+ line += indent(indent_tabs) + "Object's type:" + cl.name + "\n"
+ line += indent(indent_tabs) + self.impl.__repr__() + "\n"
+ attrs= cl.getAttributeChildren()
+ for attr in attrs:
+ attr_value = self.impl.get_attribute(attr.impl)
+ if attr.max == 1 or attr.max == 0:
+ if attr.type == "Integer":
+ line += indent(indent_tabs) + "attribute name: " + attr.name + ", value: %d " % (attr_value) +"\n"
+ elif attr.type == "Real":
+ line += indent(indent_tabs) + "attribute name: " + attr.name + ", value: %f " % (attr_value) + "\n"
+ elif attr.type == "Boolean":
+ boolstr = "true" if attr_value else "false"
+ line += indent(indent_tabs) + "attribute name: " + attr.name + ", value:" + boolstr + "\n"
+ elif attr.type == "String":
+ line += indent(indent_tabs) + "attribute name: " + attr.name + ", value:" + attr_value + "\n"
+ else:
+ a_attr = ArrayAttr(self.impl, attr)
+ a_attr.setIndent(indent_tabs)
+ line += a_attr.__str__() + "\n"
+ return line
+
def __getattr__(self, name):
return self.impl.__getattr__(name)
def __setattr__(self, name, value):
self.impl.__setattr__(name, value)
+ def __nonzero__(self):
+ return self.impl.__nonzero__();
+
+class ArrayAttr(object):
+
+
+ @staticmethod
+ def escaped_split(s, delim):
+ ret = []
+ current = []
+ itr = iter(s)
+ for ch in itr:
+ if ch == '\\':
+ try:
+ # skip the next character; it has been escaped!
+ current.append('\\')
+ current.append(itr.next())
+ except StopIteration:
+ pass
+ elif ch == delim:
+ # split! (add current to the list and reset it)
+ ret.append(''.join(current))
+ current = []
+ else:
+ current.append(ch)
+ ret.append(''.join(current))
+ return ret
+
+ def __init__(self, obj, attr):
+ import Uml
+ if attr.max==0 or attr.max == 1:
+ raise ("Attribute is not an array")
+
+ self.attr = attr
+ self.obj = obj
+ self.indent = 0
+
+ def setIndent(self,i):
+ self.indent = i
+
+ def __str__(self):
+ import Uml
+ lines = indent(self.indent) + "Array attribute name: " + self.attr.name + ", type:" + self.attr.type
+ lines += "\n"
+ attr_value=self.obj.get_attribute(self.attr.impl)
+ str_array = ArrayAttr.escaped_split(str(attr_value), ';')
+ i=0
+ for str_i in str_array:
+ lines += indent(self.indent) + self.attr.name + "[" + str(i) + "] =" + str_i
+ lines +="\n"
+ i = i + 1
+ return lines
+
+ def __getitem__(self,index):
+ attr_value=obj.get_attribute(attr.impl)
+ str_array = ArrayAttr.escaped_split(str(attr_value), ';')
+ return self.str_array[index]
+
+class StrArrayAttr(ArrayAttr):
+
+ def __init__(self, obj, attr):
+ import Uml
+ if attr.type != "String":
+ raise Exception("Attribute is not of type String")
+ super(StrArrayAttr, self).__init__(obj, attr)
+
+
+
+class IntArrayAttr(ArrayAttr):
+
+ def __init__(self, obj, attr):
+ import Uml
+ if attr.type != "Integer":
+ raise ("Attribute is not of type Integer")
+ super(StrArrayAttr, self).__init__(obj, attr)
+
+ def __getitem__(self,index):
+ return int(super(IntArrayAttr, self).__getitem__[index])
+
+class RealArrayAttr(ArrayAttr):
+
+ def __init__(self, obj, attr):
+ import Uml
+ if attr.type != "Real":
+ raise ("Attribute is not of type Real")
+ super(StrArrayAttr, self).__init__(obj, attr)
+
+ def __getitem__(self,index):
+ return int(super(RealArrayAttr, self).__getitem__[index])
+
+class BooleanArrayAttr(ArrayAttr):
+
+ def __init__(self, obj, attr):
+ import Uml
+ if attr.type != "Boolean":
+ raise ("Attribute is not of type Boolean")
+ super(StrArrayAttr, self).__init__(obj, attr)
-
-
+ def __getitem__(self,index):
+ value = super(RealArrayAttr, self).__getitem__[index]
+ return value.lower()=="true"
+
+
Modified: UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp
==============================================================================
--- UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp Wed Jan 22 16:43:39 2014 (r4249)
+++ UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp Tue Jan 28 23:40:20 2014 (r4250)
@@ -121,10 +121,11 @@
m_output << "\t" << endl;
//type method
- m_output << "\tdef type(self):"<< endl;
- m_output << "\t\t\"\"\"returning the type of object (Uml.Class)\"\"\"" << endl;
- m_output << "\t\treturn " << "Uml.Class.cast(self.meta)" << endl;
- m_output << "\t" << endl;
+ // we have this at UdmPython level
+ //m_output << "\tdef type(self):"<< endl;
+ //m_output << "\t\t\"\"\"returning the type of object (Uml.Class)\"\"\"" << endl;
+ //m_output << "\t\treturn " << "Uml.Class.cast(self.meta)" << endl;
+ //m_output << "\t" << endl;
generateChildrenAccess(cls);
generateAssociations(cls);
Modified: UDM/trunk/src/UdmPython/UdmPython.cpp
==============================================================================
--- UDM/trunk/src/UdmPython/UdmPython.cpp Wed Jan 22 16:43:39 2014 (r4249)
+++ UDM/trunk/src/UdmPython/UdmPython.cpp Tue Jan 28 23:40:20 2014 (r4250)
@@ -180,13 +180,11 @@
return Uml::Attribute();
}
-object Object_attr(Udm::Object& self, str _name) {
- std::string name = extract<std::string>(_name);
+object Object_attr_by_uml_attr(Udm::Object& self, Uml::Attribute attr)
+{
+ if (!attr)
+ throw std::runtime_error(std::string("Uml::Attribute is null in Object_attr_by_uml_attr(Udm::Object& self, Uml::Attribute attr) "));
- Uml::Attribute attr = getAttribute(self, name);
- if (!attr) {
- throw std::runtime_error(std::string("Unknown attribute '") + name + "' for class '" + static_cast<string>(self.type().name()) + "'");
- }
if (static_cast<string>(attr.type()) == "Integer") {
return object(self.getIntegerAttr(attr));
}
@@ -202,6 +200,26 @@
throw std::runtime_error(std::string("Unsupported attribute type '") + static_cast<string>(attr.type()) + "' for class '" + static_cast<string>(self.type().name()) + "'");
}
+
+object Object_attr(Udm::Object& self, str _name) {
+ std::string name = extract<std::string>(_name);
+
+ Uml::Attribute attr = getAttribute(self, name);
+ if (!attr) {
+ throw std::runtime_error(std::string("Unknown attribute '") + name + "' for class '" + static_cast<string>(self.type().name()) + "'");
+ }
+ return Object_attr_by_uml_attr(self, attr);
+}
+
+object Object_attr_by_uml_attr_as_udm_object(Udm::Object& self, Udm::Object& attr)
+{
+ if (!attr)
+ throw std::runtime_error(std::string("Uml::Attribute is null in Object_attr_by_uml_attr_as_udm_object(Udm::Object& self, Udm::Object& attr) "));
+
+ return Object_attr_by_uml_attr(self, Uml::Attribute::Cast(attr));
+
+}
+
object Object_set_attr(Udm::Object& self, str _name, object value) {
std::string name = extract<std::string>(_name);
@@ -621,6 +639,7 @@
.def("delete", &Udm::Object::DeleteObject)
.def("_children", Object_children)
.def("_adjacent", Object_adjacent)
+ .def("get_attribute",&Object_attr_by_uml_attr_as_udm_object)
.def("attr", &Object_attr)
.def("set_attr", &Object_set_attr)
.def("__getattr__", &Object___getattr__)
Modified: UDM/trunk/tests/Makefile.OSX
==============================================================================
--- UDM/trunk/tests/Makefile.OSX Wed Jan 22 16:43:39 2014 (r4249)
+++ UDM/trunk/tests/Makefile.OSX Tue Jan 28 23:40:20 2014 (r4250)
@@ -1,4 +1,4 @@
-SUBDIRS =test_generic test_generic2 test_libs test_simpleudmops testGetAdjacentObjects testOCL test_childrenorder testUdmCint test_cintstring test_crosslinks test_ns test_ns2 test_refports test_domstringdn test_UdmPython
+SUBDIRS =test_generic test_generic2 test_libs test_simpleudmops testGetAdjacentObjects testOCL test_childrenorder testUdmCint test_cintstring test_crosslinks test_ns test_ns2 test_refports test_domstringdn test_UdmPython test_UdmPythonDS
all:
Modified: UDM/trunk/tests/test_UdmPythonDS/test.py
==============================================================================
--- UDM/trunk/tests/test_UdmPythonDS/test.py Wed Jan 22 16:43:39 2014 (r4249)
+++ UDM/trunk/tests/test_UdmPythonDS/test.py Tue Jan 28 23:40:20 2014 (r4250)
@@ -7,6 +7,42 @@
+
+def walk_hierarchy_udm_o(obj, indent_tabs = 0):
+ #obj is expected to be Udm.Object
+ #walks recursively the hierarchy
+
+ if obj:
+
+ up_o = UdmPython.UdmPython(obj)
+ up_o.setIndent(indent_tabs)
+ print up_o
+ generic_children = obj.children()
+ if generic_children:
+ print UdmPython.indent(indent_tabs) + "I have found the following children at this level: %d" % (len(generic_children))
+ for child in generic_children:
+ walk_hierarchy_udm_o(child, indent_tabs+1)
+ else:
+ return
+
+def walk_hierarchy_udmp(obj, indent_tabs = 0):
+ #obj is expected to be UdmPython.UdmPython or any DS class
+ #walks recursively the hierarchy
+
+ if obj:
+
+ obj.setIndent(indent_tabs)
+ print obj
+ generic_children = obj.children()
+ if generic_children:
+ print UdmPython.indent(indent_tabs) + "I have found the following children at this level: %d" % (len(generic_children))
+ for child in generic_children:
+ walk_hierarchy_udmp(child, indent_tabs+1)
+ else:
+ return
+
+
+
pdn = udm.SmartDataNetwork(udm.uml_diagram())
pdn.open(r"../../samples/LampDiagram_udm.xml","")
LampDiagram.initialize(udm.map_uml_names(pdn.root), udm.map_uml_names(udm.uml_diagram()))
@@ -15,8 +51,25 @@
dn = udm.SmartDataNetwork(pdn.root)
dn.open(r"Lamp.mem","")
+
rf =LampDiagram.RootFolder(dn.root)
+
+#walk_hierarchy_udm_o(dn.root)
+#walk_hierarchy_udmp(UdmPython.UdmPython(dn.root))
+walk_hierarchy_udmp(rf)
+
+
l_children = rf.getLampChildren()
+
+if l_children:
+ print "I have found the following lamps: "
+ for lamp in l_children:
+ print lamp.ModelName
+ print lamp.name
+
+
+
+
lamp_1 = l_children[0]
print lamp_1.ModelName
More information about the Mobies-commit
mailing list